TestFutureRequestExecutionService.java (httpcomponents-client-4.5.7-src) | : | TestFutureRequestExecutionService.java (httpcomponents-client-4.5.8-src) | ||
---|---|---|---|---|
skipping to change at line 38 | skipping to change at line 38 | |||
import java.io.IOException; | import java.io.IOException; | |||
import java.util.LinkedList; | import java.util.LinkedList; | |||
import java.util.Queue; | import java.util.Queue; | |||
import java.util.concurrent.CancellationException; | import java.util.concurrent.CancellationException; | |||
import java.util.concurrent.CountDownLatch; | import java.util.concurrent.CountDownLatch; | |||
import java.util.concurrent.ExecutionException; | import java.util.concurrent.ExecutionException; | |||
import java.util.concurrent.ExecutorService; | import java.util.concurrent.ExecutorService; | |||
import java.util.concurrent.Executors; | import java.util.concurrent.Executors; | |||
import java.util.concurrent.Future; | import java.util.concurrent.Future; | |||
import java.util.concurrent.FutureTask; | ||||
import java.util.concurrent.TimeUnit; | import java.util.concurrent.TimeUnit; | |||
import java.util.concurrent.TimeoutException; | import java.util.concurrent.TimeoutException; | |||
import java.util.concurrent.atomic.AtomicBoolean; | import java.util.concurrent.atomic.AtomicBoolean; | |||
import org.apache.http.HttpException; | import org.apache.http.HttpException; | |||
import org.apache.http.HttpRequest; | import org.apache.http.HttpRequest; | |||
import org.apache.http.HttpResponse; | import org.apache.http.HttpResponse; | |||
import org.apache.http.client.ClientProtocolException; | import org.apache.http.client.ClientProtocolException; | |||
import org.apache.http.client.HttpClient; | import org.apache.http.client.HttpClient; | |||
import org.apache.http.client.ResponseHandler; | import org.apache.http.client.ResponseHandler; | |||
import org.apache.http.client.methods.HttpGet; | import org.apache.http.client.methods.HttpGet; | |||
import org.apache.http.client.protocol.HttpClientContext; | import org.apache.http.client.protocol.HttpClientContext; | |||
import org.apache.http.concurrent.FutureCallback; | import org.apache.http.concurrent.FutureCallback; | |||
import org.apache.http.impl.bootstrap.HttpServer; | import org.apache.http.impl.bootstrap.HttpServer; | |||
import org.apache.http.impl.bootstrap.ServerBootstrap; | import org.apache.http.impl.bootstrap.ServerBootstrap; | |||
import org.apache.http.protocol.HttpContext; | import org.apache.http.protocol.HttpContext; | |||
import org.apache.http.protocol.HttpRequestHandler; | import org.apache.http.protocol.HttpRequestHandler; | |||
import org.hamcrest.CoreMatchers; | ||||
import org.junit.After; | import org.junit.After; | |||
import org.junit.Assert; | import org.junit.Assert; | |||
import org.junit.Before; | import org.junit.Before; | |||
import org.junit.Rule; | ||||
import org.junit.Test; | import org.junit.Test; | |||
import org.junit.rules.ExpectedException; | ||||
@SuppressWarnings("boxing") // test code | @SuppressWarnings("boxing") // test code | |||
public class TestFutureRequestExecutionService { | public class TestFutureRequestExecutionService { | |||
private HttpServer localServer; | private HttpServer localServer; | |||
private String uri; | private String uri; | |||
private FutureRequestExecutionService httpAsyncClientWithFuture; | private FutureRequestExecutionService httpAsyncClientWithFuture; | |||
private final AtomicBoolean blocked = new AtomicBoolean(false); | private final AtomicBoolean blocked = new AtomicBoolean(false); | |||
@Rule | ||||
public ExpectedException thrown = ExpectedException.none(); | ||||
@Before | @Before | |||
public void before() throws Exception { | public void before() throws Exception { | |||
this.localServer = ServerBootstrap.bootstrap() | this.localServer = ServerBootstrap.bootstrap() | |||
.registerHandler("/wait", new HttpRequestHandler() { | .registerHandler("/wait", new HttpRequestHandler() { | |||
@Override | @Override | |||
public void handle( | public void handle( | |||
final HttpRequest request, final HttpResponse response, | final HttpRequest request, final HttpResponse response, | |||
final HttpContext context) throws HttpException, IOExcep tion { | final HttpContext context) throws HttpException, IOExcep tion { | |||
try { | try { | |||
skipping to change at line 112 | skipping to change at line 119 | |||
httpAsyncClientWithFuture.close(); | httpAsyncClientWithFuture.close(); | |||
} | } | |||
@Test | @Test | |||
public void shouldExecuteSingleCall() throws InterruptedException, Execution Exception { | public void shouldExecuteSingleCall() throws InterruptedException, Execution Exception { | |||
final HttpRequestFutureTask<Boolean> task = httpAsyncClientWithFuture.ex ecute( | final HttpRequestFutureTask<Boolean> task = httpAsyncClientWithFuture.ex ecute( | |||
new HttpGet(uri), HttpClientContext.create(), new OkidokiHandler()); | new HttpGet(uri), HttpClientContext.create(), new OkidokiHandler()); | |||
Assert.assertTrue("request should have returned OK", task.get().booleanV alue()); | Assert.assertTrue("request should have returned OK", task.get().booleanV alue()); | |||
} | } | |||
@Test(expected=CancellationException.class) | @Test | |||
public void shouldCancel() throws InterruptedException, ExecutionException { | public void shouldCancel() throws InterruptedException, ExecutionException { | |||
final HttpRequestFutureTask<Boolean> task = httpAsyncClientWithFuture.ex | thrown.expect(CoreMatchers.anyOf( | |||
ecute( | CoreMatchers.instanceOf(CancellationException.class), | |||
new HttpGet(uri), HttpClientContext.create(), new OkidokiHandler()); | CoreMatchers.instanceOf(ExecutionException.class))); | |||
final FutureTask<Boolean> task = httpAsyncClientWithFuture.execute( | ||||
new HttpGet(uri), HttpClientContext.create(), new OkidokiHandler | ||||
()); | ||||
task.cancel(true); | task.cancel(true); | |||
task.get(); | task.get(); | |||
} | } | |||
@Test(expected=TimeoutException.class) | @Test | |||
public void shouldTimeout() throws InterruptedException, ExecutionException, TimeoutException { | public void shouldTimeout() throws InterruptedException, ExecutionException, TimeoutException { | |||
thrown.expect(TimeoutException.class); | ||||
blocked.set(true); | blocked.set(true); | |||
final HttpRequestFutureTask<Boolean> task = httpAsyncClientWithFuture.ex ecute( | final HttpRequestFutureTask<Boolean> task = httpAsyncClientWithFuture.ex ecute( | |||
new HttpGet(uri), HttpClientContext.create(), new OkidokiHandler()); | new HttpGet(uri), HttpClientContext.create(), new OkidokiHandler()); | |||
task.get(10, TimeUnit.MILLISECONDS); | task.get(10, TimeUnit.MILLISECONDS); | |||
} | } | |||
@Test | @Test | |||
public void shouldExecuteMultipleCalls() throws Exception { | public void shouldExecuteMultipleCalls() throws Exception { | |||
final int reqNo = 100; | final int reqNo = 100; | |||
final Queue<Future<Boolean>> tasks = new LinkedList<Future<Boolean>>(); | final Queue<Future<Boolean>> tasks = new LinkedList<Future<Boolean>>(); | |||
End of changes. 9 change blocks. | ||||
5 lines changed or deleted | 18 lines changed or added |