@Test public void testThatCountsExactlyOneSubmittedRequest() throws Exception { executorService.submit(new MockTextQuery(), Optional.empty()); executorService.submit(new MockTextQuery(), Optional.empty()); executorService.submit(new MockTextQuery(), Optional.empty()); sleep(MockTextProvider.FETCH_DELAY / 2); assertEquals(1, executorService.countSubmitted()); }
@Test(timeout = 60000) public void testThatInProgressTaskCountWell() throws Exception { MockTextQuery query1 = new MockTextQuery(); MockTextQuery query2 = new MockTextQuery(); executorService.submit(query1, Optional.empty()); executorService.submit(query2, Optional.empty()); sleep(MockTextProvider.FETCH_DELAY / 2); assertEquals(2, this.executorService.countInProgress()); }
@Test(timeout = 60000) public void testThatCallbackContainsCorrectQuery() throws Exception { MockTextQuery task = new MockTextQuery(); Set<MockTextQuery> result = new HashSet<>(); ExecutionCallback<MockTextQuery, MockTextProduct> callback = (t, p) -> result.add(t); executorService.submit(task, Optional.of(callback)); executorService.shutdown(); assertTrue(task == result.iterator().next()); }
@Test(timeout = 60000) public void testThatShutdownMethodWaitsAllCurrentTask() throws Exception { MockTextQuery request1 = new MockTextQuery(); MockTextQuery request2 = new MockTextQuery(); executorService.submit(request1, Optional.empty()); executorService.submit(request2, Optional.empty()); sleep(MockTextProvider.FETCH_DELAY / 2); executorService.shutdown(); assertEquals(0, this.executorService.countInProgress()); }
@Test(timeout = 60000) public void testThatProductWillBeReturnedViaCallback() throws Exception { MockTextQuery task = new MockTextQuery("ping"); Set<String> result = new HashSet<>(); ExecutionCallback<MockTextQuery, MockTextProduct> callback = (t, p) -> result.add(p.get().getResponse()); executorService.submit(task, Optional.of(callback)); executorService.shutdown(); assertEquals("ping_pong", result.iterator().next()); }
@Test(timeout = 60000, expected = IllegalStateException.class) public void testThatCanNotSubmitWhenInShutdownState() throws Exception { new Thread( () -> { try { sleep(1000); executorService.shutdown(); } catch (InterruptedException ignored) { } }) .start(); for (int i = 0; i < 10; i++) { executorService.submit(new MockTextQuery(), Optional.empty()); sleep(MockTextProvider.FETCH_DELAY / 2); } assertTrue("service is not allowed to accept submissions after shutdown", false); }