Skip to content

Commit

Permalink
fix: Use ConcurrentHashMap for InMemoryProvider
Browse files Browse the repository at this point in the history
Signed-off-by: Ryan Prayogo <[email protected]>
  • Loading branch information
ryanprayogo committed Aug 21, 2024
1 parent 61d821a commit ebcb5ce
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
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));
}
}
}

0 comments on commit ebcb5ce

Please sign in to comment.