Skip to content

Commit ccedc40

Browse files
authored
Rework how channel is closed in netty40 ssl test (#12874)
1 parent 4cc4e1c commit ccedc40

File tree

1 file changed

+16
-30
lines changed
  • instrumentation/netty/netty-4.0/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/netty/v4_0/client

1 file changed

+16
-30
lines changed

instrumentation/netty/netty-4.0/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/netty/v4_0/client/Netty40ClientSslTest.java

+16-30
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import io.netty.handler.codec.http.HttpMethod;
3333
import io.netty.handler.codec.http.HttpVersion;
3434
import io.netty.handler.ssl.SslHandler;
35+
import io.opentelemetry.instrumentation.testing.internal.AutoCleanupExtension;
3536
import io.opentelemetry.instrumentation.testing.junit.AgentInstrumentationExtension;
3637
import io.opentelemetry.instrumentation.testing.junit.InstrumentationExtension;
3738
import io.opentelemetry.instrumentation.testing.junit.http.HttpClientTestServer;
@@ -44,7 +45,6 @@
4445
import java.util.concurrent.ExecutionException;
4546
import java.util.concurrent.TimeUnit;
4647
import java.util.concurrent.TimeoutException;
47-
import java.util.concurrent.atomic.AtomicReference;
4848
import javax.net.ssl.SSLContext;
4949
import org.jetbrains.annotations.NotNull;
5050
import org.junit.jupiter.api.AfterAll;
@@ -57,6 +57,8 @@ class Netty40ClientSslTest {
5757
@RegisterExtension
5858
static final InstrumentationExtension testing = AgentInstrumentationExtension.create();
5959

60+
@RegisterExtension static final AutoCleanupExtension cleanup = AutoCleanupExtension.create();
61+
6062
private static HttpClientTestServer server;
6163
private static EventLoopGroup eventLoopGroup;
6264

@@ -116,25 +118,19 @@ public void shouldFailSslHandshake() {
116118

117119
private static Throwable getThrowable(
118120
Bootstrap bootstrap, URI uri, DefaultFullHttpRequest request) {
119-
AtomicReference<Channel> channel = new AtomicReference<>();
120121
Throwable thrown =
121122
catchThrowable(
122123
() ->
123124
testing.runWithSpan(
124125
"parent",
125126
() -> {
126-
try {
127-
channel.set(
128-
bootstrap.connect(uri.getHost(), uri.getPort()).sync().channel());
129-
CompletableFuture<Integer> result = new CompletableFuture<>();
130-
channel.get().pipeline().addLast(new ClientHandler(result));
131-
channel.get().writeAndFlush(request).get(10, TimeUnit.SECONDS);
132-
result.get(10, TimeUnit.SECONDS);
133-
} finally {
134-
if (channel.get() != null) {
135-
channel.get().close();
136-
}
137-
}
127+
Channel channel =
128+
bootstrap.connect(uri.getHost(), uri.getPort()).sync().channel();
129+
cleanup.deferCleanup(() -> channel.close().sync());
130+
CompletableFuture<Integer> result = new CompletableFuture<>();
131+
channel.pipeline().addLast(new ClientHandler(result));
132+
channel.writeAndFlush(request).get(10, TimeUnit.SECONDS);
133+
result.get(10, TimeUnit.SECONDS);
138134
}));
139135

140136
// Then
@@ -160,22 +156,16 @@ public void shouldSuccessfullyEstablishSslHandshake() throws Exception {
160156
HttpVersion.HTTP_1_1, HttpMethod.GET, uri.getPath(), Unpooled.EMPTY_BUFFER);
161157
HttpHeaders.setHost(request, uri.getHost() + ":" + uri.getPort());
162158

163-
AtomicReference<Channel> channel = new AtomicReference<>();
164159
// when
165160
testing.runWithSpan(
166161
"parent",
167162
() -> {
168-
try {
169-
channel.set(bootstrap.connect(uri.getHost(), uri.getPort()).sync().channel());
170-
CompletableFuture<Integer> result = new CompletableFuture<>();
171-
channel.get().pipeline().addLast(new ClientHandler(result));
172-
channel.get().writeAndFlush(request).get(10, TimeUnit.SECONDS);
173-
result.get(10, TimeUnit.SECONDS);
174-
} finally {
175-
if (channel.get() != null) {
176-
channel.get().close();
177-
}
178-
}
163+
Channel channel = bootstrap.connect(uri.getHost(), uri.getPort()).sync().channel();
164+
cleanup.deferCleanup(() -> channel.close().sync());
165+
CompletableFuture<Integer> result = new CompletableFuture<>();
166+
channel.pipeline().addLast(new ClientHandler(result));
167+
channel.writeAndFlush(request).get(10, TimeUnit.SECONDS);
168+
result.get(10, TimeUnit.SECONDS);
179169
});
180170

181171
// then
@@ -207,10 +197,6 @@ public void shouldSuccessfullyEstablishSslHandshake() throws Exception {
207197
span -> {
208198
span.hasName("test-http-server").hasKind(SERVER).hasParent(trace.getSpan(3));
209199
}));
210-
211-
if (channel.get() != null) {
212-
channel.get().close().sync();
213-
}
214200
}
215201

216202
// list of default ciphers copied from netty's JdkSslContext

0 commit comments

Comments
 (0)