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 1 commit
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
Expand Up @@ -2,10 +2,10 @@

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 java.util.concurrent.ConcurrentHashMap;

import dev.openfeature.sdk.EvaluationContext;
import dev.openfeature.sdk.EventProvider;
Expand Down Expand Up @@ -44,7 +44,7 @@ public Metadata getMetadata() {
}

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

/**
Expand All @@ -67,7 +67,7 @@ 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);
this.flags = new ConcurrentHashMap<>(flags);
ProviderEventDetails details = ProviderEventDetails.builder()
.flagsChanged(new ArrayList<>(flagsChanged))
.message("flags changed")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import static dev.openfeature.sdk.Structure.mapToStructure;
import static dev.openfeature.sdk.testutils.TestFlagsUtils.buildFlags;
Expand Down Expand Up @@ -105,4 +107,23 @@ 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()));
}

@Test
void multithreadedTest() {
ExecutorService executorService = Executors.newFixedThreadPool(100);
for (int i = 0; i < 10000; i++) {
String flagKey = "multithreadedFlag" + i;
executorService.submit(() -> {
provider.updateFlag(flagKey, Flag.builder()
.variant("on", true)
.variant("off", false)
.defaultVariant("on")
.build());
});
}

for (int i = 0; i < 10000; i++) {
assertTrue(client.getBooleanValue("multithreadedFlag" + i, false));
}
}
}
Loading