-
Notifications
You must be signed in to change notification settings - Fork 167
Expand file tree
/
Copy pathFeatureFlagsModuleTests.cs
More file actions
127 lines (106 loc) · 5.21 KB
/
Copy pathFeatureFlagsModuleTests.cs
File metadata and controls
127 lines (106 loc) · 5.21 KB
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
// <copyright file="FeatureFlagsModuleTests.cs" company="Datadog">
// Unless explicitly stated otherwise all files in this repository are licensed under the Apache 2 License.
// This product includes software developed at Datadog (https://www.datadoghq.com/). Copyright 2017 Datadog, Inc.
// </copyright>
#nullable enable
using System.Collections.Generic;
using System.Collections.Specialized;
using Datadog.Trace.Configuration;
using Datadog.Trace.FeatureFlags;
using Datadog.Trace.FeatureFlags.Rcm.Model;
using Datadog.Trace.RemoteConfigurationManagement;
using Datadog.Trace.RemoteConfigurationManagement.Protocol;
using Datadog.Trace.TestHelpers;
using Datadog.Trace.Vendors.Newtonsoft.Json;
using FluentAssertions;
using Xunit;
using FeatureFlagsValueType = Datadog.Trace.FeatureFlags.ValueType;
namespace Datadog.Trace.Tests.FeatureFlags;
public class FeatureFlagsModuleTests
{
[Fact]
public void UpdateRemoteConfig_WithEmptyList_InvokesCallbackAndReturnsProviderNotReady()
{
// Arrange
var rcmManager = new MockRcmSubscriptionManager();
var settings = CreateSettings();
var module = new FeatureFlagsModule(settings, rcmManager);
var callbackInvoked = false;
module.RegisterOnNewConfigEventHandler(() => callbackInvoked = true);
// First, send a valid config so evaluator is created
var configJson = JsonConvert.SerializeObject(new ServerConfiguration
{
Flags = new FlagCollection
{
["test-flag"] = new Flag { Key = "test-flag", Enabled = true, VariationType = FeatureFlagsValueType.Boolean }
}
});
var configPath = RemoteConfigurationPath.FromPath($"datadog/2/{RcmProducts.FfeFlags}/test-config/config");
rcmManager.LastSubscription!.Invoke(
new Dictionary<string, List<RemoteConfiguration>>
{
[RcmProducts.FfeFlags] = [new RemoteConfiguration(configPath, System.Text.Encoding.UTF8.GetBytes(configJson), configJson.Length, new Dictionary<string, string> { { "sha256", "dummy" } }, 1)]
},
null);
// Verify evaluator is working (not PROVIDER_NOT_READY)
var initialResult = module.Evaluate("test-flag", FeatureFlagsValueType.Boolean, false, "user-1", null);
initialResult.Error.Should().NotBe("PROVIDER_NOT_READY");
callbackInvoked.Should().BeTrue("callback should be invoked when config is added");
// Reset for the RC-reset test
callbackInvoked = false;
// Act: Remove the config (RC reset)
rcmManager.LastSubscription!.Invoke(
new Dictionary<string, List<RemoteConfiguration>>(),
new Dictionary<string, List<RemoteConfigurationPath>>
{
[RcmProducts.FfeFlags] = [configPath]
});
// Assert
callbackInvoked.Should().BeTrue("callback should be invoked when config is removed");
var result = module.Evaluate("test-flag", FeatureFlagsValueType.Boolean, false, "user-1", null);
result.Error.Should().Be("PROVIDER_NOT_READY");
result.Reason.Should().Be(EvaluationReason.Error);
}
[Fact]
public void UpdateRemoteConfig_WithExistingPath_ReplacesPreviousConfiguration()
{
var rcmManager = new MockRcmSubscriptionManager();
var module = new FeatureFlagsModule(CreateSettings(), rcmManager);
var configPath = RemoteConfigurationPath.FromPath($"datadog/2/{RcmProducts.FfeFlags}/test-config/config");
rcmManager.LastSubscription!.Invoke(
new Dictionary<string, List<RemoteConfiguration>>
{
[RcmProducts.FfeFlags] = [CreateRemoteConfiguration(configPath, "old-flag")]
},
null);
module.Evaluate("old-flag", FeatureFlagsValueType.Boolean, false, "user-1", null).Error.Should().BeNull();
rcmManager.LastSubscription.Invoke(
new Dictionary<string, List<RemoteConfiguration>>
{
[RcmProducts.FfeFlags] = [CreateRemoteConfiguration(configPath, "new-flag")]
},
null);
module.Evaluate("old-flag", FeatureFlagsValueType.Boolean, false, "user-1", null).Error.Should().Be("FLAG_NOT_FOUND");
module.Evaluate("new-flag", FeatureFlagsValueType.Boolean, false, "user-1", null).Error.Should().BeNull();
}
private static RemoteConfiguration CreateRemoteConfiguration(RemoteConfigurationPath configPath, string flagKey)
{
var configJson = JsonConvert.SerializeObject(new ServerConfiguration
{
Flags = new FlagCollection
{
[flagKey] = new Flag { Key = flagKey, Enabled = true, VariationType = FeatureFlagsValueType.Boolean }
}
});
var configBytes = System.Text.Encoding.UTF8.GetBytes(configJson);
return new RemoteConfiguration(configPath, configBytes, configBytes.Length, new Dictionary<string, string> { { "sha256", "dummy" } }, 1);
}
private static TracerSettings CreateSettings()
{
var collection = new NameValueCollection
{
{ ConfigurationKeys.FeatureFlags.FlaggingProviderEnabled, "true" }
};
return new TracerSettings(new NameValueConfigurationSource(collection));
}
}