Search in sources :

Example 1 with GracefulShutdownService

use of org.graylog2.system.shutdown.GracefulShutdownService in project graylog2-server by Graylog2.

the class JobWorkerPoolTest method testExecute.

@Test
public void testExecute() throws Exception {
    final JobWorkerPool pool = new JobWorkerPool("test", 2, shutdownCallback, new GracefulShutdownService(), new MetricRegistry());
    final CountDownLatch latch1 = new CountDownLatch(1);
    final CountDownLatch latch2 = new CountDownLatch(1);
    final CountDownLatch task1Latch = new CountDownLatch(1);
    final CountDownLatch task2Latch = new CountDownLatch(1);
    // Before we do anything, the number of free slots should be the same as the pool size
    assertThat(pool.freeSlots()).isEqualTo(2);
    assertThat(pool.anySlotsUsed()).isFalse();
    // Execute the first task
    assertThat(pool.execute(() -> {
        Uninterruptibles.awaitUninterruptibly(task1Latch, 60, TimeUnit.SECONDS);
        latch1.countDown();
    })).isTrue();
    // The number of free slots should be reduced by one
    assertThat(pool.freeSlots()).isEqualTo(1);
    assertThat(pool.anySlotsUsed()).isTrue();
    // Execute the second task
    assertThat(pool.execute(() -> {
        Uninterruptibles.awaitUninterruptibly(task2Latch, 60, TimeUnit.SECONDS);
        latch2.countDown();
    })).isTrue();
    // The number of free slots should be reduced by one
    assertThat(pool.freeSlots()).isEqualTo(0);
    // Since there are no slots left, the tasks shouldn't be executed
    assertThat(pool.execute(() -> {
    })).isFalse();
    assertThat(pool.freeSlots()).isEqualTo(0);
    // Wait for the first task to finish
    task1Latch.countDown();
    assertThat(latch1.await(60, TimeUnit.SECONDS)).isTrue();
    // Wait for the second task to finish
    task2Latch.countDown();
    assertThat(latch2.await(60, TimeUnit.SECONDS)).isTrue();
    pool.doGracefulShutdown();
    assertThat(pool.anySlotsUsed()).isFalse();
    verify(shutdownCallback, times(1)).run();
}
Also used : MetricRegistry(com.codahale.metrics.MetricRegistry) CountDownLatch(java.util.concurrent.CountDownLatch) GracefulShutdownService(org.graylog2.system.shutdown.GracefulShutdownService) Test(org.junit.jupiter.api.Test)

Aggregations

MetricRegistry (com.codahale.metrics.MetricRegistry)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 GracefulShutdownService (org.graylog2.system.shutdown.GracefulShutdownService)1 Test (org.junit.jupiter.api.Test)1