use of org.apache.flink.core.testutils.OneShotLatch in project flink by apache.
the class AsyncWaitOperatorTest method testClosingWithBlockedEmitter.
/**
* Test case for FLINK-5638: Tests that the async wait operator can be closed even if the
* emitter is currently waiting on the checkpoint lock (e.g. in the case of two chained async
* wait operators where the latter operator's queue is currently full).
*
* Note that this test does not enforce the exact strict ordering because with the fix it is no
* longer possible. However, it provokes the described situation without the fix.
*/
@Test(timeout = 10000L)
public void testClosingWithBlockedEmitter() throws Exception {
final Object lock = new Object();
ArgumentCaptor<Throwable> failureReason = ArgumentCaptor.forClass(Throwable.class);
Environment environment = mock(Environment.class);
when(environment.getMetricGroup()).thenReturn(new UnregisteredTaskMetricsGroup());
when(environment.getTaskManagerInfo()).thenReturn(new TestingTaskManagerRuntimeInfo());
when(environment.getUserClassLoader()).thenReturn(getClass().getClassLoader());
when(environment.getTaskInfo()).thenReturn(new TaskInfo("testTask", 1, 0, 1, 0));
doNothing().when(environment).failExternally(failureReason.capture());
StreamTask<?, ?> containingTask = mock(StreamTask.class);
when(containingTask.getEnvironment()).thenReturn(environment);
when(containingTask.getCheckpointLock()).thenReturn(lock);
when(containingTask.getProcessingTimeService()).thenReturn(new TestProcessingTimeService());
StreamConfig streamConfig = mock(StreamConfig.class);
doReturn(IntSerializer.INSTANCE).when(streamConfig).getTypeSerializerIn1(any(ClassLoader.class));
final OneShotLatch closingLatch = new OneShotLatch();
final OneShotLatch outputLatch = new OneShotLatch();
Output<StreamRecord<Integer>> output = mock(Output.class);
doAnswer(new Answer() {
@Override
public Object answer(InvocationOnMock invocation) throws Throwable {
assertTrue("Output should happen under the checkpoint lock.", Thread.currentThread().holdsLock(lock));
outputLatch.trigger();
// wait until we're in the closing method of the operator
while (!closingLatch.isTriggered()) {
lock.wait();
}
return null;
}
}).when(output).collect(any(StreamRecord.class));
AsyncWaitOperator<Integer, Integer> operator = new TestAsyncWaitOperator<>(new MyAsyncFunction(), 1000L, 1, AsyncDataStream.OutputMode.ORDERED, closingLatch);
operator.setup(containingTask, streamConfig, output);
operator.open();
synchronized (lock) {
operator.processElement(new StreamRecord<>(42));
}
outputLatch.await();
synchronized (lock) {
operator.close();
}
// check that no concurrent exception has occurred
try {
verify(environment, never()).failExternally(any(Throwable.class));
} catch (Error e) {
// add the exception occurring in the emitter thread (root cause) as a suppressed
// exception
e.addSuppressed(failureReason.getValue());
throw e;
}
}
use of org.apache.flink.core.testutils.OneShotLatch in project flink by apache.
the class AkkaRpcServiceTest method testExecuteRunnable.
/**
* Tests that the {@link AkkaRpcService} can execute runnables.
*/
@Test
public void testExecuteRunnable() throws Exception {
final OneShotLatch latch = new OneShotLatch();
akkaRpcService.execute(latch::trigger);
latch.await(30L, TimeUnit.SECONDS);
}
use of org.apache.flink.core.testutils.OneShotLatch in project flink by apache.
the class AkkaRpcServiceTest method testScheduledExecutorServiceCancelWithFixedDelay.
/**
* Tests that canceling the returned future will stop the execution of the scheduled runnable.
*/
@Test
public void testScheduledExecutorServiceCancelWithFixedDelay() throws InterruptedException {
ScheduledExecutor scheduledExecutor = akkaRpcService.getScheduledExecutor();
long delay = 10L;
final OneShotLatch futureTask = new OneShotLatch();
final OneShotLatch latch = new OneShotLatch();
final OneShotLatch shouldNotBeTriggeredLatch = new OneShotLatch();
ScheduledFuture<?> future = scheduledExecutor.scheduleWithFixedDelay(() -> {
try {
if (futureTask.isTriggered()) {
shouldNotBeTriggeredLatch.trigger();
} else {
// first run
futureTask.trigger();
latch.await();
}
} catch (InterruptedException ignored) {
// ignore
}
}, delay, delay, TimeUnit.MILLISECONDS);
// wait until we're in the runnable
futureTask.await();
// cancel the scheduled future
future.cancel(false);
latch.trigger();
try {
shouldNotBeTriggeredLatch.await(5 * delay, TimeUnit.MILLISECONDS);
fail("The shouldNotBeTriggeredLatch should never be triggered.");
} catch (TimeoutException e) {
// expected
}
}
use of org.apache.flink.core.testutils.OneShotLatch in project flink by apache.
the class AlwaysThrowsDeserializationSchema method readObject.
private void readObject(java.io.ObjectInputStream in) throws IOException, ClassNotFoundException {
in.defaultReadObject();
this.isExceptionThrown = new OneShotLatch();
}
use of org.apache.flink.core.testutils.OneShotLatch in project flink by apache.
the class FutureUtilsTest method testRetryCancellation.
/**
* Tests that we can cancel a retry future.
*/
@Test
public void testRetryCancellation() throws Exception {
final int retries = 10;
final AtomicInteger atomicInteger = new AtomicInteger(0);
final OneShotLatch notificationLatch = new OneShotLatch();
final OneShotLatch waitLatch = new OneShotLatch();
final AtomicReference<Throwable> atomicThrowable = new AtomicReference<>(null);
CompletableFuture<?> retryFuture = FutureUtils.retry(() -> CompletableFuture.supplyAsync(() -> {
if (atomicInteger.incrementAndGet() == 2) {
notificationLatch.trigger();
try {
waitLatch.await();
} catch (InterruptedException e) {
atomicThrowable.compareAndSet(null, e);
}
}
throw new CompletionException(new FlinkException("Test exception"));
}, TestingUtils.defaultExecutor()), retries, TestingUtils.defaultExecutor());
// await that we have failed once
notificationLatch.await();
assertFalse(retryFuture.isDone());
// cancel the retry future
retryFuture.cancel(false);
// let the retry operation continue
waitLatch.trigger();
assertTrue(retryFuture.isCancelled());
assertEquals(2, atomicInteger.get());
if (atomicThrowable.get() != null) {
throw new FlinkException("Exception occurred in the retry operation.", atomicThrowable.get());
}
}
Aggregations