Skip to content

Commit bbf79a6

Browse files
Merge pull request #4910 from sarveshkaushal/sarveshkaushal/flaky-tests
fix: remove Thread.sleep() from DefaultWorkQueueTest
2 parents 769dbeb + fa8de8d commit bbf79a6

1 file changed

Lines changed: 33 additions & 19 deletions

File tree

‎extended/src/test/java/io/kubernetes/client/extended/workqueue/DefaultWorkQueueTest.java‎

Lines changed: 33 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,22 @@
1818
import java.util.Map;
1919
import java.util.concurrent.CountDownLatch;
2020
import org.junit.jupiter.api.Test;
21-
import org.slf4j.Logger;
22-
import org.slf4j.LoggerFactory;
21+
import java.util.concurrent.ConcurrentLinkedQueue;
22+
import java.util.concurrent.Semaphore;
23+
import java.util.concurrent.TimeUnit;
24+
import java.util.concurrent.atomic.AtomicReference;
2325

2426
class DefaultWorkQueueTest {
2527

26-
private static final Logger LOGGER = LoggerFactory.getLogger(DefaultWorkQueueTest.class);
28+
private static final long TIMEOUT_SECONDS = 10;
2729

2830
@Test
2931
void multiProducerAndConsumers() throws Exception {
3032
DefaultWorkQueue<String> queue = new DefaultWorkQueue<>();
3133
final int producerCount = 10;
3234
final int consumerCount = 5;
35+
Semaphore start = new Semaphore(0);
36+
ConcurrentLinkedQueue<Throwable> failures = new ConcurrentLinkedQueue<>();
3337

3438
// Start producers
3539
CountDownLatch producerLatch = new CountDownLatch(producerCount);
@@ -39,12 +43,13 @@ void multiProducerAndConsumers() throws Exception {
3943
new Thread(
4044
() -> {
4145
try {
46+
start.acquire();
4247
for (int j = 0; j < 50; j++) {
4348
queue.add(String.valueOf(num));
44-
Thread.sleep(10);
4549
}
46-
} catch (Exception e) {
47-
// empty body
50+
} catch (InterruptedException e) {
51+
failures.add(e);
52+
Thread.currentThread().interrupt();
4853
} finally {
4954
producerLatch.countDown();
5055
}
@@ -55,38 +60,47 @@ void multiProducerAndConsumers() throws Exception {
5560
// Start consumers
5661
CountDownLatch consumerLatch = new CountDownLatch(consumerCount);
5762
for (int i = 0; i < consumerCount; i++) {
58-
final int num = i;
5963
Thread t =
6064
new Thread(
6165
() -> {
6266
try {
67+
start.acquire();
6368
for (; ; ) {
6469
String item = queue.get();
65-
assertThat(item)
66-
.withFailMessage("Got an item added after shutdown")
67-
.isNotEqualTo("added after shutdown!");
6870
if (item == null) {
6971
return;
7072
}
71-
72-
LOGGER.info("Worker {}: begin processing {}", num, item);
73-
Thread.sleep(50);
74-
LOGGER.info("Worker {}: done processing {}", num, item);
73+
if ("added after shutdown!".equals(item)) {
74+
failures.add(new AssertionError("Got an item added after shutdown"));
75+
}
7576
queue.done(item);
7677
}
77-
} catch (Exception e) {
78-
// empty body
78+
} catch (InterruptedException e) {
79+
failures.add(e);
80+
Thread.currentThread().interrupt();
7981
} finally {
8082
consumerLatch.countDown();
8183
}
8284
});
8385
t.start();
8486
}
8587

86-
producerLatch.await();
87-
queue.shutDown();
88+
start.release(producerCount + consumerCount);
89+
90+
boolean producersFinished;
91+
try {
92+
producersFinished = producerLatch.await(TIMEOUT_SECONDS, TimeUnit.SECONDS);
93+
} finally {
94+
queue.shutDown();
95+
}
8896
queue.add("added after shutdown!");
89-
consumerLatch.await();
97+
98+
assertThat(producersFinished).as("producers finished").isTrue();
99+
assertThat(consumerLatch.await(TIMEOUT_SECONDS, TimeUnit.SECONDS))
100+
.as("consumers finished")
101+
.isTrue();
102+
assertThat(failures).isEmpty();
103+
assertThat(queue.length()).isZero();
90104
}
91105

92106
@Test

0 commit comments

Comments
 (0)