@Override public void start() throws Exception { pool.set(vertx.createSharedWorkerExecutor(poolName)); } }, onSuccess(deploymentIdRef::complete));
@Override public void start() throws Exception { WorkerExecutor exec = vertx.createSharedWorkerExecutor("vert.x-the-executor"); Thread startThread = Thread.currentThread(); AtomicReference<Thread> currentThread = new AtomicReference<>(); for (int i = 0;i < count;i++) { int val = i; exec.executeBlocking(fut -> { Thread current = Thread.currentThread(); assertNotSame(startThread, current); if (val == 0) { assertNull(currentThread.getAndSet(current)); } else { assertSame(current, currentThread.get()); } fut.complete(); }, true, onSuccess(v -> complete())); } } }, new DeploymentOptions().setWorker(true), onSuccess(id -> {}));
@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 -> { }); assertWaitUntil(() -> thread.get() != null); worker1.close(); assertNotSame(thread.get().getState(), Thread.State.TERMINATED); worker2.close(); assertWaitUntil(() -> thread.get().getState() == Thread.State.TERMINATED); }
@Test public void testPoolSize() throws Exception { String poolName = "vert.x-" + TestUtils.randomAlphaString(10); int poolSize = 5; waitFor(poolSize); WorkerExecutor worker = vertx.createSharedWorkerExecutor(poolName, poolSize); CountDownLatch latch1 = new CountDownLatch(poolSize * 100); Set<String> names = Collections.synchronizedSet(new HashSet<>()); for (int i = 0;i < poolSize * 100;i++) { worker.executeBlocking(fut -> { names.add(Thread.currentThread().getName()); latch1.countDown(); }, false, ar -> { }); } awaitLatch(latch1); assertEquals(5, names.size()); }
@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 assertWaitUntil(() -> threadName.get() != null); assertTrue(onVertxThread.get()); assertTrue(onWorkerThread.get()); assertFalse(onEventLoopThread.get()); assertTrue(threadName.get().startsWith(poolName + "-")); }
@Test public void testOrdered() { String poolName = "vert.x-" + TestUtils.randomAlphaString(10); WorkerExecutor worker = vertx.createSharedWorkerExecutor(poolName); int num = 1000; AtomicReference<Thread> t = new AtomicReference<>();
@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); Context ctx = vertx.getOrCreateContext(); ctx.runOnContext(v -> { 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(); }
@Test public void testMaxExecuteTime() { String poolName = "vert.x-" + TestUtils.randomAlphaString(10); int poolSize = 5; long maxExecuteTime = 60; TimeUnit maxExecuteTimeUnit = TimeUnit.SECONDS; WorkerExecutor worker = vertx.createSharedWorkerExecutor(poolName, poolSize, maxExecuteTime, maxExecuteTimeUnit); worker.executeBlocking(f -> { Thread t = Thread.currentThread(); assertTrue(t instanceof VertxThread); VertxThread thread = (VertxThread) t; assertEquals(maxExecuteTime, thread.getMaxExecTime()); assertEquals(maxExecuteTimeUnit, thread.getMaxExecTimeUnit()); f.complete(); }, res -> { testComplete(); }); await(); }
WorkerExecutor workerExec = vertx.createSharedWorkerExecutor("my-pool", 10);
@Test public void testWorkerPoolClose() { WorkerExecutor ex1 = vertx.createSharedWorkerExecutor("ex1"); WorkerExecutor ex1_ = vertx.createSharedWorkerExecutor("ex1"); WorkerExecutor ex2 = vertx.createSharedWorkerExecutor("ex2"); Map<String, PoolMetrics> all = FakePoolMetrics.getPoolMetrics(); FakePoolMetrics metrics1 = (FakePoolMetrics) all.get("ex1"); FakePoolMetrics metrics2 = (FakePoolMetrics) all.get("ex2"); assertNotNull(metrics1); assertNotNull(metrics2); assertNotSame(metrics1, metrics2); assertFalse(metrics1.isClosed()); assertFalse(metrics2.isClosed()); ex1_.close(); assertFalse(metrics1.isClosed()); assertFalse(metrics2.isClosed()); ex1.close(); assertTrue(metrics1.isClosed()); assertFalse(metrics2.isClosed()); ex2.close(); assertTrue(metrics1.isClosed()); assertTrue(metrics2.isClosed()); }
@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) { } // Check we can still close exec.close(); testComplete(); }); await(); } }
@Override public void start() throws Exception { pool.set(vertx.createSharedWorkerExecutor(poolName)); } }, onSuccess(deploymentIdRef::complete));
@Override public void start() throws Exception { WorkerExecutor exec = vertx.createSharedWorkerExecutor("vert.x-the-executor"); Thread startThread = Thread.currentThread(); AtomicReference<Thread> currentThread = new AtomicReference<>(); for (int i = 0;i < count;i++) { int val = i; exec.executeBlocking(fut -> { Thread current = Thread.currentThread(); assertNotSame(startThread, current); if (val == 0) { assertNull(currentThread.getAndSet(current)); } else { assertSame(current, currentThread.get()); } fut.complete(); }, true, onSuccess(v -> complete())); } } }, new DeploymentOptions().setWorker(true), onSuccess(id -> {}));
@Test public void testPoolSize() throws Exception { String poolName = "vert.x-" + TestUtils.randomAlphaString(10); int poolSize = 5; waitFor(poolSize); WorkerExecutor worker = vertx.createSharedWorkerExecutor(poolName, poolSize); CountDownLatch latch1 = new CountDownLatch(poolSize * 100); Set<String> names = Collections.synchronizedSet(new HashSet<>()); for (int i = 0;i < poolSize * 100;i++) { worker.executeBlocking(fut -> { names.add(Thread.currentThread().getName()); latch1.countDown(); }, false, ar -> { }); } awaitLatch(latch1); assertEquals(5, names.size()); }
@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 -> { }); assertWaitUntil(() -> thread.get() != null); worker1.close(); assertNotSame(thread.get().getState(), Thread.State.TERMINATED); worker2.close(); assertWaitUntil(() -> thread.get().getState() == Thread.State.TERMINATED); }
@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 assertWaitUntil(() -> threadName.get() != null); assertTrue(onVertxThread.get()); assertTrue(onWorkerThread.get()); assertFalse(onEventLoopThread.get()); assertTrue(threadName.get().startsWith(poolName + "-")); }
@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); Context ctx = vertx.getOrCreateContext(); ctx.runOnContext(v -> { 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(); }
@Test public void testMaxExecuteTime() { String poolName = "vert.x-" + TestUtils.randomAlphaString(10); int poolSize = 5; long maxExecuteTime = 60; TimeUnit maxExecuteTimeUnit = TimeUnit.SECONDS; WorkerExecutor worker = vertx.createSharedWorkerExecutor(poolName, poolSize, maxExecuteTime, maxExecuteTimeUnit); worker.executeBlocking(f -> { Thread t = Thread.currentThread(); assertTrue(t instanceof VertxThread); VertxThread thread = (VertxThread) t; assertEquals(maxExecuteTime, thread.getMaxExecTime()); assertEquals(maxExecuteTimeUnit, thread.getMaxExecTimeUnit()); f.complete(); }, res -> { testComplete(); }); await(); }
@Test public void testWorkerPoolClose() { WorkerExecutor ex1 = vertx.createSharedWorkerExecutor("ex1"); WorkerExecutor ex1_ = vertx.createSharedWorkerExecutor("ex1"); WorkerExecutor ex2 = vertx.createSharedWorkerExecutor("ex2"); Map<String, PoolMetrics> all = FakePoolMetrics.getPoolMetrics(); FakePoolMetrics metrics1 = (FakePoolMetrics) all.get("ex1"); FakePoolMetrics metrics2 = (FakePoolMetrics) all.get("ex2"); assertNotNull(metrics1); assertNotNull(metrics2); assertNotSame(metrics1, metrics2); assertFalse(metrics1.isClosed()); assertFalse(metrics2.isClosed()); ex1_.close(); assertFalse(metrics1.isClosed()); assertFalse(metrics2.isClosed()); ex1.close(); assertTrue(metrics1.isClosed()); assertFalse(metrics2.isClosed()); ex2.close(); assertTrue(metrics1.isClosed()); assertTrue(metrics2.isClosed()); }
@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) { } // Check we can still close exec.close(); testComplete(); }); await(); } }