private void testLockReleased(Consumer<CountDownLatch> action) throws Exception { CountDownLatch lockAquiredLatch = new CountDownLatch(1); vertices[0].sharedData().getLockWithTimeout("pimpo", getLockTimeout(), onSuccess(lock -> { vertices[1].sharedData().getLockWithTimeout("pimpo", getLockTimeout(), onSuccess(lock2 -> { // Eventually acquired after node1 goes down testComplete(); })); lockAquiredLatch.countDown(); })); awaitLatch(lockAquiredLatch); CountDownLatch closeLatch = new CountDownLatch(1); action.accept(closeLatch); awaitLatch(closeLatch); await(); }
@Test public void testAcquireTimeout() { getVertx().sharedData().getLock("foo", ar -> { assertTrue(ar.succeeded()); long start = System.currentTimeMillis(); getVertx().sharedData().getLockWithTimeout("foo", 1000, ar2 -> { assertFalse(ar2.succeeded()); // Should be delayed assertTrue(System.currentTimeMillis() - start >= 1000); testComplete(); }); }); await(); }
@Test public void testIllegalArguments() throws Exception { assertNullPointerException(() -> getVertx().sharedData().getLock(null, ar -> {})); assertNullPointerException(() -> getVertx().sharedData().getLock("foo", null)); assertNullPointerException(() -> getVertx().sharedData().getLockWithTimeout(null, 1, ar -> {})); assertNullPointerException(() -> getVertx().sharedData().getLockWithTimeout("foo", 1, null)); assertIllegalArgumentException(() -> getVertx().sharedData().getLockWithTimeout("foo", -1, ar -> {})); }
@Test public void testReleaseTwice() { waitFor(3); AtomicInteger count = new AtomicInteger(); // success lock count getVertx().sharedData().getLock("foo", onSuccess(lock1 -> { count.incrementAndGet(); complete(); for (int i = 0; i < 2; i++) { getVertx().sharedData().getLockWithTimeout("foo", 10, ar -> { if (ar.succeeded()) { count.incrementAndGet(); } complete(); }); } lock1.release(); lock1.release(); })); await(); assertEquals(2, count.get()); } }
private void testLockReleased(Consumer<CountDownLatch> action) throws Exception { CountDownLatch lockAquiredLatch = new CountDownLatch(1); vertices[0].sharedData().getLockWithTimeout("pimpo", getLockTimeout(), onSuccess(lock -> { vertices[1].sharedData().getLockWithTimeout("pimpo", getLockTimeout(), onSuccess(lock2 -> { // Eventually acquired after node1 goes down testComplete(); })); lockAquiredLatch.countDown(); })); awaitLatch(lockAquiredLatch); CountDownLatch closeLatch = new CountDownLatch(1); action.accept(closeLatch); awaitLatch(closeLatch); await(); }
@Test public void testAcquireTimeout() { getVertx().sharedData().getLock("foo", ar -> { assertTrue(ar.succeeded()); long start = System.currentTimeMillis(); getVertx().sharedData().getLockWithTimeout("foo", 1000, ar2 -> { assertFalse(ar2.succeeded()); // Should be delayed assertTrue(System.currentTimeMillis() - start >= 1000); testComplete(); }); }); await(); }
@Test public void testReleaseTwice() { waitFor(3); AtomicInteger count = new AtomicInteger(); // success lock count getVertx().sharedData().getLock("foo", onSuccess(lock1 -> { count.incrementAndGet(); complete(); for (int i = 0; i < 2; i++) { getVertx().sharedData().getLockWithTimeout("foo", 10, ar -> { if (ar.succeeded()) { count.incrementAndGet(); } complete(); }); } lock1.release(); lock1.release(); })); await(); assertEquals(2, count.get()); } }
@Test public void testIllegalArguments() throws Exception { assertNullPointerException(() -> getVertx().sharedData().getLock(null, ar -> {})); assertNullPointerException(() -> getVertx().sharedData().getLock("foo", null)); assertNullPointerException(() -> getVertx().sharedData().getLockWithTimeout(null, 1, ar -> {})); assertNullPointerException(() -> getVertx().sharedData().getLockWithTimeout("foo", 1, null)); assertIllegalArgumentException(() -> getVertx().sharedData().getLockWithTimeout("foo", -1, ar -> {})); }
/** * Like {@link io.vertx.rxjava.core.shareddata.SharedData#getLock} but specifying a timeout. If the lock is not obtained within the timeout * a failure will be sent to the handler. * <p> * In general lock acquision is unordered, so that sequential attempts to acquire a lock, * even from a single thread, can happen in non-sequential order. * </p> * @param name the name of the lock * @param timeout the timeout in ms * @param resultHandler the handler */ public void getLockWithTimeout(String name, long timeout, Handler<AsyncResult<io.vertx.rxjava.core.shareddata.Lock>> resultHandler) { delegate.getLockWithTimeout(name, timeout, new Handler<AsyncResult<io.vertx.core.shareddata.Lock>>() { public void handle(AsyncResult<io.vertx.core.shareddata.Lock> ar) { if (ar.succeeded()) { resultHandler.handle(io.vertx.core.Future.succeededFuture(io.vertx.rxjava.core.shareddata.Lock.newInstance(ar.result()))); } else { resultHandler.handle(io.vertx.core.Future.failedFuture(ar.cause())); } } }); }
/** * Like {@link io.vertx.rxjava.core.shareddata.SharedData#getLock} but specifying a timeout. If the lock is not obtained within the timeout * a failure will be sent to the handler. * <p> * In general lock acquision is unordered, so that sequential attempts to acquire a lock, * even from a single thread, can happen in non-sequential order. * </p> * @param name the name of the lock * @param timeout the timeout in ms * @param resultHandler the handler */ public void getLockWithTimeout(String name, long timeout, Handler<AsyncResult<io.vertx.rxjava.core.shareddata.Lock>> resultHandler) { delegate.getLockWithTimeout(name, timeout, new Handler<AsyncResult<io.vertx.core.shareddata.Lock>>() { public void handle(AsyncResult<io.vertx.core.shareddata.Lock> ar) { if (ar.succeeded()) { resultHandler.handle(io.vertx.core.Future.succeededFuture(io.vertx.rxjava.core.shareddata.Lock.newInstance(ar.result()))); } else { resultHandler.handle(io.vertx.core.Future.failedFuture(ar.cause())); } } }); }
String assemblyName = reconciliation.name(); final String lockName = getLockName(assemblyType, namespace, assemblyName); vertx.sharedData().getLockWithTimeout(lockName, LOCK_TIMEOUT_MS, res -> { if (res.succeeded()) { log.debug("{}: Lock {} acquired", reconciliation, lockName);
String name = reconciliation.name(); final String lockName = getLockName(namespace, name); vertx.sharedData().getLockWithTimeout(lockName, LOCK_TIMEOUT_MS, res -> { if (res.succeeded()) { log.debug("{}: Lock {} acquired", reconciliation, lockName);
String lockName = getLockName(); try { vertx.sharedData().getLockWithTimeout(lockName, 1000, rh -> { if (rh.failed()) { Throwable cause = rh.cause();