/**
  * Hydra task to verify queries on the region after all ops. This MUST be called as a batched
  * task, and will throw StopSchedulingTaskOnClientOrder when completed. It is necessary to
  * reinitialize the verify state variables if this is called a second time in this VM, however an
  * error is thrown if a second attempt it made without resetting the state variables.
  */
 public static void HydraTask_verifyQueries() {
   if (CQsOn) {
     // the region contents verify task comes first, so if we got to this
     // task, then we know the the region is a perfect snapshot of what
     // we expect; the CQTestInstance relies on a snapshot
     if (verifyQueryIterator == null) {
       testInstance.CQTestInstance.regionSnapshot = new HashMap(testInstance.aRegion);
       verifyQueryIterator = testInstance.queryMap.keySet().iterator();
     }
     if (verifyQueryIterator.hasNext()) {
       verifyQueryCount++;
       String cqName = (String) (verifyQueryIterator.next());
       Log.getLogWriter()
           .info(
               "Verifying query "
                   + verifyQueryCount
                   + " out of "
                   + testInstance.queryMap.size()
                   + " with name "
                   + cqName);
       testInstance.CQTestInstance.verifyQuery(cqName);
     } else {
       String aStr = "Done verifying " + verifyQueryCount + " queries";
       Log.getLogWriter().info(aStr);
       // set for next time; if this is an init task, then we will be set for the close task
       verifyQueryIterator = null;
       verifyQueryCount = 0;
       throw new StopSchedulingTaskOnClientOrder(aStr);
     }
   } else {
     String aStr = "Skipping verification of queries because CQUtilPrms.CQsOn is " + CQsOn;
     Log.getLogWriter().info(aStr);
     throw new StopSchedulingTaskOnClientOrder(aStr);
   }
 }
 /**
  * Initialize a client vm.
  *
  * @param startCQsRunning true if the initialize should start the CQs running false otherwise
  */
 private void initializeClient(boolean startCQsRunning) {
   initializeRegion("clientRegion");
   initializeInstance();
   if (isBridgeConfiguration) {
     isBridgeClient = true;
     registerInterest(testInstance.aRegion);
     if (CQsOn) {
       CQTestInstance.initializeQueryService();
       queryMap = testInstance.CQTestInstance.generateQueries(queryDepth);
       CQTestInstance.queryMap = queryMap;
       CQTestInstance.aRegion = aRegion;
       if (startCQsRunning) {
         CQTestInstance.initializeCQs();
       }
     } else {
       Log.getLogWriter().info("Not creating CQs because CQUtilPrms.CQsOn is " + CQsOn);
     }
   }
 }
 /**
  * Verify all queries by combining the results of executeWithInitialResults and subsequent
  * cqEvents.
  */
 private void verifyQueriesCombine() {
   if (CQsOn) {
     // the region contents verify task comes first, so if we got to this
     // task, then we know the the region is a perfect snapshot of what
     // we expect; the CQTestInstance relies on a snapshot
     testInstance.CQTestInstance.regionSnapshot = new HashMap(testInstance.aRegion);
     verifyQueryResultsCombine();
   } else {
     Log.getLogWriter()
         .info("Skipping verification of queries because CQUtilPrms.CQsOn is " + CQsOn);
   }
 }
 /** Initialize fields for this instance */
 public void initializeInstance() {
   numNewKeys = TestConfig.tab().intAt(CQUtilPrms.numNewKeys, -1);
   keyIntervals = (KeyIntervals) (CQUtilBB.getBB().getSharedMap().get(CQUtilBB.KeyIntervals));
   Log.getLogWriter()
       .info("initInstance, keyIntervals read from blackboard = " + keyIntervals.toString());
   int numDestroyed = keyIntervals.getNumKeys(KeyIntervals.DESTROY);
   int numKeyIntervals = keyIntervals.getNumKeys();
   totalNumKeys = numKeyIntervals + numNewKeys - numDestroyed;
   minTaskGranularitySec = TestConfig.tab().longAt(TestHelperPrms.minTaskGranularitySec, -1);
   minTaskGranularityMS = -1;
   if (minTaskGranularitySec != -1) {
     minTaskGranularityMS = minTaskGranularitySec * TestHelper.SEC_MILLI_FACTOR;
   }
   queryDepth = TestConfig.tab().intAt(CQUtilPrms.queryDepth, 1);
   Vector bridgeNames = TestConfig.tab().vecAt(BridgePrms.names, null);
   isBridgeConfiguration = bridgeNames != null;
   CQsOn = TestConfig.tab().booleanAt(CQUtilPrms.CQsOn, true);
   CQTestInstance = new CQTest();
   CQTestInstance.initializeInstance();
   Log.getLogWriter().info("numKeyIntervals is " + numKeyIntervals);
   Log.getLogWriter().info("numNewKeys is " + numNewKeys);
   Log.getLogWriter().info("numDestroyed is " + numDestroyed);
   Log.getLogWriter().info("totalNumKeys is " + totalNumKeys);
 }
  /**
   * Verify the result of the CQs registered for this VM. Query results from
   * executeWithInitialResults are combined with subsequent cq events to form the final result.
   */
  protected void verifyQueryResultsCombine() {
    Log.getLogWriter().info("In verifyQueryResultsCombine");
    Iterator it = queryMap.keySet().iterator();
    int count = 0;
    while (it.hasNext()) {
      count++;
      String cqName = (String) (it.next());
      Log.getLogWriter()
          .info("Verifying query " + count + " out of " + queryMap.size() + " with name " + cqName);
      CqQuery cq = qService.getCq(cqName);
      String queryStr = cq.getQueryString();
      String readableQueryStr = CQTest.getReadableQueryString(queryStr);

      // combine the initial selectResults with history of events
      CQHistory history = CQHistoryListener.getCQHistory(cqName);
      Map combinedMap = history.getCombinedResults();
      List combinedList = new ArrayList(combinedMap.values());
      List expectedResults = CQTestInstance.getExpectedResults(queryStr);
      List missingInCombined = new ArrayList(expectedResults);
      List unexpectedInCombined = new ArrayList(combinedList);
      unexpectedInCombined.removeAll(expectedResults);
      missingInCombined.removeAll(combinedList);

      // prepare error Strings
      StringBuffer aStr = new StringBuffer();
      if (unexpectedInCombined.size() > 0) {
        String tmpStr =
            getLocationString(unexpectedInCombined, expectedResults, history)
                + "\n"
                + "Found the following "
                + unexpectedInCombined.size()
                + " unexpected elements in combined results for cq "
                + cqName
                + ", "
                + readableQueryStr
                + ": "
                + QueryObject.toStringFull(unexpectedInCombined);
        Log.getLogWriter().info(tmpStr);
        aStr.append(tmpStr);
      }
      if (missingInCombined.size() > 0) {
        String tmpStr =
            getLocationString(missingInCombined, expectedResults, history)
                + "\n"
                + "The following "
                + missingInCombined.size()
                + " elements were missing from combined results for cq "
                + cqName
                + ", "
                + readableQueryStr
                + ": "
                + QueryObject.toStringFull(missingInCombined);
        Log.getLogWriter().info(tmpStr);
        aStr.append(tmpStr);
      }

      if (aStr.length() > 0) {
        throw new TestException(
            "Probably bug 38065: For cq "
                + cqName
                + ", "
                + readableQueryStr
                + "\n"
                + aStr.toString());
      }
    }
    Log.getLogWriter().info("Done verifying " + count + " queries");
  }