|
17 | 17 | package org.glassfish.jersey.tests.e2e.client.connector.ssl;
|
18 | 18 |
|
19 | 19 | import java.io.IOException;
|
| 20 | +import java.io.PrintWriter; |
| 21 | +import java.io.StringWriter; |
20 | 22 | import java.net.HttpURLConnection;
|
21 | 23 | import java.net.InetAddress;
|
22 | 24 | import java.net.Socket;
|
23 | 25 | import java.net.URL;
|
| 26 | +import java.util.List; |
| 27 | +import java.util.concurrent.CopyOnWriteArrayList; |
| 28 | +import java.util.concurrent.CyclicBarrier; |
| 29 | +import java.util.concurrent.ExecutorService; |
| 30 | +import java.util.concurrent.Executors; |
| 31 | +import java.util.concurrent.TimeUnit; |
24 | 32 |
|
25 | 33 | import javax.ws.rs.client.Client;
|
26 | 34 | import javax.ws.rs.client.ClientBuilder;
|
@@ -79,6 +87,61 @@ public HttpURLConnection getConnection(final URL url) throws IOException {
|
79 | 87 | assertTrue(socketFactory.isVisited());
|
80 | 88 | }
|
81 | 89 |
|
| 90 | + /** |
| 91 | + * Test for https://github.com/jersey/jersey/issues/3293 |
| 92 | + * |
| 93 | + * @author Kevin Conaway |
| 94 | + */ |
| 95 | + @Test |
| 96 | + public void testConcurrentRequestsWithCustomSSLContext() throws Exception { |
| 97 | + final SSLContext sslContext = getSslContext(); |
| 98 | + |
| 99 | + final Client client = ClientBuilder.newBuilder() |
| 100 | + .sslContext(sslContext) |
| 101 | + .register(HttpAuthenticationFeature.basic("user", "password")) |
| 102 | + .register(LoggingFeature.class) |
| 103 | + .build(); |
| 104 | + |
| 105 | + int numThreads = 5; |
| 106 | + CyclicBarrier barrier = new CyclicBarrier(numThreads); |
| 107 | + ExecutorService service = Executors.newFixedThreadPool(numThreads); |
| 108 | + List<Exception> exceptions = new CopyOnWriteArrayList<>(); |
| 109 | + |
| 110 | + for (int i = 0; i < numThreads; i++) { |
| 111 | + service.submit(() -> { |
| 112 | + try { |
| 113 | + barrier.await(1, TimeUnit.MINUTES); |
| 114 | + for (int call = 0; call < 10; call++) { |
| 115 | + final Response response = client.target(Server.BASE_URI).path("/").request().get(); |
| 116 | + assertEquals(200, response.getStatus()); |
| 117 | + } |
| 118 | + } catch (Exception ex) { |
| 119 | + exceptions.add(ex); |
| 120 | + } |
| 121 | + }); |
| 122 | + } |
| 123 | + |
| 124 | + service.shutdown(); |
| 125 | + |
| 126 | + assertTrue( |
| 127 | + service.awaitTermination(1, TimeUnit.MINUTES) |
| 128 | + ); |
| 129 | + |
| 130 | + assertTrue( |
| 131 | + toString(exceptions), |
| 132 | + exceptions.isEmpty() |
| 133 | + ); |
| 134 | + } |
| 135 | + |
| 136 | + private String toString(List<Exception> exceptions) { |
| 137 | + StringWriter writer = new StringWriter(); |
| 138 | + PrintWriter printWriter = new PrintWriter(writer); |
| 139 | + |
| 140 | + exceptions.forEach(e -> e.printStackTrace(printWriter)); |
| 141 | + |
| 142 | + return writer.toString(); |
| 143 | + } |
| 144 | + |
82 | 145 | public static class CustomSSLSocketFactory extends SSLSocketFactory {
|
83 | 146 |
|
84 | 147 | private boolean visited = false;
|
|
0 commit comments