1313import io .opentelemetry .exporter .internal .marshal .Marshaler ;
1414import io .opentelemetry .sdk .common .CompletableResultCode ;
1515import java .io .IOException ;
16+ import java .net .ServerSocket ;
1617import java .time .Duration ;
1718import java .util .Collections ;
1819import java .util .Set ;
@@ -67,9 +68,15 @@ void shutdown_CompletableResultCodeShouldWaitForThreads() throws Exception {
6768 // This test verifies that shutdown() returns a CompletableResultCode that only
6869 // completes AFTER threads terminate, not immediately.
6970
71+ // Allocate an ephemeral port and immediately close it to get a port with nothing listening
72+ int port ;
73+ try (ServerSocket socket = new ServerSocket (0 )) {
74+ port = socket .getLocalPort ();
75+ }
76+
7077 OkHttpGrpcSender <TestMarshaler > sender =
7178 new OkHttpGrpcSender <>(
72- "http://localhost:54321" , // Non-existent endpoint
79+ "http://localhost:" + port , // Non-existent endpoint to trigger thread creation
7380 null ,
7481 Duration .ofSeconds (10 ).toNanos (),
7582 Duration .ofSeconds (10 ).toNanos (),
@@ -79,7 +86,6 @@ void shutdown_CompletableResultCodeShouldWaitForThreads() throws Exception {
7986 null ,
8087 null );
8188
82- // Send a request to trigger thread creation
8389 CompletableResultCode sendResult = new CompletableResultCode ();
8490 sender .send (new TestMarshaler (), response -> sendResult .succeed (), error -> sendResult .fail ());
8591
@@ -88,9 +94,9 @@ void shutdown_CompletableResultCodeShouldWaitForThreads() throws Exception {
8894
8995 CompletableResultCode shutdownResult = sender .shutdown ();
9096
91- // The key test: the CompletableResultCode should NOT be done immediately
97+ // The key test: the CompletableResultCode should NOT be done() immediately
9298 // because we need to wait for threads to terminate.
93- // With the old code (immediate ofSuccess()) , this would fail.
99+ // Before #7840 , this would fail.
94100 assertFalse (
95101 shutdownResult .isDone (),
96102 "CompletableResultCode should not be done immediately - it should wait for thread termination" );
0 commit comments