Search in sources :

Example 1 with TcpClient

use of reactor.netty.tcp.TcpClient in project reactor-netty by reactor.

the class HttpServerTests method doTestDecodingFailureLastHttpContent.

private void doTestDecodingFailureLastHttpContent(String message, String... expectations) throws Exception {
    TcpClient tcpClient = TcpClient.create().port(disposableServer.port()).wiretap(true);
    Connection connection = tcpClient.connectNow();
    CountDownLatch latch = new CountDownLatch(1);
    connection.channel().closeFuture().addListener(f -> latch.countDown());
    AtomicReference<String> result = new AtomicReference<>();
    connection.inbound().receive().asString().doOnNext(result::set).subscribe();
    connection.outbound().sendString(Mono.just(message)).then().subscribe();
    assertThat(latch.await(30, TimeUnit.SECONDS)).isTrue();
    assertThat(result.get()).contains(expectations);
    assertThat(connection.channel().isActive()).isFalse();
}
Also used : TcpClient(reactor.netty.tcp.TcpClient) Connection(reactor.netty.Connection) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 2 with TcpClient

use of reactor.netty.tcp.TcpClient in project reactor-netty by reactor.

the class HttpServerTests method doTest.

private void doTest(int port, String message) throws Exception {
    TcpClient tcpClient = TcpClient.create().port(port).wiretap(true);
    Connection connection = tcpClient.connectNow();
    CountDownLatch latch = new CountDownLatch(2);
    connection.channel().closeFuture().addListener(f -> latch.countDown());
    AtomicReference<String> result = new AtomicReference<>();
    connection.inbound().receive().asString().doOnNext(s -> {
        result.set(s);
        latch.countDown();
    }).subscribe();
    connection.outbound().sendString(Mono.just(message)).then().subscribe();
    assertThat(latch.await(30, TimeUnit.SECONDS)).isTrue();
    assertThat(result.get()).contains("test", "connection: close");
    assertThat(connection.channel().isActive()).isFalse();
}
Also used : AttributeKey(io.netty.util.AttributeKey) HttpHeaders(io.netty.handler.codec.http.HttpHeaders) StepVerifier(reactor.test.StepVerifier) SNIHostName(javax.net.ssl.SNIHostName) AbortedException(reactor.netty.channel.AbortedException) DefaultChannelGroup(io.netty.channel.group.DefaultChannelGroup) ChannelInboundHandlerAdapter(io.netty.channel.ChannelInboundHandlerAdapter) HttpMessage(io.netty.handler.codec.http.HttpMessage) ByteBufHolder(io.netty.buffer.ByteBufHolder) HttpClientCodec(io.netty.handler.codec.http.HttpClientCodec) TcpClient(reactor.netty.tcp.TcpClient) ConnectionObserver(reactor.netty.ConnectionObserver) Future(java.util.concurrent.Future) DomainSocketAddress(io.netty.channel.unix.DomainSocketAddress) BeforeAll(org.junit.jupiter.api.BeforeAll) Duration(java.time.Duration) DEFAULT_FORM_DECODER_SPEC(reactor.netty.http.server.HttpServerFormDecoderProvider.DEFAULT_FORM_DECODER_SPEC) Path(java.nio.file.Path) LoopResources(reactor.netty.resources.LoopResources) HttpObjectDecoder(io.netty.handler.codec.http.HttpObjectDecoder) HttpRequest(io.netty.handler.codec.http.HttpRequest) Context(reactor.util.context.Context) Executors(java.util.concurrent.Executors) CountDownLatch(java.util.concurrent.CountDownLatch) LineBasedFrameDecoder(io.netty.handler.codec.LineBasedFrameDecoder) Assertions.fail(org.assertj.core.api.Assertions.fail) NettyOutbound(reactor.netty.NettyOutbound) ReferenceCountUtil(io.netty.util.ReferenceCountUtil) GZIPOutputStream(java.util.zip.GZIPOutputStream) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) HttpClient(reactor.netty.http.client.HttpClient) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ChannelOption(io.netty.channel.ChannelOption) HttpProtocol(reactor.netty.http.HttpProtocol) Nullable(reactor.util.annotation.Nullable) ArrayList(java.util.ArrayList) InsecureTrustManagerFactory(io.netty.handler.ssl.util.InsecureTrustManagerFactory) Assertions.assertThatExceptionOfType(org.assertj.core.api.Assertions.assertThatExceptionOfType) ValueSource(org.junit.jupiter.params.provider.ValueSource) PrematureCloseException(reactor.netty.http.client.PrematureCloseException) SslContext(io.netty.handler.ssl.SslContext) Publisher(org.reactivestreams.Publisher) IOException(java.io.IOException) Mono(reactor.core.publisher.Mono) Field(java.lang.reflect.Field) ReferenceCounted(io.netty.util.ReferenceCounted) Channel(io.netty.channel.Channel) DefaultHttpContent(io.netty.handler.codec.http.DefaultHttpContent) DefaultEventExecutor(io.netty.util.concurrent.DefaultEventExecutor) Flux(reactor.core.publisher.Flux) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) FutureMono(reactor.netty.FutureMono) Paths(java.nio.file.Paths) SslContextBuilder(io.netty.handler.ssl.SslContextBuilder) HttpHeaderNames(io.netty.handler.codec.http.HttpHeaderNames) ConnectionProvider(reactor.netty.resources.ConnectionProvider) ServerCookieEncoder(io.netty.handler.codec.http.cookie.ServerCookieEncoder) HttpUtil(io.netty.handler.codec.http.HttpUtil) SocketAddress(java.net.SocketAddress) Http11SslContextSpec(reactor.netty.http.Http11SslContextSpec) DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) ByteBufAllocator(io.netty.buffer.ByteBufAllocator) URISyntaxException(java.net.URISyntaxException) BiFunction(java.util.function.BiFunction) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Http2SslContextSpec(reactor.netty.http.Http2SslContextSpec) TimeoutException(java.util.concurrent.TimeoutException) BaseHttpTest(reactor.netty.BaseHttpTest) Random(java.util.Random) Unpooled(io.netty.buffer.Unpooled) GlobalEventExecutor(io.netty.util.concurrent.GlobalEventExecutor) DefaultHttpRequest(io.netty.handler.codec.http.DefaultHttpRequest) TransportConfig(reactor.netty.transport.TransportConfig) ServerCookieDecoder(io.netty.handler.codec.http.cookie.ServerCookieDecoder) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ByteBufFlux(reactor.netty.ByteBufFlux) NettyPipeline(reactor.netty.NettyPipeline) MethodSource(org.junit.jupiter.params.provider.MethodSource) ChannelGroup(io.netty.channel.group.ChannelGroup) HttpResponseStatus(io.netty.handler.codec.http.HttpResponseStatus) SignalType(reactor.core.publisher.SignalType) InetSocketAddress(java.net.InetSocketAddress) Collectors(java.util.stream.Collectors) ChannelBindException(reactor.netty.ChannelBindException) Objects(java.util.Objects) Test(org.junit.jupiter.api.Test) List(java.util.List) DisposableServer(reactor.netty.DisposableServer) HttpVersion(io.netty.handler.codec.http.HttpVersion) Tuple3(reactor.util.function.Tuple3) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) WebSocketCloseStatus(io.netty.handler.codec.http.websocketx.WebSocketCloseStatus) Tuple2(reactor.util.function.Tuple2) HttpClientRequest(reactor.netty.http.client.HttpClientRequest) AtomicReference(java.util.concurrent.atomic.AtomicReference) Function(java.util.function.Function) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) SniCompletionEvent(io.netty.handler.ssl.SniCompletionEvent) Charset(java.nio.charset.Charset) ByteBuf(io.netty.buffer.ByteBuf) Connection(reactor.netty.Connection) ExecutorService(java.util.concurrent.ExecutorService) HttpHeaderValues(io.netty.handler.codec.http.HttpHeaderValues) Assumptions.assumeThat(org.assertj.core.api.Assumptions.assumeThat) SelfSignedCertificate(io.netty.handler.ssl.util.SelfSignedCertificate) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) HttpMethod(io.netty.handler.codec.http.HttpMethod) CertificateException(java.security.cert.CertificateException) TimeUnit(java.util.concurrent.TimeUnit) HttpServerCodec(io.netty.handler.codec.http.HttpServerCodec) FullHttpResponse(io.netty.handler.codec.http.FullHttpResponse) TcpServer(reactor.netty.tcp.TcpServer) HttpContentDecompressor(io.netty.handler.codec.http.HttpContentDecompressor) Comparator(java.util.Comparator) Timeout(org.junit.jupiter.api.Timeout) TcpClient(reactor.netty.tcp.TcpClient) Connection(reactor.netty.Connection) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 3 with TcpClient

use of reactor.netty.tcp.TcpClient in project spring-boot by spring-projects.

the class NettyRSocketServerFactoryTests method createSecureTcpClient.

private TcpClient createSecureTcpClient() {
    TcpClient tcpClient = createTcpClient();
    Http11SslContextSpec sslContextSpec = Http11SslContextSpec.forClient().configure((builder) -> builder.sslProvider(SslProvider.JDK).trustManager(InsecureTrustManagerFactory.INSTANCE));
    return tcpClient.secure((spec) -> spec.sslContext(sslContextSpec));
}
Also used : TcpClient(reactor.netty.tcp.TcpClient) Http11SslContextSpec(reactor.netty.http.Http11SslContextSpec)

Example 4 with TcpClient

use of reactor.netty.tcp.TcpClient in project reactor-netty by reactor.

the class HttpClientTest method testApplyTcpClientSSLConfig.

@Test
void testApplyTcpClientSSLConfig() throws Exception {
    SslContext sslContext = SslContextBuilder.forClient().build();
    TcpClient tcpClient = TcpClient.create().secure(sslProviderBuilder -> sslProviderBuilder.sslContext(sslContext));
    HttpClient httpClient = HttpClientConnect.applyTcpClientConfig(tcpClient.configuration());
    assertThat(tcpClient.configuration().sslProvider()).isEqualTo(httpClient.configuration().sslProvider());
}
Also used : TcpClient(reactor.netty.tcp.TcpClient) SslContext(io.netty.handler.ssl.SslContext) BaseHttpTest(reactor.netty.BaseHttpTest) Test(org.junit.jupiter.api.Test)

Example 5 with TcpClient

use of reactor.netty.tcp.TcpClient in project reactor-netty by reactor.

the class DefaultLoopResourcesTest method testClientTransportWarmup.

private void testClientTransportWarmup(boolean preferNative) throws Exception {
    final DefaultLoopResources loop1 = (DefaultLoopResources) LoopResources.create("testClientTransportWarmup", 1, true);
    final NioEventLoopGroup loop2 = new NioEventLoopGroup(1);
    try {
        TcpClient tcpClient = TcpClient.create().resolver(spec -> spec.runOn(loop2)).runOn(loop1, preferNative);
        Mono<Void> warmupMono = tcpClient.warmup();
        assertThat(loop1.cacheNativeClientLoops.get()).isNull();
        assertThat(loop1.clientLoops.get()).isNull();
        warmupMono.block(Duration.ofSeconds(5));
        if (preferNative && LoopResources.hasNativeSupport()) {
            assertThat(loop1.cacheNativeClientLoops.get()).isNotNull();
            assertThat(loop1.clientLoops.get()).isNull();
        } else {
            assertThat(loop1.cacheNativeClientLoops.get()).isNull();
            assertThat(loop1.clientLoops.get()).isNotNull();
        }
    } finally {
        loop1.disposeLater().block(Duration.ofSeconds(5));
        loop2.shutdownGracefully().get(5, TimeUnit.SECONDS);
    }
}
Also used : StepVerifier(reactor.test.StepVerifier) Assumptions.assumeThat(org.assertj.core.api.Assumptions.assumeThat) OS(org.junit.jupiter.api.condition.OS) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Mono(reactor.core.publisher.Mono) AtomicReference(java.util.concurrent.atomic.AtomicReference) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) Epoll(io.netty.channel.epoll.Epoll) TimeUnit(java.util.concurrent.TimeUnit) Test(org.junit.jupiter.api.Test) TcpClient(reactor.netty.tcp.TcpClient) Duration(java.time.Duration) EnabledOnOs(org.junit.jupiter.api.condition.EnabledOnOs) TcpResources(reactor.netty.tcp.TcpResources) TcpServer(reactor.netty.tcp.TcpServer) KQueue(io.netty.channel.kqueue.KQueue) TcpClient(reactor.netty.tcp.TcpClient) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup)

Aggregations

TcpClient (reactor.netty.tcp.TcpClient)8 Connection (reactor.netty.Connection)5 AtomicReference (java.util.concurrent.atomic.AtomicReference)3 SslContext (io.netty.handler.ssl.SslContext)2 InsecureTrustManagerFactory (io.netty.handler.ssl.util.InsecureTrustManagerFactory)2 Duration (java.time.Duration)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 Test (org.junit.jupiter.api.Test)2 Flux (reactor.core.publisher.Flux)2 Mono (reactor.core.publisher.Mono)2 TcpSslContextSpec (reactor.netty.tcp.TcpSslContextSpec)2 ByteBuf (io.netty.buffer.ByteBuf)1 ByteBufAllocator (io.netty.buffer.ByteBufAllocator)1 ByteBufHolder (io.netty.buffer.ByteBufHolder)1 Unpooled (io.netty.buffer.Unpooled)1 Channel (io.netty.channel.Channel)1 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)1 ChannelInboundHandlerAdapter (io.netty.channel.ChannelInboundHandlerAdapter)1 ChannelOption (io.netty.channel.ChannelOption)1 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)1