|
10 | 10 |
|
11 | 11 | import io.opentelemetry.exporter.internal.RetryUtil; |
12 | 12 | import io.opentelemetry.exporter.internal.grpc.GrpcExporterUtil; |
| 13 | +import io.opentelemetry.exporter.internal.marshal.Marshaler; |
| 14 | +import io.opentelemetry.sdk.common.CompletableResultCode; |
| 15 | +import java.io.IOException; |
| 16 | +import java.net.ServerSocket; |
| 17 | +import java.time.Duration; |
| 18 | +import java.util.Collections; |
13 | 19 | import java.util.Set; |
| 20 | +import java.util.concurrent.CountDownLatch; |
| 21 | +import java.util.concurrent.ExecutorService; |
| 22 | +import java.util.concurrent.Executors; |
| 23 | +import java.util.concurrent.TimeUnit; |
14 | 24 | import okhttp3.MediaType; |
15 | 25 | import okhttp3.Protocol; |
16 | 26 | import okhttp3.Request; |
@@ -56,4 +66,212 @@ private static Response createResponse(int httpCode, String grpcStatus, String m |
56 | 66 | .header(GRPC_STATUS, grpcStatus) |
57 | 67 | .build(); |
58 | 68 | } |
| 69 | + |
| 70 | + @Test |
| 71 | + void shutdown_CompletableResultCodeShouldWaitForThreads() throws Exception { |
| 72 | + // This test verifies that shutdown() returns a CompletableResultCode that only |
| 73 | + // completes AFTER threads terminate, not immediately. |
| 74 | + |
| 75 | + // Allocate an ephemeral port and immediately close it to get a port with nothing listening |
| 76 | + int port; |
| 77 | + try (ServerSocket socket = new ServerSocket(0)) { |
| 78 | + port = socket.getLocalPort(); |
| 79 | + } |
| 80 | + |
| 81 | + OkHttpGrpcSender<TestMarshaler> sender = |
| 82 | + new OkHttpGrpcSender<>( |
| 83 | + "http://localhost:" + port, // Non-existent endpoint to trigger thread creation |
| 84 | + null, |
| 85 | + Duration.ofSeconds(10).toNanos(), |
| 86 | + Duration.ofSeconds(10).toNanos(), |
| 87 | + Collections::emptyMap, |
| 88 | + null, |
| 89 | + null, |
| 90 | + null, |
| 91 | + null); |
| 92 | + |
| 93 | + CompletableResultCode sendResult = new CompletableResultCode(); |
| 94 | + sender.send(new TestMarshaler(), response -> sendResult.succeed(), error -> sendResult.fail()); |
| 95 | + |
| 96 | + // Give threads time to start |
| 97 | + Thread.sleep(500); |
| 98 | + |
| 99 | + CompletableResultCode shutdownResult = sender.shutdown(); |
| 100 | + |
| 101 | + // The key test: the CompletableResultCode should NOT be done() immediately |
| 102 | + // because we need to wait for threads to terminate. |
| 103 | + // Before #7840, this would fail. |
| 104 | + assertFalse( |
| 105 | + shutdownResult.isDone(), |
| 106 | + "CompletableResultCode should not be done immediately - it should wait for thread termination"); |
| 107 | + |
| 108 | + // Now wait for it to complete |
| 109 | + shutdownResult.join(10, java.util.concurrent.TimeUnit.SECONDS); |
| 110 | + assertTrue(shutdownResult.isDone(), "CompletableResultCode should be done after waiting"); |
| 111 | + assertTrue(shutdownResult.isSuccess(), "Shutdown should complete successfully"); |
| 112 | + } |
| 113 | + |
| 114 | + @Test |
| 115 | + void shutdown_NonManagedExecutor_ReturnsImmediately() { |
| 116 | + // This test verifies that when using a non-managed executor (custom ExecutorService), |
| 117 | + // shutdown() returns an already-completed CompletableResultCode immediately. |
| 118 | + |
| 119 | + // Create a custom ExecutorService - this makes the executor non-managed |
| 120 | + ExecutorService customExecutor = Executors.newSingleThreadExecutor(); |
| 121 | + |
| 122 | + try { |
| 123 | + OkHttpGrpcSender<TestMarshaler> sender = |
| 124 | + new OkHttpGrpcSender<>( |
| 125 | + "http://localhost:8080", |
| 126 | + null, |
| 127 | + Duration.ofSeconds(10).toNanos(), |
| 128 | + Duration.ofSeconds(10).toNanos(), |
| 129 | + Collections::emptyMap, |
| 130 | + null, |
| 131 | + null, |
| 132 | + null, |
| 133 | + customExecutor); // Pass custom executor -> managedExecutor = false |
| 134 | + |
| 135 | + CompletableResultCode shutdownResult = sender.shutdown(); |
| 136 | + |
| 137 | + // Should complete immediately since executor is not managed |
| 138 | + assertTrue( |
| 139 | + shutdownResult.isDone(), |
| 140 | + "CompletableResultCode should be done immediately for non-managed executor"); |
| 141 | + assertTrue(shutdownResult.isSuccess(), "Shutdown should complete successfully"); |
| 142 | + } finally { |
| 143 | + // Clean up the custom executor |
| 144 | + customExecutor.shutdownNow(); |
| 145 | + } |
| 146 | + } |
| 147 | + |
| 148 | + @Test |
| 149 | + void shutdown_ExecutorDoesNotTerminateInTime_LogsWarningButSucceeds() throws Exception { |
| 150 | + // This test verifies that when threads don't terminate within 5 seconds, |
| 151 | + // a warning is logged but shutdown still succeeds. |
| 152 | + |
| 153 | + // Allocate an ephemeral port |
| 154 | + int port; |
| 155 | + try (ServerSocket socket = new ServerSocket(0)) { |
| 156 | + port = socket.getLocalPort(); |
| 157 | + } |
| 158 | + |
| 159 | + // Create sender with managed executor (default) |
| 160 | + OkHttpGrpcSender<TestMarshaler> sender = |
| 161 | + new OkHttpGrpcSender<>( |
| 162 | + "http://localhost:" + port, |
| 163 | + null, |
| 164 | + Duration.ofSeconds(10).toNanos(), |
| 165 | + Duration.ofSeconds(10).toNanos(), |
| 166 | + Collections::emptyMap, |
| 167 | + null, |
| 168 | + null, |
| 169 | + null, |
| 170 | + null); // null executor = managed |
| 171 | + |
| 172 | + // Start multiple requests to ensure threads are busy |
| 173 | + CountDownLatch blockCallbacks = new CountDownLatch(1); |
| 174 | + for (int i = 0; i < 3; i++) { |
| 175 | + sender.send( |
| 176 | + new TestMarshaler(), |
| 177 | + response -> { |
| 178 | + try { |
| 179 | + // Block in callback for longer than the 5-second timeout |
| 180 | + blockCallbacks.await(10, TimeUnit.SECONDS); |
| 181 | + } catch (InterruptedException e) { |
| 182 | + Thread.currentThread().interrupt(); |
| 183 | + } |
| 184 | + }, |
| 185 | + error -> { |
| 186 | + try { |
| 187 | + // Block in callback for longer than the 5-second timeout |
| 188 | + blockCallbacks.await(10, TimeUnit.SECONDS); |
| 189 | + } catch (InterruptedException e) { |
| 190 | + Thread.currentThread().interrupt(); |
| 191 | + } |
| 192 | + }); |
| 193 | + } |
| 194 | + |
| 195 | + // Give threads time to start (same pattern as existing test) |
| 196 | + Thread.sleep(500); |
| 197 | + |
| 198 | + // Shutdown will now try to terminate threads that are blocked |
| 199 | + CompletableResultCode shutdownResult = sender.shutdown(); |
| 200 | + |
| 201 | + // The shutdown should eventually complete successfully |
| 202 | + // even though threads didn't terminate in 5 seconds |
| 203 | + assertTrue( |
| 204 | + shutdownResult.join(10, TimeUnit.SECONDS).isSuccess(), |
| 205 | + "Shutdown should succeed even when threads don't terminate quickly"); |
| 206 | + |
| 207 | + // Release the blocking callbacks |
| 208 | + blockCallbacks.countDown(); |
| 209 | + } |
| 210 | + |
| 211 | + @Test |
| 212 | + void shutdown_InterruptedWhileWaiting_StillSucceeds() throws Exception { |
| 213 | + // This test verifies that if the shutdown thread is interrupted while waiting |
| 214 | + // for termination, it still marks the shutdown as successful. |
| 215 | + |
| 216 | + // Allocate an ephemeral port |
| 217 | + int port; |
| 218 | + try (ServerSocket socket = new ServerSocket(0)) { |
| 219 | + port = socket.getLocalPort(); |
| 220 | + } |
| 221 | + |
| 222 | + OkHttpGrpcSender<TestMarshaler> sender = |
| 223 | + new OkHttpGrpcSender<>( |
| 224 | + "http://localhost:" + port, |
| 225 | + null, |
| 226 | + Duration.ofSeconds(10).toNanos(), |
| 227 | + Duration.ofSeconds(10).toNanos(), |
| 228 | + Collections::emptyMap, |
| 229 | + null, |
| 230 | + null, |
| 231 | + null, |
| 232 | + null); |
| 233 | + |
| 234 | + // Trigger some activity |
| 235 | + sender.send(new TestMarshaler(), response -> {}, error -> {}); |
| 236 | + |
| 237 | + // Give threads time to start (same pattern as existing test) |
| 238 | + Thread.sleep(500); |
| 239 | + |
| 240 | + // Start shutdown |
| 241 | + CompletableResultCode shutdownResult = sender.shutdown(); |
| 242 | + |
| 243 | + // Give the shutdown thread a moment to start |
| 244 | + Thread.sleep(100); |
| 245 | + |
| 246 | + // Find and interrupt the okhttp-shutdown thread to trigger the InterruptedException path |
| 247 | + Thread[] threads = new Thread[Thread.activeCount() + 10]; |
| 248 | + int count = Thread.enumerate(threads); |
| 249 | + for (int i = 0; i < count; i++) { |
| 250 | + Thread thread = threads[i]; |
| 251 | + if (thread != null && thread.getName().equals("okhttp-shutdown")) { |
| 252 | + // Interrupt the shutdown thread to test the InterruptedException handling |
| 253 | + thread.interrupt(); |
| 254 | + break; |
| 255 | + } |
| 256 | + } |
| 257 | + |
| 258 | + // Even with interruption, shutdown should still succeed |
| 259 | + assertTrue( |
| 260 | + shutdownResult.join(10, TimeUnit.SECONDS).isSuccess(), |
| 261 | + "Shutdown should succeed even when interrupted"); |
| 262 | + } |
| 263 | + |
| 264 | + /** Simple test marshaler for testing purposes. */ |
| 265 | + private static class TestMarshaler extends Marshaler { |
| 266 | + @Override |
| 267 | + public int getBinarySerializedSize() { |
| 268 | + return 0; |
| 269 | + } |
| 270 | + |
| 271 | + @Override |
| 272 | + protected void writeTo(io.opentelemetry.exporter.internal.marshal.Serializer output) |
| 273 | + throws IOException { |
| 274 | + // Empty marshaler |
| 275 | + } |
| 276 | + } |
59 | 277 | } |
0 commit comments