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));
}
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);
}
}
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());
}
}
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();
}
}
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;
}
Aggregations