use of co.cask.cdap.internal.app.runtime.ProgramControllerServiceAdapter 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 co.cask.cdap.internal.app.runtime.ProgramControllerServiceAdapter 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