### Question:
ControllerLeaderLocator { public synchronized void invalidateCachedControllerLeader() { long now = getCurrentTimeMs(); long millisSinceLastInvalidate = now - _lastCacheInvalidationTimeMs; if (millisSinceLastInvalidate < MIN_INVALIDATE_INTERVAL_MS) { LOGGER.info( "Millis since last controller cache value invalidate {} is less than allowed frequency {}. Skipping invalidate.", millisSinceLastInvalidate, MIN_INVALIDATE_INTERVAL_MS); } else { LOGGER.info("Invalidating cached controller leader value"); _cachedControllerLeaderValid = false; _lastCacheInvalidationTimeMs = now; } } ControllerLeaderLocator(HelixManager helixManager); static void create(HelixManager helixManager); static ControllerLeaderLocator getInstance(); synchronized Pair<String, Integer> getControllerLeader(String rawTableName); synchronized void invalidateCachedControllerLeader(); static final Logger LOGGER; }### Answer:
@Test public void testInvalidateCachedControllerLeader() { HelixManager helixManager = mock(HelixManager.class); HelixDataAccessor helixDataAccessor = mock(HelixDataAccessor.class); final String leaderHost = "host"; final int leaderPort = 12345; ConfigAccessor configAccessor = mock(ConfigAccessor.class); ResourceConfig resourceConfig = mock(ResourceConfig.class); when(helixManager.getConfigAccessor()).thenReturn(configAccessor); when(configAccessor.getResourceConfig(any(), anyString())).thenReturn(resourceConfig); when(resourceConfig.getSimpleConfig(anyString())).thenReturn("false"); when(helixManager.getHelixDataAccessor()).thenReturn(helixDataAccessor); PropertyKey.Builder keyBuilder = mock(PropertyKey.Builder.class); when(helixDataAccessor.keyBuilder()).thenReturn(keyBuilder); PropertyKey controllerLeader = mock(PropertyKey.class); when(keyBuilder.controllerLeader()).thenReturn(controllerLeader); LiveInstance liveInstance = mock(LiveInstance.class); when(helixDataAccessor.getProperty(controllerLeader)).thenReturn(liveInstance); when(liveInstance.getInstanceName()).thenReturn(leaderHost + "_" + leaderPort); FakeControllerLeaderLocator.create(helixManager); FakeControllerLeaderLocator controllerLeaderLocator = FakeControllerLeaderLocator.getInstance(); Assert.assertFalse(controllerLeaderLocator.isCachedControllerLeaderValid()); Assert.assertEquals(controllerLeaderLocator.getLastCacheInvalidationTimeMs(), 0); long currentTimeMs = 31_000L; controllerLeaderLocator.setCurrentTimeMs(currentTimeMs); controllerLeaderLocator.invalidateCachedControllerLeader(); Assert.assertFalse(controllerLeaderLocator.isCachedControllerLeaderValid()); long lastCacheInvalidateMillis = controllerLeaderLocator.getLastCacheInvalidationTimeMs(); Assert.assertTrue(lastCacheInvalidateMillis > 0); Assert.assertEquals(lastCacheInvalidateMillis, currentTimeMs); currentTimeMs = 32_000L; controllerLeaderLocator.setCurrentTimeMs(currentTimeMs); controllerLeaderLocator.invalidateCachedControllerLeader(); Assert.assertFalse(controllerLeaderLocator.isCachedControllerLeaderValid()); Assert.assertEquals(controllerLeaderLocator.getLastCacheInvalidationTimeMs(), lastCacheInvalidateMillis); controllerLeaderLocator.getControllerLeader(testTable); Assert.assertTrue(controllerLeaderLocator.isCachedControllerLeaderValid()); Assert.assertEquals(controllerLeaderLocator.getLastCacheInvalidationTimeMs(), lastCacheInvalidateMillis); currentTimeMs = 33_000L; controllerLeaderLocator.setCurrentTimeMs(currentTimeMs); controllerLeaderLocator.invalidateCachedControllerLeader(); Assert.assertTrue(controllerLeaderLocator.isCachedControllerLeaderValid()); Assert.assertEquals(controllerLeaderLocator.getLastCacheInvalidationTimeMs(), lastCacheInvalidateMillis); controllerLeaderLocator.setCurrentTimeMs( controllerLeaderLocator.getCurrentTimeMs() + 2 * controllerLeaderLocator.getMinInvalidateIntervalMs()); controllerLeaderLocator.invalidateCachedControllerLeader(); Assert.assertFalse(controllerLeaderLocator.isCachedControllerLeaderValid()); Assert.assertTrue(controllerLeaderLocator.getLastCacheInvalidationTimeMs() > lastCacheInvalidateMillis); }