本文整理汇总了Java中com.google.common.util.concurrent.Service.State类的典型用法代码示例。如果您正苦于以下问题:Java State类的具体用法?Java State怎么用?Java State使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
State类属于com.google.common.util.concurrent.Service包,在下文中一共展示了State类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: testThreadedServiceStartAndWaitStopAndWait
import com.google.common.util.concurrent.Service.State; //导入依赖的package包/类
public void testThreadedServiceStartAndWaitStopAndWait() throws Throwable {
ThreadedService service = new ThreadedService();
RecordingListener listener = RecordingListener.record(service);
service.startAsync().awaitRunning();
assertEquals(State.RUNNING, service.state());
service.awaitRunChecks();
service.stopAsync().awaitTerminated();
assertEquals(State.TERMINATED, service.state());
throwIfSet(thrownByExecutionThread);
assertEquals(
ImmutableList.of(
State.STARTING,
State.RUNNING,
State.STOPPING,
State.TERMINATED),
listener.getStateHistory());
}
示例2: testRun_writesMetrics
import com.google.common.util.concurrent.Service.State; //导入依赖的package包/类
@Test
public void testRun_writesMetrics() throws Exception {
Optional<ImmutableList<MetricPoint<?>>> threeBatch =
Optional.of(ImmutableList.of(point, point, point));
exporter.startAsync();
insertAndAssert(threeBatch);
// Insert another batch in order to block until the exporter has processed the last one
insertAndAssert(threeBatch);
// Force the exporter to finish so that the verify counts below are deterministic
insertAndAssert(poisonPill);
try {
exporter.awaitTerminated(500, TimeUnit.MILLISECONDS);
} catch (TimeoutException timeout) {
fail("MetricExporter did not reach the TERMINATED state after receiving a poison pill");
}
assertThat(exporter.state()).isNotEqualTo(State.FAILED);
verify(writer, times(6)).write(point);
verify(writer, times(2)).flush();
}
示例3: notifyStopped
import com.google.common.util.concurrent.Service.State; //导入依赖的package包/类
/**
* Implementing classes should invoke this method once their service has stopped. It will cause
* the service to transition from {@link State#STOPPING} to {@link State#TERMINATED}.
*
* @throws IllegalStateException if the service is neither {@link State#STOPPING} nor {@link
* State#RUNNING}.
*/
protected final void notifyStopped() {
monitor.enter();
try {
// We check the internal state of the snapshot instead of state() directly so we don't allow
// notifyStopped() to be called while STARTING, even if stop() has already been called.
State previous = snapshot.state;
if (previous != STOPPING && previous != RUNNING) {
IllegalStateException failure =
new IllegalStateException("Cannot notifyStopped() when the service is " + previous);
notifyFailed(failure);
throw failure;
}
snapshot = new StateSnapshot(TERMINATED);
enqueueTerminatedEvent(previous);
} finally {
monitor.leave();
dispatchListenerEvents();
}
}
示例4: testNoOpServiceStartStopAndWaitUninterruptible
import com.google.common.util.concurrent.Service.State; //导入依赖的package包/类
public void testNoOpServiceStartStopAndWaitUninterruptible()
throws Exception {
NoOpService service = new NoOpService();
currentThread().interrupt();
try {
service.startAsync().awaitRunning();
assertEquals(State.RUNNING, service.state());
service.stopAsync().awaitTerminated();
assertEquals(State.TERMINATED, service.state());
assertTrue(currentThread().isInterrupted());
} finally {
Thread.interrupted(); // clear interrupt for future tests
}
}
示例5: enqueueTerminatedEvent
import com.google.common.util.concurrent.Service.State; //导入依赖的package包/类
private void enqueueTerminatedEvent(final State from) {
switch (from) {
case NEW:
listeners.enqueue(TERMINATED_FROM_NEW_EVENT);
break;
case RUNNING:
listeners.enqueue(TERMINATED_FROM_RUNNING_EVENT);
break;
case STOPPING:
listeners.enqueue(TERMINATED_FROM_STOPPING_EVENT);
break;
case STARTING:
case TERMINATED:
case FAILED:
default:
throw new AssertionError();
}
}
示例6: StateSnapshot
import com.google.common.util.concurrent.Service.State; //导入依赖的package包/类
StateSnapshot(
State internalState, boolean shutdownWhenStartupFinishes, @Nullable Throwable failure) {
checkArgument(
!shutdownWhenStartupFinishes || internalState == STARTING,
"shudownWhenStartupFinishes can only be set if state is STARTING. Got %s instead.",
internalState);
checkArgument(
!(failure != null ^ internalState == FAILED),
"A failure cause should be set if and only if the state is failed. Got %s and %s "
+ "instead.",
internalState,
failure);
this.state = internalState;
this.shutdownWhenStartupFinishes = shutdownWhenStartupFinishes;
this.failure = failure;
}
示例7: testNoOpServiceStartStop
import com.google.common.util.concurrent.Service.State; //导入依赖的package包/类
public void testNoOpServiceStartStop() throws Exception {
NoOpService service = new NoOpService();
RecordingListener listener = RecordingListener.record(service);
assertEquals(State.NEW, service.state());
assertFalse(service.isRunning());
assertFalse(service.running);
service.startAsync();
assertEquals(State.RUNNING, service.state());
assertTrue(service.isRunning());
assertTrue(service.running);
service.stopAsync();
assertEquals(State.TERMINATED, service.state());
assertFalse(service.isRunning());
assertFalse(service.running);
assertEquals(
ImmutableList.of(
State.STARTING,
State.RUNNING,
State.STOPPING,
State.TERMINATED),
listener.getStateHistory());
}
示例8: testServiceStartStop
import com.google.common.util.concurrent.Service.State; //导入依赖的package包/类
public void testServiceStartStop() {
Service a = new NoOpService();
Service b = new NoOpService();
ServiceManager manager = new ServiceManager(asList(a, b));
RecordingListener listener = new RecordingListener();
manager.addListener(listener);
assertState(manager, Service.State.NEW, a, b);
assertFalse(manager.isHealthy());
manager.startAsync().awaitHealthy();
assertState(manager, Service.State.RUNNING, a, b);
assertTrue(manager.isHealthy());
assertTrue(listener.healthyCalled);
assertFalse(listener.stoppedCalled);
assertTrue(listener.failedServices.isEmpty());
manager.stopAsync().awaitStopped();
assertState(manager, Service.State.TERMINATED, a, b);
assertFalse(manager.isHealthy());
assertTrue(listener.stoppedCalled);
assertTrue(listener.failedServices.isEmpty());
}
示例9: testManualServiceStopMultipleTimesWhileStarting
import com.google.common.util.concurrent.Service.State; //导入依赖的package包/类
/**
* 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());
}
示例10: testFailingServiceStopAndWait_runFailing
import com.google.common.util.concurrent.Service.State; //导入依赖的package包/类
public void testFailingServiceStopAndWait_runFailing() throws Exception {
RunFailingService service = new RunFailingService();
RecordingListener listener = RecordingListener.record(service);
service.startAsync();
try {
service.awaitRunning();
fail();
} catch (IllegalStateException e) {
assertEquals(EXCEPTION, service.failureCause());
assertEquals(EXCEPTION, e.getCause());
}
assertEquals(
ImmutableList.of(
State.STARTING,
State.RUNNING,
State.FAILED),
listener.getStateHistory());
}
示例11: testThreadedServiceStopIdempotenceAfterWait
import com.google.common.util.concurrent.Service.State; //导入依赖的package包/类
public void testThreadedServiceStopIdempotenceAfterWait()
throws Throwable {
ThreadedService service = new ThreadedService();
service.startAsync().awaitRunning();
assertEquals(State.RUNNING, service.state());
service.awaitRunChecks();
service.stopAsync().awaitTerminated();
service.stopAsync();
assertEquals(State.TERMINATED, service.state());
executionThread.join();
throwIfSet(thrownByExecutionThread);
}
示例12: testFailingServiceStartAndWait
import com.google.common.util.concurrent.Service.State; //导入依赖的package包/类
public void testFailingServiceStartAndWait() throws Exception {
StartFailingService service = new StartFailingService();
RecordingListener listener = RecordingListener.record(service);
try {
service.startAsync().awaitRunning();
fail();
} catch (IllegalStateException e) {
assertEquals(EXCEPTION, service.failureCause());
assertEquals(EXCEPTION, e.getCause());
}
assertEquals(
ImmutableList.of(
State.STARTING,
State.FAILED),
listener.getStateHistory());
}
示例13: testFailingServiceStopAndWait_stopFailing
import com.google.common.util.concurrent.Service.State; //导入依赖的package包/类
public void testFailingServiceStopAndWait_stopFailing() throws Exception {
StopFailingService service = new StopFailingService();
RecordingListener listener = RecordingListener.record(service);
service.startAsync().awaitRunning();
try {
service.stopAsync().awaitTerminated();
fail();
} catch (IllegalStateException e) {
assertEquals(EXCEPTION, service.failureCause());
assertEquals(EXCEPTION, e.getCause());
}
assertEquals(
ImmutableList.of(
State.STARTING,
State.RUNNING,
State.STOPPING,
State.FAILED),
listener.getStateHistory());
}
示例14: testThrowingServiceStopAndWait_runThrowing
import com.google.common.util.concurrent.Service.State; //导入依赖的package包/类
public void testThrowingServiceStopAndWait_runThrowing() throws Exception {
RunThrowingService service = new RunThrowingService();
RecordingListener listener = RecordingListener.record(service);
service.startAsync();
try {
service.awaitTerminated();
fail();
} catch (IllegalStateException e) {
assertEquals(service.exception, service.failureCause());
assertEquals(service.exception, e.getCause());
}
assertEquals(
ImmutableList.of(
State.STARTING,
State.RUNNING,
State.FAILED),
listener.getStateHistory());
}
示例15: testFailOnErrorFromStartUpListener
import com.google.common.util.concurrent.Service.State; //导入依赖的package包/类
public void testFailOnErrorFromStartUpListener() throws InterruptedException {
final Error error = new Error();
final CountDownLatch latch = new CountDownLatch(1);
TestService service = new TestService();
service.addListener(new Service.Listener() {
@Override public void running() {
throw error;
}
@Override public void failed(State from, Throwable failure) {
assertEquals(State.RUNNING, from);
assertEquals(error, failure);
latch.countDown();
}
}, directExecutor());
service.startAsync();
latch.await();
assertEquals(0, service.numberOfTimesRunCalled.get());
assertEquals(Service.State.FAILED, service.state());
}