use of com.google.common.util.concurrent.Service in project cdap by caskdata.
the class RetryOnStartFailureServiceTest method testRetrySucceed.
@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));
}
use of com.google.common.util.concurrent.Service in project cdap by caskdata.
the class RetryOnStartFailureServiceTest method testStopWhileRetrying.
@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();
}
use of com.google.common.util.concurrent.Service in project cdap by caskdata.
the class RetryOnStartFailureServiceTest method testLoggingContext.
@Test
public void testLoggingContext() {
// This test logging context set before the service started is propagated into the service
final Map<String, String> context = Collections.singletonMap("key", "value");
// Create the service before setting the context.
Service service = new RetryOnStartFailureService(new Supplier<Service>() {
@Override
public Service get() {
return new AbstractIdleService() {
@Override
protected void startUp() throws Exception {
Assert.assertEquals(context, MDC.getCopyOfContextMap());
}
@Override
protected void shutDown() throws Exception {
Assert.assertEquals(context, MDC.getCopyOfContextMap());
}
};
}
}, RetryStrategies.noRetry());
// Set the MDC context
MDC.setContextMap(context);
// Start and stop shouldn't throw
service.startAndWait();
service.stopAndWait();
}
use of com.google.common.util.concurrent.Service in project cdap by caskdata.
the class AbstractProgramRuntimeServiceTest method createProgramRunnerFactory.
/**
* Creates a {@link ProgramRunnerFactory} for creating {@link ProgramRunner}
* that always run with a {@link FastService}.
*
* @param argumentsMap the map to be populated with the user arguments for each run.
*/
private ProgramRunnerFactory createProgramRunnerFactory(final Map<ProgramId, Arguments> argumentsMap) {
return new ProgramRunnerFactory() {
@Override
public ProgramRunner create(ProgramType programType) {
return new ProgramRunner() {
@Override
public ProgramController run(Program program, ProgramOptions options) {
argumentsMap.put(program.getId(), options.getUserArguments());
Service service = new FastService();
ProgramController controller = new ProgramControllerServiceAdapter(service, program.getId(), RunIds.generate());
service.start();
return controller;
}
};
}
};
}
use of com.google.common.util.concurrent.Service in project cdap by caskdata.
the class ProgramControllerTest method testInitState.
@Test
public void testInitState() throws ExecutionException, InterruptedException {
// To test against race-condition, we start/stop service multiple times.
// If there is race, there is a chance that this test will fail in some env.
// Otherwise it should always pass
ExecutorService executor = Executors.newCachedThreadPool();
int serviceCount = 1000;
final CountDownLatch latch = new CountDownLatch(serviceCount);
ProgramId programId = new ApplicationId(NamespaceId.DEFAULT.getNamespace(), "test").service("test");
for (int i = 0; i < serviceCount; i++) {
// Creates a controller for a guava service do nothing in start/stop.
// The short time in start creates a chance to have out-of-order init() and alive() call if there is a race.
Service service = new TestService(0, 0);
ProgramController controller = new ProgramControllerServiceAdapter(service, programId, RunIds.generate());
ListenableFuture<Service.State> startCompletion = service.start();
controller.addListener(new AbstractListener() {
private volatile boolean initCalled;
@Override
public void init(ProgramController.State currentState, @Nullable Throwable cause) {
initCalled = true;
if (currentState == ProgramController.State.ALIVE) {
latch.countDown();
}
}
@Override
public void alive() {
if (initCalled) {
latch.countDown();
} else {
LOG.error("init() not called before alive()");
}
}
}, executor);
startCompletion.get();
service.stopAndWait();
}
Assert.assertTrue(latch.await(5, TimeUnit.SECONDS));
}
Aggregations