-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathOpenFeatureTests.cs
320 lines (250 loc) · 13.3 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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
using System;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Threading.Tasks;
using NSubstitute;
using OpenFeature.Constant;
using OpenFeature.Model;
using OpenFeature.Tests.Internal;
using Xunit;
namespace OpenFeature.Tests
{
[SuppressMessage("Reliability", "CA2007:Consider calling ConfigureAwait on the awaited task")]
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;
Assert.Equal(openFeature2, openFeature);
}
[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.Status.Returns(ProviderStatus.NotReady);
await Api.Instance.SetProviderAsync(providerMockDefault);
await providerMockDefault.Received(1).InitializeAsync(Api.Instance.GetContext());
var providerMockNamed = Substitute.For<FeatureProvider>();
providerMockNamed.Status.Returns(ProviderStatus.NotReady);
await Api.Instance.SetProviderAsync("the-name", providerMockNamed);
await providerMockNamed.Received(1).InitializeAsync(Api.Instance.GetContext());
}
[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.Status.Returns(ProviderStatus.NotReady);
await Api.Instance.SetProviderAsync(providerA);
await providerA.Received(1).InitializeAsync(Api.Instance.GetContext());
var providerB = Substitute.For<FeatureProvider>();
providerB.Status.Returns(ProviderStatus.NotReady);
await Api.Instance.SetProviderAsync(providerB);
await providerB.Received(1).InitializeAsync(Api.Instance.GetContext());
await providerA.Received(1).ShutdownAsync();
var providerC = Substitute.For<FeatureProvider>();
providerC.Status.Returns(ProviderStatus.NotReady);
await Api.Instance.SetProviderAsync("named", providerC);
await providerC.Received(1).InitializeAsync(Api.Instance.GetContext());
var providerD = Substitute.For<FeatureProvider>();
providerD.Status.Returns(ProviderStatus.NotReady);
await Api.Instance.SetProviderAsync("named", providerD);
await providerD.Received(1).InitializeAsync(Api.Instance.GetContext());
await providerC.Received(1).ShutdownAsync();
}
[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.Status.Returns(ProviderStatus.NotReady);
var providerB = Substitute.For<FeatureProvider>();
providerB.Status.Returns(ProviderStatus.NotReady);
await Api.Instance.SetProviderAsync(providerA);
await Api.Instance.SetProviderAsync("named", providerB);
await Api.Instance.ShutdownAsync();
await providerA.Received(1).ShutdownAsync();
await providerB.Received(1).ShutdownAsync();
}
[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 async Task OpenFeature_Should_Not_Change_Named_Providers_When_Setting_Default_Provider()
{
var openFeature = Api.Instance;
await openFeature.SetProviderAsync(new NoOpFeatureProvider());
await openFeature.SetProviderAsync(TestProvider.DefaultName, new TestProvider());
var defaultClient = openFeature.GetProviderMetadata();
var domainScopedClient = openFeature.GetProviderMetadata(TestProvider.DefaultName);
Assert.Equal(NoOpProvider.NoOpProviderName, defaultClient?.Name);
Assert.Equal(TestProvider.DefaultName, domainScopedClient?.Name);
}
[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 async Task OpenFeature_Should_Set_Default_Provide_When_No_Name_Provided()
{
var openFeature = Api.Instance;
await openFeature.SetProviderAsync(new TestProvider());
var defaultClient = openFeature.GetProviderMetadata();
Assert.Equal(TestProvider.DefaultName, defaultClient?.Name);
}
[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 async Task OpenFeature_Should_Assign_Provider_To_Existing_Client()
{
const string name = "new-client";
var openFeature = Api.Instance;
await openFeature.SetProviderAsync(name, new TestProvider());
await openFeature.SetProviderAsync(name, new NoOpFeatureProvider());
Assert.Equal(NoOpProvider.NoOpProviderName, openFeature.GetProviderMetadata(name)?.Name);
}
[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 async Task OpenFeature_Should_Allow_Multiple_Client_Names_Of_Same_Instance()
{
var openFeature = Api.Instance;
var provider = new TestProvider();
await openFeature.SetProviderAsync("a", provider);
await openFeature.SetProviderAsync("b", provider);
var clientA = openFeature.GetProvider("a");
var clientB = openFeature.GetProvider("b");
Assert.Equal(clientB, clientA);
}
[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);
Assert.Contains(hook1, openFeature.GetHooks());
Assert.Single(openFeature.GetHooks());
openFeature.AddHooks(hook2);
var expectedHooks = new[] { hook1, hook2 }.AsEnumerable();
Assert.Equal(expectedHooks, openFeature.GetHooks());
openFeature.AddHooks(new[] { hook3, hook4 });
expectedHooks = new[] { hook1, hook2, hook3, hook4 }.AsEnumerable();
Assert.Equal(expectedHooks, openFeature.GetHooks());
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 async Task OpenFeature_Should_Get_Metadata()
{
await Api.Instance.SetProviderAsync(new NoOpFeatureProvider());
var openFeature = Api.Instance;
var metadata = openFeature.GetProviderMetadata();
Assert.NotNull(metadata);
Assert.Equal(NoOpProvider.NoOpProviderName, metadata?.Name);
}
[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);
Assert.NotNull(client);
Assert.Equal(name, client.GetMetadata().Name);
Assert.Equal(version, client.GetMetadata().Version);
}
[Fact]
public void Should_Set_Given_Context()
{
var context = EvaluationContext.Empty;
Api.Instance.SetContext(context);
Assert.Equal(context, Api.Instance.GetContext());
context = EvaluationContext.Builder().Build();
Api.Instance.SetContext(context);
Assert.Equal(context, Api.Instance.GetContext());
}
[Fact]
public void Should_Always_Have_Provider()
{
Assert.NotNull(Api.Instance.GetProvider());
}
[Fact]
public async Task OpenFeature_Should_Allow_Multiple_Client_Mapping()
{
var openFeature = Api.Instance;
await openFeature.SetProviderAsync("client1", new TestProvider());
await openFeature.SetProviderAsync("client2", new NoOpFeatureProvider());
var client1 = openFeature.GetClient("client1");
var client2 = openFeature.GetClient("client2");
Assert.Equal("client1", client1.GetMetadata().Name);
Assert.Equal("client2", client2.GetMetadata().Name);
Assert.True(await client1.GetBooleanValueAsync("test", false));
Assert.False(await client2.GetBooleanValueAsync("test", false));
}
[Fact]
public void SetTransactionContextPropagator_ShouldThrowArgumentNullException_WhenNullPropagatorIsPassed()
{
// Arrange
var api = Api.Instance;
// Act & Assert
Assert.Throws<ArgumentNullException>(() => api.SetTransactionContextPropagator(null!));
}
[Fact]
public void SetTransactionContextPropagator_ShouldSetPropagator_WhenValidPropagatorIsPassed()
{
// Arrange
var api = Api.Instance;
var mockPropagator = Substitute.For<ITransactionContextPropagator>();
// Act
api.SetTransactionContextPropagator(mockPropagator);
// Assert
Assert.Equal(mockPropagator, api.GetTransactionContextPropagator());
}
[Fact]
public void SetTransactionContext_ShouldThrowArgumentNullException_WhenEvaluationContextIsNull()
{
// Arrange
var api = Api.Instance;
// Act & Assert
Assert.Throws<ArgumentNullException>(() => api.SetTransactionContext(null!));
}
[Fact]
public void SetTransactionContext_ShouldSetTransactionContext_WhenValidEvaluationContextIsProvided()
{
// Arrange
var api = Api.Instance;
var evaluationContext = EvaluationContext.Builder()
.Set("initial", "yes")
.Build();
var mockPropagator = Substitute.For<ITransactionContextPropagator>();
mockPropagator.GetTransactionContext().Returns(evaluationContext);
api.SetTransactionContextPropagator(mockPropagator);
api.SetTransactionContext(evaluationContext);
// Act
api.SetTransactionContext(evaluationContext);
var result = api.GetTransactionContext();
// Assert
mockPropagator.Received().SetTransactionContext(evaluationContext);
Assert.Equal(evaluationContext, result);
Assert.Equal(evaluationContext.GetValue("initial"), result.GetValue("initial"));
}
[Fact]
public void GetTransactionContext_ShouldReturnEmptyEvaluationContext_WhenNoPropagatorIsSet()
{
// Arrange
var api = Api.Instance;
var context = EvaluationContext.Builder().Set("status", "not-ready").Build();
api.SetTransactionContext(context);
// Act
var result = api.GetTransactionContext();
// Assert
Assert.Equal(EvaluationContext.Empty, result);
}
}
}