Search in sources :

Example 6 with OneShotLatch

use of org.apache.flink.core.testutils.OneShotLatch in project flink by apache.

the class SystemProcessingTimeServiceTest method testExceptionReporting.

@Test
public void testExceptionReporting() throws InterruptedException {
    final AtomicBoolean exceptionWasThrown = new AtomicBoolean(false);
    final OneShotLatch latch = new OneShotLatch();
    final Object lock = new Object();
    ProcessingTimeService timeServiceProvider = new SystemProcessingTimeService(new AsyncExceptionHandler() {

        @Override
        public void handleAsyncException(String message, Throwable exception) {
            exceptionWasThrown.set(true);
            latch.trigger();
        }
    }, lock);
    timeServiceProvider.registerTimer(System.currentTimeMillis(), new ProcessingTimeCallback() {

        @Override
        public void onProcessingTime(long timestamp) throws Exception {
            throw new Exception("Exception in Timer");
        }
    });
    latch.await();
    assertTrue(exceptionWasThrown.get());
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) OneShotLatch(org.apache.flink.core.testutils.OneShotLatch) Test(org.junit.Test)

Example 7 with OneShotLatch

use of org.apache.flink.core.testutils.OneShotLatch in project flink by apache.

the class SystemProcessingTimeServiceTest method testScheduleAtFixedRateHoldsLock.

/**
	 * Tests that the schedule at fixed rate callback is called under the given lock
	 */
@Test
public void testScheduleAtFixedRateHoldsLock() throws Exception {
    final Object lock = new Object();
    final AtomicReference<Throwable> errorRef = new AtomicReference<>();
    final SystemProcessingTimeService timer = new SystemProcessingTimeService(new ReferenceSettingExceptionHandler(errorRef), lock);
    final OneShotLatch awaitCallback = new OneShotLatch();
    try {
        assertEquals(0, timer.getNumTasksScheduled());
        // schedule something
        ScheduledFuture<?> future = timer.scheduleAtFixedRate(new ProcessingTimeCallback() {

            @Override
            public void onProcessingTime(long timestamp) {
                assertTrue(Thread.holdsLock(lock));
                awaitCallback.trigger();
            }
        }, 0L, 100L);
        // wait until the first execution is active
        awaitCallback.await();
        // cancel periodic callback
        future.cancel(true);
        // check that no asynchronous error was reported
        if (errorRef.get() != null) {
            throw new Exception(errorRef.get());
        }
    } finally {
        timer.shutdownService();
    }
}
Also used : OneShotLatch(org.apache.flink.core.testutils.OneShotLatch) AtomicReference(java.util.concurrent.atomic.AtomicReference) ReferenceSettingExceptionHandler(org.apache.flink.streaming.runtime.operators.TestProcessingTimeServiceTest.ReferenceSettingExceptionHandler) Test(org.junit.Test)

Example 8 with OneShotLatch

use of org.apache.flink.core.testutils.OneShotLatch in project flink by apache.

the class TaskAsyncCallTest method createQueuesAndActors.

@Before
public void createQueuesAndActors() {
    awaitLatch = new OneShotLatch();
    triggerLatch = new OneShotLatch();
}
Also used : OneShotLatch(org.apache.flink.core.testutils.OneShotLatch) Before(org.junit.Before)

Example 9 with OneShotLatch

use of org.apache.flink.core.testutils.OneShotLatch in project flink by apache.

the class AsyncCallsTest method testScheduleWithDelay.

@Test
public void testScheduleWithDelay() throws Exception {
    // to collect all the thread references
    final ReentrantLock lock = new ReentrantLock();
    final AtomicBoolean concurrentAccess = new AtomicBoolean(false);
    final OneShotLatch latch = new OneShotLatch();
    final long delay = 200;
    TestEndpoint testEndpoint = new TestEndpoint(akkaRpcService, lock);
    testEndpoint.start();
    // run something asynchronously
    testEndpoint.runAsync(new Runnable() {

        @Override
        public void run() {
            boolean holdsLock = lock.tryLock();
            if (holdsLock) {
                lock.unlock();
            } else {
                concurrentAccess.set(true);
            }
        }
    });
    final long start = System.nanoTime();
    testEndpoint.scheduleRunAsync(new Runnable() {

        @Override
        public void run() {
            boolean holdsLock = lock.tryLock();
            if (holdsLock) {
                lock.unlock();
            } else {
                concurrentAccess.set(true);
            }
            latch.trigger();
        }
    }, delay, TimeUnit.MILLISECONDS);
    latch.await();
    final long stop = System.nanoTime();
    // validate that no concurrent access happened
    assertFalse("Rpc Endpoint had concurrent access", testEndpoint.hasConcurrentAccess());
    assertFalse("Rpc Endpoint had concurrent access", concurrentAccess.get());
    assertTrue("call was not properly delayed", ((stop - start) / 1000000) >= delay);
}
Also used : ReentrantLock(java.util.concurrent.locks.ReentrantLock) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) OneShotLatch(org.apache.flink.core.testutils.OneShotLatch) Test(org.junit.Test)

Example 10 with OneShotLatch

use of org.apache.flink.core.testutils.OneShotLatch in project flink by apache.

the class AkkaRpcServiceTest method testExecuteCallable.

/**
	 * Tests that the {@link AkkaRpcService} can execute callables and returns their result as
	 * a {@link Future}.
	 */
@Test
public void testExecuteCallable() throws InterruptedException, ExecutionException, TimeoutException {
    final OneShotLatch latch = new OneShotLatch();
    final int expected = 42;
    Future<Integer> result = akkaRpcService.execute(new Callable<Integer>() {

        @Override
        public Integer call() throws Exception {
            latch.trigger();
            return expected;
        }
    });
    int actual = result.get(30L, TimeUnit.SECONDS);
    assertEquals(expected, actual);
    assertTrue(latch.isTriggered());
}
Also used : OneShotLatch(org.apache.flink.core.testutils.OneShotLatch) TimeoutException(java.util.concurrent.TimeoutException) ExecutionException(java.util.concurrent.ExecutionException) Test(org.junit.Test)

Aggregations

OneShotLatch (org.apache.flink.core.testutils.OneShotLatch)38 Test (org.junit.Test)34 Configuration (org.apache.flink.configuration.Configuration)10 Path (org.apache.flink.core.fs.Path)9 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)9 StreamConfig (org.apache.flink.streaming.api.graph.StreamConfig)8 InvocationOnMock (org.mockito.invocation.InvocationOnMock)8 CheckpointMetaData (org.apache.flink.runtime.checkpoint.CheckpointMetaData)5 Environment (org.apache.flink.runtime.execution.Environment)5 TimestampedFileInputSplit (org.apache.flink.streaming.api.functions.source.TimestampedFileInputSplit)5 OperatorStateHandles (org.apache.flink.streaming.runtime.tasks.OperatorStateHandles)5 IOException (java.io.IOException)4 TreeSet (java.util.TreeSet)4 AtomicReference (java.util.concurrent.atomic.AtomicReference)4 TextInputFormat (org.apache.flink.api.java.io.TextInputFormat)4 FileInputSplit (org.apache.flink.core.fs.FileInputSplit)4 CheckpointMetrics (org.apache.flink.runtime.checkpoint.CheckpointMetrics)4 SubtaskState (org.apache.flink.runtime.checkpoint.SubtaskState)4 ContinuousFileMonitoringFunction (org.apache.flink.streaming.api.functions.source.ContinuousFileMonitoringFunction)4 File (java.io.File)3