Created
February 6, 2018 17:24
-
-
Save nick318/a4250c7a7ecec5aa4803cd4631a1c9d9 to your computer and use it in GitHub Desktop.
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 correctValuesForConcurrentHasNext() throws ExecutionException, InterruptedException { | |
int exceptionCount = 0; | |
for (int i = 0; i <5000; i++) { | |
Iterator<Integer> syncIterator = new SyncIterator<Integer>( | |
Stream.iterate(0, j -> j + 1).limit(200) | |
.collect(toList()) | |
.iterator()); | |
CountDownLatch startSignal = new CountDownLatch(1); | |
ExecutorService executors = Executors.newFixedThreadPool(2); | |
Future<Boolean> hasNext = executors.submit(() -> { | |
startSignal.await(); | |
while (syncIterator.hasNext()) { | |
syncIterator.next(); | |
} return true; | |
}); | |
Future<Boolean> hasNext2 = executors.submit(() -> { | |
startSignal.await(); | |
while (syncIterator.hasNext()) { | |
syncIterator.next(); | |
} return true; | |
}); | |
startSignal.countDown(); | |
try { | |
MatcherAssert.assertThat( | |
"Error", | |
hasNext.get(), | |
Matchers.equalTo(hasNext2.get())); | |
} catch (ExecutionException e) { | |
exceptionCount++; | |
} | |
} | |
System.out.println(exceptionCount); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment