Skip to content

Commit f4ca3bc

Browse files
[Out-of-process-collection] Add tests for Config Project Implementation (open-telemetry#4358)
* Config tests initial setup * new config tests * Remove readme * assembly info changes. * No Warn
1 parent 84cba1e commit f4ca3bc

15 files changed

+1558
-1
lines changed

next-gen/Directory.Packages.props

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,12 @@
77
<ItemGroup Label="DirectDependencies">
88
<PackageVersion Include="Google.Protobuf" Version="[3.22.5,4.0)" />
99
<PackageVersion Include="Grpc.Net.Client" Version="[2.52.0,3.0)" />
10+
<PackageVersion Include="Microsoft.Extensions.Configuration" Version="[9.0.0,)" />
1011
<PackageVersion Include="Microsoft.Extensions.Configuration.Abstractions" Version="[8.0.0,)" />
12+
<PackageVersion Include="Microsoft.Extensions.Configuration.Json" Version="[8.0.1,)" />
13+
<PackageVersion Include="Microsoft.Extensions.DependencyInjection" Version="[8.0.1,)" />
1114
<PackageVersion Include="Microsoft.Extensions.Logging.Abstractions" Version="[8.0.0,)" />
12-
<PackageVersion Include="Microsoft.Extensions.Options" Version="[8.0.0,)" />
15+
<PackageVersion Include="Microsoft.Extensions.Options" Version="[8.0.2,)" />
1316
<PackageVersion Include="Microsoft.Extensions.Options.DataAnnotations" Version="[8.0.0,)" />
1417
</ItemGroup>
1518

next-gen/next-gen.sln

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "OpenTelemetry.AutoInstrumen
1919
EndProject
2020
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "OpenTelemetry.AutoInstrumentation.Sdk.Tests", "test\OpenTelemetry.AutoInstrumentation.Sdk.Tests\OpenTelemetry.AutoInstrumentation.Sdk.Tests.csproj", "{51A855BB-518A-A63C-FA70-9B9DD492847B}"
2121
EndProject
22+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "OpenTelemetry.AutoInstrumentation.Sdk.Configuration.Tests", "test\OpenTelemetry.AutoInstrumentation.Sdk.Configuration.Tests\OpenTelemetry.AutoInstrumentation.Sdk.Configuration.Tests.csproj", "{A1B2C3D4-5E6F-7890-ABCD-EF1234567890}"
23+
EndProject
2224
Global
2325
GlobalSection(SolutionConfigurationPlatforms) = preSolution
2426
Debug|Any CPU = Debug|Any CPU
@@ -37,6 +39,10 @@ Global
3739
{51A855BB-518A-A63C-FA70-9B9DD492847B}.Debug|Any CPU.Build.0 = Debug|Any CPU
3840
{51A855BB-518A-A63C-FA70-9B9DD492847B}.Release|Any CPU.ActiveCfg = Release|Any CPU
3941
{51A855BB-518A-A63C-FA70-9B9DD492847B}.Release|Any CPU.Build.0 = Release|Any CPU
42+
{A1B2C3D4-5E6F-7890-ABCD-EF1234567890}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
43+
{A1B2C3D4-5E6F-7890-ABCD-EF1234567890}.Debug|Any CPU.Build.0 = Debug|Any CPU
44+
{A1B2C3D4-5E6F-7890-ABCD-EF1234567890}.Release|Any CPU.ActiveCfg = Release|Any CPU
45+
{A1B2C3D4-5E6F-7890-ABCD-EF1234567890}.Release|Any CPU.Build.0 = Release|Any CPU
4046
EndGlobalSection
4147
GlobalSection(SolutionProperties) = preSolution
4248
HideSolutionNode = FALSE
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// Copyright The OpenTelemetry Authors
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
using System.Runtime.CompilerServices;
5+
6+
[assembly: InternalsVisibleTo("OpenTelemetry.AutoInstrumentation.Sdk.Configuration.Tests, PublicKey=00240000048000009400000006020000002400005253413100040000010001008d2fe7eb45c8c65611f4d8853e950bb54ccf2a475b618f69e95cd1b9e99115e80574248a7d3013625b5aca737b8c409b190012f947b8770b7e34f025e6000216477edf22ba23db76df3a5c9c126845e9fd449593f1f3f40081f6918b820a03f4b7d410c4dd3f95bb62b31e1f4df7fead370c7cccb3015885e0464c05904e6ec2")]
Lines changed: 205 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,205 @@
1+
// Copyright The OpenTelemetry Authors
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
using Microsoft.Extensions.Configuration;
5+
using Xunit;
6+
7+
namespace OpenTelemetry.Configuration.Tests;
8+
9+
public sealed class ConfigurationExtensionsTests
10+
{
11+
[Fact]
12+
public void TryParseValue_WithValidIntegerValue_ReturnsTrue()
13+
{
14+
// Arrange
15+
var configData = new Dictionary<string, string?>
16+
{
17+
{ "TestSection:TestKey", "42" }
18+
};
19+
var configuration = new ConfigurationBuilder()
20+
.AddInMemoryCollection(configData)
21+
.Build();
22+
var section = configuration.GetSection("TestSection");
23+
24+
// Act
25+
bool result = section.TryParseValue("TestKey", out int value);
26+
27+
// Assert
28+
Assert.True(result);
29+
Assert.Equal(42, value);
30+
}
31+
32+
[Fact]
33+
public void TryParseValue_WithInvalidIntegerValue_ReturnsFalse()
34+
{
35+
// Arrange
36+
var configData = new Dictionary<string, string?>
37+
{
38+
{ "TestKey", "not-a-number" }
39+
};
40+
var configuration = new ConfigurationBuilder()
41+
.AddInMemoryCollection(configData)
42+
.Build();
43+
var section = configuration.GetSection(string.Empty);
44+
45+
// Act
46+
bool result = section.TryParseValue("TestKey", out int value);
47+
48+
// Assert
49+
Assert.False(result);
50+
Assert.Equal(0, value);
51+
}
52+
53+
[Fact]
54+
public void TryParseValue_WithMissingKey_ReturnsFalse()
55+
{
56+
// Arrange
57+
var configData = new Dictionary<string, string?>();
58+
var configuration = new ConfigurationBuilder()
59+
.AddInMemoryCollection(configData)
60+
.Build();
61+
var section = configuration.GetSection(string.Empty);
62+
63+
// Act
64+
bool result = section.TryParseValue("MissingKey", out int value);
65+
66+
// Assert
67+
Assert.False(result);
68+
Assert.Equal(0, value);
69+
}
70+
71+
[Fact]
72+
public void TryParseValue_WithEmptyValue_ReturnsFalse()
73+
{
74+
// Arrange
75+
var configData = new Dictionary<string, string?>
76+
{
77+
{ "TestKey", string.Empty }
78+
};
79+
var configuration = new ConfigurationBuilder()
80+
.AddInMemoryCollection(configData)
81+
.Build();
82+
var section = configuration.GetSection(string.Empty);
83+
84+
// Act
85+
bool result = section.TryParseValue("TestKey", out int value);
86+
87+
// Assert
88+
Assert.False(result);
89+
Assert.Equal(0, value);
90+
}
91+
92+
[Fact]
93+
public void GetValueOrUseDefault_Bool_WithValidValue_ReturnsConfigValue()
94+
{
95+
// Arrange
96+
var configData = new Dictionary<string, string?>
97+
{
98+
{ "TestSection:TestKey", "true" }
99+
};
100+
var configuration = new ConfigurationBuilder()
101+
.AddInMemoryCollection(configData)
102+
.Build();
103+
var section = configuration.GetSection("TestSection");
104+
105+
// Act
106+
bool result = section.GetValueOrUseDefault("TestKey", false);
107+
108+
// Assert
109+
Assert.True(result);
110+
}
111+
112+
[Fact]
113+
public void GetValueOrUseDefault_Bool_WithInvalidValue_ReturnsDefaultValue()
114+
{
115+
// Arrange
116+
var configData = new Dictionary<string, string?>
117+
{
118+
{ "TestKey", "not-a-bool" }
119+
};
120+
var configuration = new ConfigurationBuilder()
121+
.AddInMemoryCollection(configData)
122+
.Build();
123+
var section = configuration.GetSection(string.Empty);
124+
125+
// Act
126+
bool result = section.GetValueOrUseDefault("TestKey", true);
127+
128+
// Assert
129+
Assert.True(result);
130+
}
131+
132+
[Fact]
133+
public void GetValueOrUseDefault_Bool_WithMissingKey_ReturnsDefaultValue()
134+
{
135+
// Arrange
136+
var configData = new Dictionary<string, string?>();
137+
var configuration = new ConfigurationBuilder()
138+
.AddInMemoryCollection(configData)
139+
.Build();
140+
var section = configuration.GetSection(string.Empty);
141+
142+
// Act
143+
bool result = section.GetValueOrUseDefault("MissingKey", true);
144+
145+
// Assert
146+
Assert.True(result);
147+
}
148+
149+
[Fact]
150+
public void GetValueOrUseDefault_Double_WithValidValue_ReturnsConfigValue()
151+
{
152+
// Arrange
153+
var configData = new Dictionary<string, string?>
154+
{
155+
{ "TestSection:TestKey", "3.14" }
156+
};
157+
var configuration = new ConfigurationBuilder()
158+
.AddInMemoryCollection(configData)
159+
.Build();
160+
var section = configuration.GetSection("TestSection");
161+
162+
// Act
163+
double result = section.GetValueOrUseDefault("TestKey", 2.71);
164+
165+
// Assert
166+
Assert.Equal(3.14, result);
167+
}
168+
169+
[Fact]
170+
public void GetValueOrUseDefault_Double_WithInvalidValue_ReturnsDefaultValue()
171+
{
172+
// Arrange
173+
var configData = new Dictionary<string, string?>
174+
{
175+
{ "TestKey", "not-a-double" }
176+
};
177+
var configuration = new ConfigurationBuilder()
178+
.AddInMemoryCollection(configData)
179+
.Build();
180+
var section = configuration.GetSection(string.Empty);
181+
182+
// Act
183+
double result = section.GetValueOrUseDefault("TestKey", 2.71);
184+
185+
// Assert
186+
Assert.Equal(2.71, result);
187+
}
188+
189+
[Fact]
190+
public void GetValueOrUseDefault_Double_WithMissingKey_ReturnsDefaultValue()
191+
{
192+
// Arrange
193+
var configData = new Dictionary<string, string?>();
194+
var configuration = new ConfigurationBuilder()
195+
.AddInMemoryCollection(configData)
196+
.Build();
197+
var section = configuration.GetSection(string.Empty);
198+
199+
// Act
200+
double result = section.GetValueOrUseDefault("MissingKey", 2.71);
201+
202+
// Assert
203+
Assert.Equal(2.71, result);
204+
}
205+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFrameworks>$(TestTargetFrameworks)</TargetFrameworks>
5+
<!-- TODO: Fix the No Warn-->
6+
<NoWarn>$(NoWarn);CA1515;</NoWarn>
7+
</PropertyGroup>
8+
9+
<ItemGroup>
10+
<PackageReference Include="Microsoft.Extensions.Configuration" />
11+
<PackageReference Include="Microsoft.Extensions.Configuration.Json" />
12+
<PackageReference Include="Microsoft.Extensions.DependencyInjection" />
13+
<PackageReference Include="Microsoft.Extensions.Hosting" />
14+
<PackageReference Include="Microsoft.Extensions.Options" />
15+
<PackageReference Include="Microsoft.NET.Test.Sdk" />
16+
<PackageReference Include="xunit" />
17+
<PackageReference Include="xunit.runner.visualstudio">
18+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
19+
<PrivateAssets>all</PrivateAssets>
20+
</PackageReference>
21+
</ItemGroup>
22+
23+
<ItemGroup>
24+
<ProjectReference Include="$(RepoRoot)\src\OpenTelemetry.AutoInstrumentation.Sdk.Configuration\OpenTelemetry.AutoInstrumentation.Sdk.Configuration.csproj" />
25+
<ProjectReference Include="$(RepoRoot)\src\OpenTelemetry.AutoInstrumentation.Sdk\OpenTelemetry.AutoInstrumentation.Sdk.csproj" />
26+
</ItemGroup>
27+
28+
</Project>
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
// Copyright The OpenTelemetry Authors
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
using Xunit;
5+
6+
namespace OpenTelemetry.Configuration.Tests;
7+
8+
public sealed class OpenTelemetryBatchOptionsTests
9+
{
10+
[Fact]
11+
public void Constructor_WithValidParameters_SetsProperties()
12+
{
13+
// Arrange
14+
const int maxQueueSize = 1024;
15+
const int maxExportBatchSize = 256;
16+
const int exportIntervalMilliseconds = 5000;
17+
const int exportTimeoutMilliseconds = 30000;
18+
19+
// Act
20+
var options = new OpenTelemetryBatchOptions(
21+
maxQueueSize,
22+
maxExportBatchSize,
23+
exportIntervalMilliseconds,
24+
exportTimeoutMilliseconds);
25+
26+
// Assert
27+
Assert.Equal(maxQueueSize, options.MaxQueueSize);
28+
Assert.Equal(maxExportBatchSize, options.MaxExportBatchSize);
29+
Assert.Equal(exportIntervalMilliseconds, options.ExportIntervalMilliseconds);
30+
Assert.Equal(exportTimeoutMilliseconds, options.ExportTimeoutMilliseconds);
31+
}
32+
33+
[Fact]
34+
public void Constructor_WithMinimumValues_SetsProperties()
35+
{
36+
// Arrange
37+
const int maxQueueSize = 1;
38+
const int maxExportBatchSize = 1;
39+
const int exportIntervalMilliseconds = 100;
40+
const int exportTimeoutMilliseconds = 1000;
41+
42+
// Act
43+
var options = new OpenTelemetryBatchOptions(
44+
maxQueueSize,
45+
maxExportBatchSize,
46+
exportIntervalMilliseconds,
47+
exportTimeoutMilliseconds);
48+
49+
// Assert
50+
Assert.Equal(maxQueueSize, options.MaxQueueSize);
51+
Assert.Equal(maxExportBatchSize, options.MaxExportBatchSize);
52+
Assert.Equal(exportIntervalMilliseconds, options.ExportIntervalMilliseconds);
53+
Assert.Equal(exportTimeoutMilliseconds, options.ExportTimeoutMilliseconds);
54+
}
55+
56+
[Fact]
57+
public void Constructor_WithLargeValues_SetsProperties()
58+
{
59+
// Arrange
60+
const int maxQueueSize = 10000;
61+
const int maxExportBatchSize = 5000;
62+
const int exportIntervalMilliseconds = 60000;
63+
const int exportTimeoutMilliseconds = 120000;
64+
65+
// Act
66+
var options = new OpenTelemetryBatchOptions(
67+
maxQueueSize,
68+
maxExportBatchSize,
69+
exportIntervalMilliseconds,
70+
exportTimeoutMilliseconds);
71+
72+
// Assert
73+
Assert.Equal(maxQueueSize, options.MaxQueueSize);
74+
Assert.Equal(maxExportBatchSize, options.MaxExportBatchSize);
75+
Assert.Equal(exportIntervalMilliseconds, options.ExportIntervalMilliseconds);
76+
Assert.Equal(exportTimeoutMilliseconds, options.ExportTimeoutMilliseconds);
77+
}
78+
79+
[Theory]
80+
[InlineData(512, 128, 2000, 15000)]
81+
[InlineData(2048, 512, 10000, 60000)]
82+
[InlineData(4096, 1024, 1000, 5000)]
83+
public void Constructor_WithVariousValidCombinations_SetsPropertiesCorrectly(
84+
int maxQueueSize,
85+
int maxExportBatchSize,
86+
int exportIntervalMilliseconds,
87+
int exportTimeoutMilliseconds)
88+
{
89+
// Act
90+
var options = new OpenTelemetryBatchOptions(
91+
maxQueueSize,
92+
maxExportBatchSize,
93+
exportIntervalMilliseconds,
94+
exportTimeoutMilliseconds);
95+
96+
// Assert
97+
Assert.Equal(maxQueueSize, options.MaxQueueSize);
98+
Assert.Equal(maxExportBatchSize, options.MaxExportBatchSize);
99+
Assert.Equal(exportIntervalMilliseconds, options.ExportIntervalMilliseconds);
100+
Assert.Equal(exportTimeoutMilliseconds, options.ExportTimeoutMilliseconds);
101+
}
102+
}

0 commit comments

Comments
 (0)