Skip to content

Commit fa8de8d

Browse files
fix: remove Thread.sleep() from DefaultWorkQueueTest
1 parent 8083647 commit fa8de8d

1 file changed

Lines changed: 48 additions & 38 deletions

File tree

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

Lines changed: 48 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414

1515
import static org.assertj.core.api.Assertions.assertThat;
1616

17+
import java.util.HashMap;
18+
import java.util.Map;
1719
import java.util.concurrent.CountDownLatch;
1820
import org.junit.jupiter.api.Test;
1921
import java.util.concurrent.ConcurrentLinkedQueue;
@@ -104,48 +106,56 @@ void multiProducerAndConsumers() throws Exception {
104106
@Test
105107
void addWhileProcessing() throws Exception {
106108
DefaultWorkQueue<String> queue = new DefaultWorkQueue<>();
107-
Semaphore itemDequeued = new Semaphore(0);
108-
Semaphore allowDone = new Semaphore(0);
109-
CountDownLatch consumerFinished = new CountDownLatch(1);
110-
AtomicReference<Throwable> failure = new AtomicReference<>();
111-
112-
queue.add("foo");
109+
final int producerCount = 10;
110+
final int consumerCount = 5;
113111

114-
Thread consumer =
115-
new Thread(
116-
() -> {
117-
try {
118-
String item = queue.get();
119-
itemDequeued.release();
120-
allowDone.acquire();
121-
queue.done(item);
122-
} catch (InterruptedException e) {
123-
failure.set(e);
124-
Thread.currentThread().interrupt();
125-
} finally {
126-
consumerFinished.countDown();
127-
}
128-
});
129-
consumer.start();
130-
131-
boolean dequeued = itemDequeued.tryAcquire(TIMEOUT_SECONDS, TimeUnit.SECONDS);
132-
try {
133-
assertThat(dequeued).as("consumer dequeued the item").isTrue();
134-
queue.add("foo");
135-
} finally {
136-
allowDone.release();
112+
// Start producers
113+
CountDownLatch producerLatch = new CountDownLatch(producerCount);
114+
for (int i = 0; i < producerCount; i++) {
115+
final int num = i;
116+
Thread t =
117+
new Thread(
118+
() -> {
119+
queue.add(String.valueOf(num));
120+
producerLatch.countDown();
121+
});
122+
t.start();
137123
}
138124

139-
assertThat(consumerFinished.await(TIMEOUT_SECONDS, TimeUnit.SECONDS))
140-
.as("consumer finished")
141-
.isTrue();
142-
assertThat(failure.get()).isNull();
143-
assertThat(queue.length()).isEqualTo(1);
125+
// Start consumers
126+
CountDownLatch consumerLatch = new CountDownLatch(consumerCount);
127+
for (int i = 0; i < consumerCount; i++) {
128+
Thread t =
129+
new Thread(
130+
() -> {
131+
// Every worker will re-add every item up to two times.
132+
// This tests the dirty-while-processing case.
133+
Map<String, Integer> counters = new HashMap<>();
134+
try {
135+
for (; ; ) {
136+
String item = queue.get();
137+
if (item == null) {
138+
return;
139+
}
140+
counters.putIfAbsent(item, 1);
141+
counters.computeIfPresent(item, (s, integer) -> counters.get(s) + 1);
142+
if (counters.get(item) < 2) {
143+
queue.add(item);
144+
}
145+
queue.done(item);
146+
}
147+
} catch (Exception e) {
148+
// empty body
149+
} finally {
150+
consumerLatch.countDown();
151+
}
152+
});
153+
t.start();
154+
}
144155

145-
String item = queue.get();
146-
assertThat(item).isEqualTo("foo");
147-
queue.done(item);
148-
assertThat(queue.length()).isZero();
156+
producerLatch.await();
157+
queue.shutDown();
158+
consumerLatch.await();
149159
}
150160

151161
@Test

0 commit comments

Comments
 (0)