Search in sources :

Example 6 with ServiceState

use of org.bboxdb.commons.ServiceState in project bboxdb by jnidzwetzki.

the class TestServiceState method testTransition4.

/**
 * Error dispatching
 * @throws InterruptedException
 */
@Test(timeout = 1000)
public void testTransition4() throws InterruptedException {
    final ServiceState state = new ServiceState();
    (new Thread(() -> {
        try {
            Thread.sleep(200);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        state.dipatchToStarting();
        state.dispatchToRunning();
    })).start();
    state.awaitRunning();
    Assert.assertTrue(state.isInRunningState());
}
Also used : ServiceState(org.bboxdb.commons.ServiceState) Test(org.junit.Test)

Example 7 with ServiceState

use of org.bboxdb.commons.ServiceState in project bboxdb by jnidzwetzki.

the class TestServiceState method testReset.

/**
 * Test the reset call
 */
@Test(timeout = 60000)
public void testReset() {
    final ServiceState state = new ServiceState();
    final IllegalArgumentException exception = new IllegalArgumentException();
    state.dispatchToFailed(exception);
    state.reset();
    Assert.assertEquals(null, state.getThrowable());
    Assert.assertTrue(state.isInNewState());
}
Also used : ServiceState(org.bboxdb.commons.ServiceState) Test(org.junit.Test)

Example 8 with ServiceState

use of org.bboxdb.commons.ServiceState in project bboxdb by jnidzwetzki.

the class TestServiceState method testTransition5.

/**
 * Error dispatching
 * @throws InterruptedException
 */
@Test(timeout = 1000)
public void testTransition5() throws InterruptedException {
    final ServiceState state = new ServiceState();
    final IllegalArgumentException exception = new IllegalArgumentException();
    state.dispatchToFailed(exception);
    Assert.assertTrue(state.isInFinishedState());
    Assert.assertEquals(exception, state.getThrowable());
    state.dispatchToRunning();
    state.dispatchToStopping();
    state.dispatchToRunning();
    state.dispatchToTerminated();
    Assert.assertTrue(state.isInFinishedState());
}
Also used : ServiceState(org.bboxdb.commons.ServiceState) Test(org.junit.Test)

Example 9 with ServiceState

use of org.bboxdb.commons.ServiceState in project bboxdb by jnidzwetzki.

the class TestServiceState method testCallbackListenerRegisterAndUnregister.

/**
 * Test the register and unregister method
 */
@Test(timeout = 60000)
public void testCallbackListenerRegisterAndUnregister() {
    final ServiceState state = new ServiceState();
    final Consumer<? super ServiceState> consumer = (s) -> {
    };
    // Callback is unkown
    Assert.assertFalse(state.removeCallback(consumer));
    state.registerCallback(consumer);
    // Callback is known
    Assert.assertTrue(state.removeCallback(consumer));
    // Callback is unkown
    Assert.assertFalse(state.removeCallback(consumer));
}
Also used : Consumer(java.util.function.Consumer) ServiceState(org.bboxdb.commons.ServiceState) Semaphore(java.util.concurrent.Semaphore) State(org.bboxdb.commons.ServiceState.State) Test(org.junit.Test) Assert(org.junit.Assert) ServiceState(org.bboxdb.commons.ServiceState) Test(org.junit.Test)

Example 10 with ServiceState

use of org.bboxdb.commons.ServiceState in project bboxdb by jnidzwetzki.

the class TestServiceState method testCallbackListener.

/**
 * Test the callback listener
 */
@Test(timeout = 1000)
public void testCallbackListener() {
    final Semaphore semaphore = new Semaphore(0);
    final ServiceState state = new ServiceState();
    state.registerCallback((s) -> {
        if (s.getState() == State.TERMINATED) {
            semaphore.release();
        }
    });
    Assert.assertEquals(0, semaphore.availablePermits());
    state.dipatchToStarting();
    Assert.assertEquals(0, semaphore.availablePermits());
    state.dispatchToRunning();
    Assert.assertEquals(0, semaphore.availablePermits());
    state.dispatchToStopping();
    Assert.assertEquals(0, semaphore.availablePermits());
    state.dispatchToTerminated();
    Assert.assertEquals(1, semaphore.availablePermits());
}
Also used : ServiceState(org.bboxdb.commons.ServiceState) Semaphore(java.util.concurrent.Semaphore) Test(org.junit.Test)

Aggregations

ServiceState (org.bboxdb.commons.ServiceState)12 Test (org.junit.Test)12 Semaphore (java.util.concurrent.Semaphore)2 Consumer (java.util.function.Consumer)1 State (org.bboxdb.commons.ServiceState.State)1 Assert (org.junit.Assert)1