-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathOpenFeatureActivitySourceTests.cs
More file actions
111 lines (92 loc) · 3.96 KB
/
OpenFeatureActivitySourceTests.cs
File metadata and controls
111 lines (92 loc) · 3.96 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
using System.Diagnostics;
using OpenFeature.Constant;
namespace OpenFeature.Tests;
public class OpenFeatureActivitySourceTests
{
[Fact]
public void StartActivity_ReturnsActivityWithCorrectName()
{
using var activityListener = new ActivityListener()
{
ShouldListenTo = source => true,
Sample = (ref _) => ActivitySamplingResult.AllDataAndRecorded
};
ActivitySource.AddActivityListener(activityListener);
var activity = OpenFeatureActivitySource.StartActivity("test_activity");
Assert.NotNull(activity);
Assert.Equal("test_activity", activity.OperationName);
Assert.Equal("OpenFeature", activity.Source.Name);
Assert.NotNull(activity.Source.Version);
Assert.NotEmpty(activity.Source.Version);
Assert.Equal(ActivityKind.Internal, activity.Kind);
}
[Theory]
[InlineData(ErrorType.ProviderNotReady, "provider_not_ready")]
[InlineData(ErrorType.FlagNotFound, "flag_not_found")]
[InlineData(ErrorType.ParseError, "parse_error")]
[InlineData(ErrorType.TypeMismatch, "type_mismatch")]
[InlineData(ErrorType.General, "general")]
[InlineData(ErrorType.InvalidContext, "invalid_context")]
[InlineData(ErrorType.TargetingKeyMissing, "targeting_key_missing")]
[InlineData(ErrorType.ProviderFatal, "provider_fatal")]
[InlineData((ErrorType)999, "_OTHER")]
public void GetFlagEvaluationErrorDescription_ReturnsCorrectDescription(ErrorType errorType, string expectedDescription)
{
var actual = errorType.GetFlagEvaluationErrorDescription();
Assert.Equal(expectedDescription, actual);
}
[Theory]
[InlineData("TARGETING_MATCH", "targeting_match")]
[InlineData("SPLIT", "split")]
[InlineData("DISABLED", "disabled")]
[InlineData("DEFAULT", "default")]
[InlineData("STATIC", "static")]
[InlineData("CACHED", "cached")]
[InlineData("UNKNOWN", "unknown")]
[InlineData("ERROR", "error")]
[InlineData("OTHER", "OTHER")]
public void GetFlagEvaluationReasonDescription(string? reason, string expectedDescription)
{
var actual = OpenFeatureActivitySource.GetFlagEvaluationReasonDescription(reason);
Assert.Equal(expectedDescription, actual);
}
[Fact]
public void SetTagIfRequested_AddsTag()
{
var exportedActivities = new List<Activity>();
using var activityListener = new ActivityListener()
{
ShouldListenTo = source => source.Name == "OpenFeature",
Sample = (ref _) => ActivitySamplingResult.AllDataAndRecorded,
ActivityStopped = activity => exportedActivities.Add(activity)
};
ActivitySource.AddActivityListener(activityListener);
using (var activity = OpenFeatureActivitySource.StartActivity("set_tag_if_requested"))
{
activity?.AddTagIfRequested("custom_tag_name", true);
}
Assert.Single(exportedActivities);
var actualActivity = exportedActivities.First();
Assert.Equal("custom_tag_name", actualActivity.TagObjects.First().Key);
Assert.Equal(true, actualActivity.TagObjects.First().Value);
}
[Fact]
public void SetTagIfRequested_WhenDataNotRequested_DoesNotAddTag()
{
var exportedActivities = new List<Activity>();
using var activityListener = new ActivityListener()
{
ShouldListenTo = source => source.Name == "OpenFeature",
Sample = (ref _) => ActivitySamplingResult.PropagationData,
ActivityStopped = activity => exportedActivities.Add(activity)
};
ActivitySource.AddActivityListener(activityListener);
using (var activity = OpenFeatureActivitySource.StartActivity("set_tag_if_requested"))
{
activity?.AddTagIfRequested("custom_tag_name", true);
}
Assert.Single(exportedActivities);
var actualActivity = exportedActivities.First();
Assert.Empty(actualActivity.TagObjects);
}
}