use of com.google.common.util.concurrent.Service.State 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());
}
use of com.google.common.util.concurrent.Service.State in project guava by google.
the class AbstractService method stopAsync.
@CanIgnoreReturnValue
@Override
public final Service stopAsync() {
if (monitor.enterIf(isStoppable)) {
try {
State previous = state();
switch(previous) {
case NEW:
snapshot = new StateSnapshot(TERMINATED);
enqueueTerminatedEvent(NEW);
break;
case STARTING:
snapshot = new StateSnapshot(STARTING, true, null);
enqueueStoppingEvent(STARTING);
break;
case RUNNING:
snapshot = new StateSnapshot(STOPPING);
enqueueStoppingEvent(RUNNING);
doStop();
break;
case STOPPING:
case TERMINATED:
case FAILED:
// These cases are impossible due to the if statement above.
throw new AssertionError("isStoppable is incorrectly implemented, saw: " + previous);
default:
throw new AssertionError("Unexpected state: " + previous);
}
} catch (Throwable shutdownFailure) {
notifyFailed(shutdownFailure);
} finally {
monitor.leave();
dispatchListenerEvents();
}
}
return this;
}
Aggregations