Rework how channel is closed in netty40 ssl test (#12874)

This commit is contained in:
Lauri Tulmin 2024-12-11 07:48:52 +02:00 committed by GitHub
parent 4cc4e1c0b2
commit ccedc40de9
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 16 additions and 30 deletions

View File

@ -32,6 +32,7 @@ import io.netty.handler.codec.http.HttpHeaders;
import io.netty.handler.codec.http.HttpMethod; import io.netty.handler.codec.http.HttpMethod;
import io.netty.handler.codec.http.HttpVersion; import io.netty.handler.codec.http.HttpVersion;
import io.netty.handler.ssl.SslHandler; import io.netty.handler.ssl.SslHandler;
import io.opentelemetry.instrumentation.testing.internal.AutoCleanupExtension;
import io.opentelemetry.instrumentation.testing.junit.AgentInstrumentationExtension; import io.opentelemetry.instrumentation.testing.junit.AgentInstrumentationExtension;
import io.opentelemetry.instrumentation.testing.junit.InstrumentationExtension; import io.opentelemetry.instrumentation.testing.junit.InstrumentationExtension;
import io.opentelemetry.instrumentation.testing.junit.http.HttpClientTestServer; import io.opentelemetry.instrumentation.testing.junit.http.HttpClientTestServer;
@ -44,7 +45,6 @@ import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException; import java.util.concurrent.TimeoutException;
import java.util.concurrent.atomic.AtomicReference;
import javax.net.ssl.SSLContext; import javax.net.ssl.SSLContext;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.AfterAll;
@ -57,6 +57,8 @@ class Netty40ClientSslTest {
@RegisterExtension @RegisterExtension
static final InstrumentationExtension testing = AgentInstrumentationExtension.create(); static final InstrumentationExtension testing = AgentInstrumentationExtension.create();
@RegisterExtension static final AutoCleanupExtension cleanup = AutoCleanupExtension.create();
private static HttpClientTestServer server; private static HttpClientTestServer server;
private static EventLoopGroup eventLoopGroup; private static EventLoopGroup eventLoopGroup;
@ -116,25 +118,19 @@ class Netty40ClientSslTest {
private static Throwable getThrowable( private static Throwable getThrowable(
Bootstrap bootstrap, URI uri, DefaultFullHttpRequest request) { Bootstrap bootstrap, URI uri, DefaultFullHttpRequest request) {
AtomicReference<Channel> channel = new AtomicReference<>();
Throwable thrown = Throwable thrown =
catchThrowable( catchThrowable(
() -> () ->
testing.runWithSpan( testing.runWithSpan(
"parent", "parent",
() -> { () -> {
try { Channel channel =
channel.set( bootstrap.connect(uri.getHost(), uri.getPort()).sync().channel();
bootstrap.connect(uri.getHost(), uri.getPort()).sync().channel()); cleanup.deferCleanup(() -> channel.close().sync());
CompletableFuture<Integer> result = new CompletableFuture<>(); CompletableFuture<Integer> result = new CompletableFuture<>();
channel.get().pipeline().addLast(new ClientHandler(result)); channel.pipeline().addLast(new ClientHandler(result));
channel.get().writeAndFlush(request).get(10, TimeUnit.SECONDS); channel.writeAndFlush(request).get(10, TimeUnit.SECONDS);
result.get(10, TimeUnit.SECONDS); result.get(10, TimeUnit.SECONDS);
} finally {
if (channel.get() != null) {
channel.get().close();
}
}
})); }));
// Then // Then
@ -160,22 +156,16 @@ class Netty40ClientSslTest {
HttpVersion.HTTP_1_1, HttpMethod.GET, uri.getPath(), Unpooled.EMPTY_BUFFER); HttpVersion.HTTP_1_1, HttpMethod.GET, uri.getPath(), Unpooled.EMPTY_BUFFER);
HttpHeaders.setHost(request, uri.getHost() + ":" + uri.getPort()); HttpHeaders.setHost(request, uri.getHost() + ":" + uri.getPort());
AtomicReference<Channel> channel = new AtomicReference<>();
// when // when
testing.runWithSpan( testing.runWithSpan(
"parent", "parent",
() -> { () -> {
try { Channel channel = bootstrap.connect(uri.getHost(), uri.getPort()).sync().channel();
channel.set(bootstrap.connect(uri.getHost(), uri.getPort()).sync().channel()); cleanup.deferCleanup(() -> channel.close().sync());
CompletableFuture<Integer> result = new CompletableFuture<>(); CompletableFuture<Integer> result = new CompletableFuture<>();
channel.get().pipeline().addLast(new ClientHandler(result)); channel.pipeline().addLast(new ClientHandler(result));
channel.get().writeAndFlush(request).get(10, TimeUnit.SECONDS); channel.writeAndFlush(request).get(10, TimeUnit.SECONDS);
result.get(10, TimeUnit.SECONDS); result.get(10, TimeUnit.SECONDS);
} finally {
if (channel.get() != null) {
channel.get().close();
}
}
}); });
// then // then
@ -207,10 +197,6 @@ class Netty40ClientSslTest {
span -> { span -> {
span.hasName("test-http-server").hasKind(SERVER).hasParent(trace.getSpan(3)); span.hasName("test-http-server").hasKind(SERVER).hasParent(trace.getSpan(3));
})); }));
if (channel.get() != null) {
channel.get().close().sync();
}
} }
// list of default ciphers copied from netty's JdkSslContext // list of default ciphers copied from netty's JdkSslContext