Skip to content
This repository has been archived by the owner on Nov 14, 2024. It is now read-only.

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
j-baker committed Jan 28, 2020
1 parent 7e600b7 commit 4bcc3c4
Showing 1 changed file with 21 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -128,27 +128,34 @@ public void stressTest() {
MoreExecutors.listeningDecorator(PTExecutors.newFixedThreadPool(poolSize));
AtomicLong counter = new AtomicLong(0);
Supplier<Long> supplier = new CoalescingSupplier<>(() -> {
try {
Thread.sleep(2);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new RuntimeException(e);
}
sleep(2);
return counter.incrementAndGet();
});
List<ListenableFuture<?>> futures = IntStream.range(0, poolSize)
.mapToObj(index -> executorService.submit(() -> {
long last = supplier.get();
for (int i = 0; i < 128; i++) {
long current = supplier.get();
checkState(current > last, "current > last");
last = current;
}
})).collect(Collectors.toList());
.mapToObj(index -> executorService.submit(() -> assertIncreasing(supplier)))
.collect(Collectors.toList());
executorService.shutdown();
Futures.getUnchecked(Futures.allAsList(futures));
}

private static void assertIncreasing(Supplier<Long> supplier) {
long last = supplier.get();
for (int i = 0; i < 128; i++) {
long current = supplier.get();
checkState(current > last, "current > last");
last = current;
}
}

private static void sleep(int millis) {
try {
Thread.sleep(millis);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new RuntimeException(e);
}
}

private AsyncTasks getConcurrently(int count) {
return AsyncTasks.runInParallel(supplier::get, count);
}
Expand Down

0 comments on commit 4bcc3c4

Please sign in to comment.