PNPTimeoutHandler { public long getTimeout() { readLock.lock(); try { if (defaultHearbeatPeriod == 0 || lastInterval == 0) return heartbeatFactor * defaultHearbeatPeriod; else { long averageInterval = computeAverageInterval(); if (lastInterval > averageInterval) return heartbeatFactor * Math.max(defaultHearbeatPeriod, lastInterval); else return heartbeatFactor * Math.max(defaultHearbeatPeriod, averageInterval); } } finally { readLock.unlock(); } } PNPTimeoutHandler(long defaultHearbeatPeriod, int heartbeatFactor, int heartbeatWindow); long getTimeout(); void resetNotification(); HeartBeatNotificationData getNotificationData(); void recordHeartBeat(long time);  }
@Test public void testGrowingDecreasing() { PNPTimeoutHandler handler = new PNPTimeoutHandler(10, 2, 2); recordHeartBeats(handler, Lists.newArrayList(1, 1 + 10, 1 + 10 + 12, 1 + 10 + 12 + 14, 1 + 10 + 12 + 14 + 16, 1 + 10 + 12 + 14 + 16 + 10, 1 + 10 + 12 + 14 + 16 + 10 + 10)); Assert.assertEquals("After growing beat delay and then normal period, timeout should grow then shrink to the average", 10 * 2, handler.getTimeout()); }
@Test public void testUpsAndDowns1() { PNPTimeoutHandler handler = new PNPTimeoutHandler(10, 2, 2); recordHeartBeats(handler, Lists.newArrayList(1, 1 + 10, 1 + 10 + 20, 1 + 10 + 20 + 10, 1 + 10 + 20 + 10 + 20)); Assert.assertEquals("When beat interval is oscillating between two values, if last value is bigger than average, then it should be used", 20 * 2, handler.getTimeout()); }
@Test public void testUpsAndDowns2() { PNPTimeoutHandler handler = new PNPTimeoutHandler(10, 2, 2); recordHeartBeats(handler, Lists.newArrayList(1, 1 + 10, 1 + 10 + 20, 1 + 10 + 20 + 10, 1 + 10 + 20 + 10 + 20, 1 + 10 + 20 + 10 + 20 + 10)); Assert.assertEquals("When beat interval is oscillating between two values, if last value is lower than average, then the average should be used", 15 * 2, handler.getTimeout()); }
@Test public void testDefaultZero() { PNPTimeoutHandler handler = new PNPTimeoutHandler(0, 2, 2); recordHeartBeats(handler, Lists.newArrayList(1, 1 + 10, 1 + 10 + 12, 1 + 10 + 12 + 14, 1 + 10 + 12 + 14 + 16)); Assert.assertEquals("If default heartbeat period is 0, the timeout should be zero", 0, handler.getTimeout()); }
@Test public void testNoData() { PNPTimeoutHandler handler = new PNPTimeoutHandler(10, 2, 2); recordHeartBeats(handler, Lists.newArrayList(1)); Assert.assertEquals("If not enough data to compute an interval, default should be used", 10 * 2, handler.getTimeout()); }
@Test public void testGrowing() { PNPTimeoutHandler handler = new PNPTimeoutHandler(10, 2, 2); recordHeartBeats(handler, Lists.newArrayList(1, 1 + 10, 1 + 10 + 12, 1 + 10 + 12 + 14, 1 + 10 + 12 + 14 + 16)); Assert.assertEquals("After growing beat delay, timeout should grow to the last beat interval", 16 * 2, handler.getTimeout()); }