Search in sources :

Example 1 with ChannelOperationsId

use of reactor.netty.ChannelOperationsId in project reactor-netty by reactor.

the class HttpTests method testIds.

@Test
void testIds() throws Exception {
    CountDownLatch latch = new CountDownLatch(2);
    AtomicReference<String> serverOpsShortId = new AtomicReference<>();
    AtomicReference<String> serverChannelShortId = new AtomicReference<>();
    AtomicReference<String> serverOpsLongId = new AtomicReference<>();
    AtomicReference<String> serverChannelId = new AtomicReference<>();
    AtomicReference<String> serverRequestId = new AtomicReference<>();
    disposableServer = HttpServer.create().wiretap(true).doOnConnection(conn -> {
        serverOpsShortId.set(((ChannelOperationsId) conn).asShortText());
        serverChannelShortId.set(conn.channel().id().asShortText());
        serverOpsLongId.set(((ChannelOperationsId) conn).asLongText());
        serverChannelId.set(conn.channel().toString());
        latch.countDown();
    }).handle((req, res) -> {
        serverRequestId.set(req.requestId());
        return res.sendString(Mono.just("testIds"));
    }).bindNow();
    AtomicReference<String> clientOpsShortId = new AtomicReference<>();
    AtomicReference<String> clientChannelShortId = new AtomicReference<>();
    AtomicReference<String> clientOpsLongId = new AtomicReference<>();
    AtomicReference<String> clientChannelId = new AtomicReference<>();
    AtomicReference<String> clientRequestId = new AtomicReference<>();
    ConnectionProvider provider = ConnectionProvider.create("testIds", 1);
    HttpClient client = createClient(provider, disposableServer.port()).doOnRequest((req, conn) -> {
        clientOpsShortId.set(((ChannelOperationsId) conn).asShortText());
        clientChannelShortId.set(conn.channel().id().asShortText());
        clientOpsLongId.set(((ChannelOperationsId) conn).asLongText());
        clientChannelId.set(conn.channel().toString());
        clientRequestId.set(req.requestId());
        latch.countDown();
    });
    client.get().uri("/").responseContent().aggregate().asString().as(StepVerifier::create).expectNext("testIds").expectComplete().verify(Duration.ofSeconds(5));
    assertThat(latch.await(5, TimeUnit.SECONDS)).isTrue();
    assertThat(serverChannelShortId.get()).isNotNull();
    assertThat(serverRequestId.get()).isNotNull();
    assertThat(serverOpsShortId.get()).isNotNull().isEqualTo(serverChannelShortId.get() + "-1").isEqualTo(serverRequestId.get());
    int originalChannelIdPrefixLength = "[id: 0x".length();
    assertThat(serverChannelId.get()).isNotNull();
    assertThat(serverOpsLongId.get() + ']').isNotNull().isEqualTo(serverChannelId.get().substring(originalChannelIdPrefixLength).replace(serverChannelShortId.get(), serverChannelShortId.get() + "-1"));
    assertThat(clientChannelShortId.get()).isNotNull();
    assertThat(clientRequestId.get()).isNotNull();
    assertThat(clientOpsShortId.get()).isNotNull().isEqualTo(clientChannelShortId.get() + "-1").isEqualTo(clientRequestId.get());
    assertThat(clientChannelId.get()).isNotNull();
    assertThat(clientOpsLongId.get() + ']').isNotNull().isEqualTo(clientChannelId.get().substring(originalChannelIdPrefixLength).replace(clientChannelShortId.get(), clientChannelShortId.get() + "-1"));
    client.get().uri("/").responseContent().aggregate().asString().as(StepVerifier::create).expectNext("testIds").expectComplete().verify(Duration.ofSeconds(5));
    assertThat(latch.await(5, TimeUnit.SECONDS)).isTrue();
    assertThat(serverChannelShortId.get()).isNotNull();
    assertThat(serverRequestId.get()).isNotNull();
    assertThat(serverOpsShortId.get()).isNotNull().isEqualTo(serverChannelShortId.get() + "-2").isEqualTo(serverRequestId.get());
    assertThat(serverChannelId.get()).isNotNull();
    assertThat(serverOpsLongId.get() + ']').isNotNull().isEqualTo(serverChannelId.get().substring(originalChannelIdPrefixLength).replace(serverChannelShortId.get(), serverChannelShortId.get() + "-2"));
    assertThat(clientChannelShortId.get()).isNotNull();
    assertThat(clientRequestId.get()).isNotNull();
    assertThat(clientOpsShortId.get()).isNotNull().isEqualTo(clientChannelShortId.get() + "-2").isEqualTo(clientRequestId.get());
    assertThat(clientChannelId.get()).isNotNull();
    assertThat(clientOpsLongId.get() + ']').isNotNull().isEqualTo(clientChannelId.get().substring(originalChannelIdPrefixLength).replace(clientChannelShortId.get(), clientChannelShortId.get() + "-2"));
}
Also used : Sinks(reactor.core.publisher.Sinks) StepVerifier(reactor.test.StepVerifier) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) BaseHttpTest(reactor.netty.BaseHttpTest) Tuple2(reactor.util.function.Tuple2) AtomicReference(java.util.concurrent.atomic.AtomicReference) Unpooled(io.netty.buffer.Unpooled) ByteBuf(io.netty.buffer.ByteBuf) ByteBufFlux(reactor.netty.ByteBufFlux) Duration(java.time.Duration) Assertions.assertThatExceptionOfType(org.assertj.core.api.Assertions.assertThatExceptionOfType) Assertions(org.assertj.core.api.Assertions) ChannelOperationsId(reactor.netty.ChannelOperationsId) HttpHeaderValues(io.netty.handler.codec.http.HttpHeaderValues) IOException(java.io.IOException) Mono(reactor.core.publisher.Mono) TimeUnit(java.util.concurrent.TimeUnit) Test(org.junit.jupiter.api.Test) CountDownLatch(java.util.concurrent.CountDownLatch) Flux(reactor.core.publisher.Flux) HttpServer(reactor.netty.http.server.HttpServer) HttpHeaderNames(io.netty.handler.codec.http.HttpHeaderNames) ConnectionProvider(reactor.netty.resources.ConnectionProvider) HttpClient(reactor.netty.http.client.HttpClient) ChannelOperationsId(reactor.netty.ChannelOperationsId) HttpClient(reactor.netty.http.client.HttpClient) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) ConnectionProvider(reactor.netty.resources.ConnectionProvider) BaseHttpTest(reactor.netty.BaseHttpTest) Test(org.junit.jupiter.api.Test)

Example 2 with ChannelOperationsId

use of reactor.netty.ChannelOperationsId in project reactor-netty by reactor.

the class ReactorNettyLoggingHandler method channelString.

private String channelString(Channel channel) {
    String channelStr;
    StringBuilder result;
    Connection connection = Connection.from(channel);
    if (connection instanceof ChannelOperationsId) {
        channelStr = ((ChannelOperationsId) connection).asLongText();
        if (channelStr.charAt(0) != TRACE_ID_PREFIX) {
            result = new StringBuilder(1 + channelStr.length() + 1).append(CHANNEL_ID_PREFIX).append(channelStr).append(CHANNEL_ID_SUFFIX);
        } else {
            result = new StringBuilder(channelStr);
        }
    } else {
        channelStr = channel.toString();
        if (channelStr.charAt(0) == CHANNEL_ID_PREFIX) {
            channelStr = channelStr.substring(ORIGINAL_CHANNEL_ID_PREFIX_LENGTH);
            result = new StringBuilder(1 + channelStr.length()).append(CHANNEL_ID_PREFIX).append(channelStr);
        } else {
            int ind = channelStr.indexOf(ORIGINAL_CHANNEL_ID_PREFIX);
            result = new StringBuilder(1 + (channelStr.length() - ORIGINAL_CHANNEL_ID_PREFIX_LENGTH)).append(channelStr.substring(0, ind)).append(CHANNEL_ID_PREFIX).append(channelStr.substring(ind + ORIGINAL_CHANNEL_ID_PREFIX_LENGTH));
        }
    }
    return result.toString();
}
Also used : ChannelOperationsId(reactor.netty.ChannelOperationsId) Connection(reactor.netty.Connection)

Aggregations

ChannelOperationsId (reactor.netty.ChannelOperationsId)2 ByteBuf (io.netty.buffer.ByteBuf)1 Unpooled (io.netty.buffer.Unpooled)1 HttpHeaderNames (io.netty.handler.codec.http.HttpHeaderNames)1 HttpHeaderValues (io.netty.handler.codec.http.HttpHeaderValues)1 IOException (java.io.IOException)1 Duration (java.time.Duration)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 TimeUnit (java.util.concurrent.TimeUnit)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 Assertions (org.assertj.core.api.Assertions)1 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)1 Assertions.assertThatExceptionOfType (org.assertj.core.api.Assertions.assertThatExceptionOfType)1 Test (org.junit.jupiter.api.Test)1 Flux (reactor.core.publisher.Flux)1 Mono (reactor.core.publisher.Mono)1 Sinks (reactor.core.publisher.Sinks)1 BaseHttpTest (reactor.netty.BaseHttpTest)1 ByteBufFlux (reactor.netty.ByteBufFlux)1