Skip to content

Commit 0f86c1a

Browse files
authored
Improve token credential object caching logic for identity extension (Azure#43936)
1 parent 7f0d67f commit 0f86c1a

File tree

2 files changed

+4
-4
lines changed

2 files changed

+4
-4
lines changed

sdk/identity/azure-identity-extensions/src/main/java/com/azure/identity/extensions/implementation/credential/provider/CachingTokenCredentialProvider.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ private static TokenCredential getOrCreate(Map<String, TokenCredential> cache,
5858
LOGGER.verbose("Retrieving token credential from cache.");
5959
} else {
6060
LOGGER.verbose("Caching token credential.");
61-
cache.put(tokenCredentialCacheKey, fn.apply(delegate));
61+
cache.putIfAbsent(tokenCredentialCacheKey, fn.apply(delegate));
6262
}
6363

6464
return cache.get(tokenCredentialCacheKey);

sdk/identity/azure-identity-extensions/src/test/java/com/azure/identity/extensions/implementation/template/AzureAuthenticationTemplateTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ void nonCacheTokenCredential() {
117117
}
118118

119119
@Test
120-
void getTokenAsPasswordWithDefaultCredentialProvider() throws InterruptedException {
120+
void verityTokeWithDefaultCredentialProvider() throws InterruptedException {
121121
// setup
122122
String token1 = "token1";
123123
String token2 = "token2";
@@ -152,7 +152,7 @@ void getTokenAsPasswordWithDefaultCredentialProvider() throws InterruptedExcepti
152152
}
153153

154154
@Test
155-
void getTokenAsPasswordWithCachingCredentialProvider() throws InterruptedException {
155+
void verityTokenWithCachingCredentialProvider() throws InterruptedException {
156156
int tokenExpireSeconds = 2;
157157
AtomicInteger tokenIndex1 = new AtomicInteger();
158158
AtomicInteger tokenIndex2 = new AtomicInteger(1);
@@ -180,7 +180,7 @@ void getTokenAsPasswordWithCachingCredentialProvider() throws InterruptedExcepti
180180

181181
verifyToken("token1-", 0, template);
182182
TimeUnit.SECONDS.sleep(tokenExpireSeconds + 1);
183-
verifyToken("token2-", 1, template);
183+
verifyToken("token2-", 1, template2);
184184
assertNotNull(credentialProviderMock);
185185
}
186186
}

0 commit comments

Comments
 (0)