Search in sources :

Example 1 with ReferenceSettingExceptionHandler

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

the class SystemProcessingTimeServiceTest method testImmediateShutdown.

@Test
public void testImmediateShutdown() throws Exception {
    final Object lock = new Object();
    final AtomicReference<Throwable> errorRef = new AtomicReference<>();
    final SystemProcessingTimeService timer = new SystemProcessingTimeService(new ReferenceSettingExceptionHandler(errorRef), lock);
    try {
        assertFalse(timer.isTerminated());
        final OneShotLatch latch = new OneShotLatch();
        // the task should trigger immediately and should block until terminated with interruption
        timer.registerTimer(System.currentTimeMillis(), new ProcessingTimeCallback() {

            @Override
            public void onProcessingTime(long timestamp) throws Exception {
                latch.trigger();
                Thread.sleep(100000000);
            }
        });
        latch.await();
        timer.shutdownService();
        //noinspection SynchronizationOnLocalVariableOrMethodParameter
        synchronized (lock) {
            assertTrue(timer.isTerminated());
        }
        try {
            timer.registerTimer(System.currentTimeMillis() + 1000, new ProcessingTimeCallback() {

                @Override
                public void onProcessingTime(long timestamp) {
                }
            });
            fail("should result in an exception");
        } catch (IllegalStateException e) {
        // expected
        }
        try {
            timer.scheduleAtFixedRate(new ProcessingTimeCallback() {

                @Override
                public void onProcessingTime(long timestamp) {
                }
            }, 0L, 100L);
            fail("should result in an exception");
        } catch (IllegalStateException e) {
        // expected
        }
        // obviously, we have an asynchronous interrupted exception
        assertNotNull(errorRef.get());
        assertTrue(errorRef.get().getCause() instanceof InterruptedException);
        assertEquals(0, timer.getNumTasksScheduled());
    } finally {
        timer.shutdownService();
    }
}
Also used : AtomicReference(java.util.concurrent.atomic.AtomicReference) ReferenceSettingExceptionHandler(org.apache.flink.streaming.runtime.operators.TestProcessingTimeServiceTest.ReferenceSettingExceptionHandler) OneShotLatch(org.apache.flink.core.testutils.OneShotLatch) Test(org.junit.Test)

Example 2 with ReferenceSettingExceptionHandler

use of org.apache.flink.streaming.runtime.operators.TestProcessingTimeServiceTest.ReferenceSettingExceptionHandler 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)

Example 3 with ReferenceSettingExceptionHandler

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

the class SystemProcessingTimeServiceTest method testQuiescing.

@Test
public void testQuiescing() throws Exception {
    final Object lock = new Object();
    final AtomicReference<Throwable> errorRef = new AtomicReference<>();
    final SystemProcessingTimeService timer = new SystemProcessingTimeService(new ReferenceSettingExceptionHandler(errorRef), lock);
    try {
        final OneShotLatch latch = new OneShotLatch();
        final ReentrantLock scopeLock = new ReentrantLock();
        timer.registerTimer(System.currentTimeMillis() + 20, new ProcessingTimeCallback() {

            @Override
            public void onProcessingTime(long timestamp) throws Exception {
                scopeLock.lock();
                try {
                    latch.trigger();
                    // delay a bit before leaving the method
                    Thread.sleep(5);
                } finally {
                    scopeLock.unlock();
                }
            }
        });
        // after the task triggered, shut the timer down cleanly, waiting for the task to finish
        latch.await();
        timer.quiesceAndAwaitPending();
        // should be able to immediately acquire the lock, since the task must have exited by now 
        assertTrue(scopeLock.tryLock());
        // should be able to schedule more tasks (that never get executed)
        ScheduledFuture<?> future = timer.registerTimer(System.currentTimeMillis() - 5, new ProcessingTimeCallback() {

            @Override
            public void onProcessingTime(long timestamp) throws Exception {
                throw new Exception("test");
            }
        });
        assertNotNull(future);
        // nothing should be scheduled right now
        assertEquals(0, timer.getNumTasksScheduled());
        // triggerable did, in fact, not trigger
        if (errorRef.get() != null) {
            throw new Exception(errorRef.get());
        }
    } finally {
        timer.shutdownService();
    }
}
Also used : ReentrantLock(java.util.concurrent.locks.ReentrantLock) AtomicReference(java.util.concurrent.atomic.AtomicReference) ReferenceSettingExceptionHandler(org.apache.flink.streaming.runtime.operators.TestProcessingTimeServiceTest.ReferenceSettingExceptionHandler) OneShotLatch(org.apache.flink.core.testutils.OneShotLatch) Test(org.junit.Test)

Example 4 with ReferenceSettingExceptionHandler

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

the class SystemProcessingTimeServiceTest method testScheduleAtFixedRate.

/**
	 * Tests that SystemProcessingTimeService#scheduleAtFixedRate is actually triggered multiple
	 * times.
	 */
@Test(timeout = 10000)
public void testScheduleAtFixedRate() throws Exception {
    final Object lock = new Object();
    final AtomicReference<Throwable> errorRef = new AtomicReference<>();
    final long period = 10L;
    final int countDown = 3;
    final SystemProcessingTimeService timer = new SystemProcessingTimeService(new ReferenceSettingExceptionHandler(errorRef), lock);
    final CountDownLatch countDownLatch = new CountDownLatch(countDown);
    try {
        timer.scheduleAtFixedRate(new ProcessingTimeCallback() {

            @Override
            public void onProcessingTime(long timestamp) throws Exception {
                countDownLatch.countDown();
            }
        }, 0L, period);
        countDownLatch.await();
        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) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 5 with ReferenceSettingExceptionHandler

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

the class SystemProcessingTimeServiceTest method testTriggerHoldsLock.

@Test
public void testTriggerHoldsLock() 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(), new ProcessingTimeCallback() {

            @Override
            public void onProcessingTime(long timestamp) {
                assertTrue(Thread.holdsLock(lock));
            }
        });
        // wait until the execution is over
        future.get();
        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