AsyncWrappers { public static CompletionStage<Void> lockAsync(InterProcessLock lock, long timeout, TimeUnit unit, Executor executor) { CompletableFuture<Void> future = new CompletableFuture<>(); if ( executor == null ) { CompletableFuture.runAsync(() -> lock(future, lock, timeout, unit)); } else { CompletableFuture.runAsync(() -> lock(future, lock, timeout, unit), executor); } return future; } private  AsyncWrappers(); static CompletionStage<Map<String, byte[]>> childrenWithData(AsyncCuratorFramework client, String path); static CompletionStage<Map<String, byte[]>> childrenWithData(AsyncCuratorFramework client, String path, boolean isCompressed); static CompletionStage<Void> asyncEnsureParents(AsyncCuratorFramework client, String path); static CompletionStage<Void> asyncEnsureContainers(AsyncCuratorFramework client, String path); static CompletionStage<Void> lockAsync(InterProcessLock lock, long timeout, TimeUnit unit, Executor executor); static CompletionStage<Boolean> lockAsyncIf(InterProcessLock lock, long timeout, TimeUnit unit, Executor executor); static CompletionStage<Void> lockAsync(InterProcessLock lock, Executor executor); static CompletionStage<Void> lockAsync(InterProcessLock lock, long timeout, TimeUnit unit); static CompletionStage<Boolean> lockAsyncIf(InterProcessLock lock, long timeout, TimeUnit unit); static CompletionStage<Void> lockAsync(InterProcessLock lock); static void release(InterProcessLock lock); static void release(InterProcessLock lock, boolean ignoreNoLockExceptions);  }
@Test public void testContention() throws Exception { try ( CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), new RetryOneTime(1)) ) { client.start(); InterProcessMutex lock1 = new InterProcessMutex(client, "/one/two"); InterProcessMutex lock2 = new InterProcessMutex(client, "/one/two"); CountDownLatch latch = new CountDownLatch(1); AsyncWrappers.lockAsync(lock1).thenAccept(__ -> { latch.countDown(); }); Assert.assertTrue(timing.awaitLatch(latch)); CountDownLatch latch2 = new CountDownLatch(1); AsyncWrappers.lockAsync(lock2, timing.forSleepingABit().milliseconds(), TimeUnit.MILLISECONDS).exceptionally(e -> { if ( e instanceof AsyncWrappers.TimeoutException ) { latch2.countDown(); } return null; }); Assert.assertTrue(timing.awaitLatch(latch2)); } }