Search in sources :

Example 66 with OneShotLatch

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;
    }
}
Also used : UnregisteredTaskMetricsGroup(org.apache.flink.runtime.operators.testutils.UnregisteredTaskMetricsGroup) StreamRecord(org.apache.flink.streaming.runtime.streamrecord.StreamRecord) StreamConfig(org.apache.flink.streaming.api.graph.StreamConfig) TaskInfo(org.apache.flink.api.common.TaskInfo) Mockito.doAnswer(org.mockito.Mockito.doAnswer) Answer(org.mockito.stubbing.Answer) TestingTaskManagerRuntimeInfo(org.apache.flink.runtime.util.TestingTaskManagerRuntimeInfo) InvocationOnMock(org.mockito.invocation.InvocationOnMock) Environment(org.apache.flink.runtime.execution.Environment) StreamExecutionEnvironment(org.apache.flink.streaming.api.environment.StreamExecutionEnvironment) StreamMockEnvironment(org.apache.flink.streaming.runtime.tasks.StreamMockEnvironment) TestProcessingTimeService(org.apache.flink.streaming.runtime.tasks.TestProcessingTimeService) OneShotLatch(org.apache.flink.core.testutils.OneShotLatch) Test(org.junit.Test)

Example 67 with OneShotLatch

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);
}
Also used : OneShotLatch(org.apache.flink.core.testutils.OneShotLatch) Test(org.junit.Test)

Example 68 with OneShotLatch

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
    }
}
Also used : OneShotLatch(org.apache.flink.core.testutils.OneShotLatch) ScheduledExecutor(org.apache.flink.util.concurrent.ScheduledExecutor) TimeoutException(java.util.concurrent.TimeoutException) Test(org.junit.Test)

Example 69 with OneShotLatch

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();
}
Also used : OneShotLatch(org.apache.flink.core.testutils.OneShotLatch)

Example 70 with 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());
    }
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) CompletionException(java.util.concurrent.CompletionException) OneShotLatch(org.apache.flink.core.testutils.OneShotLatch) AtomicReference(java.util.concurrent.atomic.AtomicReference) FlinkException(org.apache.flink.util.FlinkException) Test(org.junit.Test)

Aggregations

OneShotLatch (org.apache.flink.core.testutils.OneShotLatch)138 Test (org.junit.Test)118 JobID (org.apache.flink.api.common.JobID)41 CompletableFuture (java.util.concurrent.CompletableFuture)38 ExecutionException (java.util.concurrent.ExecutionException)27 Configuration (org.apache.flink.configuration.Configuration)26 IOException (java.io.IOException)24 Before (org.junit.Before)24 FlinkException (org.apache.flink.util.FlinkException)23 TestLogger (org.apache.flink.util.TestLogger)21 File (java.io.File)20 UUID (java.util.UUID)18 TimeoutException (java.util.concurrent.TimeoutException)18 TestingResourceManagerGateway (org.apache.flink.runtime.resourcemanager.utils.TestingResourceManagerGateway)18 Time (org.apache.flink.api.common.time.Time)17 TestingJobMasterGateway (org.apache.flink.runtime.jobmaster.utils.TestingJobMasterGateway)17 Rule (org.junit.Rule)17 Collections (java.util.Collections)16 ArrayBlockingQueue (java.util.concurrent.ArrayBlockingQueue)16 RpcUtils (org.apache.flink.runtime.rpc.RpcUtils)16