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"));
}
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();
}
Aggregations