This is an automated email from the ASF dual-hosted git repository. coheigea pushed a commit to branch 3.2.x-fixes in repository https://gitbox.apache.org/repos/asf/cxf.git
commit 2ca662c9fb75c7201ddef576c3a118561b3b976d Author: reta <[email protected]> AuthorDate: Sun Jul 22 18:55:03 2018 -0400 CXF-7784: Adding test case for concurrent invocations of the Invocation.Builder instance (cherry picked from commit 7d611371e968258830cd7cec4cdbb03bff79fc12) --- .../cxf/jaxrs/client/spec/ClientImplTest.java | 50 ++++++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git a/rt/rs/client/src/test/java/org/apache/cxf/jaxrs/client/spec/ClientImplTest.java b/rt/rs/client/src/test/java/org/apache/cxf/jaxrs/client/spec/ClientImplTest.java index 88cf9da..1f502e4 100644 --- a/rt/rs/client/src/test/java/org/apache/cxf/jaxrs/client/spec/ClientImplTest.java +++ b/rt/rs/client/src/test/java/org/apache/cxf/jaxrs/client/spec/ClientImplTest.java @@ -20,12 +20,22 @@ package org.apache.cxf.jaxrs.client.spec; import java.util.ArrayList; import java.util.Arrays; +import java.util.Collection; import java.util.List; +import java.util.concurrent.BrokenBarrierException; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.CompletionException; +import java.util.concurrent.CyclicBarrier; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; import java.util.logging.Handler; import java.util.logging.LogRecord; import javax.ws.rs.client.Client; import javax.ws.rs.client.ClientBuilder; +import javax.ws.rs.client.Invocation; import javax.ws.rs.client.WebTarget; import org.apache.cxf.common.logging.LogUtils; @@ -187,4 +197,44 @@ public class ClientImplTest extends Assert { } fail("did not log expected message"); } + + /** + * This test cases creates a single WebTarget instance and than calls + * the request() method concurrently from different threads verifying that + * its behavior is thread-safe. + */ + @Test + public void testAccessInvocationBuilderConcurrently() { + String address = "http://localhost:8080/bookstore/{a}/simple"; + Client client = ClientBuilder.newClient(); + + final Invocation.Builder builder = client + .target(address) + .resolveTemplate("a", "bookheaders") + .request("application/xml") + .header("a", "b"); + + final ExecutorService executor = Executors.newFixedThreadPool(20); + final CyclicBarrier barrier = new CyclicBarrier(20); + + final Collection<CompletableFuture<?>> futures = new ArrayList<>(); + for (int i = 0; i < 20; ++i) { + futures.add(CompletableFuture.supplyAsync(() -> { + try { + barrier.await(1, TimeUnit.SECONDS); + return builder.buildGet(); + } catch (final InterruptedException ex) { + Thread.interrupted(); + throw new CompletionException(ex); + } catch (BrokenBarrierException | TimeoutException ex) { + throw new CompletionException(ex); + } + }, executor)); + } + + CompletableFuture + .allOf(futures.toArray(new CompletableFuture<?>[0])) + .join(); + } + }
