Search in sources :

Example 1 with AbstractProgramController

use of io.cdap.cdap.internal.app.runtime.AbstractProgramController in project cdap by caskdata.

the class DelayedProgramControllerTest method testListener.

@Test
public void testListener() throws InterruptedException {
    ProgramRunId programRunId = new ProgramRunId("ns", "app", ProgramType.SPARK, "program", RunIds.generate().getId());
    DelayedProgramController controller = new DelayedProgramController(programRunId);
    CountDownLatch aliveLatch = new CountDownLatch(1);
    CountDownLatch killedLatch = new CountDownLatch(1);
    controller.addListener(new AbstractListener() {

        @Override
        public void init(ProgramController.State currentState, @Nullable Throwable cause) {
            if (currentState == ProgramController.State.ALIVE) {
                alive();
            } else if (currentState == ProgramController.State.KILLED) {
                completed();
            }
        }

        @Override
        public void alive() {
            aliveLatch.countDown();
        }

        @Override
        public void killed() {
            killedLatch.countDown();
        }
    }, Threads.SAME_THREAD_EXECUTOR);
    // Perform state change on the delegate
    ProgramController delegate = new AbstractProgramController(programRunId) {

        {
            started();
        }

        @Override
        protected void doSuspend() {
        // no-op
        }

        @Override
        protected void doResume() {
        // no-op
        }

        @Override
        protected void doStop() {
        // no-op
        }

        @Override
        protected void doCommand(String name, Object value) {
        // no-op
        }
    };
    controller.setProgramController(delegate);
    Assert.assertTrue(aliveLatch.await(5, TimeUnit.SECONDS));
    controller.stop();
    Assert.assertTrue(killedLatch.await(5, TimeUnit.SECONDS));
}
Also used : AbstractProgramController(io.cdap.cdap.internal.app.runtime.AbstractProgramController) AbstractProgramController(io.cdap.cdap.internal.app.runtime.AbstractProgramController) AbstractListener(io.cdap.cdap.internal.app.runtime.AbstractListener) ProgramRunId(io.cdap.cdap.proto.id.ProgramRunId) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 2 with AbstractProgramController

use of io.cdap.cdap.internal.app.runtime.AbstractProgramController in project cdap by cdapio.

the class DelayedProgramControllerTest method testListener.

@Test
public void testListener() throws InterruptedException {
    ProgramRunId programRunId = new ProgramRunId("ns", "app", ProgramType.SPARK, "program", RunIds.generate().getId());
    DelayedProgramController controller = new DelayedProgramController(programRunId);
    CountDownLatch aliveLatch = new CountDownLatch(1);
    CountDownLatch killedLatch = new CountDownLatch(1);
    controller.addListener(new AbstractListener() {

        @Override
        public void init(ProgramController.State currentState, @Nullable Throwable cause) {
            if (currentState == ProgramController.State.ALIVE) {
                alive();
            } else if (currentState == ProgramController.State.KILLED) {
                completed();
            }
        }

        @Override
        public void alive() {
            aliveLatch.countDown();
        }

        @Override
        public void killed() {
            killedLatch.countDown();
        }
    }, Threads.SAME_THREAD_EXECUTOR);
    // Perform state change on the delegate
    ProgramController delegate = new AbstractProgramController(programRunId) {

        {
            started();
        }

        @Override
        protected void doSuspend() {
        // no-op
        }

        @Override
        protected void doResume() {
        // no-op
        }

        @Override
        protected void doStop() {
        // no-op
        }

        @Override
        protected void doCommand(String name, Object value) {
        // no-op
        }
    };
    controller.setProgramController(delegate);
    Assert.assertTrue(aliveLatch.await(5, TimeUnit.SECONDS));
    controller.stop();
    Assert.assertTrue(killedLatch.await(5, TimeUnit.SECONDS));
}
Also used : AbstractProgramController(io.cdap.cdap.internal.app.runtime.AbstractProgramController) AbstractProgramController(io.cdap.cdap.internal.app.runtime.AbstractProgramController) AbstractListener(io.cdap.cdap.internal.app.runtime.AbstractListener) ProgramRunId(io.cdap.cdap.proto.id.ProgramRunId) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Aggregations

AbstractListener (io.cdap.cdap.internal.app.runtime.AbstractListener)2 AbstractProgramController (io.cdap.cdap.internal.app.runtime.AbstractProgramController)2 ProgramRunId (io.cdap.cdap.proto.id.ProgramRunId)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 Test (org.junit.Test)2