use of io.vertx.core.Vertx 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.Vertx 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.Vertx in project vert.x by eclipse.
the class LauncherExtensibilityTest method testThatCustomLauncherCanCustomizeTheClusteredOption.
@Test
public void testThatCustomLauncherCanCustomizeTheClusteredOption() {
Launcher myLauncher = new Launcher() {
@Override
protected String getMainVerticle() {
return HttpTestVerticle.class.getName();
}
@Override
public void afterStartingVertx(Vertx vertx) {
LauncherExtensibilityTest.this.vertx = vertx;
}
@Override
public void beforeStartingVertx(VertxOptions options) {
options.setClustered(true);
}
};
myLauncher.dispatch(new String[0]);
waitUntil(() -> {
try {
return RunCommandTest.getHttpCode() == 200;
} catch (IOException e) {
return false;
}
});
assertThat(this.vertx.isClustered()).isTrue();
}
use of io.vertx.core.Vertx in project vert.x by eclipse.
the class LauncherExtensibilityTest method testExtendingMainVerticle.
@Test
public void testExtendingMainVerticle() {
Launcher myLauncher = new Launcher() {
@Override
protected String getMainVerticle() {
return HttpTestVerticle.class.getName();
}
@Override
public void afterStartingVertx(Vertx vertx) {
LauncherExtensibilityTest.this.vertx = vertx;
}
};
myLauncher.dispatch(new String[0]);
waitUntil(() -> {
try {
return RunCommandTest.getHttpCode() == 200;
} catch (IOException e) {
return false;
}
});
}
use of io.vertx.core.Vertx in project vert.x by eclipse.
the class VertxTestBase method tearDown.
protected void tearDown() throws Exception {
if (vertx != null) {
CountDownLatch latch = new CountDownLatch(1);
vertx.close(ar -> {
latch.countDown();
});
awaitLatch(latch);
}
if (created != null) {
CountDownLatch latch = new CountDownLatch(created.size());
for (Vertx v : created) {
v.close(ar -> {
if (ar.failed()) {
log.error("Failed to shutdown vert.x", ar.cause());
}
latch.countDown();
});
}
assertTrue(latch.await(180, TimeUnit.SECONDS));
}
// Bit ugly
FakeClusterManager.reset();
super.tearDown();
}
Aggregations