### Question:
QueryStatusChecker implements AutoCloseable { public boolean isQueryRunning() { if (!wasStarted) { synchronized (this) { if (!wasStarted) { checkerThread.start(); wasStarted = true; } } } return isRunning.get(); } QueryStatusChecker(AmazonAthena athena, ThrottlingInvoker athenaInvoker, String queryId); boolean isQueryRunning(); @Override void close();  }### Answer:
@Test public void testFastTermination() throws InterruptedException { String queryId = "query0"; GetQueryExecutionRequest request = new GetQueryExecutionRequest().withQueryExecutionId(queryId); when(athena.getQueryExecution(request)).thenReturn(new GetQueryExecutionResult().withQueryExecution(new QueryExecution().withStatus(new QueryExecutionStatus().withState("FAILED")))); QueryStatusChecker queryStatusChecker = new QueryStatusChecker(athena, athenaInvoker, queryId); assertTrue(queryStatusChecker.isQueryRunning()); Thread.sleep(2000); assertFalse(queryStatusChecker.isQueryRunning()); verify(athena, times(1)).getQueryExecution(any()); }
@Test public void testSlowTermination() throws InterruptedException { String queryId = "query1"; GetQueryExecutionRequest request = new GetQueryExecutionRequest().withQueryExecutionId(queryId); GetQueryExecutionResult result1and2 = new GetQueryExecutionResult().withQueryExecution(new QueryExecution().withStatus(new QueryExecutionStatus().withState("RUNNING"))); GetQueryExecutionResult result3 = new GetQueryExecutionResult().withQueryExecution(new QueryExecution().withStatus(new QueryExecutionStatus().withState("SUCCEEDED"))); when(athena.getQueryExecution(request)).thenReturn(result1and2).thenReturn(result1and2).thenReturn(result3); try (QueryStatusChecker queryStatusChecker = new QueryStatusChecker(athena, athenaInvoker, queryId)) { assertTrue(queryStatusChecker.isQueryRunning()); Thread.sleep(2000); assertTrue(queryStatusChecker.isQueryRunning()); Thread.sleep(3000); assertFalse(queryStatusChecker.isQueryRunning()); verify(athena, times(3)).getQueryExecution(any()); } }
@Test public void testNotFound() throws InterruptedException { String queryId = "query2"; GetQueryExecutionRequest request = new GetQueryExecutionRequest().withQueryExecutionId(queryId); when(athena.getQueryExecution(request)).thenThrow(new InvalidRequestException("")); try (QueryStatusChecker queryStatusChecker = new QueryStatusChecker(athena, athenaInvoker, queryId)) { assertTrue(queryStatusChecker.isQueryRunning()); Thread.sleep(2000); assertTrue(queryStatusChecker.isQueryRunning()); verify(athena, times(1)).getQueryExecution(any()); } }
@Test public void testOtherError() throws InterruptedException { String queryId = "query3"; GetQueryExecutionRequest request = new GetQueryExecutionRequest().withQueryExecutionId(queryId); when(athena.getQueryExecution(request)).thenThrow(new AmazonServiceException("")); try (QueryStatusChecker queryStatusChecker = new QueryStatusChecker(athena, athenaInvoker, queryId)) { assertTrue(queryStatusChecker.isQueryRunning()); Thread.sleep(3000); assertTrue(queryStatusChecker.isQueryRunning()); verify(athena, times(2)).getQueryExecution(any()); } }