Search in sources :

Example 6 with ReferenceSettingExceptionHandler

use of org.apache.flink.streaming.runtime.operators.TestProcessingTimeServiceTest.ReferenceSettingExceptionHandler in project flink by apache.

the class SystemProcessingTimeServiceTest method testQuiesceAndAwaitingCancelsScheduledAtFixRateFuture.

/**
	 * Tests that shutting down the SystemProcessingTimeService will also cancel the scheduled at
	 * fix rate future.
	 */
@Test
public void testQuiesceAndAwaitingCancelsScheduledAtFixRateFuture() throws Exception {
    final Object lock = new Object();
    final AtomicReference<Throwable> errorRef = new AtomicReference<>();
    final long period = 10L;
    final SystemProcessingTimeService timer = new SystemProcessingTimeService(new ReferenceSettingExceptionHandler(errorRef), lock);
    try {
        ScheduledFuture<?> scheduledFuture = timer.scheduleAtFixedRate(new ProcessingTimeCallback() {

            @Override
            public void onProcessingTime(long timestamp) throws Exception {
            }
        }, 0L, period);
        assertFalse(scheduledFuture.isDone());
        // this should cancel our future
        timer.quiesceAndAwaitPending();
        assertTrue(scheduledFuture.isCancelled());
        scheduledFuture = timer.scheduleAtFixedRate(new ProcessingTimeCallback() {

            @Override
            public void onProcessingTime(long timestamp) throws Exception {
                throw new Exception("Test exception.");
            }
        }, 0L, 100L);
        assertNotNull(scheduledFuture);
        assertEquals(0, timer.getNumTasksScheduled());
        if (errorRef.get() != null) {
            throw new Exception(errorRef.get());
        }
    } finally {
        timer.shutdownService();
    }
}
Also used : AtomicReference(java.util.concurrent.atomic.AtomicReference) ReferenceSettingExceptionHandler(org.apache.flink.streaming.runtime.operators.TestProcessingTimeServiceTest.ReferenceSettingExceptionHandler) Test(org.junit.Test)

Example 7 with ReferenceSettingExceptionHandler

use of org.apache.flink.streaming.runtime.operators.TestProcessingTimeServiceTest.ReferenceSettingExceptionHandler in project flink by apache.

the class SystemProcessingTimeServiceTest method testFutureCancellation.

@Test
public void testFutureCancellation() throws Exception {
    final Object lock = new Object();
    final AtomicReference<Throwable> errorRef = new AtomicReference<>();
    final SystemProcessingTimeService timer = new SystemProcessingTimeService(new ReferenceSettingExceptionHandler(errorRef), lock);
    try {
        assertEquals(0, timer.getNumTasksScheduled());
        // schedule something
        ScheduledFuture<?> future = timer.registerTimer(System.currentTimeMillis() + 100000000, new ProcessingTimeCallback() {

            @Override
            public void onProcessingTime(long timestamp) {
            }
        });
        assertEquals(1, timer.getNumTasksScheduled());
        future.cancel(false);
        assertEquals(0, timer.getNumTasksScheduled());
        future = timer.scheduleAtFixedRate(new ProcessingTimeCallback() {

            @Override
            public void onProcessingTime(long timestamp) throws Exception {
            }
        }, 10000000000L, 50L);
        assertEquals(1, timer.getNumTasksScheduled());
        future.cancel(false);
        assertEquals(0, timer.getNumTasksScheduled());
        // check that no asynchronous error was reported
        if (errorRef.get() != null) {
            throw new Exception(errorRef.get());
        }
    } finally {
        timer.shutdownService();
    }
}
Also used : AtomicReference(java.util.concurrent.atomic.AtomicReference) ReferenceSettingExceptionHandler(org.apache.flink.streaming.runtime.operators.TestProcessingTimeServiceTest.ReferenceSettingExceptionHandler) Test(org.junit.Test)

Aggregations

AtomicReference (java.util.concurrent.atomic.AtomicReference)7 ReferenceSettingExceptionHandler (org.apache.flink.streaming.runtime.operators.TestProcessingTimeServiceTest.ReferenceSettingExceptionHandler)7 Test (org.junit.Test)7 OneShotLatch (org.apache.flink.core.testutils.OneShotLatch)3 CountDownLatch (java.util.concurrent.CountDownLatch)1 ReentrantLock (java.util.concurrent.locks.ReentrantLock)1