Skip to content

Commit

Permalink
address review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
trentjeff committed Oct 4, 2023
1 parent 3bb7aaf commit cb3f16d
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ class TlsManagerCache {
private TlsManagerCache() {
}

static TlsManager getOrCreate(Object configBean,
Function<Object, TlsManager> creator) {
static <T> TlsManager getOrCreate(T configBean,
Function<Object, TlsManager> creator) {
Objects.requireNonNull(configBean);
Objects.requireNonNull(creator);
return CACHE.computeIfAbsent(configBean, creator);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

package io.helidon.common.tls.spi;

import java.util.Objects;
import java.util.function.Function;

import io.helidon.common.config.Config;
Expand All @@ -31,18 +30,12 @@ public interface TlsManagerProvider extends ConfiguredProvider<TlsManager> {
/**
* Provides the ability to have a unique {@link TlsManager} per unique {@link Config} instance provided.
*
* @param config the raw config instance
* @param configBean the config bean instance
* @param name the config bean instance name
* @param creator the creator to apply if not already in cache, which takes the config bean instance
* @return the tls manager instance from cache, defaulting to creation from the {@code creator} if not in cache
*/
static TlsManager getOrCreate(Config config,
String name,
Object configBean,
Function<Object, TlsManager> creator) {
Objects.requireNonNull(config);
Objects.requireNonNull(name);
static <T> TlsManager getOrCreate(T configBean,
Function<Object, TlsManager> creator) {
return TlsManagerCache.getOrCreate(configBean, creator);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,41 +16,42 @@

package io.helidon.common.tls.spi;

import io.helidon.common.tls.ConfiguredTlsManager;
import java.util.concurrent.atomic.AtomicInteger;

import io.helidon.common.tls.TlsManager;
import io.helidon.config.Config;

import org.junit.jupiter.api.Test;
import org.mockito.Mockito;

import java.util.concurrent.atomic.AtomicInteger;

import static org.hamcrest.CoreMatchers.*;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.not;
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.hamcrest.CoreMatchers.sameInstance;
import static org.hamcrest.MatcherAssert.assertThat;

class TlsManagerProviderTest {

@Test
void caching() {
TlsManager mock = Mockito.mock(TlsManager.class);
Config config = Config.create();
AtomicInteger count = new AtomicInteger();

// we are using "1" and "2" here abstractly to stand in for Config beans, which would hash properly
TlsManager manager1 = TlsManagerProvider.getOrCreate(config, "test", "1", (c) -> {
TlsManager manager1 = TlsManagerProvider.getOrCreate("1", (c) -> {
count.incrementAndGet();
return mock;
});
assertThat(manager1, sameInstance(mock));
assertThat(count.get(), is(1));

TlsManager manager2 = TlsManagerProvider.getOrCreate(config, "test", "1", (c) -> {
TlsManager manager2 = TlsManagerProvider.getOrCreate("1", (c) -> {
count.incrementAndGet();
return Mockito.mock(TlsManager.class);
});
assertThat(manager2, sameInstance(mock));
assertThat(count.get(), is(1));

TlsManager manager3 = TlsManagerProvider.getOrCreate(config, "test", "2", (c) -> {
TlsManager manager3 = TlsManagerProvider.getOrCreate("2", (c) -> {
count.incrementAndGet();
return Mockito.mock(TlsManager.class);
});
Expand Down

0 comments on commit cb3f16d

Please sign in to comment.