|
15 | 15 | import java.util.Arrays; |
16 | 16 | import java.util.Collections; |
17 | 17 | import java.util.List; |
| 18 | +import java.util.concurrent.CountDownLatch; |
18 | 19 | import java.util.concurrent.ExecutorService; |
19 | 20 | import java.util.concurrent.Executors; |
20 | 21 | import java.util.concurrent.Future; |
| 22 | +import java.util.concurrent.TimeUnit; |
21 | 23 | import java.util.concurrent.atomic.AtomicBoolean; |
22 | 24 | import java.util.stream.IntStream; |
23 | 25 |
|
@@ -154,24 +156,28 @@ void testConcurrentEvaluationsDuringReplacements() throws Exception { |
154 | 156 | syncSegment(userIdRule("IsOneOf", first), Collections.emptyList(), Collections.emptyList()); |
155 | 157 |
|
156 | 158 | AtomicBoolean done = new AtomicBoolean(); |
| 159 | + CountDownLatch started = new CountDownLatch(4); |
157 | 160 | ExecutorService readers = Executors.newFixedThreadPool(4); |
158 | 161 | List<Future<Integer>> results = new ArrayList<>(); |
159 | 162 | try { |
160 | 163 | for (int t = 0; t < 4; t++) { |
161 | 164 | results.add(readers.submit(() -> { |
162 | 165 | int evaluations = 0; |
163 | | - while (!done.get()) { |
| 166 | + do { |
164 | 167 | for (int index : new int[]{0, size - 1, size, 2 * size - 1}) { |
165 | 168 | String reason = detail(index, false).getReason(); |
166 | 169 | if (!reason.equals(REASON_RULE_MATCH) && !reason.equals(REASON_FALLTHROUGH)) { |
167 | 170 | throw new AssertionError("user " + index + ": " + reason); |
168 | 171 | } |
169 | 172 | evaluations++; |
170 | 173 | } |
171 | | - } |
| 174 | + started.countDown(); |
| 175 | + } while (!done.get()); |
172 | 176 | return evaluations; |
173 | 177 | })); |
174 | 178 | } |
| 179 | + // replace only once every reader is evaluating, so replacements overlap evaluations |
| 180 | + assertTrue(started.await(10, TimeUnit.SECONDS)); |
175 | 181 | for (int i = 0; i < 200; i++) { |
176 | 182 | syncSegment(userIdRule("IsOneOf", i % 2 == 0 ? second : first), Collections.emptyList(), Collections.emptyList()); |
177 | 183 | } |
|
0 commit comments