testComplete(); } else { fail(ar4.cause());
@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 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 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 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(); }
testComplete(); } else { fail(ar4.cause());
@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 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 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 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(); }