@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 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 testAcquireOnExecuteBlocking() { Vertx vertx = getVertx(); SharedData sharedData = vertx.sharedData(); AtomicReference<Long> start = new AtomicReference<>(); }); try { awaitLatch(acquireLatch); AsyncResult<Lock> ar = lockReference.get(); if (ar.succeeded()) { }); try { awaitLatch(acquireLatch); AsyncResult<Lock> ar3 = lockReference.get(); if (ar3.succeeded()) { if (ar4.succeeded()) { assertTrue(System.currentTimeMillis() - start.get() >= 1000); testComplete(); } else { fail(ar4.cause()); fail(ar.cause()); 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()); } }
public void setUp() throws Exception { super.setUp(); startNodes(numNodes); }
@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()); } }
public void setUp() throws Exception { super.setUp(); startNodes(numNodes); }
@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 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 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 testAcquireOnExecuteBlocking() { Vertx vertx = getVertx(); SharedData sharedData = vertx.sharedData(); AtomicReference<Long> start = new AtomicReference<>(); }); try { awaitLatch(acquireLatch); AsyncResult<Lock> ar = lockReference.get(); if (ar.succeeded()) { }); try { awaitLatch(acquireLatch); AsyncResult<Lock> ar3 = lockReference.get(); if (ar3.succeeded()) { if (ar4.succeeded()) { assertTrue(System.currentTimeMillis() - start.get() >= 1000); testComplete(); } else { fail(ar4.cause()); fail(ar.cause()); 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 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 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 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 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(); }