32
32
import io .netty .handler .codec .http .HttpMethod ;
33
33
import io .netty .handler .codec .http .HttpVersion ;
34
34
import io .netty .handler .ssl .SslHandler ;
35
+ import io .opentelemetry .instrumentation .testing .internal .AutoCleanupExtension ;
35
36
import io .opentelemetry .instrumentation .testing .junit .AgentInstrumentationExtension ;
36
37
import io .opentelemetry .instrumentation .testing .junit .InstrumentationExtension ;
37
38
import io .opentelemetry .instrumentation .testing .junit .http .HttpClientTestServer ;
44
45
import java .util .concurrent .ExecutionException ;
45
46
import java .util .concurrent .TimeUnit ;
46
47
import java .util .concurrent .TimeoutException ;
47
- import java .util .concurrent .atomic .AtomicReference ;
48
48
import javax .net .ssl .SSLContext ;
49
49
import org .jetbrains .annotations .NotNull ;
50
50
import org .junit .jupiter .api .AfterAll ;
@@ -57,6 +57,8 @@ class Netty40ClientSslTest {
57
57
@ RegisterExtension
58
58
static final InstrumentationExtension testing = AgentInstrumentationExtension .create ();
59
59
60
+ @ RegisterExtension static final AutoCleanupExtension cleanup = AutoCleanupExtension .create ();
61
+
60
62
private static HttpClientTestServer server ;
61
63
private static EventLoopGroup eventLoopGroup ;
62
64
@@ -116,25 +118,19 @@ public void shouldFailSslHandshake() {
116
118
117
119
private static Throwable getThrowable (
118
120
Bootstrap bootstrap , URI uri , DefaultFullHttpRequest request ) {
119
- AtomicReference <Channel > channel = new AtomicReference <>();
120
121
Throwable thrown =
121
122
catchThrowable (
122
123
() ->
123
124
testing .runWithSpan (
124
125
"parent" ,
125
126
() -> {
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 );
138
134
}));
139
135
140
136
// Then
@@ -160,22 +156,16 @@ public void shouldSuccessfullyEstablishSslHandshake() throws Exception {
160
156
HttpVersion .HTTP_1_1 , HttpMethod .GET , uri .getPath (), Unpooled .EMPTY_BUFFER );
161
157
HttpHeaders .setHost (request , uri .getHost () + ":" + uri .getPort ());
162
158
163
- AtomicReference <Channel > channel = new AtomicReference <>();
164
159
// when
165
160
testing .runWithSpan (
166
161
"parent" ,
167
162
() -> {
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 );
179
169
});
180
170
181
171
// then
@@ -207,10 +197,6 @@ public void shouldSuccessfullyEstablishSslHandshake() throws Exception {
207
197
span -> {
208
198
span .hasName ("test-http-server" ).hasKind (SERVER ).hasParent (trace .getSpan (3 ));
209
199
}));
210
-
211
- if (channel .get () != null ) {
212
- channel .get ().close ().sync ();
213
- }
214
200
}
215
201
216
202
// list of default ciphers copied from netty's JdkSslContext
0 commit comments