use of org.apache.flink.core.testutils.OneShotLatch 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.core.testutils.OneShotLatch in project flink by apache.
the class SystemProcessingTimeServiceTest method testShutdownServiceUninterruptible.
@Test
public void testShutdownServiceUninterruptible() {
final OneShotLatch blockUntilTriggered = new OneShotLatch();
final AtomicBoolean timerFinished = new AtomicBoolean(false);
final SystemProcessingTimeService timeService = createBlockingSystemProcessingTimeService(blockUntilTriggered, timerFinished);
Assert.assertFalse(timeService.isTerminated());
final Thread interruptTarget = Thread.currentThread();
final AtomicBoolean runInterrupts = new AtomicBoolean(true);
final Thread interruptCallerThread = new Thread(() -> {
while (runInterrupts.get()) {
interruptTarget.interrupt();
try {
Thread.sleep(1);
} catch (InterruptedException ignore) {
}
}
});
interruptCallerThread.start();
final long timeoutMs = 50L;
final long startTime = System.nanoTime();
Assert.assertFalse(timeService.isTerminated());
// check that termination did not succeed (because of blocking timer execution)
Assert.assertFalse(timeService.shutdownServiceUninterruptible(timeoutMs));
// check that termination flag was set.
Assert.assertTrue(timeService.isTerminated());
// check that the blocked timer is still in flight.
Assert.assertFalse(timerFinished.get());
// check that we waited until timeout
Assert.assertTrue((System.nanoTime() - startTime) >= (1_000_000L * timeoutMs));
runInterrupts.set(false);
do {
try {
interruptCallerThread.join();
} catch (InterruptedException ignore) {
}
} while (interruptCallerThread.isAlive());
// clear the interrupted flag in case join didn't do it
final boolean ignored = Thread.interrupted();
blockUntilTriggered.trigger();
Assert.assertTrue(timeService.shutdownServiceUninterruptible(timeoutMs));
Assert.assertTrue(timerFinished.get());
}
use of org.apache.flink.core.testutils.OneShotLatch in project flink by apache.
the class TaskMailboxProcessorTest method testSuspendRunningMailboxLoop.
@Test
public void testSuspendRunningMailboxLoop() throws Exception {
// given: Thread for suspending the suspendable loop.
OneShotLatch doSomeWork = new OneShotLatch();
AtomicBoolean stop = new AtomicBoolean(false);
MailboxProcessor mailboxProcessor = new MailboxProcessor(controller -> {
doSomeWork.trigger();
if (stop.get()) {
controller.allActionsCompleted();
}
});
Thread suspendThread = new Thread(() -> {
try {
// Ensure that loop was started.
doSomeWork.await();
// when: Suspend the suspendable loop.
mailboxProcessor.suspend();
// and: Execute the command for stopping the loop.
mailboxProcessor.getMailboxExecutor(DEFAULT_PRIORITY).execute(() -> stop.set(true), "stop");
} catch (Exception ignore) {
}
});
suspendThread.start();
// when: Start the suspendable loop.
mailboxProcessor.runMailboxLoop();
suspendThread.join();
// then: Mailbox is not stopped because it was suspended before the stop command.
assertFalse(stop.get());
// when: Resume the suspendable loop.
mailboxProcessor.runMailboxLoop();
// then: Stop command successfully executed because it was in the queue.
assertFalse(mailboxProcessor.isMailboxLoopRunning());
assertTrue(stop.get());
}
use of org.apache.flink.core.testutils.OneShotLatch in project flink by apache.
the class TaskMailboxProcessorTest method testSignalUnAvailable.
@Test
public void testSignalUnAvailable() throws Exception {
final AtomicInteger counter = new AtomicInteger(0);
final AtomicReference<MailboxDefaultAction.Suspension> suspendedActionRef = new AtomicReference<>();
final OneShotLatch actionSuspendedLatch = new OneShotLatch();
final int blockAfterInvocations = 3;
final int totalInvocations = blockAfterInvocations * 2;
MailboxThread mailboxThread = new MailboxThread() {
@Override
public void runDefaultAction(Controller controller) {
if (counter.incrementAndGet() == blockAfterInvocations) {
suspendedActionRef.set(controller.suspendDefaultAction());
actionSuspendedLatch.trigger();
} else if (counter.get() == totalInvocations) {
controller.allActionsCompleted();
}
}
};
MailboxProcessor mailboxProcessor = start(mailboxThread);
actionSuspendedLatch.await();
Assert.assertEquals(blockAfterInvocations, counter.get());
MailboxDefaultAction.Suspension suspension = suspendedActionRef.get();
mailboxProcessor.getMailboxExecutor(DEFAULT_PRIORITY).execute(suspension::resume, "resume");
mailboxThread.join();
Assert.assertEquals(totalInvocations, counter.get());
}
use of org.apache.flink.core.testutils.OneShotLatch in project flink by apache.
the class DispatcherTest method testJobCleanupWithoutRecoveredJobGraph.
@Test
public void testJobCleanupWithoutRecoveredJobGraph() throws Exception {
final JobID jobIdOfRecoveredDirtyJobs = new JobID();
final TestingJobMasterServiceLeadershipRunnerFactory jobManagerRunnerFactory = new TestingJobMasterServiceLeadershipRunnerFactory();
final TestingCleanupRunnerFactory cleanupRunnerFactory = new TestingCleanupRunnerFactory();
final OneShotLatch dispatcherBootstrapLatch = new OneShotLatch();
dispatcher = createTestingDispatcherBuilder().setJobManagerRunnerFactory(jobManagerRunnerFactory).setCleanupRunnerFactory(cleanupRunnerFactory).setRecoveredDirtyJobs(Collections.singleton(new JobResult.Builder().jobId(jobIdOfRecoveredDirtyJobs).applicationStatus(ApplicationStatus.SUCCEEDED).netRuntime(1).build())).setDispatcherBootstrapFactory((ignoredDispatcherGateway, ignoredScheduledExecutor, ignoredFatalErrorHandler) -> {
dispatcherBootstrapLatch.trigger();
return new NoOpDispatcherBootstrap();
}).build();
dispatcher.start();
dispatcherBootstrapLatch.await();
final TestingJobManagerRunner cleanupRunner = cleanupRunnerFactory.takeCreatedJobManagerRunner();
assertThat("The CleanupJobManagerRunner has the wrong job ID attached.", cleanupRunner.getJobID(), is(jobIdOfRecoveredDirtyJobs));
assertThat("No JobMaster should have been started.", jobManagerRunnerFactory.getQueueSize(), is(0));
}
Aggregations