Java Code Examples for reactor.netty.DisposableServer#disposeNow()
The following examples show how to use
reactor.netty.DisposableServer#disposeNow() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: HttpCompressionClientServerTests.java From reactor-netty with Apache License 2.0 | 6 votes |
@Test public void testIssue282() { DisposableServer server = HttpServer.create() .compress(2048) .port(0) .handle((req, res) -> res.sendString(Mono.just("testtesttesttesttest"))) .bindNow(); Mono<String> response = HttpClient.create() .port(server.port()) .get() .uri("/") .responseContent() .aggregate() .asString(); StepVerifier.create(response) .expectNextMatches("testtesttesttesttest"::equals) .expectComplete() .verify(); server.disposeNow(); }
Example 2
Source File: TcpServerTests.java From reactor-netty with Apache License 2.0 | 5 votes |
@Test public void testIssue462() throws InterruptedException { final CountDownLatch countDownLatch = new CountDownLatch(1); DisposableServer server = TcpServer.create() .port(0) .handle((in, out) -> { in.receive() .log("channel") .subscribe(trip -> countDownLatch.countDown()); return Flux.never(); }) .wiretap(true) .bindNow(); assertNotNull(server); System.out.println("PORT +" + server.port()); Connection client = TcpClient.create() .port(server.port()) .handle((in, out) -> out.sendString(Flux.just("test"))) .wiretap(true) .connectNow(); assertNotNull(client); assertThat("Latch was counted down", countDownLatch.await(5, TimeUnit.SECONDS)); client.disposeNow(); server.disposeNow(); }
Example 3
Source File: HttpRedirectTest.java From reactor-netty with Apache License 2.0 | 5 votes |
@Test public void testIssue606() { final int serverPort = SocketUtils.findAvailableTcpPort(); DisposableServer server = HttpServer.create() .port(serverPort) .host("localhost") .handle((req, res) -> res.sendRedirect("http://localhost:" + serverPort)) .wiretap(true) .bindNow(); AtomicInteger followRedirects = new AtomicInteger(0); HttpClient.create() .remoteAddress(server::address) .wiretap(true) .followRedirect((req, res) -> { boolean result = req.redirectedFrom().length < 4; if (result) { followRedirects.getAndIncrement(); } return result; }) .get() .uri("/") .responseContent() .blockLast(); server.disposeNow(); assertThat(followRedirects.get()).isEqualTo(4); }
Example 4
Source File: TcpClientTests.java From reactor-netty with Apache License 2.0 | 5 votes |
@Test public void testRetryOnDifferentAddress() throws Exception { DisposableServer server = TcpServer.create() .port(0) .wiretap(true) .handle((req, res) -> res.sendString(Mono.just("test"))) .bindNow(); final CountDownLatch latch = new CountDownLatch(1); Supplier<SocketAddress> addressSupplier = new Supplier<SocketAddress>() { int i = 2; @Override public SocketAddress get() { return new InetSocketAddress("localhost", server.port() + i--); } }; Connection conn = TcpClient.create() .remoteAddress(addressSupplier) .doOnConnected(connection -> latch.countDown()) .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 100) .handle((in, out) -> Mono.never()) .wiretap(true) .connect() .retry() .block(Duration.ofSeconds(30)); assertNotNull(conn); assertTrue(latch.await(30, TimeUnit.SECONDS)); conn.disposeNow(); server.disposeNow(); }
Example 5
Source File: HttpResponseStatusCodesHandlingTests.java From reactor-netty with Apache License 2.0 | 5 votes |
@Test public void httpStatusCode404IsHandledByTheClient() { DisposableServer server = HttpServer.create() .port(0) .route(r -> r.post("/test", (req, res) -> res.send(req.receive() .log("server-received")))) .wiretap(true) .bindNow(); HttpClient client = HttpClient.create() .port(server.port()) .wiretap(true); Mono<Integer> content = client.headers(h -> h.add("Content-Type", "text/plain")) .request(HttpMethod.GET) .uri("/status/404") .send(ByteBufFlux.fromString(Flux.just("Hello") .log("client-send"))) .responseSingle((res, buf) -> Mono.just(res.status().code())) .doOnError(t -> System.err.println("Failed requesting server: " + t.getMessage())); StepVerifier.create(content) .expectNext(404) .verifyComplete(); server.disposeNow(); }
Example 6
Source File: DefaultPooledConnectionProviderTest.java From reactor-netty with Apache License 2.0 | 5 votes |
@Test public void testIssue1012() throws Exception { DisposableServer server = HttpServer.create() .port(0) .wiretap(true) .route(r -> r.get("/1", (req, resp) -> resp.sendString(Mono.just("testIssue1012"))) .get("/2", (req, res) -> Mono.error(new RuntimeException("testIssue1012")))) .bindNow(); DefaultPooledConnectionProvider provider = (DefaultPooledConnectionProvider) ConnectionProvider.create("testIssue1012", 1); CountDownLatch latch = new CountDownLatch(1); HttpClient client = HttpClient.create(provider) .port(server.port()) .wiretap(true) .doOnConnected(conn -> conn.channel().closeFuture().addListener(f -> latch.countDown())); client.get() .uri("/1") .responseContent() .aggregate() .block(Duration.ofSeconds(30)); client.get() .uri("/2") .responseContent() .aggregate() .onErrorResume(e -> Mono.empty()) .block(Duration.ofSeconds(30)); assertThat(latch.await(30, TimeUnit.SECONDS)).isTrue(); provider.channelPools.forEach((k, v) -> assertThat(v.metrics().acquiredSize()).isEqualTo(0)); provider.disposeLater() .block(Duration.ofSeconds(30)); server.disposeNow(); }
Example 7
Source File: TcpClientTests.java From reactor-netty with Apache License 2.0 | 5 votes |
@Test @SuppressWarnings("deprecation") public void testAddressSupplier() { DisposableServer server = TcpServer.create() .port(0) .handle((req, res) -> res.send(req.receive() .retain())) .wiretap(true) .bindNow(); Connection conn = TcpClient.create() .addressSupplier(server::address) .connectNow(); conn.outbound() .sendString(Mono.just("testAddressSupplier")) .then() .subscribe(); String result = conn.inbound() .receive() .asString() .blockFirst(); assertEquals("testAddressSupplier", result); conn.disposeNow(); server.disposeNow(); }
Example 8
Source File: HttpSendFileTests.java From reactor-netty with Apache License 2.0 | 5 votes |
private void assertSendFile(Function<HttpServerResponse, NettyOutbound> fn, boolean compression, int compressionSize, BiPredicate<HttpServerRequest, HttpServerResponse> compressionPredicate, Consumer<String> bodyAssertion) { HttpServer server = HttpServer.create(); if (compressionPredicate != null) { server = server.compress(compressionPredicate); } if (compressionSize > -1) { server = server.compress(compressionSize); } DisposableServer context = customizeServerOptions(server) .handle((req, resp) -> fn.apply(resp)) .wiretap(true) .bindNow(); HttpClient client; if (compression) { client = HttpClient.create() .remoteAddress(context::address) .compress(true); } else { client = HttpClient.create() .remoteAddress(context::address); } Mono<String> response = customizeClientOptions(client) .wiretap(true) .get() .uri("/foo") .responseSingle((res, byteBufMono) -> byteBufMono.asString(StandardCharsets.UTF_8)); String body = response.block(Duration.ofSeconds(5)); context.disposeNow(); bodyAssertion.accept(body); }
Example 9
Source File: HttpRedirectTest.java From reactor-netty with Apache License 2.0 | 5 votes |
@Test public void redirectDisabledByDefault() { DisposableServer server = HttpServer.create() .port(0) .host("localhost") .wiretap(true) .route(r -> r.get("/1", (req, res) -> res.sendRedirect("/3")) .get("/3", (req, res) -> res.status(200) .sendString(Mono.just("OK")))) .wiretap(true) .bindNow(); HttpClientResponse response = HttpClient.create() .remoteAddress(server::address) .wiretap(true) .get() .uri("/1") .response() .block(Duration.ofSeconds(30)); assertThat(response).isNotNull(); assertThat(response.status()).isEqualTo(HttpResponseStatus.FOUND); assertThat(response.responseHeaders().get("location")).isEqualTo("/3"); server.disposeNow(); }
Example 10
Source File: TcpServerTests.java From reactor-netty with Apache License 2.0 | 5 votes |
@Test public void exposesRemoteAddress() throws InterruptedException { final int port = SocketUtils.findAvailableTcpPort(); final CountDownLatch latch = new CountDownLatch(1); DisposableServer server = TcpServer.create() .port(port) .handle((in, out) -> { in.withConnection(c -> { InetSocketAddress addr = (InetSocketAddress) c.address(); assertNotNull("remote address is not null", addr.getAddress()); latch.countDown(); }); return Flux.never(); }) .wiretap(true) .bindNow(); assertNotNull(server); Connection client = TcpClient.create().port(port) .handle((in, out) -> out.sendString(Flux.just("Hello World!"))) .wiretap(true) .connectNow(); assertNotNull(client); assertTrue("Latch was counted down", latch.await(5, TimeUnit.SECONDS)); client.disposeNow(); server.disposeNow(); }
Example 11
Source File: SslProviderTests.java From reactor-netty with Apache License 2.0 | 5 votes |
@Test public void testProtocolH2SslConfiguration() { DisposableServer disposableServer = server.protocol(HttpProtocol.H2) .secure(spec -> spec.sslContext(builder)) .bindNow(); assertEquals(2, protocols.size()); assertTrue(protocols.contains("h2")); assertTrue(io.netty.handler.ssl.SslProvider.isAlpnSupported(io.netty.handler.ssl.SslProvider.OPENSSL) ? sslContext instanceof OpenSslContext : sslContext instanceof JdkSslContext); disposableServer.disposeNow(); }
Example 12
Source File: SslProviderTests.java From reactor-netty with Apache License 2.0 | 5 votes |
@Test public void testSslConfigurationProtocolH2_1() { DisposableServer disposableServer = server.secure(spec -> spec.sslContext(builder)) .protocol(HttpProtocol.H2) .bindNow(); assertEquals(2, protocols.size()); assertTrue(protocols.contains("h2")); assertTrue(io.netty.handler.ssl.SslProvider.isAlpnSupported(io.netty.handler.ssl.SslProvider.OPENSSL) ? sslContext instanceof OpenSslContext : sslContext instanceof JdkSslContext); disposableServer.disposeNow(); }
Example 13
Source File: TcpServerTests.java From reactor-netty with Apache License 2.0 | 5 votes |
@Test public void testChannelGroupClosesAllConnections() throws Exception { MonoProcessor<Void> serverConnDisposed = MonoProcessor.create(); ChannelGroup group = new DefaultChannelGroup(new DefaultEventExecutor()); CountDownLatch latch = new CountDownLatch(1); DisposableServer boundServer = TcpServer.create() .port(0) .doOnConnection(c -> { c.onDispose() .subscribe(serverConnDisposed); group.add(c.channel()); latch.countDown(); }) .wiretap(true) .bindNow(); TcpClient.create() .remoteAddress(boundServer::address) .wiretap(true) .connect() .subscribe(); assertTrue(latch.await(30, TimeUnit.SECONDS)); boundServer.disposeNow(); FutureMono.from(group.close()) .block(Duration.ofSeconds(30)); serverConnDisposed.block(Duration.ofSeconds(5)); }
Example 14
Source File: TcpServerTests.java From reactor-netty with Apache License 2.0 | 5 votes |
@Test(timeout = 10000) public void testHang() { DisposableServer httpServer = HttpServer.create() .port(0) .host("0.0.0.0") .route(r -> r.get("/data", (request, response) -> response.send(Mono.empty()))) .wiretap(true) .bindNow(); assertNotNull(httpServer); httpServer.disposeNow(); }
Example 15
Source File: HttpCompressionClientServerTests.java From reactor-netty with Apache License 2.0 | 4 votes |
@Test public void testIssue825_1() { int port1 = SocketUtils.findAvailableTcpPort(); int port2 = SocketUtils.findAvailableTcpPort(); AtomicReference<Throwable> error = new AtomicReference<>(); DisposableServer server1 = HttpServer.create() .port(port1) .compress(true) .handle((in, out) -> out.send( HttpClient.create() .port(port2) .wiretap(true) .get() .uri("/") .responseContent() .doOnError(error::set))) // .retain() deliberately not invoked // so that .release() in FluxReceive.drainReceiver will fail //.retain())) .wiretap(true) .bindNow(); DisposableServer server2 = HttpServer.create() .port(port2) .handle((in, out) -> out.sendString(Mono.just("reply"))) .wiretap(true) .bindNow(); StepVerifier.create( HttpClient.create() .port(port1) .wiretap(true) .compress(true) .get() .uri("/") .responseContent() .aggregate() .asString()) .expectError() .verify(Duration.ofSeconds(30)); assertThat(error.get()).isNotNull() .isInstanceOf(RuntimeException.class); server1.disposeNow(); server2.disposeNow(); }
Example 16
Source File: TcpServerTests.java From reactor-netty with Apache License 2.0 | 4 votes |
private void assertSendFile(Function<NettyOutbound, NettyOutbound> fn) { DisposableServer context = TcpServer.create() .handle((in, out) -> in.receive() .asString() .flatMap(word -> "GOGOGO".equals(word) ? fn.apply(out).then() : out.sendString(Mono.just("NOPE")))) .wiretap(true) .bindNow(); assertNotNull(context); MonoProcessor<String> m1 = MonoProcessor.create(); MonoProcessor<String> m2 = MonoProcessor.create(); Connection client1 = TcpClient.create() .port(context.port()) .handle((in, out) -> { in.receive() .asString() .log("-----------------CLIENT1") .subscribe(m1::onNext); return out.sendString(Mono.just("gogogo")) .neverComplete(); }) .wiretap(true) .connectNow(); Connection client2 = TcpClient.create() .port(context.port()) .handle((in, out) -> { in.receive() .asString(StandardCharsets.UTF_8) .take(2) .reduceWith(String::new, String::concat) .log("-----------------CLIENT2") .subscribe(m2::onNext); return out.sendString(Mono.just("GOGOGO")) .neverComplete(); }) .wiretap(true) .connectNow(); assertNotNull(client2); String client1Response = m1.block(); String client2Response = m2.block(); client1.disposeNow(); client2.disposeNow(); context.disposeNow(); Assertions.assertThat(client1Response).isEqualTo("NOPE"); Assertions.assertThat(client2Response) .startsWith("This is an UTF-8 file that is larger than 1024 bytes. " + "It contains accents like é.") .contains("1024 mark here ->") .contains("<- 1024 mark here") .endsWith("End of File"); }
Example 17
Source File: TcpServerTests.java From reactor-netty with Apache License 2.0 | 4 votes |
@Test public void tcpServerHandlesJsonPojosOverSsl() throws Exception { final CountDownLatch latch = new CountDownLatch(2); SelfSignedCertificate cert = new SelfSignedCertificate(); SslContextBuilder serverOptions = SslContextBuilder.forServer(cert.certificate(), cert.privateKey()) .sslProvider(SslProvider.JDK); SslContext clientOptions = SslContextBuilder.forClient() .trustManager(InsecureTrustManagerFactory.INSTANCE) .sslProvider(SslProvider.JDK) .build(); log.debug("Using SslContext: {}", clientOptions); final TcpServer server = TcpServer.create() .host("localhost") .secure(sslContextSpec -> sslContextSpec.sslContext(serverOptions)); ObjectMapper m = new ObjectMapper(); DisposableServer connectedServer = server.handle((in, out) -> { in.receive() .asByteArray() .map(bb -> { try { return m.readValue(bb, Pojo.class); } catch (IOException io) { throw Exceptions.propagate(io); } }) .log("conn") .subscribe(data -> { if ("John Doe".equals(data.getName())) { latch.countDown(); } }); return out.sendString(Mono.just("Hi")) .neverComplete(); }) .wiretap(true) .bindNow(); assertNotNull(connectedServer); final TcpClient client = TcpClient.create() .host("localhost") .port(connectedServer.port()) .secure(spec -> spec.sslContext(clientOptions)); Connection connectedClient = client.handle((in, out) -> { //in in.receive() .asString() .log("receive") .subscribe(data -> { if (data.equals("Hi")) { latch.countDown(); } }); //out return out.send(Flux.just(new Pojo("John" + " Doe")) .map(s -> { try (ByteArrayOutputStream os = new ByteArrayOutputStream()) { m.writeValue(os, s); return out.alloc() .buffer() .writeBytes(os.toByteArray()); } catch (IOException ioe) { throw Exceptions.propagate(ioe); } })) .neverComplete(); }) .wiretap(true) .connectNow(); assertNotNull(connectedClient); assertTrue("Latch was counted down", latch.await(5, TimeUnit.SECONDS)); connectedClient.disposeNow(); connectedServer.disposeNow(); }
Example 18
Source File: TcpServerTests.java From reactor-netty with Apache License 2.0 | 4 votes |
@Test @SuppressWarnings("FutureReturnValueIgnored") public void testHalfClosedConnection() throws Exception { DisposableServer server = TcpServer.create() .port(0) .childOption(ChannelOption.ALLOW_HALF_CLOSURE, true) .wiretap(true) .handle((in, out) -> in.receive() .asString() .doOnNext(s -> { if (s.endsWith("257\n")) { out.sendString(Mono.just("END") .delayElement(Duration.ofMillis(100))) .then() .subscribe(); } }) .then()) .bindNow(); Connection conn = TcpClient.create() .remoteAddress(server::address) .wiretap(true) .connectNow(); CountDownLatch latch = new CountDownLatch(1); conn.inbound() .receive() .asString() .subscribe(s -> { if ("END".equals(s)) { latch.countDown(); } }); conn.outbound() .sendString(Flux.range(1, 257).map(count -> count + "\n")) .then() .subscribe(null, null, () -> ((io.netty.channel.socket.SocketChannel) conn.channel()).shutdownOutput()); // FutureReturnValueIgnored assertTrue(latch.await(30, TimeUnit.SECONDS)); conn.disposeNow(); server.disposeNow(); }
Example 19
Source File: TcpClientTests.java From reactor-netty with Apache License 2.0 | 4 votes |
private void doTestIssue600(boolean withLoop) { DisposableServer server = TcpServer.create() .port(0) .handle((req, res) -> res.send(req.receive() .retain() .delaySubscription(Duration.ofSeconds(1)))) .wiretap(true) .bindNow(); ConnectionProvider pool = ConnectionProvider.create("doTestIssue600", 10); LoopResources loop = LoopResources.create("test", 4, true); TcpClient client; if (withLoop) { client = TcpClient.create(pool) .remoteAddress(server::address) .runOn(loop); } else { client = TcpClient.create(pool) .remoteAddress(server::address); } Set<String> threadNames = new ConcurrentSkipListSet<>(); StepVerifier.create( Flux.range(1,4) .flatMap(i -> client.handle((in, out) -> { threadNames.add(Thread.currentThread().getName()); return out.send(Flux.empty()); }) .connect())) .expectNextCount(4) .expectComplete() .verify(Duration.ofSeconds(30)); pool.dispose(); loop.dispose(); server.disposeNow(); Assertions.assertThat(threadNames.size()).isGreaterThan(1); }
Example 20
Source File: FluxReceiveTest.java From reactor-netty with Apache License 2.0 | 4 votes |
@Test public void testByteBufsReleasedWhenTimeout() { byte[] content = new byte[1024*8]; Random rndm = new Random(); rndm.nextBytes(content); DisposableServer server1 = HttpServer.create() .port(0) .route(routes -> routes.get("/target", (req, res) -> req.receive() .thenMany(res.sendByteArray(Flux.just(content) .delayElements(Duration.ofMillis(100)))))) .bindNow(); DisposableServer server2 = HttpServer.create() .port(0) .route(routes -> routes.get("/forward", (req, res) -> HttpClient.create() .port(server1.port()) .get() .uri("/target") .responseContent() .aggregate() .asString() .log() .timeout(Duration.ofMillis(50)) .then())) .bindNow(); Flux.range(0, 50) .flatMap(i -> HttpClient.create() .port(server2.port()) .get() .uri("/forward") .responseContent() .log() .onErrorResume(t -> Mono.empty())) .blockLast(Duration.ofSeconds(15)); server1.disposeNow(); server2.disposeNow(); }