ConfigServiceWithCache extends AbstractConfigService { @Override protected Release findLatestActiveRelease(String appId, String clusterName, String namespaceName, ApolloNotificationMessages clientMessages) { String key = ReleaseMessageKeyGenerator.generate(appId, clusterName, namespaceName); Tracer.logEvent(TRACER_EVENT_CACHE_GET, key); ConfigCacheEntry cacheEntry = configCache.getUnchecked(key); if (clientMessages != null && clientMessages.has(key) && clientMessages.get(key) > cacheEntry.getNotificationId()) { invalidate(key); cacheEntry = configCache.getUnchecked(key); } return cacheEntry.getRelease(); } ConfigServiceWithCache(); @Override void handleMessage(ReleaseMessage message, String channel);  }
@Test public void testFindLatestActiveRelease() throws Exception { when(releaseMessageService.findLatestReleaseMessageForMessages(Lists.newArrayList(someKey))).thenReturn (someReleaseMessage); when(releaseService.findLatestActiveRelease(someAppId, someClusterName, someNamespaceName)).thenReturn (someRelease); when(someReleaseMessage.getId()).thenReturn(someNotificationId); Release release = configServiceWithCache.findLatestActiveRelease(someAppId, someClusterName, someNamespaceName, someNotificationMessages); Release anotherRelease = configServiceWithCache.findLatestActiveRelease(someAppId, someClusterName, someNamespaceName, someNotificationMessages); int retryTimes = 100; for (int i = 0; i < retryTimes; i++) { configServiceWithCache.findLatestActiveRelease(someAppId, someClusterName, someNamespaceName, someNotificationMessages); } assertEquals(someRelease, release); assertEquals(someRelease, anotherRelease); verify(releaseMessageService, times(1)).findLatestReleaseMessageForMessages(Lists.newArrayList(someKey)); verify(releaseService, times(1)).findLatestActiveRelease(someAppId, someClusterName, someNamespaceName); }
@Test public void testFindLatestActiveReleaseWithReleaseNotFound() throws Exception { when(releaseMessageService.findLatestReleaseMessageForMessages(Lists.newArrayList(someKey))).thenReturn(null); when(releaseService.findLatestActiveRelease(someAppId, someClusterName, someNamespaceName)).thenReturn(null); Release release = configServiceWithCache.findLatestActiveRelease(someAppId, someClusterName, someNamespaceName, someNotificationMessages); Release anotherRelease = configServiceWithCache.findLatestActiveRelease(someAppId, someClusterName, someNamespaceName, someNotificationMessages); int retryTimes = 100; for (int i = 0; i < retryTimes; i++) { configServiceWithCache.findLatestActiveRelease(someAppId, someClusterName, someNamespaceName, someNotificationMessages); } assertNull(release); assertNull(anotherRelease); verify(releaseMessageService, times(1)).findLatestReleaseMessageForMessages(Lists.newArrayList(someKey)); verify(releaseService, times(1)).findLatestActiveRelease(someAppId, someClusterName, someNamespaceName); }
@Test public void testFindLatestActiveReleaseWithDirtyRelease() throws Exception { long someNewNotificationId = someNotificationId + 1; ReleaseMessage anotherReleaseMessage = mock(ReleaseMessage.class); Release anotherRelease = mock(Release.class); when(releaseMessageService.findLatestReleaseMessageForMessages(Lists.newArrayList(someKey))).thenReturn (someReleaseMessage); when(releaseService.findLatestActiveRelease(someAppId, someClusterName, someNamespaceName)).thenReturn (someRelease); when(someReleaseMessage.getId()).thenReturn(someNotificationId); Release release = configServiceWithCache.findLatestActiveRelease(someAppId, someClusterName, someNamespaceName, someNotificationMessages); when(releaseMessageService.findLatestReleaseMessageForMessages(Lists.newArrayList(someKey))).thenReturn (anotherReleaseMessage); when(releaseService.findLatestActiveRelease(someAppId, someClusterName, someNamespaceName)).thenReturn (anotherRelease); when(anotherReleaseMessage.getId()).thenReturn(someNewNotificationId); Release stillOldRelease = configServiceWithCache.findLatestActiveRelease(someAppId, someClusterName, someNamespaceName, someNotificationMessages); someNotificationMessages.put(someKey, someNewNotificationId); Release shouldBeNewRelease = configServiceWithCache.findLatestActiveRelease(someAppId, someClusterName, someNamespaceName, someNotificationMessages); assertEquals(someRelease, release); assertEquals(someRelease, stillOldRelease); assertEquals(anotherRelease, shouldBeNewRelease); verify(releaseMessageService, times(2)).findLatestReleaseMessageForMessages(Lists.newArrayList(someKey)); verify(releaseService, times(2)).findLatestActiveRelease(someAppId, someClusterName, someNamespaceName); }
@Test public void testFindLatestActiveReleaseWithIrrelevantMessages() throws Exception { long someNewNotificationId = someNotificationId + 1; String someIrrelevantKey = "someIrrelevantKey"; when(releaseMessageService.findLatestReleaseMessageForMessages(Lists.newArrayList(someKey))).thenReturn (someReleaseMessage); when(releaseService.findLatestActiveRelease(someAppId, someClusterName, someNamespaceName)).thenReturn (someRelease); when(someReleaseMessage.getId()).thenReturn(someNotificationId); Release release = configServiceWithCache.findLatestActiveRelease(someAppId, someClusterName, someNamespaceName, someNotificationMessages); Release stillOldRelease = configServiceWithCache.findLatestActiveRelease(someAppId, someClusterName, someNamespaceName, someNotificationMessages); someNotificationMessages.put(someIrrelevantKey, someNewNotificationId); Release shouldStillBeOldRelease = configServiceWithCache.findLatestActiveRelease(someAppId, someClusterName, someNamespaceName, someNotificationMessages); assertEquals(someRelease, release); assertEquals(someRelease, stillOldRelease); assertEquals(someRelease, shouldStillBeOldRelease); verify(releaseMessageService, times(1)).findLatestReleaseMessageForMessages(Lists.newArrayList(someKey)); verify(releaseService, times(1)).findLatestActiveRelease(someAppId, someClusterName, someNamespaceName); }