Search in sources :

Example 1 with OneShotLatch

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

the class StreamTaskTest method testCancellationNotBlockedOnLock.

@Test
public void testCancellationNotBlockedOnLock() throws Exception {
    SYNC_LATCH = new OneShotLatch();
    StreamConfig cfg = new StreamConfig(new Configuration());
    Task task = createTask(CancelLockingTask.class, 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();
    SYNC_LATCH.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) Configuration(org.apache.flink.configuration.Configuration) OneShotLatch(org.apache.flink.core.testutils.OneShotLatch) StreamConfig(org.apache.flink.streaming.api.graph.StreamConfig) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 2 with OneShotLatch

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

the class StreamTaskTest method testCancellationFailsWithBlockingLock.

@Test
public void testCancellationFailsWithBlockingLock() throws Exception {
    SYNC_LATCH = new OneShotLatch();
    StreamConfig cfg = new StreamConfig(new Configuration());
    Task task = createTask(CancelFailingTask.class, 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();
    SYNC_LATCH.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) Configuration(org.apache.flink.configuration.Configuration) OneShotLatch(org.apache.flink.core.testutils.OneShotLatch) StreamConfig(org.apache.flink.streaming.api.graph.StreamConfig) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 3 with OneShotLatch

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

the class SystemProcessingTimeServiceTest method testExceptionReportingScheduleAtFixedRate.

@Test
public void testExceptionReportingScheduleAtFixedRate() throws InterruptedException {
    final AtomicBoolean exceptionWasThrown = new AtomicBoolean(false);
    final OneShotLatch latch = new OneShotLatch();
    final Object lock = new Object();
    ProcessingTimeService timeServiceProvider = new SystemProcessingTimeService(new AsyncExceptionHandler() {

        @Override
        public void handleAsyncException(String message, Throwable exception) {
            exceptionWasThrown.set(true);
            latch.trigger();
        }
    }, lock);
    timeServiceProvider.scheduleAtFixedRate(new ProcessingTimeCallback() {

        @Override
        public void onProcessingTime(long timestamp) throws Exception {
            throw new Exception("Exception in Timer");
        }
    }, 0L, 100L);
    latch.await();
    assertTrue(exceptionWasThrown.get());
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) OneShotLatch(org.apache.flink.core.testutils.OneShotLatch) Test(org.junit.Test)

Example 4 with OneShotLatch

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

the class SystemProcessingTimeServiceTest method testExceptionReporting.

@Test
public void testExceptionReporting() throws InterruptedException {
    final AtomicBoolean exceptionWasThrown = new AtomicBoolean(false);
    final OneShotLatch latch = new OneShotLatch();
    final Object lock = new Object();
    ProcessingTimeService timeServiceProvider = new SystemProcessingTimeService(new AsyncExceptionHandler() {

        @Override
        public void handleAsyncException(String message, Throwable exception) {
            exceptionWasThrown.set(true);
            latch.trigger();
        }
    }, lock);
    timeServiceProvider.registerTimer(System.currentTimeMillis(), new ProcessingTimeCallback() {

        @Override
        public void onProcessingTime(long timestamp) throws Exception {
            throw new Exception("Exception in Timer");
        }
    });
    latch.await();
    assertTrue(exceptionWasThrown.get());
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) OneShotLatch(org.apache.flink.core.testutils.OneShotLatch) Test(org.junit.Test)

Example 5 with OneShotLatch

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

the class SystemProcessingTimeServiceTest method testScheduleAtFixedRateHoldsLock.

/**
	 * Tests that the schedule at fixed rate callback is called under the given lock
	 */
@Test
public void testScheduleAtFixedRateHoldsLock() throws Exception {
    final Object lock = new Object();
    final AtomicReference<Throwable> errorRef = new AtomicReference<>();
    final SystemProcessingTimeService timer = new SystemProcessingTimeService(new ReferenceSettingExceptionHandler(errorRef), lock);
    final OneShotLatch awaitCallback = new OneShotLatch();
    try {
        assertEquals(0, timer.getNumTasksScheduled());
        // schedule something
        ScheduledFuture<?> future = timer.scheduleAtFixedRate(new ProcessingTimeCallback() {

            @Override
            public void onProcessingTime(long timestamp) {
                assertTrue(Thread.holdsLock(lock));
                awaitCallback.trigger();
            }
        }, 0L, 100L);
        // wait until the first execution is active
        awaitCallback.await();
        // cancel periodic callback
        future.cancel(true);
        // check that no asynchronous error was reported
        if (errorRef.get() != null) {
            throw new Exception(errorRef.get());
        }
    } finally {
        timer.shutdownService();
    }
}
Also used : OneShotLatch(org.apache.flink.core.testutils.OneShotLatch) AtomicReference(java.util.concurrent.atomic.AtomicReference) ReferenceSettingExceptionHandler(org.apache.flink.streaming.runtime.operators.TestProcessingTimeServiceTest.ReferenceSettingExceptionHandler) Test(org.junit.Test)

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