Skip to content

Commit

Permalink
fix: provider status incorrect (#187)
Browse files Browse the repository at this point in the history
Signed-off-by: Todd Baert <todd.baert@dynatrace.com>
  • Loading branch information
toddbaert authored Apr 24, 2024
1 parent 65461c1 commit 6108d45
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 4 deletions.
10 changes: 6 additions & 4 deletions src/OpenFeature.Contrib.Providers.Flagd/FlagdProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public sealed class FlagdProvider : FeatureProvider
{
const string ProviderName = "flagd Provider";
private readonly FlagdConfig _config;
private ProviderStatus _status;
private ProviderStatus _status = ProviderStatus.NotReady;
private readonly Metadata _providerMetadata = new Metadata(ProviderName);

private readonly Resolver.Resolver _resolver;
Expand Down Expand Up @@ -118,11 +118,13 @@ public override Task Initialize(EvaluationContext context)
{
await _resolver.Init();
_status = ProviderStatus.Ready;
}).ContinueWith((t) =>
{
_status = ProviderStatus.Error;
if (t.IsFaulted) throw t.Exception;
if (t.IsFaulted)
{
_status = ProviderStatus.Error;
throw t.Exception;
};
});
}

Expand Down
23 changes: 23 additions & 0 deletions test/OpenFeature.Contrib.Providers.Flagd.Test/FlagdProviderTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Google.Protobuf.WellKnownTypes;
using Grpc.Core;
using NSubstitute;
using NSubstitute.ExceptionExtensions;
using NSubstitute.ReceivedExtensions;
using OpenFeature.Constant;
using OpenFeature.Contrib.Providers.Flagd.Resolver.InProcess;
Expand Down Expand Up @@ -525,6 +526,28 @@ public async Task TestCacheAsync()
mockGrpcClient.Received(Quantity.AtLeastOne()).EventStream(Arg.Any<EventStreamRequest>(), null, null, CancellationToken.None);
}

[Fact]
public async Task TestResolverInit_Success_Ready()
{
var mockResolver = Substitute.For<Resolver.Resolver>();
mockResolver.Init().Returns(Task.CompletedTask);
var provider = new FlagdProvider(resolver: mockResolver);
await provider.Initialize(EvaluationContext.Empty);

Assert.Equal(ProviderStatus.Ready, provider.GetStatus());
}

[Fact]
public async Task TestResolverInit_Failure_Error()
{
var mockResolver = Substitute.For<Resolver.Resolver>();
mockResolver.Init().ThrowsAsync(new Exception("fake exception"));
var provider = new FlagdProvider(resolver: mockResolver);
await Assert.ThrowsAsync<AggregateException>(() => provider.Initialize(EvaluationContext.Empty));

Assert.Equal(ProviderStatus.Error, provider.GetStatus());
}

[Fact]
public async Task TestCacheHitAsync()
{
Expand Down

0 comments on commit 6108d45

Please sign in to comment.