diff options
Diffstat (limited to 'guava-tests/test/com/google/common/util/concurrent/ServiceManagerTest.java')
-rw-r--r-- | guava-tests/test/com/google/common/util/concurrent/ServiceManagerTest.java | 73 |
1 files changed, 32 insertions, 41 deletions
diff --git a/guava-tests/test/com/google/common/util/concurrent/ServiceManagerTest.java b/guava-tests/test/com/google/common/util/concurrent/ServiceManagerTest.java index 9f22a10cd..44a667d14 100644 --- a/guava-tests/test/com/google/common/util/concurrent/ServiceManagerTest.java +++ b/guava-tests/test/com/google/common/util/concurrent/ServiceManagerTest.java @@ -16,10 +16,13 @@ package com.google.common.util.concurrent; +import static com.google.common.base.StandardSystemProperty.JAVA_SPECIFICATION_VERSION; +import static com.google.common.base.StandardSystemProperty.OS_NAME; import static com.google.common.truth.Truth.assertThat; import static com.google.common.util.concurrent.MoreExecutors.directExecutor; import static java.util.Arrays.asList; import static java.util.concurrent.TimeUnit.SECONDS; +import static org.junit.Assert.assertThrows; import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableSet; @@ -121,6 +124,10 @@ public class ServiceManagerTest extends TestCase { } public void testServiceStartupTimes() { + if (isWindows() && isJava8()) { + // Flaky there: https://github.com/google/guava/pull/6731#issuecomment-1736298607 + return; + } Service a = new NoOpDelayedService(150); Service b = new NoOpDelayedService(353); ServiceManager serviceManager = new ServiceManager(asList(a, b)); @@ -132,6 +139,10 @@ public class ServiceManagerTest extends TestCase { } public void testServiceStartupDurations() { + if (isWindows() && isJava8()) { + // Flaky there: https://github.com/google/guava/pull/6731#issuecomment-1736298607 + return; + } Service a = new NoOpDelayedService(150); Service b = new NoOpDelayedService(353); ServiceManager serviceManager = new ServiceManager(asList(a, b)); @@ -207,11 +218,7 @@ public class ServiceManagerTest extends TestCase { RecordingListener listener = new RecordingListener(); manager.addListener(listener, directExecutor()); assertState(manager, Service.State.NEW, a, b, c, d, e); - try { - manager.startAsync().awaitHealthy(); - fail(); - } catch (IllegalStateException expected) { - } + assertThrows(IllegalStateException.class, () -> manager.startAsync().awaitHealthy()); assertFalse(listener.healthyCalled); assertState(manager, Service.State.RUNNING, a, c, e); assertEquals(ImmutableSet.of(b, d), listener.failedServices); @@ -231,11 +238,7 @@ public class ServiceManagerTest extends TestCase { RecordingListener listener = new RecordingListener(); manager.addListener(listener, directExecutor()); assertState(manager, Service.State.NEW, a, b); - try { - manager.startAsync().awaitHealthy(); - fail(); - } catch (IllegalStateException expected) { - } + assertThrows(IllegalStateException.class, () -> manager.startAsync().awaitHealthy()); assertTrue(listener.healthyCalled); assertEquals(ImmutableSet.of(b), listener.failedServices); @@ -278,19 +281,11 @@ public class ServiceManagerTest extends TestCase { Service a = new NoOpDelayedService(50); ServiceManager manager = new ServiceManager(asList(a)); manager.startAsync(); - try { - manager.awaitHealthy(1, TimeUnit.MILLISECONDS); - fail(); - } catch (TimeoutException expected) { - } + assertThrows(TimeoutException.class, () -> manager.awaitHealthy(1, TimeUnit.MILLISECONDS)); manager.awaitHealthy(5, SECONDS); // no exception thrown manager.stopAsync(); - try { - manager.awaitStopped(1, TimeUnit.MILLISECONDS); - fail(); - } catch (TimeoutException expected) { - } + assertThrows(TimeoutException.class, () -> manager.awaitStopped(1, TimeUnit.MILLISECONDS)); manager.awaitStopped(5, SECONDS); // no exception thrown } @@ -303,11 +298,7 @@ public class ServiceManagerTest extends TestCase { ServiceManager manager = new ServiceManager(asList(a)); RecordingListener listener = new RecordingListener(); manager.addListener(listener, directExecutor()); - try { - manager.startAsync().awaitHealthy(); - fail(); - } catch (IllegalStateException expected) { - } + assertThrows(IllegalStateException.class, () -> manager.startAsync().awaitHealthy()); assertTrue(listener.stoppedCalled); } @@ -320,11 +311,7 @@ public class ServiceManagerTest extends TestCase { ServiceManager manager = new ServiceManager(asList(a)); RecordingListener listener = new RecordingListener(); manager.addListener(listener, directExecutor()); - try { - manager.startAsync().awaitHealthy(); - fail(); - } catch (IllegalStateException expected) { - } + assertThrows(IllegalStateException.class, () -> manager.startAsync().awaitHealthy()); assertFalse(listener.healthyCalled); } @@ -523,11 +510,7 @@ public class ServiceManagerTest extends TestCase { logger.addHandler(logHandler); NoOpService service = new NoOpService(); service.startAsync(); - try { - new ServiceManager(Arrays.asList(service)); - fail(); - } catch (IllegalArgumentException expected) { - } + assertThrows(IllegalArgumentException.class, () -> new ServiceManager(Arrays.asList(service))); service.stopAsync(); // Nothing was logged! assertEquals(0, logHandler.getStoredLogRecords().size()); @@ -548,6 +531,7 @@ public class ServiceManagerTest extends TestCase { service1.startAsync(); delegate.addListener(listener, executor); } + // Delegates from here on down @Override public final Service startAsync() { @@ -594,12 +578,11 @@ public class ServiceManagerTest extends TestCase { return delegate.failureCause(); } }; - try { - new ServiceManager(Arrays.asList(service1, service2)); - fail(); - } catch (IllegalArgumentException expected) { - assertThat(expected.getMessage()).contains("started transitioning asynchronously"); - } + IllegalArgumentException expected = + assertThrows( + IllegalArgumentException.class, + () -> new ServiceManager(Arrays.asList(service1, service2))); + assertThat(expected.getMessage()).contains("started transitioning asynchronously"); } /** @@ -678,4 +661,12 @@ public class ServiceManagerTest extends TestCase { failedServices.add(service); } } + + private static boolean isWindows() { + return OS_NAME.value().startsWith("Windows"); + } + + private static boolean isJava8() { + return JAVA_SPECIFICATION_VERSION.value().equals("1.8"); + } } |