@Test public void testRetrySucceed() throws InterruptedException { CountDownLatch startLatch = new CountDownLatch(1); Service service = new RetryOnStartFailureService( createServiceSupplier(3, startLatch, new CountDownLatch(1), false), RetryStrategies.fixDelay(10, TimeUnit.MILLISECONDS)); service.startAndWait(); Assert.assertTrue(startLatch.await(1, TimeUnit.SECONDS)); }
@Test public void testStopWhileRetrying() throws InterruptedException { // This test the service can be stopped during failure retry CountDownLatch failureLatch = new CountDownLatch(1); Service service = new RetryOnStartFailureService( createServiceSupplier(1000, new CountDownLatch(1), failureLatch, false), RetryStrategies.fixDelay(10, TimeUnit.MILLISECONDS)); service.startAndWait(); Assert.assertTrue(failureLatch.await(1, TimeUnit.SECONDS)); service.stopAndWait(); }
@Test public void testRetryFail() throws InterruptedException { CountDownLatch startLatch = new CountDownLatch(1); Service service = new RetryOnStartFailureService( createServiceSupplier(1000, startLatch, new CountDownLatch(1), false), RetryStrategies.limit(10, RetryStrategies.fixDelay(10, TimeUnit.MILLISECONDS))); final CountDownLatch failureLatch = new CountDownLatch(1); service.addListener(new ServiceListenerAdapter() { @Override public void failed(Service.State from, Throwable failure) { failureLatch.countDown(); } }, Threads.SAME_THREAD_EXECUTOR); service.start(); Assert.assertTrue(failureLatch.await(1, TimeUnit.SECONDS)); Assert.assertFalse(startLatch.await(100, TimeUnit.MILLISECONDS)); }
@Test public void testStopFailurePropagate() throws InterruptedException, TimeoutException, ExecutionException { // This test the underlying service stop state is propagated if the start was successful CountDownLatch startLatch = new CountDownLatch(1); final RetryOnStartFailureService service = new RetryOnStartFailureService( createServiceSupplier(0, startLatch, new CountDownLatch(1), true), RetryStrategies.fixDelay(10, TimeUnit.MILLISECONDS)); service.startAndWait(); // block until the underlying service started successfully Assert.assertTrue(startLatch.await(1, TimeUnit.SECONDS)); // As documented in the RetryOnStartFailureService, there is a small race after the // underlying service started to the time when the wrapping retry service knows about it // In order to capture the stop failure correctly, we need to wait until the retry service captured // the actual service instance that get started Tasks.waitFor(true, new Callable<Boolean>() { @Override public Boolean call() throws Exception { return service.getStartedService() != null; } }, 5, TimeUnit.SECONDS, 100, TimeUnit.MILLISECONDS); try { service.stopAndWait(); Assert.fail("Expected failure in stopping"); } catch (Exception e) { Assert.assertEquals("Intentional failure to shutdown", Throwables.getRootCause(e).getMessage()); } }