Created
February 8, 2018 17:21
-
-
Save nick318/543bf5e2aea11c7050a3286d6f4f24ee to your computer and use it in GitHub Desktop.
Test for next method
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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