Skip to content

Commit

Permalink
test(apollo-biz): Optimize the test case
Browse files Browse the repository at this point in the history
  • Loading branch information
klboke committed Apr 4, 2023
1 parent c7ac0f9 commit 63fee52
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,9 @@ protected Release findActiveOne(long id, ApolloNotificationMessages clientMessag
@Override
protected Release findLatestActiveRelease(String appId, String clusterName, String namespaceName,
ApolloNotificationMessages clientMessages) {
String cacheKey = ReleaseMessageKeyGenerator.generate(appId, clusterName, namespaceName);
String messageKey = ReleaseMessageKeyGenerator.generate(appId, clusterName, namespaceName);
String cacheKey = messageKey;

if (bizConfig.isConfigServiceCacheKeyIgnoreCase()) {
cacheKey = cacheKey.toLowerCase();
}
Expand All @@ -160,7 +162,6 @@ protected Release findLatestActiveRelease(String appId, String clusterName, Stri

ConfigCacheEntry cacheEntry = configCache.getUnchecked(cacheKey);

String messageKey = ReleaseMessageKeyGenerator.generate(appId, clusterName, namespaceName);
//cache is out-dated
if (clientMessages != null && clientMessages.has(messageKey) &&
clientMessages.get(messageKey) > cacheEntry.getNotificationId()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,7 @@ public void setUp() throws Exception {
someNotificationId = 1;

normalSomeKey = ReleaseMessageKeyGenerator.generate(someAppId, someClusterName, someNamespaceName);
lowerCaseSomeKey = ReleaseMessageKeyGenerator.generate(someAppId, someClusterName, someNamespaceName)
.toLowerCase();
lowerCaseSomeKey = normalSomeKey.toLowerCase();
someNotificationMessages = new ApolloNotificationMessages();
}

Expand Down Expand Up @@ -160,9 +159,9 @@ public void testFindLatestActiveRelease() {
when(releaseMessageService.findLatestReleaseMessageForMessages(Lists.newArrayList(lowerCaseSomeKey)))
.thenReturn(someReleaseMessage);
when(releaseService.findLatestActiveRelease(
matches(Pattern.compile(someAppId, Pattern.CASE_INSENSITIVE)),
matches(Pattern.compile(someClusterName, Pattern.CASE_INSENSITIVE)),
matches(Pattern.compile(someNamespaceName, Pattern.CASE_INSENSITIVE))
matchesCaseInsensitive(someAppId),
matchesCaseInsensitive(someClusterName),
matchesCaseInsensitive(someNamespaceName)
)).thenReturn(someRelease);
when(someReleaseMessage.getId()).thenReturn(someNotificationId);

Expand Down Expand Up @@ -194,9 +193,9 @@ public void testFindLatestActiveReleaseWithReleaseNotFound() {
when(releaseMessageService.findLatestReleaseMessageForMessages(
Lists.newArrayList(lowerCaseSomeKey))).thenReturn(null);
when(releaseService.findLatestActiveRelease(
matches(Pattern.compile(someAppId, Pattern.CASE_INSENSITIVE)),
matches(Pattern.compile(someClusterName, Pattern.CASE_INSENSITIVE)),
matches(Pattern.compile(someNamespaceName, Pattern.CASE_INSENSITIVE))
matchesCaseInsensitive(someAppId),
matchesCaseInsensitive(someClusterName),
matchesCaseInsensitive(someNamespaceName)
)).thenReturn(null);

Release release = configServiceWithCache.findLatestActiveRelease(someAppId, someClusterName,
Expand Down Expand Up @@ -234,9 +233,9 @@ public void testFindLatestActiveReleaseWithDirtyRelease() {
Lists.newArrayList(lowerCaseSomeKey))).thenReturn
(someReleaseMessage);
when(releaseService.findLatestActiveRelease(
matches(Pattern.compile(someAppId, Pattern.CASE_INSENSITIVE)),
matches(Pattern.compile(someClusterName, Pattern.CASE_INSENSITIVE)),
matches(Pattern.compile(someNamespaceName, Pattern.CASE_INSENSITIVE))
matchesCaseInsensitive(someAppId),
matchesCaseInsensitive(someClusterName),
matchesCaseInsensitive(someNamespaceName)
)).thenReturn(someRelease);
when(someReleaseMessage.getId()).thenReturn(someNotificationId);

Expand All @@ -247,9 +246,9 @@ public void testFindLatestActiveReleaseWithDirtyRelease() {
when(releaseMessageService.findLatestReleaseMessageForMessages(
Lists.newArrayList(lowerCaseSomeKey))).thenReturn(anotherReleaseMessage);
when(releaseService.findLatestActiveRelease(
matches(Pattern.compile(someAppId, Pattern.CASE_INSENSITIVE)),
matches(Pattern.compile(someClusterName, Pattern.CASE_INSENSITIVE)),
matches(Pattern.compile(someNamespaceName, Pattern.CASE_INSENSITIVE))
matchesCaseInsensitive(someAppId),
matchesCaseInsensitive(someClusterName),
matchesCaseInsensitive(someNamespaceName)
)).thenReturn(anotherRelease);
when(anotherReleaseMessage.getId()).thenReturn(someNewNotificationId);

Expand Down Expand Up @@ -286,9 +285,9 @@ public void testFindLatestActiveReleaseWithReleaseMessageNotification() {
when(releaseMessageService.findLatestReleaseMessageForMessages(Lists.newArrayList(lowerCaseSomeKey)))
.thenReturn(someReleaseMessage);
when(releaseService.findLatestActiveRelease(
matches(Pattern.compile(someAppId, Pattern.CASE_INSENSITIVE)),
matches(Pattern.compile(someClusterName, Pattern.CASE_INSENSITIVE)),
matches(Pattern.compile(someNamespaceName, Pattern.CASE_INSENSITIVE))
matchesCaseInsensitive(someAppId),
matchesCaseInsensitive(someClusterName),
matchesCaseInsensitive(someNamespaceName)
)).thenReturn(someRelease);
when(someReleaseMessage.getId()).thenReturn(someNotificationId);

Expand All @@ -299,9 +298,9 @@ public void testFindLatestActiveReleaseWithReleaseMessageNotification() {
when(releaseMessageService.findLatestReleaseMessageForMessages(Lists.newArrayList(lowerCaseSomeKey)))
.thenReturn(anotherReleaseMessage);
when(releaseService.findLatestActiveRelease(
matches(Pattern.compile(someAppId, Pattern.CASE_INSENSITIVE)),
matches(Pattern.compile(someClusterName, Pattern.CASE_INSENSITIVE)),
matches(Pattern.compile(someNamespaceName, Pattern.CASE_INSENSITIVE))
matchesCaseInsensitive(someAppId),
matchesCaseInsensitive(someClusterName),
matchesCaseInsensitive(someNamespaceName)
)).thenReturn(anotherRelease);

when(anotherReleaseMessage.getMessage()).thenReturn(lowerCaseSomeKey);
Expand Down Expand Up @@ -344,9 +343,9 @@ public void testFindLatestActiveReleaseWithIrrelevantMessages() {
when(releaseMessageService.findLatestReleaseMessageForMessages(Lists.newArrayList(lowerCaseSomeKey)))
.thenReturn(someReleaseMessage);
when(releaseService.findLatestActiveRelease(
matches(Pattern.compile(someAppId, Pattern.CASE_INSENSITIVE)),
matches(Pattern.compile(someClusterName, Pattern.CASE_INSENSITIVE)),
matches(Pattern.compile(someNamespaceName, Pattern.CASE_INSENSITIVE))
matchesCaseInsensitive(someAppId),
matchesCaseInsensitive(someClusterName),
matchesCaseInsensitive(someNamespaceName)
)).thenReturn(someRelease);
when(someReleaseMessage.getId()).thenReturn(someNotificationId);

Expand Down Expand Up @@ -375,4 +374,9 @@ public void testFindLatestActiveReleaseWithIrrelevantMessages() {
someClusterName.toLowerCase(),
someNamespaceName.toLowerCase());
}

private String matchesCaseInsensitive(final String regex) {
return matches(Pattern.compile(regex, Pattern.CASE_INSENSITIVE));
}

}

0 comments on commit 63fee52

Please sign in to comment.