Skip to content

Commit 2f0cd6b

Browse files
committed
Better determinism across CPUs with varying thread scheduling algorithms
1 parent 389a308 commit 2f0cd6b

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

src/test/java/com/jnape/palatable/lambda/io/IOTest.java

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -349,18 +349,19 @@ public void monitorSync() throws InterruptedException {
349349
Object lock = new Object();
350350
List<String> accesses = new ArrayList<>();
351351
CountDownLatch oneStarted = new CountDownLatch(1);
352+
CountDownLatch twoStarted = new CountDownLatch(1);
352353
CountDownLatch finishLine = new CountDownLatch(2);
353354

354355
IO<Unit> one = IO.monitorSync(lock, io(() -> {
355356
accesses.add("one entered");
356357
oneStarted.countDown();
357-
Thread.sleep(10);
358+
twoStarted.await();
359+
Thread.sleep(100);
358360
accesses.add("one exited");
359361
finishLine.countDown();
360362
}));
361363

362364
IO<Unit> two = IO.monitorSync(lock, io(() -> {
363-
oneStarted.await();
364365
accesses.add("two entered");
365366
accesses.add("two exited");
366367
finishLine.countDown();
@@ -370,11 +371,16 @@ public void monitorSync() throws InterruptedException {
370371
start();
371372
}};
372373

373-
new Thread(two::unsafePerformIO) {{
374+
oneStarted.await();
375+
376+
new Thread(() -> {
377+
twoStarted.countDown();
378+
two.unsafePerformIO();
379+
}) {{
374380
start();
375381
}};
376382

377-
if (!finishLine.await(15, SECONDS))
383+
if (!finishLine.await(1, SECONDS))
378384
fail("Expected threads to have completed by now, only got this far: " + accesses);
379385
assertEquals(asList("one entered", "one exited", "two entered", "two exited"), accesses);
380386
}

0 commit comments

Comments
 (0)