Skip to content

Commit

Permalink
Adding example for global hook.
Browse files Browse the repository at this point in the history
Signed-off-by: André Silva <[email protected]>
  • Loading branch information
askpt committed May 15, 2024
1 parent 8cd3710 commit a9ec44b
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 12 deletions.
35 changes: 35 additions & 0 deletions test/OpenFeature.Extensions.Hosting.Tests/HostingTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,41 @@ public async Task Can_register_some_feature_provider()
Assert.Empty(app.Services.GetServices<Hook>());
Assert.NotEmpty(app.Services.GetServices<FeatureProvider>());

#pragma warning disable xUnit1030
await app.StopAsync().ConfigureAwait(false);
#pragma warning restore xUnit1030
}

[Fact]
public async Task Can_register_some_feature_provider_and_global_hook()
{
var builder = Host.CreateApplicationBuilder();

builder.Services.AddOpenFeature(b =>
{
b.AddSomeFeatureProvider();
b.AddSomeHook();
});

using var app = builder.Build();

Assert.Equal(Api.Instance, app.Services.GetRequiredService<Api>());
Assert.Equal("No-op Provider", app.Services.GetRequiredService<Api>().GetProviderMetadata().Name);

#pragma warning disable xUnit1030
await app.StartAsync().ConfigureAwait(false);
#pragma warning restore xUnit1030

Assert.Equal(Api.Instance, app.Services.GetRequiredService<Api>());
Assert.Equal(SomeFeatureProvider.Name, app.Services.GetRequiredService<Api>().GetProviderMetadata().Name);
Assert.Equal(SomeFeatureProvider.Name, app.Services.GetRequiredService<IFeatureClient>().GetMetadata().Name);
Assert.NotEmpty(app.Services.GetServices<Hook>());

Assert.Empty(Api.Instance.GetContext().AsDictionary());
Assert.Empty(app.Services.GetRequiredService<EvaluationContextBuilder>().Build().AsDictionary());
Assert.Empty(app.Services.GetServices<EvaluationContext>());
Assert.NotEmpty(app.Services.GetServices<FeatureProvider>());

#pragma warning disable xUnit1030
await app.StopAsync().ConfigureAwait(false);
#pragma warning restore xUnit1030
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
using System;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;

namespace OpenFeature.Extensions.Hosting.Tests.TestingModels;

public class SomeHook : Hook
{

}
public class SomeHook : Hook;

public static class SomeHookExtensions
{
Expand All @@ -18,14 +14,8 @@ public static OpenFeatureBuilder AddSomeHook(this OpenFeatureBuilder builder)
throw new ArgumentNullException(nameof(builder));
}

builder.ServiceCollection.TryAddEnumerable(ServiceDescriptor.Singleton<FeatureProvider, SomeFeatureProvider>());
builder.TryAddOpenFeatureClient(SomeFeatureProvider.Name);
builder.ServiceCollection.TryAddSingleton<Hook, SomeHook>();

return builder;
}

public static void AddSomeFeatureProvider(this OpenFeatureBuilder builder, Action<OpenFeatureBuilder> configure)
{
throw new NotImplementedException();
}
}

0 comments on commit a9ec44b

Please sign in to comment.