|
| 1 | +package dev.openfeature.sdk; |
| 2 | + |
| 3 | +import com.vmlens.api.AllInterleavings; |
| 4 | +import com.vmlens.api.Runner; |
| 5 | +import dev.openfeature.sdk.providers.memory.Flag; |
| 6 | +import dev.openfeature.sdk.providers.memory.InMemoryProvider; |
| 7 | +import java.util.HashMap; |
| 8 | +import java.util.Map; |
| 9 | +import org.junit.jupiter.api.AfterEach; |
| 10 | +import org.junit.jupiter.api.BeforeEach; |
| 11 | +import org.junit.jupiter.api.Test; |
| 12 | + |
| 13 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 14 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 15 | + |
| 16 | +/** |
| 17 | + * Javadoc. |
| 18 | + */ |
| 19 | +class VmLensTest { |
| 20 | + final OpenFeatureAPI api = new OpenFeatureAPI(); |
| 21 | + |
| 22 | + @BeforeEach |
| 23 | + void setUp() { |
| 24 | + var flags = new HashMap<String, Flag<?>>(); |
| 25 | + flags.put("a", Flag.builder().variant("a", "def").defaultVariant("a").build()); |
| 26 | + flags.put("b", Flag.builder().variant("a", "as").defaultVariant("a").build()); |
| 27 | + api.setProviderAndWait(new InMemoryProvider(flags)); |
| 28 | + } |
| 29 | + |
| 30 | + @AfterEach |
| 31 | + void tearDown() { |
| 32 | + api.clearHooks(); |
| 33 | + api.shutdown(); |
| 34 | + } |
| 35 | + |
| 36 | + @Test |
| 37 | + void concurrentFlagEvaluations() { |
| 38 | + var client = api.getClient(); |
| 39 | + try (AllInterleavings allInterleavings = new AllInterleavings("Concurrent evaluations")) { |
| 40 | + while (allInterleavings.hasNext()) { |
| 41 | + Runner.runParallel( |
| 42 | + () -> assertEquals("def", client.getStringValue("a", "a")), |
| 43 | + () -> assertEquals("as", client.getStringValue("b", "b")) |
| 44 | + ); |
| 45 | + } |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + @Test |
| 50 | + void concurrentFlagEvaluationsAndHookAdditions() { |
| 51 | + var client = api.getClient(); |
| 52 | + try (AllInterleavings allInterleavings = new AllInterleavings("Concurrent evaluations and hook additions")) { |
| 53 | + while (allInterleavings.hasNext()) { |
| 54 | + Runner.runParallel( |
| 55 | + () -> assertEquals("def", client.getStringValue("a", "a")), |
| 56 | + () -> client.addHooks(new StringHook() {}) |
| 57 | + ); |
| 58 | + } |
| 59 | + } |
| 60 | + } |
| 61 | +// todo add tests: |
| 62 | + // concurrent changing of context thorugh client.setctx... and flags with a targeting rule depending on that context |
| 63 | + // concurrent setting of context thorugh client.setctx... and flags with a targeting rule depending on that context |
| 64 | + // concurrent changing of context through a hook and flags with a targeting rule depending on that context |
| 65 | + // concurrent setting of context through a hook and flags with a targeting rule depending on that context |
| 66 | + |
| 67 | + @Test |
| 68 | + void concurrentClientCreations() { |
| 69 | + try (AllInterleavings allInterleavings = new AllInterleavings("Concurrent creations of the Client")) { |
| 70 | + while (allInterleavings.hasNext()) { |
| 71 | + Runner.runParallel( |
| 72 | + api::getClient, |
| 73 | + api::getClient |
| 74 | + ); |
| 75 | + } |
| 76 | + } |
| 77 | + // keep the linter happy |
| 78 | + assertTrue(true); |
| 79 | + } |
| 80 | + |
| 81 | + @Test |
| 82 | + void concurrentContextSetting() { |
| 83 | + var client = api.getClient(); |
| 84 | + try (AllInterleavings allInterleavings = new AllInterleavings( |
| 85 | + "Concurrently setting the context and evaluating a flag")) { |
| 86 | + while (allInterleavings.hasNext()) { |
| 87 | + Runner.runParallel( |
| 88 | + () -> assertEquals("def", client.getStringValue("a", "a")), |
| 89 | + () -> client.setEvaluationContext(new ImmutableContext(Map.of("a", new Value("b")))), |
| 90 | + () -> client.setEvaluationContext(new ImmutableContext(Map.of("c", new Value("d")))) |
| 91 | + ); |
| 92 | + } |
| 93 | + } |
| 94 | + } |
| 95 | +} |
0 commit comments