Search in sources :

Example 51 with AtomicInteger

use of java.util.concurrent.atomic.AtomicInteger in project vert.x by eclipse.

the class MetricsTest method testThreadPoolMetricsWithNamedExecuteBlocking.

@Test
public void testThreadPoolMetricsWithNamedExecuteBlocking() {
    // Close the instance automatically created
    vertx.close();
    vertx = Vertx.vertx(new VertxOptions().setMetricsOptions(new MetricsOptions().setEnabled(true).setFactory(new FakeMetricsFactory())));
    WorkerExecutor workerExec = vertx.createSharedWorkerExecutor("my-pool", 10);
    Map<String, PoolMetrics> all = FakePoolMetrics.getPoolMetrics();
    FakePoolMetrics metrics = (FakePoolMetrics) all.get("my-pool");
    assertThat(metrics.getPoolSize(), is(10));
    assertThat(metrics.numberOfIdleThreads(), is(10));
    Handler<Future<Void>> job = getSomeDumbTask();
    AtomicInteger counter = new AtomicInteger();
    AtomicBoolean hadWaitingQueue = new AtomicBoolean();
    AtomicBoolean hadIdle = new AtomicBoolean();
    AtomicBoolean hadRunning = new AtomicBoolean();
    for (int i = 0; i < 100; i++) {
        workerExec.executeBlocking(job, false, ar -> {
            if (metrics.numberOfWaitingTasks() > 0) {
                hadWaitingQueue.set(true);
            }
            if (metrics.numberOfIdleThreads() > 0) {
                hadIdle.set(true);
            }
            if (metrics.numberOfRunningTasks() > 0) {
                hadRunning.set(true);
            }
            if (counter.incrementAndGet() == 100) {
                testComplete();
            }
        });
    }
    await();
    assertEquals(metrics.numberOfSubmittedTask(), 100);
    assertEquals(metrics.numberOfCompletedTasks(), 100);
    assertTrue(hadIdle.get());
    assertTrue(hadWaitingQueue.get());
    assertTrue(hadRunning.get());
    assertEquals(metrics.numberOfIdleThreads(), 10);
    assertEquals(metrics.numberOfRunningTasks(), 0);
    assertEquals(metrics.numberOfWaitingTasks(), 0);
}
Also used : MetricsOptions(io.vertx.core.metrics.MetricsOptions) PoolMetrics(io.vertx.core.spi.metrics.PoolMetrics) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Test(org.junit.Test)

Example 52 with AtomicInteger

use of java.util.concurrent.atomic.AtomicInteger in project vert.x by eclipse.

the class MetricsTest method testReceiveMessagePublished.

private void testReceiveMessagePublished(Vertx from, Vertx to, boolean expectedLocal, int expectedHandlers) {
    FakeEventBusMetrics eventBusMetrics = FakeMetricsBase.getMetrics(to.eventBus());
    AtomicInteger count = new AtomicInteger();
    for (int i = 0; i < expectedHandlers; i++) {
        MessageConsumer<Object> consumer = to.eventBus().consumer(ADDRESS1);
        consumer.completionHandler(done -> {
            assertTrue(done.succeeded());
            if (count.incrementAndGet() == expectedHandlers) {
                String msg = TestUtils.randomAlphaString(10);
                from.eventBus().publish(ADDRESS1, msg);
            }
        });
        int index = i;
        consumer.handler(msg -> {
            if (index == 0) {
                assertEquals(Arrays.asList(new ReceivedMessage(ADDRESS1, true, expectedLocal, expectedHandlers)), eventBusMetrics.getReceivedMessages());
                testComplete();
            }
        });
    }
    await();
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger)

Example 53 with AtomicInteger

use of java.util.concurrent.atomic.AtomicInteger in project vert.x by eclipse.

the class MetricsTest method testWithWorkerVerticle.

private void testWithWorkerVerticle(DeploymentOptions options) {
    AtomicInteger counter = new AtomicInteger();
    Map<String, PoolMetrics> all = FakePoolMetrics.getPoolMetrics();
    FakePoolMetrics metrics = (FakePoolMetrics) all.get("vert.x-worker-thread");
    assertThat(metrics.getPoolSize(), is(getOptions().getInternalBlockingPoolSize()));
    assertThat(metrics.numberOfIdleThreads(), is(getOptions().getWorkerPoolSize()));
    AtomicBoolean hadWaitingQueue = new AtomicBoolean();
    AtomicBoolean hadIdle = new AtomicBoolean();
    AtomicBoolean hadRunning = new AtomicBoolean();
    int count = 100;
    Verticle worker = new AbstractVerticle() {

        @Override
        public void start(Future<Void> done) throws Exception {
            vertx.eventBus().localConsumer("message", d -> {
                try {
                    Thread.sleep(10);
                    if (metrics.numberOfWaitingTasks() > 0) {
                        hadWaitingQueue.set(true);
                    }
                    if (metrics.numberOfIdleThreads() > 0) {
                        hadIdle.set(true);
                    }
                    if (metrics.numberOfRunningTasks() > 0) {
                        hadRunning.set(true);
                    }
                    if (counter.incrementAndGet() == count) {
                        testComplete();
                    }
                } catch (InterruptedException e) {
                    Thread.currentThread().isInterrupted();
                }
            });
            done.complete();
        }
    };
    vertx.deployVerticle(worker, options, s -> {
        for (int i = 0; i < count; i++) {
            vertx.eventBus().send("message", i);
        }
    });
    await();
    // The verticle deployment is also executed on the worker thread pool
    assertEquals(metrics.numberOfSubmittedTask(), count + 1);
    assertEquals(metrics.numberOfCompletedTasks(), count + 1);
    assertTrue(hadIdle.get());
    assertTrue(hadWaitingQueue.get());
    assertTrue(hadRunning.get());
    assertEquals(metrics.numberOfIdleThreads(), getOptions().getWorkerPoolSize());
    assertEquals(metrics.numberOfRunningTasks(), 0);
    assertEquals(metrics.numberOfWaitingTasks(), 0);
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) PoolMetrics(io.vertx.core.spi.metrics.PoolMetrics)

Example 54 with AtomicInteger

use of java.util.concurrent.atomic.AtomicInteger in project vert.x by eclipse.

the class NetTest method testContexts.

@Test
public void testContexts() throws Exception {
    Set<ContextImpl> contexts = new ConcurrentHashSet<>();
    AtomicInteger cnt = new AtomicInteger();
    AtomicReference<ContextImpl> serverConnectContext = new AtomicReference<>();
    // Server connect handler should always be called with same context
    server.connectHandler(sock -> {
        sock.handler(sock::write);
        ContextImpl serverContext = ((VertxInternal) vertx).getContext();
        if (serverConnectContext.get() != null) {
            assertSame(serverConnectContext.get(), serverContext);
        } else {
            serverConnectContext.set(serverContext);
        }
    });
    CountDownLatch latch = new CountDownLatch(1);
    AtomicReference<ContextImpl> listenContext = new AtomicReference<>();
    server.listen(ar -> {
        assertTrue(ar.succeeded());
        listenContext.set(((VertxInternal) vertx).getContext());
        latch.countDown();
    });
    awaitLatch(latch);
    CountDownLatch latch2 = new CountDownLatch(1);
    int numConns = 10;
    // Each connect should be in its own context
    for (int i = 0; i < numConns; i++) {
        client.connect(1234, "localhost", conn -> {
            contexts.add(((VertxInternal) vertx).getContext());
            if (cnt.incrementAndGet() == numConns) {
                assertEquals(numConns, contexts.size());
                latch2.countDown();
            }
        });
    }
    awaitLatch(latch2);
    // Close should be in own context
    server.close(ar -> {
        assertTrue(ar.succeeded());
        ContextImpl closeContext = ((VertxInternal) vertx).getContext();
        assertFalse(contexts.contains(closeContext));
        assertNotSame(serverConnectContext.get(), closeContext);
        assertFalse(contexts.contains(listenContext.get()));
        assertSame(serverConnectContext.get(), listenContext.get());
        testComplete();
    });
    server = null;
    await();
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 55 with AtomicInteger

use of java.util.concurrent.atomic.AtomicInteger in project vert.x by eclipse.

the class NetTest method clientCloseHandlers.

void clientCloseHandlers(boolean closeFromClient) {
    client.connect(1234, "localhost", ar -> {
        AtomicInteger counter = new AtomicInteger(0);
        ar.result().endHandler(v -> assertEquals(1, counter.incrementAndGet()));
        ar.result().closeHandler(v -> {
            assertEquals(2, counter.incrementAndGet());
            testComplete();
        });
        if (closeFromClient) {
            ar.result().close();
        }
    });
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger)

Aggregations

AtomicInteger (java.util.concurrent.atomic.AtomicInteger)7986 Test (org.junit.Test)3775 CountDownLatch (java.util.concurrent.CountDownLatch)1072 ArrayList (java.util.ArrayList)1018 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)849 List (java.util.List)740 IOException (java.io.IOException)719 AtomicReference (java.util.concurrent.atomic.AtomicReference)574 HashMap (java.util.HashMap)499 Map (java.util.Map)460 Test (org.testng.annotations.Test)419 File (java.io.File)337 ExecutorService (java.util.concurrent.ExecutorService)337 Test (org.junit.jupiter.api.Test)334 AtomicLong (java.util.concurrent.atomic.AtomicLong)329 TimeUnit (java.util.concurrent.TimeUnit)323 HashSet (java.util.HashSet)315 Arrays (java.util.Arrays)308 Set (java.util.Set)284 Collections (java.util.Collections)266