@Test
  public void testFilterRegistrationDuringGII() throws Exception {
    final Host host = Host.getHost(0);
    VM server1 = host.getVM(0);
    VM server2 = host.getVM(1);
    VM client1 = host.getVM(2);
    VM client2 = host.getVM(3);

    Wait.pause(3 * 1000);

    createServer(server1);

    final int port1 = server1.invoke(CqQueryUsingPoolDUnitTest::getCacheServerPort);
    final String host0 = NetworkUtils.getServerHostName(server1.getHost());

    final int[] ports = AvailablePortHelper.getRandomAvailableTCPPorts(1);

    String poolName = "testFilterRegistrationDuringGII";
    createPool(client1, poolName, new String[] {host0, host0}, new int[] {port1, ports[0]}, "-1");
    createPool(client2, poolName, new String[] {host0, host0}, new int[] {port1, ports[0]}, "-1");

    createClient(client1, new int[] {port1, ports[0]}, host0, "-1", poolName);
    createClient(client2, new int[] {port1, ports[0]}, host0, "-1", poolName);

    // Create CQs.
    final int numCQs = 2;
    for (int i = 0; i < numCQs; i++) {
      // Create CQs.
      createCQ(client1, poolName, "client1_" + i, cqs[i]);
      executeCQ(client1, "client1_" + i, false, null);
      createCQ(client2, poolName, "client2_" + i, cqs[i]);
      executeCQ(client2, "client2_" + i, false, null);
    }

    final int interestSize = 20;
    registerInterestListCQ(client1, regions[0], interestSize, false);
    registerInterestListCQ(client2, regions[0], 0, true);

    Wait.pause(1 * 1000);

    // CREATE.
    createValues(server1, regions[0], 100);
    createValues(server1, regions[1], 10);

    waitForCreated(client1, "client1_0", KEY + 10);

    // Create server2.
    server2.invoke(new CacheSerializableRunnable("Create Cache Server") {
      @Override
      public void run2() throws CacheException {
        LogWriterUtils.getLogWriter().info("### Create Cache Server. ###");
        AttributesFactory factory = new AttributesFactory();
        factory.setScope(Scope.DISTRIBUTED_ACK);
        factory.setMirrorType(MirrorType.KEYS_VALUES);

        for (final String region : regions) {
          createRegion(region, factory.createRegionAttributes());
        }

        InitialImageOperation.slowImageProcessing = 100;
        try {
          try {
            startBridgeServer(ports[0], true);
          } catch (Exception ex) {
            Assert.fail("While starting CacheServer", ex);
          }

          while (true) {
            if (InitialImageOperation.slowImageSleeps.get() > 0) {
              // Create events while GII for HARegion is in progress.
              LocalRegion region1 = (LocalRegion) getRootRegion().getSubregion(regions[0]);
              for (int i = 90; i <= 120; i++) {
                region1.put(KEY + i, new Portfolio(i, i));
              }
              break;
            }
            Wait.pause(20);
          }
        } finally {
          InitialImageOperation.slowImageProcessing = 0;
        }
      }
    });

    Wait.pause(3 * 1000);

    // Check if CQs are registered as part of GII.
    server2.invoke(new CacheSerializableRunnable("Create values") {
      @Override
      public void run2() throws CacheException {
        DefaultQueryService qs = (DefaultQueryService) getCache().getQueryService();
        Collection<CacheClientProxy> proxies = CacheClientNotifier.getInstance().getClientProxies();
        Iterator iter = proxies.iterator();
        try {
          for (CacheClientProxy p : proxies) {
            ClientProxyMembershipID clientId = p.getProxyID();
            List cqs = qs.getCqService().getAllClientCqs(clientId);
            getCache().getLogger()
                .fine("Number of CQs found for client :" + clientId + " are :" + cqs.size());
            if (cqs.size() != numCQs) {
              fail("Number of CQs registerted by the client is :" + cqs.size()
                  + " less than expected : " + numCQs);
            }
            CqQuery cq = (CqQuery) cqs.get(0);
            LocalRegion region1 = (LocalRegion) getRootRegion().getSubregion(regions[0]);
            if (cq.getName().startsWith("client1_")) {
              if (region1.getFilterProfile().getKeysOfInterest(clientId) == null || region1
                  .getFilterProfile().getKeysOfInterest(clientId).size() != interestSize) {
                fail("Interest registartion during Secondary HARegion creation has failed.");
              }
            } else {
              if (!region1.getFilterProfile().isInterestedInAllKeys(clientId)) {
                fail("Interest registartion during Secondary HARegion creation has failed.");
              }
            }
          }
        } catch (Exception ex) {
          fail("Exception while validating filter count. ", ex);
        }
      }
    });

    // Close.
    closeClient(client1);
    closeClient(client2);
    closeServer(server1);
    closeServer(server2);
  }
Test Filter registration during GII. Bug fix 39014
geode-cq/src/distributedTest/java/org/apache/geode/cache/query/cq/dunit/CqQueryUsingPoolDUnitTest.java