Skip to content

Commit 4d91fa0

Browse files
[Infra] Refactor EventSource tests (#6896)
Co-authored-by: Rajkumar Rangaraj <rajrang@microsoft.com>
1 parent 28dc317 commit 4d91fa0

File tree

10 files changed

+216
-141
lines changed

10 files changed

+216
-141
lines changed

test/OpenTelemetry.Api.Tests/EventSourceTests.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@ namespace OpenTelemetry.Api.Tests;
1010
public class EventSourceTests
1111
{
1212
[Fact]
13-
public void EventSourceTest_OpenTelemetryApiEventSource()
14-
{
15-
EventSourceTestHelper.MethodsAreImplementedConsistentlyWithTheirAttributes(OpenTelemetryApiEventSource.Log);
16-
}
13+
public void EventSourceTests_OpenTelemetryApiEventSource() =>
14+
EventSourceTestHelper.ValidateEventSourceIds<OpenTelemetryApiEventSource>();
1715
}

test/OpenTelemetry.Exporter.OpenTelemetryProtocol.Tests/EventSourceTests.cs

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,14 @@ namespace OpenTelemetry.Exporter.OpenTelemetryProtocol.Tests;
1111
public class EventSourceTests
1212
{
1313
[Fact]
14-
public void EventSourceTest_OpenTelemetryProtocolExporterEventSource()
15-
{
16-
EventSourceTestHelper.MethodsAreImplementedConsistentlyWithTheirAttributes(OpenTelemetryProtocolExporterEventSource.Log);
17-
}
14+
public void EventSourceTests_OpenTelemetryProtocolExporterEventSource() =>
15+
EventSourceTestHelper.ValidateEventSourceIds<OpenTelemetryProtocolExporterEventSource>();
1816

1917
[Fact]
20-
public void EventSourceTest_PersistentStorageAbstractionsEventSource()
21-
{
22-
EventSourceTestHelper.MethodsAreImplementedConsistentlyWithTheirAttributes(PersistentStorageAbstractionsEventSource.Log);
23-
}
18+
public void EventSourceTests_PersistentStorageAbstractionsEventSource() =>
19+
EventSourceTestHelper.ValidateEventSourceIds<PersistentStorageAbstractionsEventSource>();
2420

2521
[Fact]
26-
public void EventSourceTest_PersistentStorageEventSource()
27-
{
28-
EventSourceTestHelper.MethodsAreImplementedConsistentlyWithTheirAttributes(PersistentStorageEventSource.Log);
29-
}
22+
public void EventSourceTests_PersistentStorageEventSource() =>
23+
EventSourceTestHelper.ValidateEventSourceIds<PersistentStorageEventSource>();
3024
}

test/OpenTelemetry.Exporter.Prometheus.HttpListener.Tests/EventSourceTests.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@ namespace OpenTelemetry.Exporter.Prometheus.Tests;
99
public class EventSourceTests
1010
{
1111
[Fact]
12-
public void EventSourceTest_PrometheusExporterEventSource()
13-
{
14-
EventSourceTestHelper.MethodsAreImplementedConsistentlyWithTheirAttributes(PrometheusExporterEventSource.Log);
15-
}
12+
public void EventSourceTests_PrometheusExporterEventSource() =>
13+
EventSourceTestHelper.ValidateEventSourceIds<PrometheusExporterEventSource>();
1614
}

test/OpenTelemetry.Exporter.Zipkin.Tests/EventSourceTests.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@ namespace OpenTelemetry.Exporter.Zipkin.Tests;
1010
public class EventSourceTests
1111
{
1212
[Fact]
13-
public void EventSourceTest_ZipkinExporterEventSource()
14-
{
15-
EventSourceTestHelper.MethodsAreImplementedConsistentlyWithTheirAttributes(ZipkinExporterEventSource.Log);
16-
}
13+
public void EventSourceTests_ZipkinExporterEventSource() =>
14+
EventSourceTestHelper.ValidateEventSourceIds<ZipkinExporterEventSource>();
1715
}

test/OpenTelemetry.Extensions.Hosting.Tests/EventSourceTests.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@ namespace OpenTelemetry.Extensions.Hosting.Tests;
1010
public class EventSourceTests
1111
{
1212
[Fact]
13-
public void EventSourceTest_HostingExtensionsEventSource()
14-
{
15-
EventSourceTestHelper.MethodsAreImplementedConsistentlyWithTheirAttributes(HostingExtensionsEventSource.Log);
16-
}
13+
public void EventSourceTests_HostingExtensionsEventSource() =>
14+
EventSourceTestHelper.ValidateEventSourceIds<HostingExtensionsEventSource>();
1715
}

test/OpenTelemetry.Extensions.Propagators.Tests/EventSourceTests.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@ namespace OpenTelemetry.Extensions.Propagators.Tests;
1010
public class EventSourceTests
1111
{
1212
[Fact]
13-
public void EventSourceTest_PropagatorsEventSource()
14-
{
15-
EventSourceTestHelper.MethodsAreImplementedConsistentlyWithTheirAttributes(OpenTelemetryPropagatorsEventSource.Log);
16-
}
13+
public void EventSourceTests_OpenTelemetryPropagatorsEventSource() =>
14+
EventSourceTestHelper.ValidateEventSourceIds<OpenTelemetryPropagatorsEventSource>();
1715
}
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
// Copyright The OpenTelemetry Authors
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
// Adapted from https://github.com/dotnet/aspnetcore/blob/3a973a5f4d28242262f27c86eb3f14299fe712ba/src/Testing/test/EventSourceValidatorTests.cs
5+
6+
using System.Diagnostics.Tracing;
7+
using Xunit;
8+
9+
namespace OpenTelemetry.Tests;
10+
11+
public static class EventSourceTestHelperTests
12+
{
13+
[Fact]
14+
public static void ValidateEventSourceIds_PassesForCorrectEventSource()
15+
=> EventSourceTestHelper.ValidateEventSourceIds<CorrectEventSource>();
16+
17+
// GenerateManifest(Strict) detects the mismatch via IL inspection
18+
// and our validator surfaces it through Assert.Fail.
19+
// The exact runtime error message varies by .NET version, so we
20+
// only verify the validator rejects the bad source.
21+
22+
[Fact]
23+
public static void ValidateEventSourceIds_FailsForMismatchedWriteEventId()
24+
{
25+
#if !NETFRAMEWORK
26+
if (!OperatingSystem.IsWindows())
27+
{
28+
// Only supported on Windows
29+
return;
30+
}
31+
#endif
32+
33+
Assert.ThrowsAny<Exception>(
34+
EventSourceTestHelper.ValidateEventSourceIds<MismatchedIdEventSource>);
35+
}
36+
37+
[Fact]
38+
public static void ValidateEventSourceIds_FailsForDuplicateEventIds()
39+
{
40+
// The duplicate ID message is produced by our validator code.
41+
var ex = Assert.ThrowsAny<Exception>(
42+
EventSourceTestHelper.ValidateEventSourceIds<DuplicateIdEventSource>);
43+
44+
Assert.Contains("Duplicate EventId 1", ex.Message, StringComparison.Ordinal);
45+
Assert.Contains("EventAlpha", ex.Message, StringComparison.Ordinal);
46+
Assert.Contains("EventBeta", ex.Message, StringComparison.Ordinal);
47+
}
48+
49+
[Fact]
50+
public static void ValidateEventSourceIds_FailsForNonEventSourceType()
51+
{
52+
// The guard clause message is produced by our validator code.
53+
var ex = Assert.ThrowsAny<Exception>(
54+
() => EventSourceTestHelper.ValidateEventSourceIds(typeof(string)));
55+
56+
Assert.Contains("does not derive from EventSource", ex.Message, StringComparison.Ordinal);
57+
}
58+
59+
[Fact]
60+
public static void ValidateEventSourceIds_PassesForEventSourceWithNoEvents() =>
61+
EventSourceTestHelper.ValidateEventSourceIds<EmptyEventSource>();
62+
63+
[Fact]
64+
public static void ValidateEventSourceIds_PassesForEventSourceWithMultipleParameterTypes() =>
65+
EventSourceTestHelper.ValidateEventSourceIds<MultiParamEventSource>();
66+
67+
#pragma warning disable CA1812
68+
69+
// -- Test-only EventSource implementations --
70+
71+
[EventSource(Name = "Test-Correct")]
72+
private sealed class CorrectEventSource : EventSource
73+
{
74+
[Event(1, Level = EventLevel.Informational)]
75+
public void EventOne(string message) => this.WriteEvent(1, message);
76+
77+
[Event(2, Level = EventLevel.Verbose)]
78+
public void EventTwo(int count) => this.WriteEvent(2, count);
79+
80+
[Event(3, Level = EventLevel.Warning)]
81+
public void EventThree() => this.WriteEvent(3);
82+
}
83+
84+
[EventSource(Name = "Test-MismatchedId")]
85+
private sealed class MismatchedIdEventSource : EventSource
86+
{
87+
[Event(1, Level = EventLevel.Informational)]
88+
public void EventOne(int value) => this.WriteEvent(99, value);
89+
}
90+
91+
[EventSource(Name = "Test-DuplicateId")]
92+
private sealed class DuplicateIdEventSource : EventSource
93+
{
94+
[Event(1, Level = EventLevel.Informational)]
95+
public void EventAlpha(string message) => this.WriteEvent(1, message);
96+
97+
[Event(1, Level = EventLevel.Informational)]
98+
public void EventBeta(int count) => this.WriteEvent(1, count);
99+
}
100+
101+
[EventSource(Name = "Test-Empty")]
102+
private sealed class EmptyEventSource : EventSource;
103+
104+
[EventSource(Name = "Test-MultiParam")]
105+
private sealed class MultiParamEventSource : EventSource
106+
{
107+
[Event(1, Level = EventLevel.Informational)]
108+
public void EventWithString(string value) => this.WriteEvent(1, value);
109+
110+
[Event(2, Level = EventLevel.Informational)]
111+
public void EventWithInt(int value) => this.WriteEvent(2, value);
112+
113+
[Event(3, Level = EventLevel.Informational)]
114+
public void EventWithLong(long value) => this.WriteEvent(3, value);
115+
116+
[Event(4, Level = EventLevel.Informational)]
117+
public void EventWithMultiple(string name, int count) => this.WriteEvent(4, name, count);
118+
119+
[Event(5, Level = EventLevel.Informational)]
120+
public void EventWithNoArgs() => this.WriteEvent(5);
121+
}
122+
123+
#pragma warning restore CA1812
124+
}

test/OpenTelemetry.Tests/EventSourceTests.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@ namespace OpenTelemetry.Tests;
99
public class EventSourceTests
1010
{
1111
[Fact]
12-
public void EventSourceTest_OpenTelemetrySdkEventSource()
13-
{
14-
EventSourceTestHelper.MethodsAreImplementedConsistentlyWithTheirAttributes(OpenTelemetrySdkEventSource.Log);
15-
}
12+
public void EventSourceTests_OpenTelemetrySdkEventSource() =>
13+
EventSourceTestHelper.ValidateEventSourceIds<OpenTelemetrySdkEventSource>();
1614
}

0 commit comments

Comments
 (0)