use of io.vertx.core.shareddata.Lock in project vert.x by eclipse.
the class AsynchronousLockTest method testAcquireOnExecuteBlocking.
@Test
public void testAcquireOnExecuteBlocking() {
Vertx vertx = getVertx();
SharedData sharedData = vertx.sharedData();
AtomicReference<Long> start = new AtomicReference<>();
vertx.<Lock>executeBlocking(future -> {
CountDownLatch acquireLatch = new CountDownLatch(1);
AtomicReference<AsyncResult<Lock>> lockReference = new AtomicReference<>();
sharedData.getLock("foo", ar -> {
lockReference.set(ar);
acquireLatch.countDown();
});
try {
awaitLatch(acquireLatch);
AsyncResult<Lock> ar = lockReference.get();
if (ar.succeeded()) {
future.complete(ar.result());
} else {
future.fail(ar.cause());
}
} catch (InterruptedException e) {
future.fail(e);
}
}, ar -> {
if (ar.succeeded()) {
start.set(System.currentTimeMillis());
vertx.setTimer(1000, tid -> {
ar.result().release();
});
vertx.executeBlocking(future -> {
CountDownLatch acquireLatch = new CountDownLatch(1);
AtomicReference<AsyncResult<Lock>> lockReference = new AtomicReference<>();
sharedData.getLock("foo", ar2 -> {
lockReference.set(ar2);
acquireLatch.countDown();
});
try {
awaitLatch(acquireLatch);
AsyncResult<Lock> ar3 = lockReference.get();
if (ar3.succeeded()) {
future.complete(ar3.result());
} else {
future.fail(ar3.cause());
}
} catch (InterruptedException e) {
future.fail(e);
}
}, ar4 -> {
if (ar4.succeeded()) {
assertTrue(System.currentTimeMillis() - start.get() >= 1000);
testComplete();
} else {
fail(ar4.cause());
}
});
} else {
fail(ar.cause());
}
});
await();
}
use of io.vertx.core.shareddata.Lock in project vert.x by eclipse.
the class AsynchronousLockTest method testAcquireOnSameEventLoop.
@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());
assertTrue(System.currentTimeMillis() - start.get() >= 1000);
testComplete();
});
});
});
});
await();
}
use of io.vertx.core.shareddata.Lock in project vert.x by eclipse.
the class AsynchronousLockTest method testAcquireDifferentLocks.
@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();
}
use of io.vertx.core.shareddata.Lock in project vert.x by eclipse.
the class AsynchronousLockTest method testAcquire.
@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());
assertTrue(System.currentTimeMillis() - start >= 1000);
testComplete();
});
});
await();
}
Aggregations