List of usage examples for java.util.concurrent ScheduledThreadPoolExecutor getCompletedTaskCount
public long getCompletedTaskCount()
From source file:com.ksc.http.timers.ClientExecutionAndRequestTimerTestUtils.java
private static void assertNumberOfTasksTriggered(ScheduledThreadPoolExecutor timerExecutor, int expectedNumberOfTasks) { waitBeforeAssertOnExecutor();/*from w ww . j av a 2 s .c o m*/ assertEquals(expectedNumberOfTasks, timerExecutor.getCompletedTaskCount()); }
From source file:com.amazonaws.http.AmazonHttpClientRequestTimeoutTest.java
@Test public void testRequestTimerCancelledTask() throws IOException, InterruptedException { ClientConfiguration config = new ClientConfiguration().withRequestTimeout(5 * 1000).withMaxErrorRetry(0); HttpClientFactory httpClientFactory = new HttpClientFactory(); HttpClient rawHttpClient = spy(httpClientFactory.createHttpClient(config)); HttpResponseProxy responseProxy = createHttpResponseProxySpy(); doReturn(responseProxy).when(rawHttpClient).execute(any(HttpRequestBase.class), any(HttpContext.class)); String localhostEndpoint = "http://localhost:0"; Request<?> request = new EmptyHttpRequest(localhostEndpoint, HttpMethodName.GET); AmazonHttpClient httpClient = new AmazonHttpClient(config, rawHttpClient, null); try {// w w w . j a v a 2 s. co m httpClient.execute(request, new NullResponseHandler(), new NullErrorResponseHandler(), new ExecutionContext()); Assert.fail("Should have been unable to unmarshall the response!"); } catch (AmazonClientException e) { Assert.assertTrue(e.getCause() instanceof RuntimeException); RuntimeException re = (RuntimeException) e.getCause(); Assert.assertTrue(re.getMessage().contains("Unable to unmarshall response")); } /* Verify the response was buffered when enforcing the request timeout. */ verify(responseProxy).setEntity(any(BufferedHttpEntity.class)); /* Verify cancelled tasks are removed on demand and the core threads die out after the keep alive time. */ ScheduledThreadPoolExecutor httpRequestTimer = httpClient.getHttpRequestTimer().getExecutor(); Assert.assertEquals(0, httpRequestTimer.getCompletedTaskCount()); Assert.assertEquals(0, httpRequestTimer.getQueue().size()); Assert.assertEquals(1, httpRequestTimer.getPoolSize()); Thread.sleep(httpRequestTimer.getKeepAliveTime(TimeUnit.MILLISECONDS) + 1000); Assert.assertEquals(0, httpRequestTimer.getPoolSize()); httpClient.shutdown(); }
From source file:com.amazonaws.http.AmazonHttpClientRequestTimeoutTest.java
@Test(timeout = 25 * 1000) public void testRequestTimeoutEnforcedWithRetries() throws IOException { ProblematicServer server = new ProblematicServer(ProblematicServer.ServerIssue.OVERLOADED); server.startServer();/* w w w. j ava2 s.c o m*/ int maxRetries = 2; ClientConfiguration config = new ClientConfiguration().withRequestTimeout(1 * 1000) .withMaxErrorRetry(maxRetries); HttpClientFactory httpClientFactory = new HttpClientFactory(); HttpClient rawHttpClient = spy(httpClientFactory.createHttpClient(config)); String localhostEndpoint = "http://localhost:" + server.getPort(); Request<?> request = new EmptyHttpRequest(localhostEndpoint, HttpMethodName.GET); AmazonHttpClient httpClient = new AmazonHttpClient(config, rawHttpClient, null); try { httpClient.execute(request, new NullResponseHandler(), new NullErrorResponseHandler(), new ExecutionContext()); Assert.fail("Client-side request timeout is expected!"); } catch (AmazonClientException e) { /* Assert the expected exception and number of requests. */ Assert.assertTrue(e.getCause() instanceof HttpRequestTimeoutException); int expectedNumberOfRequests = 1 + maxRetries; verify(rawHttpClient, times(expectedNumberOfRequests)).execute(any(HttpRequestBase.class), any(HttpContext.class)); ScheduledThreadPoolExecutor httpRequestTimer = httpClient.getHttpRequestTimer().getExecutor(); Assert.assertEquals(expectedNumberOfRequests, httpRequestTimer.getCompletedTaskCount()); } server.stopServer(); httpClient.shutdown(); }