ResourceLock { @SneakyThrows(InterruptedException.class) public void doAwait() { lock.lock(); try { condition.await(DEFAULT_TIMEOUT_MILLISECONDS, TimeUnit.MILLISECONDS); } finally { lock.unlock(); } }  @SneakyThrows(InterruptedException.class) void doAwait(); void doNotify();  }
@Test public void assertDoAwait() throws InterruptedException { int numberOfThreads = 10; ResourceLock resourceLock = new ResourceLock(); ExecutorService service = Executors.newFixedThreadPool(numberOfThreads); CountDownLatch latch = new CountDownLatch(numberOfThreads); AtomicInteger counter = new AtomicInteger(); for (int i = 0; i < numberOfThreads; i++) { service.submit(() -> { resourceLock.doAwait(); counter.incrementAndGet(); latch.countDown(); }); } latch.await(); assertThat(numberOfThreads, is(counter.get())); }
@Test public void assertDoAwaitThrowsException() throws InterruptedException { int numberOfThreads = 10; ResourceLock resourceLock = new ResourceLock(); ExecutorService service = Executors.newFixedThreadPool(numberOfThreads); CountDownLatch latch = new CountDownLatch(numberOfThreads); AtomicInteger counter = new AtomicInteger(); for (int i = 0; i < numberOfThreads; i++) { service.submit(() -> { resourceLock.doAwait(); counter.incrementAndGet(); latch.countDown(); }); } latch.await(100, TimeUnit.MILLISECONDS); service.shutdownNow(); assertThat(numberOfThreads, not(counter.get())); }