Search in sources :

Example 61 with OneShotLatch

use of org.apache.flink.core.testutils.OneShotLatch in project flink by apache.

the class BackgroundTaskTest method testAbortSkipsTasksWhichHaveNotBeenStarted.

@Test
public void testAbortSkipsTasksWhichHaveNotBeenStarted() {
    final OneShotLatch blockingLatch = new OneShotLatch();
    final BlockingQueue<Integer> taskCompletions = new ArrayBlockingQueue<>(2);
    final BackgroundTask<Void> backgroundTask = BackgroundTask.initialBackgroundTask(() -> {
        blockingLatch.await();
        taskCompletions.offer(1);
        return null;
    }, TEST_EXECUTOR_RESOURCE.getExecutor()).runAfter(() -> {
        taskCompletions.offer(2);
        return null;
    }, TEST_EXECUTOR_RESOURCE.getExecutor());
    final BackgroundTask<Void> finalTask = backgroundTask.runAfter(() -> {
        taskCompletions.offer(3);
        return null;
    }, TEST_EXECUTOR_RESOURCE.getExecutor());
    backgroundTask.abort();
    blockingLatch.trigger();
    finalTask.getTerminationFuture().join();
    assertThat(taskCompletions, Matchers.contains(1, 3));
}
Also used : ArrayBlockingQueue(java.util.concurrent.ArrayBlockingQueue) OneShotLatch(org.apache.flink.core.testutils.OneShotLatch) Test(org.junit.Test)

Example 62 with OneShotLatch

use of org.apache.flink.core.testutils.OneShotLatch in project flink by apache.

the class StreamTaskTest method testExecuteMailboxActionsAfterLeavingInputProcessorMailboxLoop.

@Test
public void testExecuteMailboxActionsAfterLeavingInputProcessorMailboxLoop() throws Exception {
    OneShotLatch latch = new OneShotLatch();
    try (MockEnvironment mockEnvironment = new MockEnvironmentBuilder().build()) {
        RunningTask<StreamTask<?, ?>> task = runTask(() -> new StreamTask<Object, StreamOperator<Object>>(mockEnvironment) {

            @Override
            protected void init() throws Exception {
            }

            @Override
            protected void processInput(MailboxDefaultAction.Controller controller) throws Exception {
                mailboxProcessor.getMailboxExecutor(0).execute(latch::trigger, "trigger");
                controller.suspendDefaultAction();
                mailboxProcessor.suspend();
            }
        });
        latch.await();
        task.waitForTaskCompletion(false);
    }
}
Also used : MockEnvironmentBuilder(org.apache.flink.runtime.operators.testutils.MockEnvironmentBuilder) MailboxDefaultAction(org.apache.flink.streaming.runtime.tasks.mailbox.MailboxDefaultAction) MockEnvironment(org.apache.flink.runtime.operators.testutils.MockEnvironment) OneShotLatch(org.apache.flink.core.testutils.OneShotLatch) StreamOperator(org.apache.flink.streaming.api.operators.StreamOperator) OneInputStreamOperator(org.apache.flink.streaming.api.operators.OneInputStreamOperator) AbstractStreamOperator(org.apache.flink.streaming.api.operators.AbstractStreamOperator) CheckpointException(org.apache.flink.runtime.checkpoint.CheckpointException) FunctionWithException(org.apache.flink.util.function.FunctionWithException) AsynchronousException(org.apache.flink.runtime.taskmanager.AsynchronousException) FlinkRuntimeException(org.apache.flink.util.FlinkRuntimeException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) CancelTaskException(org.apache.flink.runtime.execution.CancelTaskException) RunnableWithException(org.apache.flink.util.function.RunnableWithException) TimeoutException(java.util.concurrent.TimeoutException) ExpectedTestException(org.apache.flink.runtime.operators.testutils.ExpectedTestException) SupplierWithException(org.apache.flink.util.function.SupplierWithException) BiConsumerWithException(org.apache.flink.util.function.BiConsumerWithException) ExpectedException(org.junit.rules.ExpectedException) Test(org.junit.Test)

Example 63 with OneShotLatch

use of org.apache.flink.core.testutils.OneShotLatch in project flink by apache.

the class StreamTaskCancellationTest method testCanceleablesCanceledOnCancelTaskError.

@Test
public void testCanceleablesCanceledOnCancelTaskError() throws Exception {
    CancelFailingTask.syncLatch = new OneShotLatch();
    StreamConfig cfg = new StreamConfig(new Configuration());
    try (NettyShuffleEnvironment shuffleEnvironment = new NettyShuffleEnvironmentBuilder().build()) {
        Task task = createTask(CancelFailingTask.class, shuffleEnvironment, cfg, new Configuration());
        // start the task and wait until it runs
        // execution state RUNNING is not enough, we need to wait until the stream task's run()
        // method
        // is entered
        task.startTaskThread();
        CancelFailingTask.syncLatch.await();
        // cancel the execution - this should lead to smooth shutdown
        task.cancelExecution();
        task.getExecutingThread().join();
        assertEquals(ExecutionState.CANCELED, task.getExecutionState());
    }
}
Also used : Task(org.apache.flink.runtime.taskmanager.Task) StreamTaskTest.createTask(org.apache.flink.streaming.runtime.tasks.StreamTaskTest.createTask) Configuration(org.apache.flink.configuration.Configuration) OneShotLatch(org.apache.flink.core.testutils.OneShotLatch) StreamConfig(org.apache.flink.streaming.api.graph.StreamConfig) NettyShuffleEnvironmentBuilder(org.apache.flink.runtime.io.network.NettyShuffleEnvironmentBuilder) NettyShuffleEnvironment(org.apache.flink.runtime.io.network.NettyShuffleEnvironment) Test(org.junit.Test)

Example 64 with OneShotLatch

use of org.apache.flink.core.testutils.OneShotLatch in project flink by apache.

the class SystemProcessingTimeServiceTest method testImmediateShutdown.

@Test
public void testImmediateShutdown() throws Exception {
    final CompletableFuture<Throwable> errorFuture = new CompletableFuture<>();
    final SystemProcessingTimeService timer = createSystemProcessingTimeService(errorFuture);
    try {
        assertFalse(timer.isTerminated());
        final OneShotLatch latch = new OneShotLatch();
        // the task should trigger immediately and sleep until terminated with interruption
        timer.registerTimer(System.currentTimeMillis(), timestamp -> {
            latch.trigger();
            Thread.sleep(100000000);
        });
        latch.await();
        timer.shutdownService();
        assertTrue(timer.isTerminated());
        assertEquals(0, timer.getNumTasksScheduled());
        try {
            timer.registerTimer(System.currentTimeMillis() + 1000, timestamp -> fail("should not be called"));
            fail("should result in an exception");
        } catch (IllegalStateException e) {
        // expected
        }
        try {
            timer.scheduleAtFixedRate(timestamp -> fail("should not be called"), 0L, 100L);
            fail("should result in an exception");
        } catch (IllegalStateException e) {
        // expected
        }
        // check that the task eventually responded to interruption
        assertThat(errorFuture.get(30L, TimeUnit.SECONDS), instanceOf(InterruptedException.class));
    } finally {
        timer.shutdownService();
    }
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) OneShotLatch(org.apache.flink.core.testutils.OneShotLatch) Test(org.junit.Test)

Example 65 with OneShotLatch

use of org.apache.flink.core.testutils.OneShotLatch in project flink by apache.

the class SystemProcessingTimeServiceTest method createBlockingSystemProcessingTimeService.

private static SystemProcessingTimeService createBlockingSystemProcessingTimeService(final OneShotLatch blockUntilTriggered, final AtomicBoolean check) {
    final OneShotLatch waitUntilTimerStarted = new OneShotLatch();
    Preconditions.checkState(!check.get());
    final SystemProcessingTimeService timeService = new SystemProcessingTimeService(exception -> {
    });
    timeService.scheduleAtFixedRate(timestamp -> {
        waitUntilTimerStarted.trigger();
        boolean unblocked = false;
        while (!unblocked) {
            try {
                blockUntilTriggered.await();
                unblocked = true;
            } catch (InterruptedException ignore) {
            }
        }
        check.set(true);
    }, 0L, 10L);
    try {
        waitUntilTimerStarted.await();
    } catch (InterruptedException e) {
        Assert.fail("Problem while starting up service.");
    }
    return timeService;
}
Also used : OneShotLatch(org.apache.flink.core.testutils.OneShotLatch)

Aggregations

OneShotLatch (org.apache.flink.core.testutils.OneShotLatch)138 Test (org.junit.Test)118 JobID (org.apache.flink.api.common.JobID)41 CompletableFuture (java.util.concurrent.CompletableFuture)38 ExecutionException (java.util.concurrent.ExecutionException)27 Configuration (org.apache.flink.configuration.Configuration)26 IOException (java.io.IOException)24 Before (org.junit.Before)24 FlinkException (org.apache.flink.util.FlinkException)23 TestLogger (org.apache.flink.util.TestLogger)21 File (java.io.File)20 UUID (java.util.UUID)18 TimeoutException (java.util.concurrent.TimeoutException)18 TestingResourceManagerGateway (org.apache.flink.runtime.resourcemanager.utils.TestingResourceManagerGateway)18 Time (org.apache.flink.api.common.time.Time)17 TestingJobMasterGateway (org.apache.flink.runtime.jobmaster.utils.TestingJobMasterGateway)17 Rule (org.junit.Rule)17 Collections (java.util.Collections)16 ArrayBlockingQueue (java.util.concurrent.ArrayBlockingQueue)16 RpcUtils (org.apache.flink.runtime.rpc.RpcUtils)16