@Test public void testRestPlainTextUpgrade() throws Exception { //when NettyHttpClient client = NettyHttpClient.newHttp2ClientWithHttp11Upgrade(); client.start("localhost", 8080); FullHttpRequest putValueInCacheRequest = new DefaultFullHttpRequest(HTTP_1_1, POST, "/rest/default/test", wrappedBuffer("test".getBytes(CharsetUtil.UTF_8))); client.sendRequest(putValueInCacheRequest); Queue<FullHttpResponse> responses = client.getResponses(); //then assertEquals(1, responses.size()); assertEquals(HttpResponseStatus.OK, responses.poll().status()); }
public void stop() throws Exception { if (http2) nettyHttpClient.stop(); else http1Client.stop(); executor.shutdownNow(); }
@Test public void shouldUpgradeUsingALPN() throws Exception { SkipTestNG.skipSinceJDK(10); // TODO: OpenSSL ALPN doesn't seem to work. Restructure the test to use internal JDK ALPN if (!OpenSsl.isAlpnSupported()) { throw new IllegalStateException("OpenSSL is not present, can not test TLS/ALPN support. Version: " + OpenSsl.versionString() + " Cause: " + OpenSsl.unavailabilityCause()); } //given restServer = RestServerHelper.defaultRestServer("http2testcache") .withKeyStore(KEY_STORE_PATH, "secret", "pkcs12") .start(TestResourceTracker.getCurrentTestShortName()); client = NettyHttpClient.newHttp2ClientWithALPN(KEY_STORE_PATH, "secret"); client.start(restServer.getHost(), restServer.getPort()); FullHttpRequest putValueInCacheRequest = new DefaultFullHttpRequest(HTTP_1_1, POST, "/rest/http2testcache/test", wrappedBuffer("test".getBytes(CharsetUtil.UTF_8))); //when client.sendRequest(putValueInCacheRequest); Queue<FullHttpResponse> responses = client.getResponses(); //then Assertions.assertThat(responses).hasSize(1); Assertions.assertThat(responses.element().status().code()).isEqualTo(200); Assertions.assertThat(restServer.getCacheManager().getCache("http2testcache").size()).isEqualTo(1); }
public void performGets(int pertentageOfMisses, int numberOfGets, String existingKey, String nonExistingKey) throws Exception { Random r = ThreadLocalRandom.current(); for (int i = 0; i < numberOfGets; ++i) { String key = r.nextInt(100) < pertentageOfMisses ? nonExistingKey : existingKey; executorCompletionService.submit(() -> { if (http2) { FullHttpRequest getRequest = new DefaultFullHttpRequest(HTTP_1_1, GET, "/rest/default/" + key); nettyHttpClient.sendRequest(getRequest); } else { try { String scheme = usesTLS ? "https" : "http"; http1Client .GET(String.format("%s://localhost:%d/rest/%s/%s", scheme, port, "default", key)); } catch (Exception e) { throw new RuntimeException(e); } } return 1; }); } for (int i = 0; i < numberOfGets; ++i) { executorCompletionService.take().get(); } if (http2) { nettyHttpClient.getResponses(); } }
public void start(String address, int port, int threads, boolean http2) throws Exception { this.address = address; this.port = port; this.http2 = http2; if (http2) nettyHttpClient.start(address, port); else http1Client.start(); executor = Executors.newFixedThreadPool(threads); executorCompletionService = new ExecutorCompletionService(executor); }
public BenchmarkHttpClient() { nettyHttpClient = NettyHttpClient.newHttp2ClientWithHttp11Upgrade(); http1Client = new HttpClient(); }
public BenchmarkHttpClient(String keystorePath, String keystorePassword, String trustStorePath, String trustStorePassword) throws Exception { if (!OpenSsl.isAlpnSupported()) { throw new IllegalStateException("OpenSSL is not present, can not test TLS/ALPN support"); } nettyHttpClient = NettyHttpClient.newHttp2ClientWithALPN(keystorePath, keystorePassword); SslContextFactory sslContextFactory = new SslContextFactory(); sslContextFactory.setTrustStorePassword(trustStorePath); sslContextFactory.setTrustStorePassword(trustStorePassword); sslContextFactory.setKeyStorePath(keystorePath); sslContextFactory.setKeyStorePassword(keystorePassword); http1Client = new HttpClient(sslContextFactory); usesTLS = true; }
public static NettyHttpClient newHttp2ClientWithHttp11Upgrade() { return new NettyHttpClient(null, new Http2ClientInitializer(null, Integer.MAX_VALUE)); }
NettyHttpClient client = NettyHttpClient.newHttp2ClientWithALPN(TRUST_STORE_PATH, TRUST_STORE_PASSWORD); client.start("localhost", port); client.sendRequest(putValueInCacheRequest); Queue<FullHttpResponse> responses = client.getResponses();
public void performPuts(int numberOfInserts) throws Exception { for (int i = 0; i < numberOfInserts; ++i) { String randomKey = UUID.randomUUID().toString(); executorCompletionService.submit(() -> { if (http2) { FullHttpRequest putValueInCacheRequest = new DefaultFullHttpRequest(HTTP_1_1, POST, "/rest/default/" + randomKey, wrappedBuffer("test".getBytes(CharsetUtil.UTF_8))); nettyHttpClient.sendRequest(putValueInCacheRequest); return 1; } else { try { String scheme = usesTLS ? "https" : "http"; http1Client .POST(String.format("%s://localhost:%d/rest/%s/%s", scheme, port, "default", "randomKey")) .content(new StringContentProvider("test")) .send(); return 1; } catch (Exception e) { throw new RuntimeException(e); } } }); } for (int i = 0; i < numberOfInserts; ++i) { executorCompletionService.take().get(); } if (http2) { nettyHttpClient.getResponses(); } }
public static NettyHttpClient newHttp11Client() { return new NettyHttpClient(null, new Http11ClientInitializer(null, Integer.MAX_VALUE)); }
@Test public void shouldUpgradeUsingHTTP11Upgrade() throws Exception { //given restServer = RestServerHelper.defaultRestServer("http2testcache").start(TestResourceTracker.getCurrentTestShortName()); client = NettyHttpClient.newHttp2ClientWithHttp11Upgrade(); client.start(restServer.getHost(), restServer.getPort()); FullHttpRequest putValueInCacheRequest = new DefaultFullHttpRequest(HTTP_1_1, POST, "/rest/http2testcache/test", wrappedBuffer("test".getBytes(CharsetUtil.UTF_8))); //when client.sendRequest(putValueInCacheRequest); Queue<FullHttpResponse> responses = client.getResponses(); //then Assertions.assertThat(responses).hasSize(1); Assertions.assertThat(responses.element().status().code()).isEqualTo(200); Assertions.assertThat(restServer.getCacheManager().getCache("http2testcache").size()).isEqualTo(1); } }
public static NettyHttpClient newHttp2ClientWithALPN(String keystorePath, String keystorePassword) throws Exception { SslContext sslContext = NettyTruststoreUtil.createTruststoreContext(keystorePath, keystorePassword.toCharArray(), ApplicationProtocolNames.HTTP_2); return new NettyHttpClient(sslContext, new Http2ClientInitializer(sslContext, Integer.MAX_VALUE)); }
@BeforeMethod public void afterMethod() { if (restServer != null) { restServer.stop(); } if (client != null) { client.stop(); } }
@Test public void shouldUpgradeThroughHTTP11UpgradeHeaders() throws Exception { //given RestServer restServer1 = RestTestingUtil.createDefaultRestServer("default"); RestServerRouteDestination restDestination = new RestServerRouteDestination("rest1", restServer1); SinglePortRouteSource singlePortSource = new SinglePortRouteSource(); Route<SinglePortRouteSource, RestServerRouteDestination> routeToRest = new Route<>(singlePortSource, restDestination); RouterConfigurationBuilder routerConfigurationBuilder = new RouterConfigurationBuilder(); routerConfigurationBuilder .singlePort() .port(0) .ip(InetAddress.getLoopbackAddress()) .routing() .add(routeToRest); router = new Router(routerConfigurationBuilder.build()); router.start(); int port = router.getRouter(EndpointRouter.Protocol.SINGLE_PORT).get().getPort(); //when NettyHttpClient client = NettyHttpClient.newHttp2ClientWithHttp11Upgrade(); client.start("localhost", port); FullHttpRequest putValueInCacheRequest = new DefaultFullHttpRequest(HTTP_1_1, POST, "/rest/default/test", wrappedBuffer("test".getBytes(CharsetUtil.UTF_8))); client.sendRequest(putValueInCacheRequest); Queue<FullHttpResponse> responses = client.getResponses(); //then assertThat(responses).hasSize(1); assertThat(responses.poll().status()).isEqualTo(HttpResponseStatus.OK); }