use of javax.ws.rs.client.AsyncInvoker in project jersey by jersey.
the class GenericResponseTest method testAsyncPost.
@Test
public void testAsyncPost() throws ExecutionException, InterruptedException {
GenericType<Response> generic = new GenericType<Response>(Response.class);
Entity entity = Entity.entity("entity", MediaType.WILDCARD_TYPE);
WebTarget target = target("resource");
final AsyncInvoker async = target.request().async();
Response response = async.post(entity, generic).get();
Assert.assertEquals(200, response.getStatus());
Assert.assertEquals("entity", response.readEntity(String.class));
}
use of javax.ws.rs.client.AsyncInvoker in project jersey by jersey.
the class GenericResponseTest method testAsyncGet.
@Test
public void testAsyncGet() throws ExecutionException, InterruptedException {
GenericType<Response> generic = new GenericType<Response>(Response.class);
WebTarget target = target("resource");
final AsyncInvoker async = target.request().async();
Response response = async.get(generic).get();
Assert.assertEquals(200, response.getStatus());
Assert.assertEquals("get", response.readEntity(String.class));
}
use of javax.ws.rs.client.AsyncInvoker in project jersey by jersey.
the class BasicClientTest method runCustomExecutorTestAsync.
private void runCustomExecutorTestAsync(Client client) throws InterruptedException, ExecutionException {
AsyncInvoker async = client.target(getBaseUri()).path("resource").request().async();
Future<Response> f1 = async.post(text("post"));
final Response response = f1.get();
final String entity = response.readEntity(String.class);
assertEquals("custom-async-request-post", entity);
}
use of javax.ws.rs.client.AsyncInvoker in project jersey by jersey.
the class AsyncTest method testClientThreadPool.
@Test
@Ignore("Unstable test.")
public void testClientThreadPool() throws Exception {
final AsyncInvoker invoker = ClientBuilder.newClient(new ClientConfig().property(ClientProperties.ASYNC_THREADPOOL_SIZE, 9)).target(getBaseUri()).path("threadpool").request().async();
final CountDownLatch latch = new CountDownLatch(100);
final int threadCount = Thread.activeCount();
final List<Thread> threads = new ArrayList<Thread>(20);
for (int i = 0; i < 20; i++) {
threads.add(new Thread(new Runnable() {
@Override
public void run() throws RuntimeException {
for (int i = 0; i < 5; i++) {
try {
assertThat(invoker.get().get().readEntity(String.class), equalTo("GET"));
assertThat(Thread.activeCount() - threadCount - 20, lessThanOrEqualTo(10));
latch.countDown();
} catch (final InterruptedException e) {
fail();
} catch (final ExecutionException e) {
fail();
}
}
}
}));
}
for (final Thread thread : threads) {
thread.start();
}
assertTrue(latch.await(10 * getAsyncTimeoutMultiplier(), TimeUnit.SECONDS));
}
use of javax.ws.rs.client.AsyncInvoker in project jersey by jersey.
the class JerseyInvocationTest method runtimeExceptionInAsyncInvocation.
@Test
public void runtimeExceptionInAsyncInvocation() throws ExecutionException, InterruptedException {
final AsyncInvoker ai = ClientBuilder.newClient().register(new ExceptionInvokerFilter()).target("http://localhost:888/").request().async();
try {
ai.get().get();
fail("ExecutionException should be thrown");
} catch (ExecutionException ee) {
assertEquals(ProcessingException.class, ee.getCause().getClass());
assertEquals(RuntimeException.class, ee.getCause().getCause().getClass());
}
}
Aggregations