|
16 | 16 |
|
17 | 17 | package org.glassfish.jersey.tests.e2e; |
18 | 18 |
|
| 19 | +import java.io.IOException; |
19 | 20 | import java.util.Collection; |
20 | 21 | import java.util.Collections; |
21 | 22 | import java.util.IdentityHashMap; |
22 | 23 | import java.util.List; |
| 24 | +import java.util.HashSet; |
23 | 25 | import java.util.Set; |
24 | 26 | import java.util.concurrent.Callable; |
| 27 | +import java.util.concurrent.CountDownLatch; |
25 | 28 | import java.util.concurrent.ExecutionException; |
26 | 29 | import java.util.concurrent.ExecutorService; |
27 | 30 | import java.util.concurrent.Executors; |
|
37 | 40 | import javax.ws.rs.Produces; |
38 | 41 | import javax.ws.rs.client.Client; |
39 | 42 | import javax.ws.rs.client.ClientBuilder; |
| 43 | +import javax.ws.rs.client.ClientRequestContext; |
| 44 | +import javax.ws.rs.client.ClientRequestFilter; |
40 | 45 | import javax.ws.rs.container.AsyncResponse; |
41 | 46 | import javax.ws.rs.container.Suspended; |
42 | 47 | import javax.ws.rs.core.Application; |
|
52 | 57 | import org.glassfish.jersey.test.JerseyTest; |
53 | 58 | import org.junit.Test; |
54 | 59 | import static org.junit.Assert.assertEquals; |
| 60 | +import static org.junit.Assert.assertTrue; |
55 | 61 |
|
56 | 62 | /** |
57 | 63 | * {@link org.glassfish.jersey.spi.ExecutorServiceProvider} E2E tests. |
@@ -137,17 +143,21 @@ public void dispose(final ExecutorService executorService) { |
137 | 143 | executorService.shutdownNow(); |
138 | 144 | } |
139 | 145 |
|
140 | | - private class CustomExecutorService implements ExecutorService { |
| 146 | + /* package private */ class CustomExecutorService implements ExecutorService { |
141 | 147 |
|
142 | 148 | private final ExecutorService delegate; |
143 | 149 | private final AtomicBoolean isCleanedUp; |
144 | 150 |
|
145 | 151 | public CustomExecutorService() { |
146 | | - this.isCleanedUp = new AtomicBoolean(false); |
147 | | - this.delegate = Executors.newSingleThreadExecutor(new ThreadFactoryBuilder() |
| 152 | + this(Executors.newSingleThreadExecutor(new ThreadFactoryBuilder() |
148 | 153 | .setNameFormat("async-request-%d") |
149 | 154 | .setUncaughtExceptionHandler(new JerseyProcessingUncaughtExceptionHandler()) |
150 | | - .build()); |
| 155 | + .build())); |
| 156 | + } |
| 157 | + |
| 158 | + public CustomExecutorService(ExecutorService delegate) { |
| 159 | + this.isCleanedUp = new AtomicBoolean(false); |
| 160 | + this.delegate = delegate; |
151 | 161 |
|
152 | 162 | executorCreationCount++; |
153 | 163 | executors.add(this); |
@@ -231,6 +241,17 @@ public void execute(Runnable command) { |
231 | 241 | } |
232 | 242 | } |
233 | 243 |
|
| 244 | + public static class SecondCustomExecutorProvider extends CustomExecutorProvider { |
| 245 | + public static final String NAME_FORMAT = "second-async-request"; |
| 246 | + |
| 247 | + public ExecutorService getExecutorService() { |
| 248 | + return new CustomExecutorService(Executors.newSingleThreadExecutor(new ThreadFactoryBuilder() |
| 249 | + .setNameFormat(NAME_FORMAT + "-%d") |
| 250 | + .setUncaughtExceptionHandler(new JerseyProcessingUncaughtExceptionHandler()) |
| 251 | + .build())); |
| 252 | + } |
| 253 | + } |
| 254 | + |
234 | 255 | private static final CustomExecutorProvider serverExecutorProvider = new CustomExecutorProvider(); |
235 | 256 |
|
236 | 257 | @Override |
@@ -355,4 +376,56 @@ public void testCustomNamedServerExecutorsInjectionAndReleasing() throws Excepti |
355 | 376 |
|
356 | 377 | setUp(); // re-starting test container to ensure proper post-test tearDown. |
357 | 378 | } |
| 379 | + |
| 380 | + @Test |
| 381 | + public void testClientBuilderExecutorServiceTakesPrecedenceOverRegistered() throws Exception { |
| 382 | + serverExecutorProvider.reset(); |
| 383 | + CountDownLatch nameLatch = new CountDownLatch(1); |
| 384 | + Set<String> threadName = new HashSet<>(1); |
| 385 | + |
| 386 | + final CustomExecutorProvider executorProvider = new CustomExecutorProvider(); |
| 387 | + Client client = ClientBuilder.newBuilder().register(new ClientRequestFilter() { |
| 388 | + @Override |
| 389 | + public void filter(ClientRequestContext requestContext) throws IOException { |
| 390 | + threadName.add(Thread.currentThread().getName()); |
| 391 | + nameLatch.countDown(); |
| 392 | + } |
| 393 | + }).executorService(new SecondCustomExecutorProvider().getExecutorService()).register(executorProvider).build(); |
| 394 | + |
| 395 | + client.target(getBaseUri()).path("resource").request().async().get(String.class).get(); |
| 396 | + assertTrue(nameLatch.await(10, TimeUnit.SECONDS)); |
| 397 | + assertEquals(threadName.size(), 1); |
| 398 | + assertTrue(threadName.iterator().next().startsWith(SecondCustomExecutorProvider.NAME_FORMAT)); |
| 399 | + |
| 400 | + tearDown(); // stopping test container |
| 401 | + client.close(); |
| 402 | + |
| 403 | + setUp(); // re-starting test container to ensure proper post-test tearDown. |
| 404 | + } |
| 405 | + |
| 406 | + @Test |
| 407 | + public void testRegisteredExecutorServiceDoesNotTakesPrecedenceOverClientBuilder() throws Exception { |
| 408 | + serverExecutorProvider.reset(); |
| 409 | + CountDownLatch nameLatch = new CountDownLatch(1); |
| 410 | + Set<String> threadName = new HashSet<>(1); |
| 411 | + |
| 412 | + final CustomExecutorProvider executorProvider = new CustomExecutorProvider(); |
| 413 | + Client client = ClientBuilder.newBuilder().register(executorProvider).register(new ClientRequestFilter() { |
| 414 | + @Override |
| 415 | + public void filter(ClientRequestContext requestContext) throws IOException { |
| 416 | + threadName.add(Thread.currentThread().getName()); |
| 417 | + nameLatch.countDown(); |
| 418 | + } |
| 419 | + }).executorService(new SecondCustomExecutorProvider().getExecutorService()).build(); |
| 420 | + |
| 421 | + client.target(getBaseUri()).path("resource").request().async().get(String.class).get(); |
| 422 | + assertTrue(nameLatch.await(10, TimeUnit.SECONDS)); |
| 423 | + assertEquals(threadName.size(), 1); |
| 424 | + assertTrue(threadName.iterator().next().startsWith(SecondCustomExecutorProvider.NAME_FORMAT)); |
| 425 | + |
| 426 | + tearDown(); // stopping test container |
| 427 | + client.close(); |
| 428 | + |
| 429 | + setUp(); // re-starting test container to ensure proper post-test tearDown. |
| 430 | + } |
358 | 431 | } |
0 commit comments