|
| 1 | +package dev.openfeature.sdk.isolated; |
| 2 | + |
| 3 | +import static org.assertj.core.api.Assertions.assertThat; |
| 4 | + |
| 5 | +import dev.openfeature.sdk.FeatureProvider; |
| 6 | +import dev.openfeature.sdk.ImmutableContext; |
| 7 | +import dev.openfeature.sdk.NoOpProvider; |
| 8 | +import dev.openfeature.sdk.NoOpTransactionContextPropagator; |
| 9 | +import dev.openfeature.sdk.OpenFeatureAPI; |
| 10 | +import dev.openfeature.sdk.ThreadLocalTransactionContextPropagator; |
| 11 | +import dev.openfeature.sdk.providers.memory.Flag; |
| 12 | +import dev.openfeature.sdk.providers.memory.InMemoryProvider; |
| 13 | +import java.util.Map; |
| 14 | +import java.util.concurrent.atomic.AtomicBoolean; |
| 15 | +import org.junit.jupiter.api.AfterEach; |
| 16 | +import org.junit.jupiter.api.DisplayName; |
| 17 | +import org.junit.jupiter.api.Test; |
| 18 | + |
| 19 | +class IsolatedAPITest { |
| 20 | + |
| 21 | + private final OpenFeatureAPI singleton = OpenFeatureAPI.getInstance(); |
| 22 | + |
| 23 | + @AfterEach |
| 24 | + void restoreSingleton() { |
| 25 | + singleton.shutdown(); |
| 26 | + singleton.clearHooks(); |
| 27 | + singleton.setEvaluationContext(null); |
| 28 | + singleton.setTransactionContextPropagator(new NoOpTransactionContextPropagator()); |
| 29 | + } |
| 30 | + |
| 31 | + /** |
| 32 | + * Requirement 1.8.1 — factory creates new, distinct instances that |
| 33 | + * conform to the API contract. |
| 34 | + */ |
| 35 | + @Test |
| 36 | + @DisplayName("factory creates distinct API instances") |
| 37 | + void factoryCreatesDistinctInstances() { |
| 38 | + OpenFeatureAPI api1 = OpenFeatureAPIFactory.createAPI(); |
| 39 | + OpenFeatureAPI api2 = OpenFeatureAPIFactory.createAPI(); |
| 40 | + |
| 41 | + assertThat(api1).isInstanceOf(OpenFeatureAPI.class).isNotSameAs(api2); |
| 42 | + } |
| 43 | + |
| 44 | + /** |
| 45 | + * Requirement 1.8.1 — isolated instances do not share state with |
| 46 | + * the global singleton. Singleton state is restored after the test |
| 47 | + * via {@link #restoreSingleton()}. |
| 48 | + */ |
| 49 | + @Test |
| 50 | + @DisplayName("isolated instance does not interfere with singleton") |
| 51 | + void isolatedInstanceDoesNotInterfereWithSingleton() { |
| 52 | + // record singleton baseline |
| 53 | + FeatureProvider singletonProvider = singleton.getProvider(); |
| 54 | + |
| 55 | + OpenFeatureAPI isolated = OpenFeatureAPIFactory.createAPI(); |
| 56 | + assertThat(isolated).isNotSameAs(singleton); |
| 57 | + |
| 58 | + // mutate only the isolated instance |
| 59 | + isolated.setProvider(new InMemoryProvider(Map.of())); |
| 60 | + isolated.addHooks(new NoOpHook()); |
| 61 | + isolated.setEvaluationContext(new ImmutableContext("isolated-key")); |
| 62 | + |
| 63 | + // singleton remains at baseline |
| 64 | + assertThat(singleton.getProvider()).isSameAs(singletonProvider); |
| 65 | + assertThat(singleton.getHooks()).isEmpty(); |
| 66 | + assertThat(singleton.getEvaluationContext()).isNull(); |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * Requirement 1.8.1 — providers are isolated between instances. |
| 71 | + */ |
| 72 | + @Test |
| 73 | + @DisplayName("providers are isolated between instances") |
| 74 | + void providerIsolation() { |
| 75 | + OpenFeatureAPI api1 = OpenFeatureAPIFactory.createAPI(); |
| 76 | + OpenFeatureAPI api2 = OpenFeatureAPIFactory.createAPI(); |
| 77 | + |
| 78 | + InMemoryProvider provider = new InMemoryProvider(Map.of()); |
| 79 | + api1.setProvider(provider); |
| 80 | + |
| 81 | + assertThat(api1.getProvider()).isSameAs(provider); |
| 82 | + assertThat(api2.getProvider()).isInstanceOf(NoOpProvider.class); |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * Requirement 1.8.1 — hooks are isolated between instances. |
| 87 | + */ |
| 88 | + @Test |
| 89 | + @DisplayName("hooks are isolated between instances") |
| 90 | + void hookIsolation() { |
| 91 | + OpenFeatureAPI api1 = OpenFeatureAPIFactory.createAPI(); |
| 92 | + OpenFeatureAPI api2 = OpenFeatureAPIFactory.createAPI(); |
| 93 | + |
| 94 | + api1.addHooks(new NoOpHook()); |
| 95 | + |
| 96 | + assertThat(api1.getHooks()).hasSize(1); |
| 97 | + assertThat(api2.getHooks()).isEmpty(); |
| 98 | + } |
| 99 | + |
| 100 | + /** |
| 101 | + * Requirement 1.8.1 — evaluation context is isolated between instances. |
| 102 | + */ |
| 103 | + @Test |
| 104 | + @DisplayName("evaluation context is isolated between instances") |
| 105 | + void evaluationContextIsolation() { |
| 106 | + OpenFeatureAPI api1 = OpenFeatureAPIFactory.createAPI(); |
| 107 | + OpenFeatureAPI api2 = OpenFeatureAPIFactory.createAPI(); |
| 108 | + |
| 109 | + api1.setEvaluationContext(new ImmutableContext("key-1")); |
| 110 | + api2.setEvaluationContext(new ImmutableContext("key-2")); |
| 111 | + |
| 112 | + assertThat(api1.getEvaluationContext().getTargetingKey()).isEqualTo("key-1"); |
| 113 | + assertThat(api2.getEvaluationContext().getTargetingKey()).isEqualTo("key-2"); |
| 114 | + } |
| 115 | + |
| 116 | + /** |
| 117 | + * Requirement 1.8.1 — event handlers are isolated between instances. |
| 118 | + */ |
| 119 | + @Test |
| 120 | + @DisplayName("event handlers are isolated between instances") |
| 121 | + void eventHandlerIsolation() throws Exception { |
| 122 | + OpenFeatureAPI api1 = OpenFeatureAPIFactory.createAPI(); |
| 123 | + OpenFeatureAPI api2 = OpenFeatureAPIFactory.createAPI(); |
| 124 | + |
| 125 | + AtomicBoolean api1HandlerCalled = new AtomicBoolean(false); |
| 126 | + AtomicBoolean api2HandlerCalled = new AtomicBoolean(false); |
| 127 | + |
| 128 | + api1.onProviderReady(details -> api1HandlerCalled.set(true)); |
| 129 | + api2.onProviderReady(details -> api2HandlerCalled.set(true)); |
| 130 | + |
| 131 | + // setting a provider on api1 should only trigger api1's handler |
| 132 | + api1.setProviderAndWait(new NoOpProvider()); |
| 133 | + |
| 134 | + assertThat(api1HandlerCalled.get()).isTrue(); |
| 135 | + assertThat(api2HandlerCalled.get()).isFalse(); |
| 136 | + } |
| 137 | + |
| 138 | + /** |
| 139 | + * Requirement 1.8.1 — transaction context propagators are isolated |
| 140 | + * between instances. |
| 141 | + */ |
| 142 | + @Test |
| 143 | + @DisplayName("transaction context propagator is isolated between instances") |
| 144 | + void transactionContextPropagatorIsolation() { |
| 145 | + OpenFeatureAPI api1 = OpenFeatureAPIFactory.createAPI(); |
| 146 | + OpenFeatureAPI api2 = OpenFeatureAPIFactory.createAPI(); |
| 147 | + |
| 148 | + ThreadLocalTransactionContextPropagator propagator = new ThreadLocalTransactionContextPropagator(); |
| 149 | + api1.setTransactionContextPropagator(propagator); |
| 150 | + |
| 151 | + assertThat(api1.getTransactionContextPropagator()).isSameAs(propagator); |
| 152 | + assertThat(api2.getTransactionContextPropagator()).isInstanceOf(NoOpTransactionContextPropagator.class); |
| 153 | + } |
| 154 | + |
| 155 | + /** |
| 156 | + * Requirement 1.8.2 — an isolated instance conforms to the same API |
| 157 | + * contract (provider, hooks, context, client creation, flag evaluation). |
| 158 | + */ |
| 159 | + @Test |
| 160 | + @DisplayName("isolated instance conforms to API contract") |
| 161 | + void isolatedInstanceConformsToAPIContract() { |
| 162 | + OpenFeatureAPI api = OpenFeatureAPIFactory.createAPI(); |
| 163 | + |
| 164 | + // provider management |
| 165 | + InMemoryProvider provider = new InMemoryProvider(Map.of( |
| 166 | + "flag1", Flag.builder().variant("on", true).variant("off", false).defaultVariant("on").build())); |
| 167 | + api.setProvider(provider); |
| 168 | + assertThat(api.getProvider()).isSameAs(provider); |
| 169 | + assertThat(api.getProviderMetadata()).isNotNull(); |
| 170 | + |
| 171 | + // hooks |
| 172 | + NoOpHook hook = new NoOpHook(); |
| 173 | + api.addHooks(hook); |
| 174 | + assertThat(api.getHooks()).containsExactly(hook); |
| 175 | + |
| 176 | + // context |
| 177 | + api.setEvaluationContext(new ImmutableContext("targeting-key")); |
| 178 | + assertThat(api.getEvaluationContext().getTargetingKey()).isEqualTo("targeting-key"); |
| 179 | + |
| 180 | + // client creation and flag evaluation |
| 181 | + var client = api.getClient("test-domain", "1.0"); |
| 182 | + assertThat(client.getMetadata().getDomain()).isEqualTo("test-domain"); |
| 183 | + assertThat(client.getBooleanValue("flag1", false)).isTrue(); |
| 184 | + } |
| 185 | + |
| 186 | + /** |
| 187 | + * Requirement 1.8.1 — clearHooks on one instance does not affect another. |
| 188 | + */ |
| 189 | + @Test |
| 190 | + @DisplayName("clearHooks does not affect other instances") |
| 191 | + void clearHooksDoesNotAffectOtherInstances() { |
| 192 | + OpenFeatureAPI api1 = OpenFeatureAPIFactory.createAPI(); |
| 193 | + OpenFeatureAPI api2 = OpenFeatureAPIFactory.createAPI(); |
| 194 | + |
| 195 | + NoOpHook hook = new NoOpHook(); |
| 196 | + api1.addHooks(hook); |
| 197 | + api2.addHooks(hook); |
| 198 | + |
| 199 | + api1.clearHooks(); |
| 200 | + |
| 201 | + assertThat(api1.getHooks()).isEmpty(); |
| 202 | + assertThat(api2.getHooks()).hasSize(1); |
| 203 | + } |
| 204 | + |
| 205 | + /** |
| 206 | + * Requirement 1.8.2 — clients from different isolated instances use |
| 207 | + * their own instance's provider. |
| 208 | + */ |
| 209 | + @Test |
| 210 | + @DisplayName("clients use their own instance's provider") |
| 211 | + void clientUsesItsOwnInstanceProvider() { |
| 212 | + OpenFeatureAPI api1 = OpenFeatureAPIFactory.createAPI(); |
| 213 | + OpenFeatureAPI api2 = OpenFeatureAPIFactory.createAPI(); |
| 214 | + |
| 215 | + api1.setProvider(new InMemoryProvider(Map.of( |
| 216 | + "flag1", Flag.builder().variant("on", true).variant("off", false).defaultVariant("on").build()))); |
| 217 | + |
| 218 | + var client1 = api1.getClient(); |
| 219 | + var client2 = api2.getClient(); |
| 220 | + |
| 221 | + assertThat(client1.getBooleanValue("flag1", false)).isTrue(); |
| 222 | + // api2 has NoOpProvider, so it returns the default |
| 223 | + assertThat(client2.getBooleanValue("flag1", false)).isFalse(); |
| 224 | + } |
| 225 | +} |
0 commit comments