@Test public void testFailedTaskWithMultipleSubmitters() { final AtomicInteger successCount = new AtomicInteger(); final AtomicInteger failureCount = new AtomicInteger(); final AtomicInteger concurrency = new AtomicInteger(); final CountDownLatch startLatch = new CountDownLatch(1); final CountDownLatch completionLatch = new CountDownLatch(100); AsyncSemaphore<Runnable> asyncSemaphore = new AsyncSemaphore<>(2, executor, task -> { throw assertFailedConcurrency(concurrency); }); final Queue<ListenableFuture<?>> futures = new ConcurrentLinkedQueue<>(); for (int i = 0; i < 100; i++) { executor.execute(() -> { Uninterruptibles.awaitUninterruptibly(startLatch, 1, TimeUnit.MINUTES); ListenableFuture<?> future = asyncSemaphore.submit(Assert::fail); futures.add(future); addCallback(future, completionCallback(successCount, failureCount, completionLatch), directExecutor()); }); } startLatch.countDown(); Uninterruptibles.awaitUninterruptibly(completionLatch, 1, TimeUnit.MINUTES); for (ListenableFuture<?> future : futures) { try { future.get(); Assert.fail(); } catch (Exception ignored) { } } Assert.assertEquals(successCount.get(), 0); Assert.assertEquals(failureCount.get(), 100); }
public ListenableFuture<?> submit(T task) { QueuedTask<T> queuedTask = new QueuedTask<>(task); queuedTasks.add(queuedTask); acquirePermit(); return queuedTask.getCompletionFuture(); }
AsyncSemaphore { public ListenableFuture<?> submit(T task) { QueuedTask<T> queuedTask = new QueuedTask<>(task); queuedTasks.add(queuedTask); acquirePermit(); return queuedTask.getCompletionFuture(); } }
AsyncSemaphore { public ListenableFuture<?> submit(T task) { QueuedTask<T> queuedTask = new QueuedTask<>(task); queuedTasks.add(queuedTask); acquirePermit(); return queuedTask.getCompletionFuture(); } AsyncSemaphore(int maxPermits, Executor submitExecutor, Function<T, ListenableFuture<?>> submitter); }
AsyncSemaphore { public ListenableFuture<?> submit(T task) { QueuedTask<T> queuedTask = new QueuedTask<>(task); queuedTasks.add(queuedTask); acquirePermit(); return queuedTask.getCompletionFuture(); } AsyncSemaphore(int maxPermits, Executor submitExecutor, Function<T, ListenableFuture<?>> submitter); ListenableFuture<?> submit(T task); }
AsyncSemaphore { public ListenableFuture<?> submit(T task) { QueuedTask<T> queuedTask = new QueuedTask<>(task); queuedTasks.add(queuedTask); acquirePermit(); return queuedTask.getCompletionFuture(); } AsyncSemaphore(int maxPermits, Executor submitExecutor, Function<T, ListenableFuture<?>> submitter); ListenableFuture<?> submit(T task);  }