Skip to content

Commit 8b949ef

Browse files
committed
test(util): Replace sleep with latch in ExecCallbacksTest
Replaces the arbitrary 30-second sleep with a latch, preserving timeout behavior while allowing the background callback to be released when the test finishes.
1 parent 9872bad commit 8b949ef

1 file changed

Lines changed: 18 additions & 12 deletions

File tree

‎util/src/test/java/io/kubernetes/client/ExecCallbacksTest.java‎

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import java.util.ArrayList;
2626
import java.util.List;
2727
import java.util.concurrent.CompletableFuture;
28+
import java.util.concurrent.CountDownLatch;
2829
import java.util.concurrent.ExecutionException;
2930
import java.util.concurrent.Future;
3031
import java.util.concurrent.TimeUnit;
@@ -37,18 +38,23 @@ class ExecCallbacksTest {
3738

3839
@Test
3940
void maxTimeout() throws Exception {
40-
Exec exec = getExec((future, io) -> Thread.sleep(30_000L));
41-
42-
long startTime = System.currentTimeMillis();
43-
Future<Integer> promise =
44-
exec.exec("ns-1", "pod1", null, null, null, 1_000L, true, "random-command");
45-
int exitCode = promise.get(10_000, TimeUnit.MILLISECONDS);
46-
long delta = System.currentTimeMillis() - startTime;
47-
48-
assertThat(delta)
49-
.isGreaterThanOrEqualTo(1_000L)
50-
.isLessThanOrEqualTo(10_000L);
51-
assertThat(exitCode).isEqualTo(Integer.MAX_VALUE);
41+
CountDownLatch release = new CountDownLatch(1);
42+
try {
43+
Exec exec = getExec((future, io) -> release.await());
44+
45+
long startTime = System.currentTimeMillis();
46+
Future<Integer> promise =
47+
exec.exec("ns-1", "pod1", null, null, null, 1_000L, true, "random-command");
48+
int exitCode = promise.get(10_000, TimeUnit.MILLISECONDS);
49+
long delta = System.currentTimeMillis() - startTime;
50+
51+
assertThat(delta)
52+
.isGreaterThanOrEqualTo(1_000L)
53+
.isLessThanOrEqualTo(10_000L);
54+
assertThat(exitCode).isEqualTo(Integer.MAX_VALUE);
55+
} finally {
56+
release.countDown();
57+
}
5258
}
5359

5460
@Test

0 commit comments

Comments
 (0)