@Test public void testWaitUntilMasterDown() throws Exception { HeartbeatDAO hbDao = mock(HeartbeatDAO.class); when(hbDao.findLastHeartbeat(Mockito.anyString())).then(new Answer<Date>() { Date d =new Date(); @Override public Date answer(InvocationOnMock invocation) throws Throwable { return d; } }); final AtomicBoolean waked = new AtomicBoolean(false); final Heartbeater hb = new MongoHeartbeater(); Field daoField = hb.getClass().getDeclaredField("heartbeatDAO"); daoField.setAccessible(true); daoField.set(hb, hbDao); Thread t = new Thread() { public void run() { try { hb.waitUntilMasterDown("127.0.0.1", 100, 300); waked.set(true); } catch (InterruptedException e) { } } }; t.start(); t.join(400); Assert.assertTrue(waked.get()); }
@Override public void waitUntilMasterDown(String ip, long checkInterval, long maxStopTime) throws InterruptedException { long startTime = System.currentTimeMillis(); LOG.info("start to wait " + ip + " master stop beating"); while (true) { Date beat = null; beat = heartbeatDAO.findLastHeartbeat(ip); if (beat == null) { LOG.warn(ip + " no beat"); if (System.currentTimeMillis() - startTime > maxStopTime) { break; } } else { if (LOG.isDebugEnabled()) { LOG.debug(ip + " beat at " + beat.getTime()); } long now = System.currentTimeMillis(); long lastBeatTime = beat.getTime(); if (now - lastBeatTime > maxStopTime) { break; } } Thread.sleep(checkInterval); } LOG.info(ip + " master stop beating, slave waked up"); }
MongoHeartbeater implements Heartbeater { @Override public void waitUntilMasterDown(String ip, long checkInterval, long maxStopTime) throws InterruptedException { long startTime = System.currentTimeMillis(); LOG.info("start to wait " + ip + " master stop beating"); while (true) { Date beat = null; beat = heartbeatDAO.findLastHeartbeat(ip); if (beat == null) { LOG.warn(ip + " no beat"); if (System.currentTimeMillis() - startTime > maxStopTime) { break; } } else { if (LOG.isDebugEnabled()) { LOG.debug(ip + " beat at " + beat.getTime()); } long now = System.currentTimeMillis(); long lastBeatTime = beat.getTime(); if (now - lastBeatTime > maxStopTime) { break; } } Thread.sleep(checkInterval); } LOG.info(ip + " master stop beating, slave waked up"); } }
MongoHeartbeater implements Heartbeater { @Override public void waitUntilMasterDown(String ip, long checkInterval, long maxStopTime) throws InterruptedException { long startTime = System.currentTimeMillis(); LOG.info("start to wait " + ip + " master stop beating"); while (true) { Date beat = null; beat = heartbeatDAO.findLastHeartbeat(ip); if (beat == null) { LOG.warn(ip + " no beat"); if (System.currentTimeMillis() - startTime > maxStopTime) { break; } } else { if (LOG.isDebugEnabled()) { LOG.debug(ip + " beat at " + beat.getTime()); } long now = System.currentTimeMillis(); long lastBeatTime = beat.getTime(); if (now - lastBeatTime > maxStopTime) { break; } } Thread.sleep(checkInterval); } LOG.info(ip + " master stop beating, slave waked up"); }  }
MongoHeartbeater implements Heartbeater { @Override public void waitUntilMasterDown(String ip, long checkInterval, long maxStopTime) throws InterruptedException { long startTime = System.currentTimeMillis(); LOG.info("start to wait " + ip + " master stop beating"); while (true) { Date beat = null; beat = heartbeatDAO.findLastHeartbeat(ip); if (beat == null) { LOG.warn(ip + " no beat"); if (System.currentTimeMillis() - startTime > maxStopTime) { break; } } else { if (LOG.isDebugEnabled()) { LOG.debug(ip + " beat at " + beat.getTime()); } long now = System.currentTimeMillis(); long lastBeatTime = beat.getTime(); if (now - lastBeatTime > maxStopTime) { break; } } Thread.sleep(checkInterval); } LOG.info(ip + " master stop beating, slave waked up"); }  void setHeartbeatDAO(HeartbeatDAO heartbeatDAO); @Override void beat(String ip); @Override void waitUntilMasterDown(String ip, long checkInterval, long maxStopTime); @Override void waitUntilMasterUp(String ip, long checkInterval, long maxStopTime); }
MongoHeartbeater implements Heartbeater { @Override public void waitUntilMasterDown(String ip, long checkInterval, long maxStopTime) throws InterruptedException { long startTime = System.currentTimeMillis(); LOG.info("start to wait " + ip + " master stop beating"); while (true) { Date beat = null; beat = heartbeatDAO.findLastHeartbeat(ip); if (beat == null) { LOG.warn(ip + " no beat"); if (System.currentTimeMillis() - startTime > maxStopTime) { break; } } else { if (LOG.isDebugEnabled()) { LOG.debug(ip + " beat at " + beat.getTime()); } long now = System.currentTimeMillis(); long lastBeatTime = beat.getTime(); if (now - lastBeatTime > maxStopTime) { break; } } Thread.sleep(checkInterval); } LOG.info(ip + " master stop beating, slave waked up"); }  void setHeartbeatDAO(HeartbeatDAO heartbeatDAO); @Override void beat(String ip); @Override void waitUntilMasterDown(String ip, long checkInterval, long maxStopTime); @Override void waitUntilMasterUp(String ip, long checkInterval, long maxStopTime);  }