Skip to content

fix: Use ConcurrentHashMap for InMemoryProvider #1057

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Aug 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
package dev.openfeature.sdk.providers.memory;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import dev.openfeature.sdk.EvaluationContext;
import dev.openfeature.sdk.EventProvider;
import dev.openfeature.sdk.Metadata;
Expand All @@ -24,6 +17,13 @@
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

/**
* In-memory provider.
*/
Expand All @@ -33,7 +33,7 @@ public class InMemoryProvider extends EventProvider {
@Getter
private static final String NAME = "InMemoryProvider";

private Map<String, Flag<?>> flags;
private final Map<String, Flag<?>> flags;

@Getter
private ProviderState state = ProviderState.NOT_READY;
Expand All @@ -44,11 +44,11 @@ public Metadata getMetadata() {
}

public InMemoryProvider(Map<String, Flag<?>> flags) {
this.flags = new HashMap<>(flags);
this.flags = new ConcurrentHashMap<>(flags);
}

/**
* Initialize the provider.
* Initializes the provider.
* @param evaluationContext evaluation context
* @throws Exception on error
*/
Expand All @@ -60,14 +60,15 @@ public void initialize(EvaluationContext evaluationContext) throws Exception {
}

/**
* Updating provider flags configuration, replacing existing flags.
* @param flags the flags to use instead of the previous flags.
* Updates the provider flags configuration.
* For existing flags, the new configurations replace the old one.
* For new flags, they are added to the configuration.
* @param newFlags the new flag configurations
*/
public void updateFlags(Map<String, Flag<?>> flags) {
Set<String> flagsChanged = new HashSet<>();
flagsChanged.addAll(this.flags.keySet());
flagsChanged.addAll(flags.keySet());
this.flags = new HashMap<>(flags);
public void updateFlags(Map<String, Flag<?>> newFlags) {
Set<String> flagsChanged = new HashSet<>(newFlags.keySet());
this.flags.putAll(newFlags);

ProviderEventDetails details = ProviderEventDetails.builder()
.flagsChanged(new ArrayList<>(flagsChanged))
.message("flags changed")
Expand All @@ -76,13 +77,15 @@ public void updateFlags(Map<String, Flag<?>> flags) {
}

/**
* Updating provider flags configuration with adding or updating a flag.
* @param flag the flag to update. If a flag with this key already exists, new flag replaces it.
* Updates a single provider flag configuration.
* For existing flag, the new configuration replaces the old one.
* For new flag, they are added to the configuration.
* @param newFlag the flag to update
*/
public void updateFlag(String flagKey, Flag<?> flag) {
this.flags.put(flagKey, flag);
public void updateFlag(String flagKey, Flag<?> newFlag) {
this.flags.put(flagKey, newFlag);
ProviderEventDetails details = ProviderEventDetails.builder()
.flagsChanged(Arrays.asList(flagKey))
.flagsChanged(Collections.singletonList(flagKey))
.message("flag added/updated")
.build();
emitProviderConfigurationChanged(details);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,30 @@

import com.google.common.collect.ImmutableMap;
import dev.openfeature.sdk.Client;
import dev.openfeature.sdk.EventDetails;
import dev.openfeature.sdk.ImmutableContext;
import dev.openfeature.sdk.OpenFeatureAPI;
import dev.openfeature.sdk.Value;
import dev.openfeature.sdk.exceptions.FlagNotFoundError;
import dev.openfeature.sdk.exceptions.ProviderNotReadyError;
import dev.openfeature.sdk.exceptions.TypeMismatchError;
import lombok.SneakyThrows;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.util.HashMap;
import java.util.Map;
import java.util.function.Consumer;

import static dev.openfeature.sdk.Structure.mapToStructure;
import static dev.openfeature.sdk.testutils.TestFlagsUtils.buildFlags;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.awaitility.Awaitility.await;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.argThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
Expand All @@ -32,8 +37,8 @@ class InMemoryProviderTest {
private static InMemoryProvider provider;

@SneakyThrows
@BeforeAll
static void beforeAll() {
@BeforeEach
void beforeEach() {
Map<String, Flag<?>> flags = buildFlags();
provider = spy(new InMemoryProvider(flags));
OpenFeatureAPI.getInstance().onProviderConfigurationChanged(eventDetails -> {});
Expand Down Expand Up @@ -105,4 +110,19 @@ void shouldThrowIfNotInitialized() {
// ErrorCode.PROVIDER_NOT_READY should be returned when evaluated via the client
assertThrows(ProviderNotReadyError.class, ()-> inMemoryProvider.getBooleanEvaluation("fail_not_initialized", false, new ImmutableContext()));
}

@SuppressWarnings("unchecked")
@Test
void emitChangedFlagsOnlyIfThereAreChangedFlags() {
Consumer<EventDetails> handler = mock(Consumer.class);
Map<String, Flag<?>> flags = buildFlags();

OpenFeatureAPI.getInstance().onProviderConfigurationChanged(handler);
OpenFeatureAPI.getInstance().setProviderAndWait(provider);

provider.updateFlags(flags);

await().untilAsserted(() -> verify(handler, times(1))
.accept(argThat(details -> details.getFlagsChanged().size() == buildFlags().size())));
}
}
Loading