Skip to content

fix: ability to set provider after shutdown #556

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Aug 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/main/java/dev/openfeature/sdk/EventSupport.java
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,11 @@ public void runHandler(Consumer<EventDetails> handler, EventDetails eventDetails
* Stop the event handler task executor.
*/
public void shutdown() {
taskExecutor.shutdown();
try {
taskExecutor.shutdown();
} catch (Exception e) {
log.warn("Exception while attempting to shutdown task executor", e);
}
}

// Handler store maintains a set of handlers for each event type.
Expand Down
31 changes: 17 additions & 14 deletions src/main/java/dev/openfeature/sdk/OpenFeatureAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,15 @@
public class OpenFeatureAPI implements EventBus<OpenFeatureAPI> {
// package-private multi-read/single-write lock
static AutoCloseableReentrantReadWriteLock lock = new AutoCloseableReentrantReadWriteLock();
private EvaluationContext evaluationContext;
private final List<Hook> apiHooks;
private ProviderRepository providerRepository = new ProviderRepository();
private EventSupport eventSupport = new EventSupport();
private ProviderRepository providerRepository;
private EventSupport eventSupport;
private EvaluationContext evaluationContext;

protected OpenFeatureAPI() {
apiHooks = new ArrayList<>();
providerRepository = new ProviderRepository();
eventSupport = new EventSupport();
}

private static class SingletonHolder {
Expand Down Expand Up @@ -190,9 +192,19 @@ public void clearHooks() {
}
}

/**
* Shut down and reset the current status of OpenFeature API.
* This call cleans up all active providers and attempts to shut down internal event handling mechanisms.
* Once shut down is complete, API is reset and ready to use again.
* */
public void shutdown() {
providerRepository.shutdown();
eventSupport.shutdown();
try (AutoCloseableLock __ = lock.writeLockAutoCloseable()) {
providerRepository.shutdown();
eventSupport.shutdown();

providerRepository = new ProviderRepository();
eventSupport = new EventSupport();
}
}

/**
Expand Down Expand Up @@ -264,15 +276,6 @@ void addHandler(String clientName, ProviderEvent event, Consumer<EventDetails> h
}
}

/**
* This method is only here for testing as otherwise all tests after the API
* shutdown test would fail.
*/
final void reset() {
providerRepository = new ProviderRepository();
eventSupport = new EventSupport();
}

/**
* Runs the handlers associated with a particular provider.
*
Expand Down
17 changes: 15 additions & 2 deletions src/test/java/dev/openfeature/sdk/ShutdownBehaviorSpecTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,22 @@ void mustShutdownAllProvidersOnShuttingDownApi() {
verify(defaultProvider).shutdown();
verify(namedProvider).shutdown();
});

api.reset();
}
}


@Test
@DisplayName("once shutdown is complete, api must be ready to use again")
void apiIsReadyToUseAfterShutdown() {
final OpenFeatureAPI openFeatureAPI = OpenFeatureAPI.getInstance();

NoOpProvider p1 = new NoOpProvider();
openFeatureAPI.setProvider(p1);

openFeatureAPI.shutdown();

NoOpProvider p2 = new NoOpProvider();
openFeatureAPI.setProvider(p2);
}
}
}