forked from open-feature/dotnet-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOpenFeatureTests.cs
246 lines (189 loc) · 10.8 KB
/
OpenFeatureTests.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
using System.Linq;
using System.Threading.Tasks;
using FluentAssertions;
using NSubstitute;
using OpenFeature.Constant;
using OpenFeature.Model;
using OpenFeature.Tests.Internal;
using Xunit;
namespace OpenFeature.Tests
{
public class OpenFeatureTests : ClearOpenFeatureInstanceFixture
{
[Fact]
[Specification("1.1.1", "The `API`, and any state it maintains SHOULD exist as a global singleton, even in cases wherein multiple versions of the `API` are present at runtime.")]
public void OpenFeature_Should_Be_Singleton()
{
var openFeature = Api.Instance;
var openFeature2 = Api.Instance;
openFeature.Should().BeSameAs(openFeature2);
}
[Fact]
[Specification("1.1.2.2", "The provider mutator function MUST invoke the initialize function on the newly registered provider before using it to resolve flag values.")]
public async Task OpenFeature_Should_Initialize_Provider()
{
var providerMockDefault = Substitute.For<FeatureProvider>();
providerMockDefault.GetStatus().Returns(ProviderStatus.NotReady);
await Api.Instance.SetProvider(providerMockDefault).ConfigureAwait(false);
await providerMockDefault.Received(1).Initialize(Api.Instance.GetContext()).ConfigureAwait(false);
var providerMockNamed = Substitute.For<FeatureProvider>();
providerMockNamed.GetStatus().Returns(ProviderStatus.NotReady);
await Api.Instance.SetProvider("the-name", providerMockNamed).ConfigureAwait(false);
await providerMockNamed.Received(1).Initialize(Api.Instance.GetContext()).ConfigureAwait(false);
}
[Fact]
[Specification("1.1.2.3",
"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.")]
public async Task OpenFeature_Should_Shutdown_Unused_Provider()
{
var providerA = Substitute.For<FeatureProvider>();
providerA.GetStatus().Returns(ProviderStatus.NotReady);
await Api.Instance.SetProvider(providerA).ConfigureAwait(false);
await providerA.Received(1).Initialize(Api.Instance.GetContext()).ConfigureAwait(false);
var providerB = Substitute.For<FeatureProvider>();
providerB.GetStatus().Returns(ProviderStatus.NotReady);
await Api.Instance.SetProvider(providerB).ConfigureAwait(false);
await providerB.Received(1).Initialize(Api.Instance.GetContext()).ConfigureAwait(false);
await providerA.Received(1).Shutdown().ConfigureAwait(false);
var providerC = Substitute.For<FeatureProvider>();
providerC.GetStatus().Returns(ProviderStatus.NotReady);
await Api.Instance.SetProvider("named", providerC).ConfigureAwait(false);
await providerC.Received(1).Initialize(Api.Instance.GetContext()).ConfigureAwait(false);
var providerD = Substitute.For<FeatureProvider>();
providerD.GetStatus().Returns(ProviderStatus.NotReady);
await Api.Instance.SetProvider("named", providerD).ConfigureAwait(false);
await providerD.Received(1).Initialize(Api.Instance.GetContext()).ConfigureAwait(false);
await providerC.Received(1).Shutdown().ConfigureAwait(false);
}
[Fact]
[Specification("1.6.1", "The API MUST define a mechanism to propagate a shutdown request to active providers.")]
public async Task OpenFeature_Should_Support_Shutdown()
{
var providerA = Substitute.For<FeatureProvider>();
providerA.GetStatus().Returns(ProviderStatus.NotReady);
var providerB = Substitute.For<FeatureProvider>();
providerB.GetStatus().Returns(ProviderStatus.NotReady);
await Api.Instance.SetProvider(providerA).ConfigureAwait(false);
await Api.Instance.SetProvider("named", providerB).ConfigureAwait(false);
await Api.Instance.Shutdown().ConfigureAwait(false);
await providerA.Received(1).Shutdown().ConfigureAwait(false);
await providerB.Received(1).Shutdown().ConfigureAwait(false);
}
[Fact]
[Specification("1.1.3", "The `API` MUST provide a function to bind a given `provider` to one or more client `name`s. If the client-name already has a bound provider, it is overwritten with the new mapping.")]
public void OpenFeature_Should_Not_Change_Named_Providers_When_Setting_Default_Provider()
{
var openFeature = Api.Instance;
openFeature.SetProvider(new NoOpFeatureProvider());
openFeature.SetProvider(TestProvider.DefaultName, new TestProvider());
var defaultClient = openFeature.GetProviderMetadata();
var namedClient = openFeature.GetProviderMetadata(TestProvider.DefaultName);
defaultClient.Name.Should().Be(NoOpProvider.NoOpProviderName);
namedClient.Name.Should().Be(TestProvider.DefaultName);
}
[Fact]
[Specification("1.1.3", "The `API` MUST provide a function to bind a given `provider` to one or more client `name`s. If the client-name already has a bound provider, it is overwritten with the new mapping.")]
public void OpenFeature_Should_Set_Default_Provide_When_No_Name_Provided()
{
var openFeature = Api.Instance;
openFeature.SetProvider(new TestProvider());
var defaultClient = openFeature.GetProviderMetadata();
defaultClient.Name.Should().Be(TestProvider.DefaultName);
}
[Fact]
[Specification("1.1.3", "The `API` MUST provide a function to bind a given `provider` to one or more client `name`s. If the client-name already has a bound provider, it is overwritten with the new mapping.")]
public void OpenFeature_Should_Assign_Provider_To_Existing_Client()
{
const string name = "new-client";
var openFeature = Api.Instance;
openFeature.SetProvider(name, new TestProvider());
openFeature.SetProvider(name, new NoOpFeatureProvider());
openFeature.GetProviderMetadata(name).Name.Should().Be(NoOpProvider.NoOpProviderName);
}
[Fact]
[Specification("1.1.3", "The `API` MUST provide a function to bind a given `provider` to one or more client `name`s. If the client-name already has a bound provider, it is overwritten with the new mapping.")]
public void OpenFeature_Should_Allow_Multiple_Client_Names_Of_Same_Instance()
{
var openFeature = Api.Instance;
var provider = new TestProvider();
openFeature.SetProvider("a", provider);
openFeature.SetProvider("b", provider);
var clientA = openFeature.GetProvider("a");
var clientB = openFeature.GetProvider("b");
clientA.Should().Be(clientB);
}
[Fact]
[Specification("1.1.4", "The `API` MUST provide a function to add `hooks` which accepts one or more API-conformant `hooks`, and appends them to the collection of any previously added hooks. When new hooks are added, previously added hooks are not removed.")]
public void OpenFeature_Should_Add_Hooks()
{
var openFeature = Api.Instance;
var hook1 = Substitute.For<Hook>();
var hook2 = Substitute.For<Hook>();
var hook3 = Substitute.For<Hook>();
var hook4 = Substitute.For<Hook>();
openFeature.ClearHooks();
openFeature.AddHooks(hook1);
openFeature.GetHooks().Should().Contain(hook1);
Assert.Single(openFeature.GetHooks());
openFeature.AddHooks(hook2);
openFeature.GetHooks().Should().ContainInOrder(hook1, hook2);
openFeature.GetHooks().Count().Should().Be(2);
openFeature.AddHooks(new[] { hook3, hook4 });
openFeature.GetHooks().Should().ContainInOrder(hook1, hook2, hook3, hook4);
openFeature.GetHooks().Count().Should().Be(4);
openFeature.ClearHooks();
Assert.Empty(openFeature.GetHooks());
}
[Fact]
[Specification("1.1.5", "The API MUST provide a function for retrieving the metadata field of the configured `provider`.")]
public void OpenFeature_Should_Get_Metadata()
{
Api.Instance.SetProvider(new NoOpFeatureProvider()).Wait();
var openFeature = Api.Instance;
var metadata = openFeature.GetProviderMetadata();
metadata.Should().NotBeNull();
metadata.Name.Should().Be(NoOpProvider.NoOpProviderName);
}
[Theory]
[InlineData("client1", "version1")]
[InlineData("client2", null)]
[InlineData(null, null)]
[Specification("1.1.6", "The `API` MUST provide a function for creating a `client` which accepts the following options: - name (optional): A logical string identifier for the client.")]
public void OpenFeature_Should_Create_Client(string name = null, string version = null)
{
var openFeature = Api.Instance;
var client = openFeature.GetClient(name, version);
client.Should().NotBeNull();
client.GetMetadata().Name.Should().Be(name);
client.GetMetadata().Version.Should().Be(version);
}
[Fact]
public void Should_Set_Given_Context()
{
var context = EvaluationContext.Empty;
Api.Instance.SetContext(context);
Api.Instance.GetContext().Should().BeSameAs(context);
context = EvaluationContext.Builder().Build();
Api.Instance.SetContext(context);
Api.Instance.GetContext().Should().BeSameAs(context);
}
[Fact]
public void Should_Always_Have_Provider()
{
Api.Instance.GetProvider().Should().NotBeNull();
}
[Fact]
public void OpenFeature_Should_Allow_Multiple_Client_Mapping()
{
var openFeature = Api.Instance;
openFeature.SetProvider("client1", new TestProvider());
openFeature.SetProvider("client2", new NoOpFeatureProvider());
var client1 = openFeature.GetClient("client1");
var client2 = openFeature.GetClient("client2");
client1.GetMetadata().Name.Should().Be("client1");
client2.GetMetadata().Name.Should().Be("client2");
client1.GetBooleanValue("test", false).Result.Should().BeTrue();
client2.GetBooleanValue("test", false).Result.Should().BeFalse();
}
}
}