@Test public void testFailedTaskSubmission() throws Exception { final AtomicInteger successCount = new AtomicInteger(); final AtomicInteger failureCount = new AtomicInteger(); final AtomicInteger concurrency = new AtomicInteger(); final CountDownLatch completionLatch = new CountDownLatch(1000); AsyncSemaphore<Runnable> asyncSemaphore = new AsyncSemaphore<>(2, executor, task -> { throw assertFailedConcurrency(concurrency); }); List<ListenableFuture<?>> futures = new ArrayList<>(); for (int i = 0; i < 1000; i++) { ListenableFuture<?> future = asyncSemaphore.submit(Assert::fail); addCallback(future, completionCallback(successCount, failureCount, completionLatch), directExecutor()); futures.add(future); } completionLatch.await(1, TimeUnit.MINUTES); for (ListenableFuture<?> future : futures) { try { future.get(); Assert.fail(); } catch (Exception ignored) { } } Assert.assertEquals(successCount.get(), 0); Assert.assertEquals(failureCount.get(), 1000); }
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);  }