Skip to content

Commit f0b06cf

Browse files
emmaeng700oz-agent
andcommitted
test: replace Thread.sleep with sync primitives in tests
Replace flaky Thread.sleep calls with proper synchronization: - SharedProcessorTest: use Semaphore(0).acquire() instead of Thread.sleep(10s) so the worker blocks until interrupted by shutdownNow(), making the test deterministic and instant. - KubernetesReconcilerCreatorTest: use Wait.poll() instead of Thread.sleep(500ms) to actively poll until the work queue is populated, making the test faster and non-flaky. Addresses part of #1223. Co-Authored-By: Oz <oz-agent@warp.dev>
1 parent 1c1dad0 commit f0b06cf

2 files changed

Lines changed: 9 additions & 4 deletions

File tree

spring/src/test/java/io/kubernetes/client/spring/extended/controller/KubernetesReconcilerCreatorTest.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import io.kubernetes.client.extended.controller.reconciler.Reconciler;
2323
import io.kubernetes.client.extended.controller.reconciler.Request;
2424
import io.kubernetes.client.extended.controller.reconciler.Result;
25+
import io.kubernetes.client.extended.wait.Wait;
2526
import io.kubernetes.client.extended.workqueue.WorkQueue;
2627
import io.kubernetes.client.informer.SharedInformer;
2728
import io.kubernetes.client.informer.SharedInformerFactory;
@@ -46,6 +47,7 @@
4647
import io.kubernetes.client.spring.extended.controller.annotation.UpdateWatchEventFilter;
4748
import io.kubernetes.client.spring.extended.controller.factory.KubernetesControllerFactory;
4849
import io.kubernetes.client.util.ClientBuilder;
50+
import java.time.Duration;
4951
import java.util.LinkedList;
5052
import java.util.function.Function;
5153
import jakarta.annotation.Resource;
@@ -194,9 +196,10 @@ void simplePodController() throws InterruptedException {
194196
}
195197
});
196198

197-
Thread.sleep(500);
198-
199199
WorkQueue<Request> workQueue = ((DefaultController) testController).getWorkQueue();
200+
boolean itemAdded =
201+
Wait.poll(Duration.ofMillis(10), Duration.ofSeconds(5), () -> workQueue.length() >= 1);
202+
assertThat(itemAdded).isTrue();
200203
assertThat(workQueue.length()).isEqualTo(1);
201204
assertThat(workQueue.get().getName()).isEqualTo("foo");
202205
sharedInformerFactory.stopAllRegisteredInformers();

util/src/test/java/io/kubernetes/client/informer/cache/SharedProcessorTest.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import java.time.Duration;
2222
import java.util.concurrent.CountDownLatch;
2323
import java.util.concurrent.Executors;
24+
import java.util.concurrent.Semaphore;
2425
import org.junit.jupiter.api.Test;
2526

2627
class SharedProcessorTest {
@@ -69,11 +70,12 @@ void shutdownGracefully() throws InterruptedException {
6970
TestWorker<V1Pod> slowWorker = new TestWorker<>(null, 0);
7071
final boolean[] interrupted = {false};
7172
CountDownLatch latch = new CountDownLatch(1);
73+
Semaphore blocker = new Semaphore(0);
7274
slowWorker.setTask(
7375
() -> {
7476
try {
75-
// sleep 10s so that it could be interrupted by shutdownNow()
76-
Thread.sleep(10 * 1000);
77+
// block until interrupted by shutdownNow()
78+
blocker.acquire();
7779
} catch (InterruptedException e) {
7880
interrupted[0] = true;
7981
} finally {

0 commit comments

Comments
 (0)