HealthCheckerFactory { public static TimeoutManager startHealthChecker(Connection connection, int probeFrequencyPerMinute, long probeTimeoutInMillis) { try { EntityRef<HealthCheck, Properties, Object> check = connection.getEntityRef(HealthCheck.class, HealthCheck.VERSION, NAME); HealthCheck hc = check.fetchEntity(null); if (probeFrequencyPerMinute < 1 || probeFrequencyPerMinute > 120) { throw new IllegalArgumentException("probe frequency must be greater than zero and less than 120"); } return new HealthCheckTimeoutManager(connection, hc).start(probeTimeoutInMillis, probeFrequencyPerMinute); } catch (EntityNotProvidedException notvalid) { throw new IllegalStateException("healthchecker entity is not installed with the name " + NAME, notvalid); } catch (EntityNotFoundException notfound) { throw new IllegalStateException("healthchecker entity is not installed with the name " + NAME, notfound); } catch (EntityVersionMismatchException version) { throw new IllegalStateException("healthchecker entity is not the right version on the name " + NAME, version); } }  static TimeoutManager startHealthChecker(Connection connection, int probeFrequencyPerMinute, long probeTimeoutInMillis);  }
@Test public void testCloseOnException() throws Throwable { System.out.println("startHealthChecker"); Connection connection = mock(Connection.class); EntityRef ref = mock(EntityRef.class); HealthCheck hc = mock(HealthCheck.class); when(ref.fetchEntity(null)).thenReturn(hc); final Timer timer = new Timer(); when(hc.getTimer()).thenReturn(timer); when(hc.ping(anyString())).thenThrow(new IllegalStateException("not connected")); when(connection.getEntityRef(any(Class.class), anyLong(), anyString())).thenReturn(ref); TimeoutManager result = HealthCheckerFactory.startHealthChecker(connection, 60, 60000); final Semaphore latch = new Semaphore(0); TimerTask tt = new TimerTask() { @Override public void run() { timer.cancel(); latch.release(); } }; timer.schedule(tt, 0); latch.acquire(); verify(connection).close(); try { result.addTimeoutListener(new TimeoutListener() { @Override public void connectionClosed(Connection target) { throw new UnsupportedOperationException("Not supported yet."); } @Override public void probeFailed(Connection target) { throw new UnsupportedOperationException("Not supported yet."); } }); Assert.fail("should throw"); } catch (IllegalStateException closed) { } }