|
| 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 | +} |
0 commit comments