use of io.vertx.core.WorkerExecutor in project vert.x by eclipse.
the class NamedWorkerPoolTest method testCloseWorkerPoolsWhenVertxCloses.
@Test
public void testCloseWorkerPoolsWhenVertxCloses() {
Vertx vertx = Vertx.vertx();
WorkerExecutor exec = vertx.createSharedWorkerExecutor("vert.x-123");
vertx.close(v -> {
try {
vertx.executeBlocking(fut -> fail(), ar -> fail());
fail();
} catch (RejectedExecutionException ignore) {
}
try {
exec.executeBlocking(fut -> fail(), ar -> fail());
fail();
} catch (RejectedExecutionException ignore) {
}
exec.close();
testComplete();
});
await();
}
use of io.vertx.core.WorkerExecutor in project vert.x by eclipse.
the class NamedWorkerPoolTest method testCloseWorkerPool.
@Test
public void testCloseWorkerPool() throws Exception {
String poolName = "vert.x-" + TestUtils.randomAlphaString(10);
AtomicReference<Thread> thread = new AtomicReference<>();
WorkerExecutor worker1 = vertx.createSharedWorkerExecutor(poolName);
WorkerExecutor worker2 = vertx.createSharedWorkerExecutor(poolName);
worker1.executeBlocking(fut -> {
thread.set(Thread.currentThread());
}, ar -> {
});
waitUntil(() -> thread.get() != null);
worker1.close();
assertNotSame(thread.get().getState(), Thread.State.TERMINATED);
worker2.close();
waitUntil(() -> thread.get().getState() == Thread.State.TERMINATED);
}
use of io.vertx.core.WorkerExecutor in project vert.x by eclipse.
the class NamedWorkerPoolTest method testOrdered.
@Test
public void testOrdered() {
String poolName = "vert.x-" + TestUtils.randomAlphaString(10);
WorkerExecutor worker = vertx.createSharedWorkerExecutor(poolName);
int num = 1000;
AtomicReference<Thread> t = new AtomicReference<>();
CountDownLatch submitted = new CountDownLatch(1);
Context ctx = vertx.getOrCreateContext();
ctx.runOnContext(v -> {
for (int i = 0; i < num; i++) {
boolean first = i == 0;
boolean last = i == num - 1;
worker.executeBlocking(fut -> {
if (first) {
try {
awaitLatch(submitted);
} catch (InterruptedException e) {
fail(e);
return;
}
assertNull(t.get());
t.set(Thread.currentThread());
} else {
assertEquals(t.get(), Thread.currentThread());
}
assertTrue(Thread.currentThread().getName().startsWith(poolName + "-"));
fut.complete(null);
}, ar -> {
if (last) {
testComplete();
}
});
}
});
submitted.countDown();
await();
}
use of io.vertx.core.WorkerExecutor in project vert.x by eclipse.
the class NamedWorkerPoolTest method testUnordered.
@Test
public void testUnordered() throws Exception {
String poolName = "vert.x-" + TestUtils.randomAlphaString(10);
int num = 5;
waitFor(num);
WorkerExecutor worker = vertx.createSharedWorkerExecutor(poolName);
CountDownLatch latch1 = new CountDownLatch(num);
CountDownLatch latch2 = new CountDownLatch(1);
for (int i = 0; i < num; i++) {
worker.executeBlocking(fut -> {
latch1.countDown();
try {
awaitLatch(latch2);
} catch (InterruptedException e) {
fail(e);
return;
}
assertTrue(Thread.currentThread().getName().startsWith(poolName + "-"));
fut.complete(null);
}, false, ar -> {
complete();
});
}
awaitLatch(latch1);
latch2.countDown();
await();
}
use of io.vertx.core.WorkerExecutor in project vert.x by eclipse.
the class NamedWorkerPoolTest method testThread.
@Test
public void testThread() {
String poolName = TestUtils.randomAlphaString(10);
WorkerExecutor worker = vertx.createSharedWorkerExecutor(poolName);
AtomicBoolean onVertxThread = new AtomicBoolean();
AtomicBoolean onWorkerThread = new AtomicBoolean();
AtomicBoolean onEventLoopThread = new AtomicBoolean();
AtomicReference<String> threadName = new AtomicReference<>();
worker.executeBlocking(fut -> {
onVertxThread.set(Context.isOnVertxThread());
onWorkerThread.set(Context.isOnWorkerThread());
onEventLoopThread.set(Context.isOnEventLoopThread());
threadName.set(Thread.currentThread().getName());
fut.complete(null);
}, ar -> {
testComplete();
});
// Use regular assertions because the thread name does not start with "vert.x-"
// and it confuses the VertxTestBase asserts
waitUntil(() -> threadName.get() != null);
assertTrue(onVertxThread.get());
assertTrue(onWorkerThread.get());
assertFalse(onEventLoopThread.get());
assertTrue(threadName.get().startsWith(poolName + "-"));
}
Aggregations