QueryMonitor { public void monitorQueryExecution(final ExecutionContext executionContext) { monitorQueryExecution(executionContext, defaultMaxQueryExecutionTime); } QueryMonitor(final ScheduledThreadPoolExecutor executor,
      final InternalCache cache,
      final long defaultMaxQueryExecutionTime); void monitorQueryExecution(final ExecutionContext executionContext); void stopMonitoringQueryExecution(final ExecutionContext executionContext); static void throwExceptionIfQueryOnCurrentThreadIsCanceled(); void stopMonitoring(); static boolean isLowMemory(); static long getMemoryUsedBytes(); void setLowMemory(final boolean isLowMemory, final long usedBytes);  }
@Test public void monitorQueryThreadCqQueryIsNotMonitored() { final ExecutionContext executionContext = mock(ExecutionContext.class); when(executionContext.isCqQueryContext()).thenReturn(true); monitor.monitorQueryExecution(executionContext); Mockito.verify(scheduledThreadPoolExecutor, never()).schedule(captor.capture(), anyLong(), isA(TimeUnit.class)); }
@Test public void monitorQueryThreadExpirationTaskScheduled() { final ExecutionContext executionContext = mock(ExecutionContext.class); monitor.monitorQueryExecution(executionContext); Mockito.verify(scheduledThreadPoolExecutor, times(1)).schedule(captor.capture(), anyLong(), isA(TimeUnit.class)); captor.getValue().run(); Mockito.verify(executionContext, times(1)) .setQueryCanceledException(isA(QueryExecutionTimeoutException.class)); assertThatThrownBy(QueryMonitor::throwExceptionIfQueryOnCurrentThreadIsCanceled) .isExactlyInstanceOf(QueryExecutionCanceledException.class); }