Search in sources :

Example 1 with Listener

use of com.google.common.util.concurrent.Service.Listener in project guava by google.

the class AbstractServiceTest method testManualServiceStopMultipleTimesWhileStarting.

/**
   * This tests for a bug where if {@link Service#stopAsync()} was called while the service was
   * {@link State#STARTING} more than once, the {@link Listener#stopping(State)} callback would get
   * called multiple times.
   */
public void testManualServiceStopMultipleTimesWhileStarting() throws Exception {
    ManualSwitchedService service = new ManualSwitchedService();
    final AtomicInteger stopppingCount = new AtomicInteger();
    service.addListener(new Listener() {

        @Override
        public void stopping(State from) {
            stopppingCount.incrementAndGet();
        }
    }, directExecutor());
    service.startAsync();
    service.stopAsync();
    assertEquals(1, stopppingCount.get());
    service.stopAsync();
    assertEquals(1, stopppingCount.get());
}
Also used : Listener(com.google.common.util.concurrent.Service.Listener) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) State(com.google.common.util.concurrent.Service.State)

Example 2 with Listener

use of com.google.common.util.concurrent.Service.Listener in project guava by google.

the class AbstractServiceTest method testListenerDoesntDeadlockOnStopAndWaitFromTerminated.

public void testListenerDoesntDeadlockOnStopAndWaitFromTerminated() throws Exception {
    final NoOpThreadedService service = new NoOpThreadedService();
    service.addListener(new Listener() {

        @Override
        public void terminated(State from) {
            service.stopAsync().awaitTerminated();
        }
    }, directExecutor());
    service.startAsync().awaitRunning();
    Thread thread = new Thread() {

        @Override
        public void run() {
            service.stopAsync().awaitTerminated();
        }
    };
    thread.start();
    thread.join(LONG_TIMEOUT_MILLIS);
    assertFalse(thread + " is deadlocked", thread.isAlive());
}
Also used : Listener(com.google.common.util.concurrent.Service.Listener) State(com.google.common.util.concurrent.Service.State) Thread.currentThread(java.lang.Thread.currentThread)

Example 3 with Listener

use of com.google.common.util.concurrent.Service.Listener in project guava by google.

the class AbstractServiceTest method testListenerDoesntDeadlockOnStartAndWaitFromRunning.

public void testListenerDoesntDeadlockOnStartAndWaitFromRunning() throws Exception {
    final NoOpThreadedService service = new NoOpThreadedService();
    service.addListener(new Listener() {

        @Override
        public void running() {
            service.awaitRunning();
        }
    }, directExecutor());
    service.startAsync().awaitRunning(LONG_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS);
    service.stopAsync();
}
Also used : Listener(com.google.common.util.concurrent.Service.Listener)

Aggregations

Listener (com.google.common.util.concurrent.Service.Listener)3 State (com.google.common.util.concurrent.Service.State)2 Thread.currentThread (java.lang.Thread.currentThread)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1