|
| 1 | +package dev.openfeature.sdk.vmlens; |
| 2 | + |
| 3 | +import static org.assertj.core.api.Assertions.assertThat; |
| 4 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 5 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 6 | + |
| 7 | +import com.vmlens.api.AllInterleavings; |
| 8 | +import com.vmlens.api.Runner; |
| 9 | +import dev.openfeature.sdk.ImmutableContext; |
| 10 | +import dev.openfeature.sdk.OpenFeatureAPI; |
| 11 | +import dev.openfeature.sdk.OpenFeatureAPITestUtil; |
| 12 | +import dev.openfeature.sdk.Value; |
| 13 | +import dev.openfeature.sdk.providers.memory.Flag; |
| 14 | +import dev.openfeature.sdk.providers.memory.InMemoryProvider; |
| 15 | +import java.util.HashMap; |
| 16 | +import java.util.Map; |
| 17 | +import org.junit.jupiter.api.AfterEach; |
| 18 | +import org.junit.jupiter.api.BeforeEach; |
| 19 | +import org.junit.jupiter.api.Test; |
| 20 | + |
| 21 | +class VmLensTest { |
| 22 | + final OpenFeatureAPI api = OpenFeatureAPITestUtil.createAPI(); |
| 23 | + |
| 24 | + @BeforeEach |
| 25 | + void setUp() { |
| 26 | + var flags = new HashMap<String, Flag<?>>(); |
| 27 | + flags.put("a", Flag.builder().variant("a", "def").defaultVariant("a").build()); |
| 28 | + flags.put("b", Flag.builder().variant("a", "as").defaultVariant("a").build()); |
| 29 | + api.setProviderAndWait(new InMemoryProvider(flags)); |
| 30 | + } |
| 31 | + |
| 32 | + @AfterEach |
| 33 | + void tearDown() { |
| 34 | + api.clearHooks(); |
| 35 | + api.shutdown(); |
| 36 | + } |
| 37 | + |
| 38 | + @Test |
| 39 | + void concurrentClientCreations() { |
| 40 | + try (AllInterleavings allInterleavings = new AllInterleavings("Concurrent creations of the Client")) { |
| 41 | + while (allInterleavings.hasNext()) { |
| 42 | + Runner.runParallel(api::getClient, api::getClient); |
| 43 | + } |
| 44 | + } |
| 45 | + // keep the linter happy |
| 46 | + assertTrue(true); |
| 47 | + } |
| 48 | + |
| 49 | + @Test |
| 50 | + void concurrentFlagEvaluations() { |
| 51 | + var client = api.getClient(); |
| 52 | + try (AllInterleavings allInterleavings = new AllInterleavings("Concurrent evaluations")) { |
| 53 | + while (allInterleavings.hasNext()) { |
| 54 | + Runner.runParallel( |
| 55 | + () -> assertEquals("def", client.getStringValue("a", "a")), |
| 56 | + () -> assertEquals("as", client.getStringValue("b", "b"))); |
| 57 | + } |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + @Test |
| 62 | + void concurrentContextSetting() { |
| 63 | + var client = api.getClient(); |
| 64 | + var contextA = new ImmutableContext(Map.of("a", new Value("b"))); |
| 65 | + var contextB = new ImmutableContext(Map.of("c", new Value("d"))); |
| 66 | + try (AllInterleavings allInterleavings = |
| 67 | + new AllInterleavings("Concurrently setting the context and evaluating a flag")) { |
| 68 | + while (allInterleavings.hasNext()) { |
| 69 | + Runner.runParallel( |
| 70 | + () -> assertEquals("def", client.getStringValue("a", "a")), |
| 71 | + () -> client.setEvaluationContext(contextA), |
| 72 | + () -> client.setEvaluationContext(contextB)); |
| 73 | + assertThat(client.getEvaluationContext()).isIn(contextA, contextB); |
| 74 | + } |
| 75 | + } |
| 76 | + } |
| 77 | +} |
0 commit comments