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