Skip to content
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

feat: synchronize initialization and shutdown #635

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
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public class FlagdProvider extends EventProvider implements FeatureProvider {
private final ReadWriteLock lock = new ReentrantReadWriteLock();
private final Resolver flagResolver;
private ProviderState state = ProviderState.NOT_READY;
private boolean initialized = false;

private EvaluationContext evaluationContext;

Expand Down Expand Up @@ -63,15 +64,25 @@ public FlagdProvider(final FlagdOptions options) {
}

@Override
public void initialize(EvaluationContext evaluationContext) throws Exception {
public synchronized void initialize(EvaluationContext evaluationContext) throws Exception {
if (this.initialized) {
return;
}

this.evaluationContext = evaluationContext;
this.flagResolver.init();
this.initialized = true;
}

@Override
public void shutdown() {
public synchronized void shutdown() {
if (!initialized) {
return;
}

try {
this.flagResolver.shutdown();
this.initialized = false;
} catch (Exception e) {
log.error("Error during shutdown {}", FLAGD_PROVIDER, e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import dev.openfeature.contrib.providers.flagd.resolver.grpc.GrpcConnector;
import dev.openfeature.contrib.providers.flagd.resolver.grpc.GrpcResolver;
import dev.openfeature.contrib.providers.flagd.resolver.grpc.cache.Cache;
import dev.openfeature.flagd.grpc.Schema.EventStreamResponse;
import dev.openfeature.flagd.grpc.Schema.ResolveBooleanRequest;
import dev.openfeature.flagd.grpc.Schema.ResolveBooleanResponse;
import dev.openfeature.flagd.grpc.Schema.ResolveFloatResponse;
Expand All @@ -15,6 +14,7 @@
import dev.openfeature.flagd.grpc.ServiceGrpc;
import dev.openfeature.flagd.grpc.ServiceGrpc.ServiceBlockingStub;
import dev.openfeature.flagd.grpc.ServiceGrpc.ServiceStub;
import dev.openfeature.sdk.EvaluationContext;
import dev.openfeature.sdk.FlagEvaluationDetails;
import dev.openfeature.sdk.ImmutableContext;
import dev.openfeature.sdk.ImmutableMetadata;
Expand Down Expand Up @@ -50,6 +50,7 @@
import static org.mockito.ArgumentMatchers.argThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.mockStatic;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

Expand Down Expand Up @@ -788,7 +789,35 @@ void contextMerging() throws Exception {
ctx -> ctx.asMap().entrySet().containsAll(expectedCtx.entrySet())));
}

// test utils
@Test
void initializationAndShutdown() throws Exception{
// given
final FlagdProvider provider = new FlagdProvider();
final EvaluationContext ctx = new ImmutableContext();

final Resolver resolverMock = mock(Resolver.class);

Field flagResolver = FlagdProvider.class.getDeclaredField("flagResolver");
flagResolver.setAccessible(true);
flagResolver.set(provider, resolverMock);

// when

// validate multiple initialization
provider.initialize(ctx);
provider.initialize(ctx);

// validate multiple shutdowns
provider.shutdown();
provider.shutdown();

// then
verify(resolverMock, times(1)).init();
verify(resolverMock, times(1)).shutdown();
}


// test helper

// create provider with given grpc connector
private FlagdProvider createProvider(GrpcConnector grpc) {
Expand Down