Skip to content

Commit 920b594

Browse files
committed
test: start replacements once every reader is evaluating
A reader thread that first ran after the replacements finished saw the done flag, returned zero evaluations and failed the test. Each reader now evaluates at least once and the replacements wait for all four, so they always overlap concurrent evaluations.
1 parent a454042 commit 920b594

1 file changed

Lines changed: 8 additions & 2 deletions

File tree

‎src/test/java/co/featbit/server/ListTargetingTest.java‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,11 @@
1515
import java.util.Arrays;
1616
import java.util.Collections;
1717
import java.util.List;
18+
import java.util.concurrent.CountDownLatch;
1819
import java.util.concurrent.ExecutorService;
1920
import java.util.concurrent.Executors;
2021
import java.util.concurrent.Future;
22+
import java.util.concurrent.TimeUnit;
2123
import java.util.concurrent.atomic.AtomicBoolean;
2224
import java.util.stream.IntStream;
2325

@@ -154,24 +156,28 @@ void testConcurrentEvaluationsDuringReplacements() throws Exception {
154156
syncSegment(userIdRule("IsOneOf", first), Collections.emptyList(), Collections.emptyList());
155157

156158
AtomicBoolean done = new AtomicBoolean();
159+
CountDownLatch started = new CountDownLatch(4);
157160
ExecutorService readers = Executors.newFixedThreadPool(4);
158161
List<Future<Integer>> results = new ArrayList<>();
159162
try {
160163
for (int t = 0; t < 4; t++) {
161164
results.add(readers.submit(() -> {
162165
int evaluations = 0;
163-
while (!done.get()) {
166+
do {
164167
for (int index : new int[]{0, size - 1, size, 2 * size - 1}) {
165168
String reason = detail(index, false).getReason();
166169
if (!reason.equals(REASON_RULE_MATCH) && !reason.equals(REASON_FALLTHROUGH)) {
167170
throw new AssertionError("user " + index + ": " + reason);
168171
}
169172
evaluations++;
170173
}
171-
}
174+
started.countDown();
175+
} while (!done.get());
172176
return evaluations;
173177
}));
174178
}
179+
// replace only once every reader is evaluating, so replacements overlap evaluations
180+
assertTrue(started.await(10, TimeUnit.SECONDS));
175181
for (int i = 0; i < 200; i++) {
176182
syncSegment(userIdRule("IsOneOf", i % 2 == 0 ? second : first), Collections.emptyList(), Collections.emptyList());
177183
}

0 commit comments

Comments
 (0)