Skip to content

Commit fb42a92

Browse files
fix: ability to set provider after shutdown (#556)
* fix shutdown Signed-off-by: Kavindu Dodanduwa <[email protected]> * Grammer fix for code comment Signed-off-by: Kavindu Dodanduwa <[email protected]> --------- Signed-off-by: Kavindu Dodanduwa <[email protected]> Signed-off-by: Kavindu Dodanduwa <[email protected]>
1 parent a6eabc3 commit fb42a92

File tree

3 files changed

+37
-17
lines changed

3 files changed

+37
-17
lines changed

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,11 @@ public void runHandler(Consumer<EventDetails> handler, EventDetails eventDetails
145145
* Stop the event handler task executor.
146146
*/
147147
public void shutdown() {
148-
taskExecutor.shutdown();
148+
try {
149+
taskExecutor.shutdown();
150+
} catch (Exception e) {
151+
log.warn("Exception while attempting to shutdown task executor", e);
152+
}
149153
}
150154

151155
// Handler store maintains a set of handlers for each event type.

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

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,15 @@
2121
public class OpenFeatureAPI implements EventBus<OpenFeatureAPI> {
2222
// package-private multi-read/single-write lock
2323
static AutoCloseableReentrantReadWriteLock lock = new AutoCloseableReentrantReadWriteLock();
24-
private EvaluationContext evaluationContext;
2524
private final List<Hook> apiHooks;
26-
private ProviderRepository providerRepository = new ProviderRepository();
27-
private EventSupport eventSupport = new EventSupport();
25+
private ProviderRepository providerRepository;
26+
private EventSupport eventSupport;
27+
private EvaluationContext evaluationContext;
2828

2929
protected OpenFeatureAPI() {
3030
apiHooks = new ArrayList<>();
31+
providerRepository = new ProviderRepository();
32+
eventSupport = new EventSupport();
3133
}
3234

3335
private static class SingletonHolder {
@@ -190,9 +192,19 @@ public void clearHooks() {
190192
}
191193
}
192194

195+
/**
196+
* Shut down and reset the current status of OpenFeature API.
197+
* This call cleans up all active providers and attempts to shut down internal event handling mechanisms.
198+
* Once shut down is complete, API is reset and ready to use again.
199+
* */
193200
public void shutdown() {
194-
providerRepository.shutdown();
195-
eventSupport.shutdown();
201+
try (AutoCloseableLock __ = lock.writeLockAutoCloseable()) {
202+
providerRepository.shutdown();
203+
eventSupport.shutdown();
204+
205+
providerRepository = new ProviderRepository();
206+
eventSupport = new EventSupport();
207+
}
196208
}
197209

198210
/**
@@ -264,15 +276,6 @@ void addHandler(String clientName, ProviderEvent event, Consumer<EventDetails> h
264276
}
265277
}
266278

267-
/**
268-
* This method is only here for testing as otherwise all tests after the API
269-
* shutdown test would fail.
270-
*/
271-
final void reset() {
272-
providerRepository = new ProviderRepository();
273-
eventSupport = new EventSupport();
274-
}
275-
276279
/**
277280
* Runs the handlers associated with a particular provider.
278281
*

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

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,9 +109,22 @@ void mustShutdownAllProvidersOnShuttingDownApi() {
109109
verify(defaultProvider).shutdown();
110110
verify(namedProvider).shutdown();
111111
});
112-
113-
api.reset();
114112
}
115113
}
114+
115+
116+
@Test
117+
@DisplayName("once shutdown is complete, api must be ready to use again")
118+
void apiIsReadyToUseAfterShutdown() {
119+
final OpenFeatureAPI openFeatureAPI = OpenFeatureAPI.getInstance();
120+
121+
NoOpProvider p1 = new NoOpProvider();
122+
openFeatureAPI.setProvider(p1);
123+
124+
openFeatureAPI.shutdown();
125+
126+
NoOpProvider p2 = new NoOpProvider();
127+
openFeatureAPI.setProvider(p2);
128+
}
116129
}
117130
}

0 commit comments

Comments
 (0)