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