Skip to content

Commit

Permalink
feat: synchronize initialization and shutdown (#635)
Browse files Browse the repository at this point in the history
Signed-off-by: Kavindu Dodanduwa <kavindudodanduwa@gmail.com>
  • Loading branch information
Kavindu-Dodan committed Jan 17, 2024
1 parent 4f6c150 commit 2d98cb8
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 4 deletions.
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

0 comments on commit 2d98cb8

Please sign in to comment.