@Test public void testwaitUntilMasterUp() throws Exception { HeartbeatDAO hbDao = mock(HeartbeatDAO.class); when(hbDao.findLastHeartbeat(Mockito.anyString())).then(new Answer<Date>() { Date d =new Date((new Date()).getTime() - 300); int reqTimes = 0; @Override public Date answer(InvocationOnMock invocation) throws Throwable { if(reqTimes++ > 4) { return new Date(); } 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.waitUntilMasterUp("127.0.0.1", 100, 300); waked.set(true); } catch (InterruptedException e) { } } }; long beatStart = System.currentTimeMillis(); t.start(); t.join(600); Assert.assertTrue(System.currentTimeMillis() - beatStart > 300); Assert.assertTrue(waked.get()); }
@Override public void waitUntilMasterUp(String ip, long checkInterval, long maxStopTime) throws InterruptedException { Date beat = null; LOG.info("start to wait " + ip + " master up"); while (true) { try { beat = heartbeatDAO.findLastHeartbeat(ip); } catch (Exception e) { LOG.error("error find last heartbeat", e); Thread.sleep(1000); continue; } if (beat != null) { long lastBeatTime = beat.getTime(); long now = System.currentTimeMillis(); if (now - lastBeatTime < maxStopTime) { if (LOG.isDebugEnabled()) { LOG.debug(ip + " beat at " + beat.getTime()); } break; } } Thread.sleep(checkInterval); } LOG.info(ip + " master up, slave shutdown"); }
MongoHeartbeater implements Heartbeater { @Override public void waitUntilMasterUp(String ip, long checkInterval, long maxStopTime) throws InterruptedException { Date beat = null; LOG.info("start to wait " + ip + " master up"); while (true) { try { beat = heartbeatDAO.findLastHeartbeat(ip); } catch (Exception e) { LOG.error("error find last heartbeat", e); Thread.sleep(1000); continue; } if (beat != null) { long lastBeatTime = beat.getTime(); long now = System.currentTimeMillis(); if (now - lastBeatTime < maxStopTime) { if (LOG.isDebugEnabled()) { LOG.debug(ip + " beat at " + beat.getTime()); } break; } } Thread.sleep(checkInterval); } LOG.info(ip + " master up, slave shutdown"); } }
MongoHeartbeater implements Heartbeater { @Override public void waitUntilMasterUp(String ip, long checkInterval, long maxStopTime) throws InterruptedException { Date beat = null; LOG.info("start to wait " + ip + " master up"); while (true) { try { beat = heartbeatDAO.findLastHeartbeat(ip); } catch (Exception e) { LOG.error("error find last heartbeat", e); Thread.sleep(1000); continue; } if (beat != null) { long lastBeatTime = beat.getTime(); long now = System.currentTimeMillis(); if (now - lastBeatTime < maxStopTime) { if (LOG.isDebugEnabled()) { LOG.debug(ip + " beat at " + beat.getTime()); } break; } } Thread.sleep(checkInterval); } LOG.info(ip + " master up, slave shutdown"); }  }
MongoHeartbeater implements Heartbeater { @Override public void waitUntilMasterUp(String ip, long checkInterval, long maxStopTime) throws InterruptedException { Date beat = null; LOG.info("start to wait " + ip + " master up"); while (true) { try { beat = heartbeatDAO.findLastHeartbeat(ip); } catch (Exception e) { LOG.error("error find last heartbeat", e); Thread.sleep(1000); continue; } if (beat != null) { long lastBeatTime = beat.getTime(); long now = System.currentTimeMillis(); if (now - lastBeatTime < maxStopTime) { if (LOG.isDebugEnabled()) { LOG.debug(ip + " beat at " + beat.getTime()); } break; } } Thread.sleep(checkInterval); } LOG.info(ip + " master up, slave shutdown"); }  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 waitUntilMasterUp(String ip, long checkInterval, long maxStopTime) throws InterruptedException { Date beat = null; LOG.info("start to wait " + ip + " master up"); while (true) { try { beat = heartbeatDAO.findLastHeartbeat(ip); } catch (Exception e) { LOG.error("error find last heartbeat", e); Thread.sleep(1000); continue; } if (beat != null) { long lastBeatTime = beat.getTime(); long now = System.currentTimeMillis(); if (now - lastBeatTime < maxStopTime) { if (LOG.isDebugEnabled()) { LOG.debug(ip + " beat at " + beat.getTime()); } break; } } Thread.sleep(checkInterval); } LOG.info(ip + " master up, slave shutdown"); }  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);  }