fix: ability to set provider after shutdown (#556)
* fix shutdown Signed-off-by: Kavindu Dodanduwa <kavindudodanduwa@gmail.com> * Grammer fix for code comment Signed-off-by: Kavindu Dodanduwa <Kavindu-Dodan@users.noreply.github.com> --------- Signed-off-by: Kavindu Dodanduwa <kavindudodanduwa@gmail.com> Signed-off-by: Kavindu Dodanduwa <Kavindu-Dodan@users.noreply.github.com>
This commit is contained in:
parent
a6eabc391d
commit
fb42a92e9b
|
|
@ -145,7 +145,11 @@ class EventSupport {
|
||||||
* Stop the event handler task executor.
|
* Stop the event handler task executor.
|
||||||
*/
|
*/
|
||||||
public void shutdown() {
|
public void shutdown() {
|
||||||
|
try {
|
||||||
taskExecutor.shutdown();
|
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.
|
// Handler store maintains a set of handlers for each event type.
|
||||||
|
|
|
||||||
|
|
@ -21,13 +21,15 @@ import lombok.extern.slf4j.Slf4j;
|
||||||
public class OpenFeatureAPI implements EventBus<OpenFeatureAPI> {
|
public class OpenFeatureAPI implements EventBus<OpenFeatureAPI> {
|
||||||
// package-private multi-read/single-write lock
|
// package-private multi-read/single-write lock
|
||||||
static AutoCloseableReentrantReadWriteLock lock = new AutoCloseableReentrantReadWriteLock();
|
static AutoCloseableReentrantReadWriteLock lock = new AutoCloseableReentrantReadWriteLock();
|
||||||
private EvaluationContext evaluationContext;
|
|
||||||
private final List<Hook> apiHooks;
|
private final List<Hook> apiHooks;
|
||||||
private ProviderRepository providerRepository = new ProviderRepository();
|
private ProviderRepository providerRepository;
|
||||||
private EventSupport eventSupport = new EventSupport();
|
private EventSupport eventSupport;
|
||||||
|
private EvaluationContext evaluationContext;
|
||||||
|
|
||||||
protected OpenFeatureAPI() {
|
protected OpenFeatureAPI() {
|
||||||
apiHooks = new ArrayList<>();
|
apiHooks = new ArrayList<>();
|
||||||
|
providerRepository = new ProviderRepository();
|
||||||
|
eventSupport = new EventSupport();
|
||||||
}
|
}
|
||||||
|
|
||||||
private static class SingletonHolder {
|
private static class SingletonHolder {
|
||||||
|
|
@ -190,9 +192,19 @@ public class OpenFeatureAPI implements EventBus<OpenFeatureAPI> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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() {
|
public void shutdown() {
|
||||||
|
try (AutoCloseableLock __ = lock.writeLockAutoCloseable()) {
|
||||||
providerRepository.shutdown();
|
providerRepository.shutdown();
|
||||||
eventSupport.shutdown();
|
eventSupport.shutdown();
|
||||||
|
|
||||||
|
providerRepository = new ProviderRepository();
|
||||||
|
eventSupport = new EventSupport();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -264,15 +276,6 @@ public class OpenFeatureAPI implements EventBus<OpenFeatureAPI> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* 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.
|
* Runs the handlers associated with a particular provider.
|
||||||
*
|
*
|
||||||
|
|
|
||||||
|
|
@ -109,9 +109,22 @@ class ShutdownBehaviorSpecTest {
|
||||||
verify(defaultProvider).shutdown();
|
verify(defaultProvider).shutdown();
|
||||||
verify(namedProvider).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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue