@Test
  public void monitorMultipleQueryExecutionsThenStopMonitoringNoRemainingCancellationTasksRunning() {
    cache = (InternalCache) new CacheFactory().set(LOCATORS, "").set(MCAST_PORT, "0").create();
    final DefaultQuery query =
        (DefaultQuery) cache.getQueryService()
            .newQuery("SELECT DISTINCT * FROM " + SEPARATOR + "exampleRegion");
    executionContext = new QueryExecutionContext(null, cache, query);
    final ExecutionContext executionContext2 = new QueryExecutionContext(null, cache, query);

    final ScheduledThreadPoolExecutor queryMonitorExecutor = new ScheduledThreadPoolExecutor(1);

    final QueryMonitor queryMonitor = new QueryMonitor(
        queryMonitorExecutor,
        cache,
        NEVER_EXPIRE_MILLIS);

    // We want to ensure isolation of cancellation tasks for different query threads/executions.
    // Here we ensure that if we monitor/unmonitor two executions in different threads that
    // both cancellation tasks are removed from the executor queue.
    final Thread firstClientQueryThread = new Thread(() -> {
      queryMonitor.monitorQueryExecution(executionContext);

      final Thread secondClientQueryThread = new Thread(() -> {
        queryMonitor.monitorQueryExecution(executionContext2);
        queryMonitor.stopMonitoringQueryExecution(executionContext2);
      });

      secondClientQueryThread.start();
      try {
        secondClientQueryThread.join();
      } catch (final InterruptedException ex) {
        Assert.fail("Unexpected exception while executing query. Details:\n"
            + ExceptionUtils.getStackTrace(ex));
      }

      queryMonitor.stopMonitoringQueryExecution(executionContext);
    });

    firstClientQueryThread.start();

    // Both cancellation tasks should have been removed upon stopping monitoring the queries on
    // each thread, so the task queue size should be 0
    GeodeAwaitility.await().until(() -> queryMonitorExecutor.getQueue().size() == 0);
  }
Test QueryMonitor, integrated with its ScheduledThreadPoolExecutor.

Mock DefaultQuery.
geode-core/src/integrationTest/java/org/apache/geode/cache/query/internal/QueryMonitorIntegrationTest.java