Skip to content

Commit 289a4ae

Browse files
jimmyjamesclaude
andcommitted
fix: improve TelemetryTest concurrent access test robustness
Assert awaitTermination returns true for clear timeout failures, and wrap test body in try/finally with shutdownNow to prevent thread leaks. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent d2058e8 commit 289a4ae

1 file changed

Lines changed: 25 additions & 18 deletions

File tree

src/test/java/dev/openfga/sdk/telemetry/TelemetryTest.java

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -33,24 +33,31 @@ void shouldReturnSameMetricsInstanceUnderConcurrentAccess() throws InterruptedEx
3333
CountDownLatch startLatch = new CountDownLatch(1);
3434
List<Metrics> results = new CopyOnWriteArrayList<>();
3535

36-
// when
37-
for (int i = 0; i < threadCount; i++) {
38-
executor.submit(() -> {
39-
try {
40-
startLatch.await();
41-
results.add(telemetry.metrics());
42-
} catch (InterruptedException e) {
43-
Thread.currentThread().interrupt();
44-
}
45-
});
36+
try {
37+
// when
38+
for (int i = 0; i < threadCount; i++) {
39+
executor.submit(() -> {
40+
try {
41+
startLatch.await();
42+
results.add(telemetry.metrics());
43+
} catch (InterruptedException e) {
44+
Thread.currentThread().interrupt();
45+
}
46+
});
47+
}
48+
startLatch.countDown();
49+
executor.shutdown();
50+
boolean terminated = executor.awaitTermination(5, java.util.concurrent.TimeUnit.SECONDS);
51+
assertThat(terminated).isTrue();
52+
53+
// then
54+
assertThat(results).hasSize(threadCount);
55+
Metrics expected = results.get(0);
56+
assertThat(results).allSatisfy(m -> assertThat(m).isSameAs(expected));
57+
} finally {
58+
if (!executor.isTerminated()) {
59+
executor.shutdownNow();
60+
}
4661
}
47-
startLatch.countDown();
48-
executor.shutdown();
49-
executor.awaitTermination(5, java.util.concurrent.TimeUnit.SECONDS);
50-
51-
// then
52-
assertThat(results).hasSize(threadCount);
53-
Metrics expected = results.get(0);
54-
assertThat(results).allSatisfy(m -> assertThat(m).isSameAs(expected));
5562
}
5663
}

0 commit comments

Comments
 (0)