diff options
author | Android Build Coastguard Worker <android-build-coastguard-worker@google.com> | 2024-02-03 00:32:48 +0000 |
---|---|---|
committer | Gerrit Code Review <noreply-gerritcodereview@google.com> | 2024-02-03 00:32:48 +0000 |
commit | f25b489aa8cf9cce098394ef0e7f81c79c634f26 (patch) | |
tree | cbc7e4894708533a0ddc4e61da1d74e64393e0e1 /guava-tests/test/com/google/common/util/concurrent/AbstractServiceTest.java | |
parent | 205e5f2e2dc52e2212426602327ea0d630e1731f (diff) | |
parent | 0ccd979d1e45372e0b57851529eddd9c40671577 (diff) | |
download | guava-simpleperf-release.tar.gz |
Merge "Snap for 11400057 from fcb68273955c94ebe12fda34018935b0c25d56dc to simpleperf-release" into simpleperf-releasesimpleperf-release
Diffstat (limited to 'guava-tests/test/com/google/common/util/concurrent/AbstractServiceTest.java')
-rw-r--r-- | guava-tests/test/com/google/common/util/concurrent/AbstractServiceTest.java | 129 |
1 files changed, 39 insertions, 90 deletions
diff --git a/guava-tests/test/com/google/common/util/concurrent/AbstractServiceTest.java b/guava-tests/test/com/google/common/util/concurrent/AbstractServiceTest.java index 5f4210621..e4d310331 100644 --- a/guava-tests/test/com/google/common/util/concurrent/AbstractServiceTest.java +++ b/guava-tests/test/com/google/common/util/concurrent/AbstractServiceTest.java @@ -20,6 +20,7 @@ import static com.google.common.truth.Truth.assertThat; import static com.google.common.util.concurrent.MoreExecutors.directExecutor; import static java.lang.Thread.currentThread; import static java.util.concurrent.TimeUnit.SECONDS; +import static org.junit.Assert.assertThrows; import com.google.common.collect.ImmutableList; import com.google.common.collect.Iterables; @@ -449,12 +450,9 @@ public class AbstractServiceTest extends TestCase { service.notifyFailed(new Exception("1")); service.notifyFailed(new Exception("2")); assertThat(service.failureCause()).hasMessageThat().isEqualTo("1"); - try { - service.awaitRunning(); - fail(); - } catch (IllegalStateException e) { - assertThat(e).hasCauseThat().hasMessageThat().isEqualTo("1"); - } + IllegalStateException e = + assertThrows(IllegalStateException.class, () -> service.awaitRunning()); + assertThat(e).hasCauseThat().hasMessageThat().isEqualTo("1"); } private class ThreadedService extends AbstractService { @@ -531,11 +529,7 @@ public class AbstractServiceTest extends TestCase { service.stopAsync(); assertEquals(State.TERMINATED, service.state()); - try { - service.startAsync(); - fail(); - } catch (IllegalStateException expected) { - } + assertThrows(IllegalStateException.class, () -> service.startAsync()); assertEquals(State.TERMINATED, Iterables.getOnlyElement(listener.getStateHistory())); } @@ -543,13 +537,10 @@ public class AbstractServiceTest extends TestCase { StartFailingService service = new StartFailingService(); RecordingListener listener = RecordingListener.record(service); - try { - service.startAsync().awaitRunning(); - fail(); - } catch (IllegalStateException e) { - assertEquals(EXCEPTION, service.failureCause()); - assertThat(e).hasCauseThat().isEqualTo(EXCEPTION); - } + IllegalStateException e = + assertThrows(IllegalStateException.class, () -> service.startAsync().awaitRunning()); + assertEquals(EXCEPTION, service.failureCause()); + assertThat(e).hasCauseThat().isEqualTo(EXCEPTION); assertEquals(ImmutableList.of(State.STARTING, State.FAILED), listener.getStateHistory()); } @@ -558,13 +549,10 @@ public class AbstractServiceTest extends TestCase { RecordingListener listener = RecordingListener.record(service); service.startAsync().awaitRunning(); - try { - service.stopAsync().awaitTerminated(); - fail(); - } catch (IllegalStateException e) { - assertEquals(EXCEPTION, service.failureCause()); - assertThat(e).hasCauseThat().isEqualTo(EXCEPTION); - } + IllegalStateException e = + assertThrows(IllegalStateException.class, () -> service.stopAsync().awaitTerminated()); + assertEquals(EXCEPTION, service.failureCause()); + assertThat(e).hasCauseThat().isEqualTo(EXCEPTION); assertEquals( ImmutableList.of(State.STARTING, State.RUNNING, State.STOPPING, State.FAILED), listener.getStateHistory()); @@ -575,13 +563,10 @@ public class AbstractServiceTest extends TestCase { RecordingListener listener = RecordingListener.record(service); service.startAsync(); - try { - service.awaitRunning(); - fail(); - } catch (IllegalStateException e) { - assertEquals(EXCEPTION, service.failureCause()); - assertThat(e).hasCauseThat().isEqualTo(EXCEPTION); - } + IllegalStateException e = + assertThrows(IllegalStateException.class, () -> service.awaitRunning()); + assertEquals(EXCEPTION, service.failureCause()); + assertThat(e).hasCauseThat().isEqualTo(EXCEPTION); assertEquals( ImmutableList.of(State.STARTING, State.RUNNING, State.FAILED), listener.getStateHistory()); } @@ -590,13 +575,10 @@ public class AbstractServiceTest extends TestCase { StartThrowingService service = new StartThrowingService(); RecordingListener listener = RecordingListener.record(service); - try { - service.startAsync().awaitRunning(); - fail(); - } catch (IllegalStateException e) { - assertEquals(service.exception, service.failureCause()); - assertThat(e).hasCauseThat().isEqualTo(service.exception); - } + IllegalStateException e = + assertThrows(IllegalStateException.class, () -> service.startAsync().awaitRunning()); + assertEquals(service.exception, service.failureCause()); + assertThat(e).hasCauseThat().isEqualTo(service.exception); assertEquals(ImmutableList.of(State.STARTING, State.FAILED), listener.getStateHistory()); } @@ -605,13 +587,10 @@ public class AbstractServiceTest extends TestCase { RecordingListener listener = RecordingListener.record(service); service.startAsync().awaitRunning(); - try { - service.stopAsync().awaitTerminated(); - fail(); - } catch (IllegalStateException e) { - assertEquals(service.exception, service.failureCause()); - assertThat(e).hasCauseThat().isEqualTo(service.exception); - } + IllegalStateException e = + assertThrows(IllegalStateException.class, () -> service.stopAsync().awaitTerminated()); + assertEquals(service.exception, service.failureCause()); + assertThat(e).hasCauseThat().isEqualTo(service.exception); assertEquals( ImmutableList.of(State.STARTING, State.RUNNING, State.STOPPING, State.FAILED), listener.getStateHistory()); @@ -622,37 +601,23 @@ public class AbstractServiceTest extends TestCase { RecordingListener listener = RecordingListener.record(service); service.startAsync(); - try { - service.awaitTerminated(); - fail(); - } catch (IllegalStateException e) { - assertEquals(service.exception, service.failureCause()); - assertThat(e).hasCauseThat().isEqualTo(service.exception); - } + IllegalStateException e = + assertThrows(IllegalStateException.class, () -> service.awaitTerminated()); + assertEquals(service.exception, service.failureCause()); + assertThat(e).hasCauseThat().isEqualTo(service.exception); assertEquals( ImmutableList.of(State.STARTING, State.RUNNING, State.FAILED), listener.getStateHistory()); } public void testFailureCause_throwsIfNotFailed() { StopFailingService service = new StopFailingService(); - try { - service.failureCause(); - fail(); - } catch (IllegalStateException expected) { - } + assertThrows(IllegalStateException.class, () -> service.failureCause()); service.startAsync().awaitRunning(); - try { - service.failureCause(); - fail(); - } catch (IllegalStateException expected) { - } - try { - service.stopAsync().awaitTerminated(); - fail(); - } catch (IllegalStateException e) { - assertEquals(EXCEPTION, service.failureCause()); - assertThat(e).hasCauseThat().isEqualTo(EXCEPTION); - } + assertThrows(IllegalStateException.class, () -> service.failureCause()); + IllegalStateException e = + assertThrows(IllegalStateException.class, () -> service.stopAsync().awaitTerminated()); + assertEquals(EXCEPTION, service.failureCause()); + assertThat(e).hasCauseThat().isEqualTo(EXCEPTION); } public void testAddListenerAfterFailureDoesntCauseDeadlock() throws InterruptedException { @@ -912,40 +877,24 @@ public class AbstractServiceTest extends TestCase { public void testNotifyStartedWhenNotStarting() { AbstractService service = new DefaultService(); - try { - service.notifyStarted(); - fail(); - } catch (IllegalStateException expected) { - } + assertThrows(IllegalStateException.class, () -> service.notifyStarted()); } public void testNotifyStoppedWhenNotRunning() { AbstractService service = new DefaultService(); - try { - service.notifyStopped(); - fail(); - } catch (IllegalStateException expected) { - } + assertThrows(IllegalStateException.class, () -> service.notifyStopped()); } public void testNotifyFailedWhenNotStarted() { AbstractService service = new DefaultService(); - try { - service.notifyFailed(new Exception()); - fail(); - } catch (IllegalStateException expected) { - } + assertThrows(IllegalStateException.class, () -> service.notifyFailed(new Exception())); } public void testNotifyFailedWhenTerminated() { NoOpService service = new NoOpService(); service.startAsync().awaitRunning(); service.stopAsync().awaitTerminated(); - try { - service.notifyFailed(new Exception()); - fail(); - } catch (IllegalStateException expected) { - } + assertThrows(IllegalStateException.class, () -> service.notifyFailed(new Exception())); } private static class DefaultService extends AbstractService { |