Skip to content

Commit

Permalink
Remove concurrency of DittoPublicKeyProviderTest to prevent accidenta…
Browse files Browse the repository at this point in the history
…l failures.

Signed-off-by: Yufei Cai <yufei.cai@bosch.io>
  • Loading branch information
yufei-cai committed Jul 14, 2022
1 parent f65a793 commit 27a1161
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionStage;
import java.util.concurrent.Executor;
import java.util.function.Function;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
Expand Down Expand Up @@ -105,6 +106,19 @@ private DittoPublicKeyProvider(final JwtSubjectIssuersConfig jwtSubjectIssuersCo
publicKeyCache = CaffeineCache.of(caffeine, loader, cacheName);
}

DittoPublicKeyProvider(final JwtSubjectIssuersConfig jwtSubjectIssuersConfig,
final HttpClientFacade httpClient,
final OAuthConfig oAuthConfig,
final Function<AsyncCacheLoader<PublicKeyIdWithIssuer, PublicKeyWithParser>,
Cache<PublicKeyIdWithIssuer, PublicKeyWithParser>> publicKeyCacheFactory) {

this.jwtSubjectIssuersConfig = argumentNotNull(jwtSubjectIssuersConfig);
this.httpClient = argumentNotNull(httpClient);
materializer = SystemMaterializer.get(httpClient::getActorSystem).materializer();
this.oAuthConfig = oAuthConfig;
publicKeyCache = publicKeyCacheFactory.apply(this::loadPublicKeyWithParser);
}

/**
* Returns a new {@code PublicKeyProvider} for the given parameters.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,17 @@

import java.time.Duration;
import java.util.Collections;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.concurrent.*;

import com.github.benmanes.caffeine.cache.AsyncCacheLoader;
import com.github.benmanes.caffeine.cache.Caffeine;
import org.eclipse.ditto.base.model.signals.commands.exceptions.GatewayAuthenticationProviderUnavailableException;
import org.eclipse.ditto.gateway.service.security.cache.PublicKeyIdWithIssuer;
import org.eclipse.ditto.gateway.service.util.config.security.OAuthConfig;
import org.eclipse.ditto.internal.utils.cache.Cache;
import org.eclipse.ditto.internal.utils.cache.CaffeineCache;
import org.eclipse.ditto.internal.utils.cache.config.CacheConfig;
import org.eclipse.ditto.internal.utils.http.HttpClientFacade;
import org.eclipse.ditto.json.JsonArray;
Expand Down Expand Up @@ -77,11 +80,9 @@ public void setup() {
when(httpClientMock.getActorSystem()).thenReturn(actorSystem);
final JwtSubjectIssuersConfig subjectIssuersConfig = JwtSubjectIssuersConfig.fromJwtSubjectIssuerConfigs(
Collections.singleton(new JwtSubjectIssuerConfig(SubjectIssuer.GOOGLE, "google.com")));
when(cacheConfigMock.getMaximumSize()).thenReturn(100L);
when(cacheConfigMock.getExpireAfterWrite()).thenReturn(Duration.ofMinutes(3));
when(oauthConfigMock.getAllowedClockSkew()).thenReturn(Duration.ofSeconds(1));
underTest = DittoPublicKeyProvider.of(subjectIssuersConfig, httpClientMock, cacheConfigMock,
getClass().getSimpleName(), oauthConfigMock);
underTest = new DittoPublicKeyProvider(subjectIssuersConfig, httpClientMock,
oauthConfigMock, DittoPublicKeyProviderTest::thisThreadCache);
}

@After
Expand Down Expand Up @@ -207,4 +208,12 @@ private void mockSuccessfulPublicKeysRequestWithoutMatchingKeyId() {
.thenReturn(CompletableFuture.completedFuture(publicKeysResponse));
}

private static <K, V> Cache<K, V> thisThreadCache(final AsyncCacheLoader<K, V> loader) {
final var caffeine = Caffeine.newBuilder()
.maximumSize(100)
.expireAfterWrite(Duration.ofMinutes(3L))
.executor(Runnable::run);

return CaffeineCache.of(caffeine, loader, DittoPublicKeyProviderTest.class.getSimpleName());
}
}

0 comments on commit 27a1161

Please sign in to comment.