use of org.apache.flink.api.common.operators.ProcessingTimeService.ProcessingTimeCallback 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 AtomicReference<Throwable> errorRef = new AtomicReference<>();
final long period = 10L;
final SystemProcessingTimeService timer = createSystemProcessingTimeService(errorRef);
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.quiesce().get();
// exception to be on the safe side
try {
scheduledFuture.get();
fail("scheduled future is not cancelled");
} catch (CancellationException ignored) {
// expected
}
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();
}
}
use of org.apache.flink.api.common.operators.ProcessingTimeService.ProcessingTimeCallback in project flink by apache.
the class SystemProcessingTimeServiceTest method testQuiescing.
@Test
public void testQuiescing() throws Exception {
final AtomicReference<Throwable> errorRef = new AtomicReference<>();
final SystemProcessingTimeService timer = createSystemProcessingTimeService(errorRef);
try {
final OneShotLatch latch = new OneShotLatch();
final ReentrantLock scopeLock = new ReentrantLock();
timer.registerTimer(timer.getCurrentProcessingTime() + 20L, 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.quiesce().get();
// 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(timer.getCurrentProcessingTime() - 5L, new ProcessingTimeCallback() {
@Override
public void onProcessingTime(long timestamp) throws Exception {
throw new Exception("test");
}
});
assertNotNull(future);
// nothing should be scheduled right now
assertEquals(0L, timer.getNumTasksScheduled());
// triggerable did, in fact, not trigger
if (errorRef.get() != null) {
throw new Exception(errorRef.get());
}
} finally {
timer.shutdownService();
}
}
use of org.apache.flink.api.common.operators.ProcessingTimeService.ProcessingTimeCallback 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 AtomicReference<Throwable> errorRef = new AtomicReference<>();
final long period = 10L;
final int countDown = 3;
final SystemProcessingTimeService timer = createSystemProcessingTimeService(errorRef);
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();
}
}
use of org.apache.flink.api.common.operators.ProcessingTimeService.ProcessingTimeCallback in project flink by apache.
the class StreamTaskTimerTest method testErrorReporting.
@Test
public void testErrorReporting() throws Exception {
AtomicReference<Throwable> errorRef = new AtomicReference<>();
OneShotLatch latch = new OneShotLatch();
testHarness.getEnvironment().setExternalExceptionHandler(ex -> {
errorRef.set(ex);
latch.trigger();
});
ProcessingTimeCallback callback = timestamp -> {
throw new Exception("Exception in Timer");
};
timeService.registerTimer(System.currentTimeMillis(), callback);
latch.await();
assertThat(errorRef.get(), instanceOf(Exception.class));
}
Aggregations