use of org.apache.flink.runtime.util.DirectExecutorService in project flink by apache.
the class HeartbeatManagerTest method testHeartbeatTimeout.
/**
* Tests that a heartbeat timeout is signaled if the heartbeat is not reported in time.
*
* @throws Exception
*/
@Test
public void testHeartbeatTimeout() throws Exception {
long heartbeatTimeout = 100L;
int numHeartbeats = 10;
long heartbeatInterval = 20L;
Object payload = new Object();
ResourceID ownResourceID = new ResourceID("foobar");
ResourceID targetResourceID = new ResourceID("barfoo");
TestingHeartbeatListener heartbeatListener = new TestingHeartbeatListener(payload);
ScheduledExecutorService scheduledExecutorService = mock(ScheduledExecutorService.class);
ScheduledFuture<?> scheduledFuture = mock(ScheduledFuture.class);
doReturn(scheduledFuture).when(scheduledExecutorService).schedule(any(Runnable.class), anyLong(), any(TimeUnit.class));
Object expectedObject = new Object();
HeartbeatManagerImpl<Object, Object> heartbeatManager = new HeartbeatManagerImpl<>(heartbeatTimeout, ownResourceID, heartbeatListener, new DirectExecutorService(), new ScheduledExecutorServiceAdapter(new ScheduledThreadPoolExecutor(1)), LOG);
HeartbeatTarget<Object> heartbeatTarget = mock(HeartbeatTarget.class);
Future<ResourceID> timeoutFuture = heartbeatListener.getTimeoutFuture();
heartbeatManager.monitorTarget(targetResourceID, heartbeatTarget);
for (int i = 0; i < numHeartbeats; i++) {
heartbeatManager.receiveHeartbeat(targetResourceID, expectedObject);
Thread.sleep(heartbeatInterval);
}
assertFalse(timeoutFuture.isDone());
ResourceID timeoutResourceID = timeoutFuture.get(2 * heartbeatTimeout, TimeUnit.MILLISECONDS);
assertEquals(targetResourceID, timeoutResourceID);
}
Aggregations