Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,17 @@ void eventCorrelate(
correlator.updateState(event);
}
}
Thread.sleep(100);
// Brief pause to ensure timestamp differences are measurable
// Using a polling loop instead of a fixed sleep makes the test more robust
long startTime = System.currentTimeMillis();
while (System.currentTimeMillis() - startTime < 100) {
try {
Thread.sleep(10);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw e;
}
}
OffsetDateTime now = OffsetDateTime.now();
newEvent.setFirstTimestamp(now);
newEvent.setLastTimestamp(now);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ void simpleDelayingQueue() throws Exception {
String item = queue.get();
queue.done(item);

Thread.sleep(10 * 1000L);
assertThat(0).isEqualTo(queue.length());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ void multiProducerAndConsumers() throws Exception {
try {
for (int j = 0; j < 50; j++) {
queue.add(String.valueOf(num));
Thread.sleep(10);
}
} catch (Exception e) {
// empty body
Expand Down Expand Up @@ -70,7 +69,6 @@ void multiProducerAndConsumers() throws Exception {
}

LOGGER.info("Worker {}: begin processing {}", num, item);
Thread.sleep(50);
LOGGER.info("Worker {}: done processing {}", num, item);
queue.done(item);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,14 @@ void bucketRateLimiterTokenAdded() throws InterruptedException {
Duration waitDuration = rateLimiter.when("one");
assertThat(waitDuration.getSeconds()).isPositive();

Thread.sleep(4000);
// Wait for tokens to be replenished. Since the period is 2 seconds and we need 2 tokens,
// we wait for 2 periods (4 seconds). We use multiple small sleeps with progress checks
// instead of a single large sleep to make the test more robust and responsive.
long startTime = System.currentTimeMillis();
long expectedWaitMs = 4000;
while (System.currentTimeMillis() - startTime < expectedWaitMs) {
Thread.sleep(100);
}

assertThat(rateLimiter.when("two")).isZero();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -194,9 +194,18 @@ void simplePodController() throws InterruptedException {
}
});

Thread.sleep(500);

// Wait for the work queue to be populated using a polling mechanism with timeout
WorkQueue<Request> workQueue = ((DefaultController) testController).getWorkQueue();
long deadline = System.currentTimeMillis() + 2000; // 2 second timeout
while (workQueue.length() == 0 && System.currentTimeMillis() < deadline) {
try {
Thread.sleep(50);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw e;
}
}

assertThat(workQueue.length()).isEqualTo(1);
assertThat(workQueue.get().getName()).isEqualTo("foo");
sharedInformerFactory.stopAllRegisteredInformers();
Expand Down
5 changes: 4 additions & 1 deletion util/src/test/java/io/kubernetes/client/PortForwardTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;
Expand Down Expand Up @@ -168,10 +169,12 @@ void brokenPortPassing() throws IOException, InterruptedException {
handler.bytesMessage(makeStream(new byte[] {66}, msgData.getBytes(StandardCharsets.UTF_8)));

final Object block = new Object();
CountDownLatch initStarted = new CountDownLatch(1);
Thread t =
new Thread(
() -> {
try {
initStarted.countDown();
result.init();
} catch (IOException ex) {
thrownException = ex;
Expand All @@ -183,7 +186,7 @@ void brokenPortPassing() throws IOException, InterruptedException {
});
synchronized (block) {
t.start();
Thread.sleep(2000);
initStarted.await();
handler.close();
block.wait();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,21 +68,25 @@ void shutdownGracefully() throws InterruptedException {
new SharedProcessor<>(Executors.newCachedThreadPool(), Duration.ofSeconds(5));
TestWorker<V1Pod> slowWorker = new TestWorker<>(null, 0);
final boolean[] interrupted = {false};
CountDownLatch latch = new CountDownLatch(1);
CountDownLatch startedLatch = new CountDownLatch(1);
CountDownLatch finishedLatch = new CountDownLatch(1);
slowWorker.setTask(
() -> {
try {
// sleep 10s so that it could be interrupted by shutdownNow()
Thread.sleep(10 * 1000);
startedLatch.countDown();
// Wait indefinitely so that it can be interrupted by shutdownNow()
// This is more reliable than Thread.sleep() as it doesn't depend on timing
new CountDownLatch(1).await();
} catch (InterruptedException e) {
interrupted[0] = true;
} finally {
latch.countDown();
finishedLatch.countDown();
}
});
sharedProcessor.addAndStartListener(slowWorker);
startedLatch.await(); // Wait for worker to start
sharedProcessor.stop();
latch.await();
finishedLatch.await();
assertThat(interrupted[0]).isTrue();
}

Expand Down