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

fix: provider status incorrect #187

Merged
merged 4 commits into from
Apr 24, 2024
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
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;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isn't a change, it just makes things explicit.

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;
};
Comment on lines +123 to +127
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actual bug.

});
}

Expand Down
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
Loading