public void waitForCqState(VM vm, final String cqName, final int state) {
    vm.invoke(new CacheSerializableRunnable("Wait For cq State") {
      @Override
      public void run2() throws CacheException {
        // Get CQ Service.
        QueryService cqService = null;
        try {
          cqService = getCache().getQueryService();
        } catch (Exception cqe) {
          Assert.fail("Failed to getCQService.", cqe);
        }

        CqQuery cQuery = cqService.getCq(cqName);
        if (cQuery == null) {
          fail("Failed to get CqQuery for CQ : " + cqName);
        }

        // Get CQ State.
        CqStateImpl cqState = (CqStateImpl) cQuery.getState();
        // Wait max time, till the CQ state is as expected.
        final long start = System.currentTimeMillis();
        while (cqState.getState() != state) {
          assertTrue(
              "Waited over " + MAX_TIME + "ms for Cq State to be changed to " + state
                  + "; consider raising " + WAIT_PROPERTY,
              (System.currentTimeMillis() - start) < MAX_TIME);
          Wait.pause(100);
        }
      }
    });
  }
Waits till the CQ state is same as the expected. Waits for max time, if the CQ state is not
same as expected throws exception.
geode-cq/src/distributedTest/java/org/apache/geode/cache/query/cq/dunit/CqQueryUsingPoolDUnitTest.java