|
| 1 | +/* |
| 2 | + * Copyright (c) 2021 Oracle and/or its affiliates. All rights reserved. |
| 3 | + * |
| 4 | + * This program and the accompanying materials are made available under the |
| 5 | + * terms of the Eclipse Public License v. 2.0, which is available at |
| 6 | + * http://www.eclipse.org/legal/epl-2.0. |
| 7 | + * |
| 8 | + * This Source Code may also be made available under the following Secondary |
| 9 | + * Licenses when the conditions for such availability set forth in the |
| 10 | + * Eclipse Public License v. 2.0 are satisfied: GNU General Public License, |
| 11 | + * version 2 with the GNU Classpath Exception, which is available at |
| 12 | + * https://www.gnu.org/software/classpath/license.html. |
| 13 | + * |
| 14 | + * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 |
| 15 | + */ |
| 16 | + |
| 17 | +package org.glassfish.jersey.jdk.connector.internal; |
| 18 | + |
| 19 | +import static org.junit.Assert.assertEquals; |
| 20 | +import static org.junit.Assert.assertTrue; |
| 21 | + |
| 22 | +import java.net.URI; |
| 23 | +import java.util.ArrayList; |
| 24 | +import java.util.List; |
| 25 | +import java.util.Optional; |
| 26 | +import java.util.concurrent.CountDownLatch; |
| 27 | +import java.util.concurrent.ExecutionException; |
| 28 | +import java.util.concurrent.ExecutorService; |
| 29 | +import java.util.concurrent.Executors; |
| 30 | +import java.util.concurrent.Future; |
| 31 | +import java.util.concurrent.TimeUnit; |
| 32 | + |
| 33 | +import javax.net.ssl.SSLContext; |
| 34 | +import javax.net.ssl.SSLParameters; |
| 35 | +import javax.ws.rs.GET; |
| 36 | +import javax.ws.rs.Path; |
| 37 | +import javax.ws.rs.core.Application; |
| 38 | +import javax.ws.rs.core.Response; |
| 39 | +import javax.ws.rs.core.UriBuilder; |
| 40 | + |
| 41 | +import org.glassfish.jersey.SslConfigurator; |
| 42 | +import org.glassfish.jersey.client.ClientConfig; |
| 43 | +import org.glassfish.jersey.jdk.connector.JdkConnectorProvider; |
| 44 | +import org.glassfish.jersey.server.ResourceConfig; |
| 45 | +import org.glassfish.jersey.test.JerseyTest; |
| 46 | +import org.glassfish.jersey.test.TestProperties; |
| 47 | +import org.junit.Test; |
| 48 | + |
| 49 | +public class StressTest extends JerseyTest { |
| 50 | + |
| 51 | + private static final int SSL_PORT = 29999; |
| 52 | + private static final int ITERATIONS = 10000; |
| 53 | + // Must be less than the server thread pool. Is there any way to get that value?. |
| 54 | + private static final int N_REQUESTS = 10; |
| 55 | + private static final SSLContext SSL_CONTEXT; |
| 56 | + private static final ExecutorService executor = Executors.newFixedThreadPool(N_REQUESTS); |
| 57 | + private static CountDownLatch requests; |
| 58 | + private static CountDownLatch latch; |
| 59 | + |
| 60 | + static { |
| 61 | + System.setProperty("javax.net.ssl.keyStore", SslFilterTest.class.getResource("/keystore_server").getPath()); |
| 62 | + System.setProperty("javax.net.ssl.keyStorePassword", "asdfgh"); |
| 63 | + System.setProperty("javax.net.ssl.trustStore", SslFilterTest.class.getResource("/truststore_server").getPath()); |
| 64 | + System.setProperty("javax.net.ssl.trustStorePassword", "asdfgh"); |
| 65 | + try { |
| 66 | + SSL_CONTEXT = SslConfigurator.newInstance() |
| 67 | + .trustStoreFile(SslFilterTest.class.getResource("/truststore_server").getPath()) |
| 68 | + .trustStorePassword("asdfgh") |
| 69 | + .keyStoreFile(SslFilterTest.class.getResource("/keystore_server").getPath()) |
| 70 | + .keyStorePassword("asdfgh").createSSLContext(); |
| 71 | + } catch (Exception e) { |
| 72 | + e.printStackTrace(); |
| 73 | + throw new IllegalStateException(e); |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + @Path("/test") |
| 78 | + public static class TestResource { |
| 79 | + @GET |
| 80 | + @Path("/1") |
| 81 | + public String test1() throws InterruptedException { |
| 82 | + requests.countDown(); |
| 83 | + if (latch.await(100, TimeUnit.SECONDS)) { |
| 84 | + return "test1"; |
| 85 | + } else { |
| 86 | + throw new IllegalStateException("Timeout"); |
| 87 | + } |
| 88 | + } |
| 89 | + @GET |
| 90 | + @Path("/2") |
| 91 | + public String test2() { |
| 92 | + return "test2"; |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + @Override |
| 97 | + protected Application configure() { |
| 98 | + enable(TestProperties.LOG_TRAFFIC); |
| 99 | + enable(TestProperties.DUMP_ENTITY); |
| 100 | + return new ResourceConfig(TestResource.class); |
| 101 | + } |
| 102 | + |
| 103 | + @Override |
| 104 | + protected void configureClient(ClientConfig config) { |
| 105 | + config.connectorProvider(new JdkConnectorProvider()); |
| 106 | + } |
| 107 | + |
| 108 | + @Override |
| 109 | + protected Optional<SSLContext> getSslContext() { |
| 110 | + return Optional.of(SSL_CONTEXT); |
| 111 | + } |
| 112 | + |
| 113 | + @Override |
| 114 | + protected Optional<SSLParameters> getSslParameters() { |
| 115 | + return Optional.of(SSL_CONTEXT.createSSLEngine("localhost", SSL_PORT).getSSLParameters()); |
| 116 | + } |
| 117 | + |
| 118 | + @Override |
| 119 | + protected URI getBaseUri() { |
| 120 | + return UriBuilder.fromUri("https://localhost/").port(SSL_PORT).build(); |
| 121 | + } |
| 122 | + |
| 123 | + @Test |
| 124 | + public void hangAllRequestsStatus200() throws InterruptedException, ExecutionException { |
| 125 | + assertEquals("https", getBaseUri().getScheme()); |
| 126 | + for (int i = 0; i < ITERATIONS; i++) { |
| 127 | + requests = new CountDownLatch(N_REQUESTS); |
| 128 | + latch = new CountDownLatch(1); |
| 129 | + List<Future<Response>> responses = new ArrayList<>(); |
| 130 | + for (int j = 0; j < N_REQUESTS; j++) { |
| 131 | + Future<Response> future = executor.submit(() -> target("/test/1").request().get()); |
| 132 | + responses.add(future); |
| 133 | + } |
| 134 | + assertTrue(requests.await(100, TimeUnit.SECONDS)); |
| 135 | + latch.countDown(); |
| 136 | + for (Future<Response> response : responses) { |
| 137 | + assertEquals(200, response.get().getStatus()); |
| 138 | + } |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + @Test |
| 143 | + public void randomnessStatus200() throws InterruptedException, ExecutionException { |
| 144 | + assertEquals("https", getBaseUri().getScheme()); |
| 145 | + for (int i = 0; i < ITERATIONS; i++) { |
| 146 | + List<Future<Response>> responses = new ArrayList<>(); |
| 147 | + for (int j = 0; j < N_REQUESTS; j++) { |
| 148 | + Future<Response> future = executor.submit(() -> target("/test/2").request().get()); |
| 149 | + responses.add(future); |
| 150 | + } |
| 151 | + for (Future<Response> response : responses) { |
| 152 | + assertEquals(200, response.get().getStatus()); |
| 153 | + } |
| 154 | + } |
| 155 | + } |
| 156 | + |
| 157 | +} |
0 commit comments