@Test public void testAcquireDifferentLocks() { getVertx().sharedData().getLock("foo", ar -> { assertTrue(ar.succeeded()); long start = System.currentTimeMillis(); Lock lock = ar.result(); getVertx().sharedData().getLock("bar", ar2 -> { assertTrue(ar2.succeeded()); assertTrue(System.currentTimeMillis() - start < 2000); 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 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(); }
CountDownLatch acquireLatch = new CountDownLatch(1); AtomicReference<AsyncResult<Lock>> lockReference = new AtomicReference<>(); sharedData.getLock("foo", ar -> { lockReference.set(ar); acquireLatch.countDown(); CountDownLatch acquireLatch = new CountDownLatch(1); AtomicReference<AsyncResult<Lock>> lockReference = new AtomicReference<>(); sharedData.getLock("foo", ar2 -> { lockReference.set(ar2); acquireLatch.countDown();
@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 testAcquire() { getVertx().sharedData().getLock("foo", ar -> { assertTrue(ar.succeeded()); long start = System.currentTimeMillis(); Lock lock = ar.result(); vertx.setTimer(1000, tid -> { lock.release(); }); getVertx().sharedData().getLock("foo", ar2 -> { assertTrue(ar2.succeeded()); // Should be delayed assertTrue(System.currentTimeMillis() - start >= 1000); testComplete(); }); }); 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 testAcquireOnSameEventLoop() { Vertx vertx = getVertx(); Context context = vertx.getOrCreateContext(); SharedData sharedData = vertx.sharedData(); AtomicReference<Long> start = new AtomicReference<>(); context.runOnContext(v -> { sharedData.getLock("foo", ar -> { assertTrue(ar.succeeded()); start.set(System.currentTimeMillis()); Lock lock = ar.result(); vertx.setTimer(1000, tid -> { lock.release(); }); context.runOnContext(v2 -> { sharedData.getLock("foo", ar2 -> { assertTrue(ar2.succeeded()); // Should be delayed assertTrue(System.currentTimeMillis() - start.get() >= 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 testAcquireDifferentLocksOnSameEventLoop() { Vertx vertx = getVertx(); Context context = vertx.getOrCreateContext(); SharedData sharedData = vertx.sharedData(); AtomicInteger stage = new AtomicInteger(); context.runOnContext(v -> { sharedData.getLock("foo", onSuccess(foo -> { assertTrue(stage.compareAndSet(0, 1)); // Create another lock request sharedData.getLock("foo", onSuccess(foo1 -> { assertEquals(2, stage.get()); foo1.release(); testComplete(); })); // Should not be blocked by second request for lock "foo" sharedData.getLock("bar", onSuccess(bar -> { assertTrue(stage.compareAndSet(1, 2)); foo.release(); bar.release(); })); })); }); 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 testAcquireDifferentLocks() { getVertx().sharedData().getLock("foo", ar -> { assertTrue(ar.succeeded()); long start = System.currentTimeMillis(); Lock lock = ar.result(); getVertx().sharedData().getLock("bar", ar2 -> { assertTrue(ar2.succeeded()); assertTrue(System.currentTimeMillis() - start < 2000); testComplete(); }); }); await(); }
CountDownLatch acquireLatch = new CountDownLatch(1); AtomicReference<AsyncResult<Lock>> lockReference = new AtomicReference<>(); sharedData.getLock("foo", ar -> { lockReference.set(ar); acquireLatch.countDown(); CountDownLatch acquireLatch = new CountDownLatch(1); AtomicReference<AsyncResult<Lock>> lockReference = new AtomicReference<>(); sharedData.getLock("foo", ar2 -> { lockReference.set(ar2); acquireLatch.countDown();
@Test public void testAcquire() { getVertx().sharedData().getLock("foo", ar -> { assertTrue(ar.succeeded()); long start = System.currentTimeMillis(); Lock lock = ar.result(); vertx.setTimer(1000, tid -> { lock.release(); }); getVertx().sharedData().getLock("foo", ar2 -> { assertTrue(ar2.succeeded()); // Should be delayed assertTrue(System.currentTimeMillis() - start >= 1000); testComplete(); }); }); await(); }
@Test public void testAcquireOnSameEventLoop() { Vertx vertx = getVertx(); Context context = vertx.getOrCreateContext(); SharedData sharedData = vertx.sharedData(); AtomicReference<Long> start = new AtomicReference<>(); context.runOnContext(v -> { sharedData.getLock("foo", ar -> { assertTrue(ar.succeeded()); start.set(System.currentTimeMillis()); Lock lock = ar.result(); vertx.setTimer(1000, tid -> { lock.release(); }); context.runOnContext(v2 -> { sharedData.getLock("foo", ar2 -> { assertTrue(ar2.succeeded()); // Should be delayed assertTrue(System.currentTimeMillis() - start.get() >= 1000); testComplete(); }); }); }); }); await(); }
@Test public void testAcquireDifferentLocksOnSameEventLoop() { Vertx vertx = getVertx(); Context context = vertx.getOrCreateContext(); SharedData sharedData = vertx.sharedData(); AtomicInteger stage = new AtomicInteger(); context.runOnContext(v -> { sharedData.getLock("foo", onSuccess(foo -> { assertTrue(stage.compareAndSet(0, 1)); // Create another lock request sharedData.getLock("foo", onSuccess(foo1 -> { assertEquals(2, stage.get()); foo1.release(); testComplete(); })); // Should not be blocked by second request for lock "foo" sharedData.getLock("bar", onSuccess(bar -> { assertTrue(stage.compareAndSet(1, 2)); foo.release(); bar.release(); })); })); }); await(); }
/** * Get an asynchronous lock with the specified name. The lock will be passed to the handler when it is available. * <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 resultHandler the handler */ public void getLock(String name, Handler<AsyncResult<io.vertx.rxjava.core.shareddata.Lock>> resultHandler) { delegate.getLock(name, 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())); } } }); }
/** * Get an asynchronous lock with the specified name. The lock will be passed to the handler when it is available. * <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 resultHandler the handler */ public void getLock(String name, Handler<AsyncResult<io.vertx.rxjava.core.shareddata.Lock>> resultHandler) { delegate.getLock(name, 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())); } } }); }