Skip to content

Commit fb2181f

Browse files
authored
fix: shutdown now clears hooks, evaluationContext and transactionContextPropagator as per spec 1.6.2 (#1936)
Signed-off-by: jarebudev <23311805+jarebudev@users.noreply.github.com>
1 parent 51754a5 commit fb2181f

2 files changed

Lines changed: 28 additions & 6 deletions

File tree

src/main/java/dev/openfeature/sdk/OpenFeatureAPI.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,9 @@ public void shutdown() {
355355
try (AutoCloseableLock ignored = lock.writeLockAutoCloseable()) {
356356
providerRepository = new ProviderRepository(this);
357357
eventSupport = new EventSupport();
358+
clearHooks();
359+
setEvaluationContext(ImmutableContext.EMPTY);
360+
setTransactionContextPropagator(new NoOpTransactionContextPropagator());
358361
}
359362
}
360363
}

src/test/java/dev/openfeature/sdk/ShutdownBehaviorSpecTest.java

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package dev.openfeature.sdk;
22

3+
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
4+
import static org.junit.jupiter.api.Assertions.assertNotEquals;
5+
import static org.junit.jupiter.api.Assertions.assertTrue;
36
import static org.mockito.Mockito.*;
47

58
import dev.openfeature.sdk.fixtures.ProviderFixture;
@@ -131,16 +134,32 @@ void mustShutdownAllProvidersOnShuttingDownApi() {
131134
}
132135

133136
@Test
134-
@DisplayName("once shutdown is complete, api must be ready to use again")
135-
void apiIsReadyToUseAfterShutdown() {
137+
@Specification(
138+
number = "1.6.2",
139+
text =
140+
"The API's `shutdown` function MUST reset all state of the API, removing all hooks, event handlers, evaluation context, transaction context propagators, and providers.")
141+
@DisplayName("shutdown must reset the state of the API")
142+
void apiStateMustBeResetOnShuttingDownApi() {
136143

137-
NoOpProvider p1 = new NoOpProvider();
138-
api.setProvider(p1);
144+
FeatureProvider provider = ProviderFixture.createMockedProvider();
145+
TransactionContextPropagator transactionContextPropagator = mock(TransactionContextPropagator.class);
146+
EvaluationContext evaluationContext = mock(EvaluationContext.class);
147+
148+
api.addHooks(mock(Hook.class));
149+
api.setProvider(provider);
150+
api.setEvaluationContext(evaluationContext);
151+
api.setTransactionContextPropagator(transactionContextPropagator);
139152

140153
api.shutdown();
141154

142-
NoOpProvider p2 = new NoOpProvider();
143-
api.setProvider(p2);
155+
assertNotEquals(provider, api.getProvider());
156+
assertTrue(api.getHooks().isEmpty());
157+
assertNotEquals(evaluationContext, api.getEvaluationContext());
158+
assertNotEquals(transactionContextPropagator, api.getTransactionContextPropagator());
159+
160+
assertInstanceOf(NoOpProvider.class, api.getProvider());
161+
assertInstanceOf(ImmutableContext.class, api.getEvaluationContext());
162+
assertInstanceOf(NoOpTransactionContextPropagator.class, api.getTransactionContextPropagator());
144163
}
145164

146165
@Test

0 commit comments

Comments
 (0)