@Test public void testUpdateToInitial() throws InterruptedException { DebounceHealthMonitorStrategy sut = new DebounceHealthMonitorStrategy(50); final int[] updateCount = new int[1]; sut.registerListener(new HealthMonitorStrategyListener() { @Override public void onUpdate(boolean isHealthy) { updateCount[0]++; } }); sut.update(true); Assert.assertEquals(1, updateCount[0]); sut.update(true); Assert.assertEquals(1, updateCount[0]); Thread.sleep(100); Assert.assertEquals(1, updateCount[0]); }
@Override public synchronized void update(final boolean isHealthy) { if (scheduledExecutor == null) { scheduledExecutor = Executors.newSingleThreadScheduledExecutor(createThreadFactory()); adviseListeners(isHealthy); currentState = isHealthy; } if (scheduledFuture == null) { if (isHealthy != currentState) { scheduledFuture = scheduledExecutor.schedule(new Runnable() { @Override public void run() { boolean update = false; synchronized (DebounceHealthMonitorStrategy.this) { update = scheduledFuture != null; scheduledFuture = null; if (update) { currentState = isHealthy; } } if (update) { adviseListeners(isHealthy); } } }, debouncePeriod, TimeUnit.MILLISECONDS); } } else { if (currentState == isHealthy) { scheduledFuture.cancel(false); scheduledFuture = null; } } }
DebounceHealthMonitorStrategy extends AbstractHealthMonitorStrategy { @Override public synchronized void update(final boolean isHealthy) { if (scheduledExecutor == null) { scheduledExecutor = Executors.newSingleThreadScheduledExecutor(createThreadFactory()); adviseListeners(isHealthy); currentState = isHealthy; } if (scheduledFuture == null) { if (isHealthy != currentState) { scheduledFuture = scheduledExecutor.schedule(new Runnable() { @Override public void run() { boolean update = false; synchronized (DebounceHealthMonitorStrategy.this) { update = scheduledFuture != null; scheduledFuture = null; if (update) { currentState = isHealthy; } } if (update) { adviseListeners(isHealthy); } } }, debouncePeriod, TimeUnit.MILLISECONDS); } } else { if (currentState == isHealthy) { scheduledFuture.cancel(false); scheduledFuture = null; } } } }
DebounceHealthMonitorStrategy extends AbstractHealthMonitorStrategy { @Override public synchronized void update(final boolean isHealthy) { if (scheduledExecutor == null) { scheduledExecutor = Executors.newSingleThreadScheduledExecutor(createThreadFactory()); adviseListeners(isHealthy); currentState = isHealthy; } if (scheduledFuture == null) { if (isHealthy != currentState) { scheduledFuture = scheduledExecutor.schedule(new Runnable() { @Override public void run() { boolean update = false; synchronized (DebounceHealthMonitorStrategy.this) { update = scheduledFuture != null; scheduledFuture = null; if (update) { currentState = isHealthy; } } if (update) { adviseListeners(isHealthy); } } }, debouncePeriod, TimeUnit.MILLISECONDS); } } else { if (currentState == isHealthy) { scheduledFuture.cancel(false); scheduledFuture = null; } } } DebounceHealthMonitorStrategy(long debouncePeriod); }
DebounceHealthMonitorStrategy extends AbstractHealthMonitorStrategy { @Override public synchronized void update(final boolean isHealthy) { if (scheduledExecutor == null) { scheduledExecutor = Executors.newSingleThreadScheduledExecutor(createThreadFactory()); adviseListeners(isHealthy); currentState = isHealthy; } if (scheduledFuture == null) { if (isHealthy != currentState) { scheduledFuture = scheduledExecutor.schedule(new Runnable() { @Override public void run() { boolean update = false; synchronized (DebounceHealthMonitorStrategy.this) { update = scheduledFuture != null; scheduledFuture = null; if (update) { currentState = isHealthy; } } if (update) { adviseListeners(isHealthy); } } }, debouncePeriod, TimeUnit.MILLISECONDS); } } else { if (currentState == isHealthy) { scheduledFuture.cancel(false); scheduledFuture = null; } } } DebounceHealthMonitorStrategy(long debouncePeriod); @Override synchronized void update(final boolean isHealthy); }
DebounceHealthMonitorStrategy extends AbstractHealthMonitorStrategy { @Override public synchronized void update(final boolean isHealthy) { if (scheduledExecutor == null) { scheduledExecutor = Executors.newSingleThreadScheduledExecutor(createThreadFactory()); adviseListeners(isHealthy); currentState = isHealthy; } if (scheduledFuture == null) { if (isHealthy != currentState) { scheduledFuture = scheduledExecutor.schedule(new Runnable() { @Override public void run() { boolean update = false; synchronized (DebounceHealthMonitorStrategy.this) { update = scheduledFuture != null; scheduledFuture = null; if (update) { currentState = isHealthy; } } if (update) { adviseListeners(isHealthy); } } }, debouncePeriod, TimeUnit.MILLISECONDS); } } else { if (currentState == isHealthy) { scheduledFuture.cancel(false); scheduledFuture = null; } } } DebounceHealthMonitorStrategy(long debouncePeriod); @Override synchronized void update(final boolean isHealthy);  }