Skip to content

Commit 366e1fe

Browse files
committed
Add CancellationToken optional parameter to SetProviderAsync
Signed-off-by: Kyle Julian <[email protected]>
1 parent 10a43c9 commit 366e1fe

File tree

2 files changed

+27
-6
lines changed

2 files changed

+27
-6
lines changed

src/OpenFeature/Api.cs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,13 @@ internal Api() { }
3838
/// Sets the default feature provider. In order to wait for the provider to be set, and initialization to complete,
3939
/// await the returned task.
4040
/// </summary>
41-
/// <remarks>The provider cannot be set to null. Attempting to set the provider to null has no effect. May throw an exception if <paramref name="featureProvider"/> cannot be initialized.</remarks>
4241
/// <param name="featureProvider">Implementation of <see cref="FeatureProvider"/></param>
43-
public async Task SetProviderAsync(FeatureProvider featureProvider)
42+
/// <param name="cancellationToken">Propagates notification that the provider initialization should be canceled.</param>
43+
/// <returns>A <see cref="Task"/> that completes once Provider initialization is complete.</returns>
44+
public async Task SetProviderAsync(FeatureProvider featureProvider, CancellationToken cancellationToken = default)
4445
{
4546
this._eventExecutor.RegisterDefaultFeatureProvider(featureProvider);
46-
await this._repository.SetProviderAsync(featureProvider, this.GetContext(), this.AfterInitialization, this.AfterError).ConfigureAwait(false);
47-
47+
await this._repository.SetProviderAsync(featureProvider, this.GetContext(), this.AfterInitialization, this.AfterError, cancellationToken).ConfigureAwait(false);
4848
}
4949

5050
/// <summary>
@@ -54,15 +54,17 @@ public async Task SetProviderAsync(FeatureProvider featureProvider)
5454
/// <remarks>The provider cannot be set to null. Attempting to set the provider to null has no effect. May throw an exception if <paramref name="featureProvider"/> cannot be initialized.</remarks>
5555
/// <param name="domain">An identifier which logically binds clients with providers</param>
5656
/// <param name="featureProvider">Implementation of <see cref="FeatureProvider"/></param>
57+
/// <param name="cancellationToken">Propagates notification that the provider initialization should be canceled.</param>
5758
/// <exception cref="ArgumentNullException">domain cannot be null or empty</exception>
58-
public async Task SetProviderAsync(string domain, FeatureProvider featureProvider)
59+
/// <returns>A <see cref="Task"/> that completes once Provider initialization is complete.</returns>
60+
public async Task SetProviderAsync(string domain, FeatureProvider featureProvider, CancellationToken cancellationToken = default)
5961
{
6062
if (string.IsNullOrWhiteSpace(domain))
6163
{
6264
throw new ArgumentNullException(nameof(domain));
6365
}
6466
this._eventExecutor.RegisterClientFeatureProvider(domain, featureProvider);
65-
await this._repository.SetProviderAsync(domain, featureProvider, this.GetContext(), this.AfterInitialization, this.AfterError).ConfigureAwait(false);
67+
await this._repository.SetProviderAsync(domain, featureProvider, this.GetContext(), this.AfterInitialization, this.AfterError, cancellationToken).ConfigureAwait(false);
6668
}
6769

6870
/// <summary>

test/OpenFeature.Tests/OpenFeatureTests.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,25 @@ public async Task OpenFeature_Should_Initialize_Provider()
3434
await providerMockNamed.Received(1).InitializeAsync(Api.Instance.GetContext());
3535
}
3636

37+
[Fact]
38+
public async Task OpenFeature_Should_Initialize_Provider_WithCancellationToken()
39+
{
40+
var providerMockDefault = Substitute.For<FeatureProvider>();
41+
providerMockDefault.Status.Returns(ProviderStatus.NotReady);
42+
43+
using var cancellationTokenSource = new CancellationTokenSource();
44+
var cancellationToken = cancellationTokenSource.Token;
45+
46+
await Api.Instance.SetProviderAsync(providerMockDefault, cancellationToken);
47+
await providerMockDefault.Received(1).InitializeAsync(Api.Instance.GetContext(), cancellationToken);
48+
49+
var providerMockNamed = Substitute.For<FeatureProvider>();
50+
providerMockNamed.Status.Returns(ProviderStatus.NotReady);
51+
52+
await Api.Instance.SetProviderAsync("the-name", providerMockNamed, cancellationToken);
53+
await providerMockNamed.Received(1).InitializeAsync(Api.Instance.GetContext(), cancellationToken);
54+
}
55+
3756
[Fact]
3857
[Specification("1.1.2.3",
3958
"The provider mutator function MUST invoke the shutdown function on the previously registered provider once it's no longer being used to resolve flag values.")]

0 commit comments

Comments
 (0)