Skip to content

Instantly share code, notes, and snippets.

@nick318
Created February 8, 2018 17:21
Show Gist options
  • Save nick318/543bf5e2aea11c7050a3286d6f4f24ee to your computer and use it in GitHub Desktop.
Save nick318/543bf5e2aea11c7050a3286d6f4f24ee to your computer and use it in GitHub Desktop.
Test for next method
@Test
public void correctValuesForConcurrentNext() throws ExecutionException, InterruptedException {
for (int i = 0; i < 5000; i++) {
final Iterator<String> syncIterator = new SyncIterator<>(
new ListOf<>("a", "b")
.iterator());
final CountDownLatch startSignal = new CountDownLatch(1);
final ExecutorService executors = Executors.newFixedThreadPool(2);
final Future<String> letter = executors.submit(() -> {
startSignal.await();
return syncIterator.next();
});
final Future<String> letter2 = executors.submit(() -> {
startSignal.await();
return syncIterator.next();
});
startSignal.countDown();
MatcherAssert.assertThat(
"Two threads have read the same value (race condition)",
letter.get(),
Matchers.not(letter2.get())
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment