|
14 | 14 |
|
15 | 15 | import static org.assertj.core.api.Assertions.assertThat; |
16 | 16 |
|
| 17 | +import java.util.HashMap; |
| 18 | +import java.util.Map; |
17 | 19 | import java.util.concurrent.CountDownLatch; |
18 | 20 | import org.junit.jupiter.api.Test; |
19 | 21 | import java.util.concurrent.ConcurrentLinkedQueue; |
@@ -104,48 +106,56 @@ void multiProducerAndConsumers() throws Exception { |
104 | 106 | @Test |
105 | 107 | void addWhileProcessing() throws Exception { |
106 | 108 | 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; |
113 | 111 |
|
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(); |
137 | 123 | } |
138 | 124 |
|
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 | + } |
144 | 155 |
|
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(); |
149 | 159 | } |
150 | 160 |
|
151 | 161 | @Test |
|
0 commit comments