@Override
          public void run2() throws CacheException {
            QueryService cqService = getCache().getQueryService();
            // Get CqQuery object.
            CqQuery cq1 = cqService.getCq(cqName);
            if (cq1 == null) {
              fail("Failed to get CQ " + cqName);
            }

            SelectResults cqResults = null;

            try {
              cqResults = cq1.executeWithInitialResults();
            } catch (Exception ex) {
              AssertionError err = new AssertionError("Failed to execute  CQ " + cqName, ex);
              throw err;
            }

            // getLogWriter().info("initial result size = " + cqResults.size());

            CqQueryTestListener cqListener =
                (CqQueryTestListener) cq1.getCqAttributes().getCqListener();
            // Wait for the last key to arrive.
            cqListener.waitForCreated("" + totalObjects);

            // Check if the events from CqListener are in order.
            int oldId = 0;
            for (Object cqEvent : cqListener.events.toArray()) {
              int newId = new Integer(cqEvent.toString());
              if (oldId > newId) {
                fail("Queued events for CQ Listener during execution with "
                    + "Initial results is not in the order in which they are created.");
              }
              oldId = newId;
            }

            // Check if all the IDs are present as part of Select Results and CQ Events.
            HashSet ids = new HashSet(cqListener.events);
            for (Object o : cqResults.asList()) {
              Struct s = (Struct) o;
              ids.add(s.get("key"));
            }

            // Iterator iter = cqResults.asSet().iterator();
            // while (iter.hasNext()) {
            // Portfolio p = (Portfolio)iter.next();
            // ids.add(p.getPk());
            // //getLogWriter().info("Result set value : " + p.getPk());
            // }

            HashSet missingIds = new HashSet();
            String key = "";
            for (int i = 1; i <= totalObjects; i++) {
              key = "" + i;
              if (!(ids.contains(key))) {
                missingIds.add(key);
              }
            }

            if (!missingIds.isEmpty()) {
              fail("Missing Keys in either ResultSet or the Cq Event list. "
                  + " Missing keys : [size : " + missingIds.size() + "]" + missingIds
                  + " Ids in ResultSet and CQ Events :" + ids);
            }

          }
Test for events created during the CQ query execution. When CQs are executed using
executeWithInitialResults there may be possibility that the region changes during that time may
not be reflected in the query result set thus making the query data and region data
inconsistent.
geode-cq/src/distributedTest/java/org/apache/geode/cache/query/cq/dunit/CqDataDUnitTest.java