|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | + |
| 4 | +using System; |
| 5 | +using System.Diagnostics.Tracing; |
| 6 | +using Microsoft.AspNetCore.InternalTesting.Tracing; |
| 7 | +using Xunit; |
| 8 | + |
| 9 | +namespace Microsoft.AspNetCore.InternalTesting; |
| 10 | + |
| 11 | +public class EventSourceValidatorTests |
| 12 | +{ |
| 13 | + [Fact] |
| 14 | + public void ValidateEventSourceIds_PassesForCorrectEventSource() |
| 15 | + { |
| 16 | + EventSourceValidator.ValidateEventSourceIds<CorrectEventSource>(); |
| 17 | + } |
| 18 | + |
| 19 | + [Fact] |
| 20 | + public void ValidateEventSourceIds_FailsForMismatchedWriteEventId() |
| 21 | + { |
| 22 | + // GenerateManifest(Strict) detects the mismatch via IL inspection |
| 23 | + // and our validator surfaces it through Assert.Fail. |
| 24 | + // The exact runtime error message varies by .NET version, so we |
| 25 | + // only verify the validator rejects the bad source. |
| 26 | + Assert.ThrowsAny<Exception>( |
| 27 | + () => EventSourceValidator.ValidateEventSourceIds<MismatchedIdEventSource>()); |
| 28 | + } |
| 29 | + |
| 30 | + [Fact] |
| 31 | + public void ValidateEventSourceIds_FailsForDuplicateEventIds() |
| 32 | + { |
| 33 | + // The duplicate ID message is produced by our validator code. |
| 34 | + var ex = Assert.ThrowsAny<Exception>( |
| 35 | + () => EventSourceValidator.ValidateEventSourceIds<DuplicateIdEventSource>()); |
| 36 | + |
| 37 | + Assert.Contains("Duplicate EventId 1", ex.Message); |
| 38 | + Assert.Contains("EventAlpha", ex.Message); |
| 39 | + Assert.Contains("EventBeta", ex.Message); |
| 40 | + } |
| 41 | + |
| 42 | + [Fact] |
| 43 | + public void ValidateEventSourceIds_FailsForNonEventSourceType() |
| 44 | + { |
| 45 | + // The guard clause message is produced by our validator code. |
| 46 | + var ex = Assert.ThrowsAny<Exception>( |
| 47 | + () => EventSourceValidator.ValidateEventSourceIds(typeof(string))); |
| 48 | + |
| 49 | + Assert.Contains("does not derive from EventSource", ex.Message); |
| 50 | + } |
| 51 | + |
| 52 | + [Fact] |
| 53 | + public void ValidateEventSourceIds_PassesForEventSourceWithNoEvents() |
| 54 | + { |
| 55 | + EventSourceValidator.ValidateEventSourceIds<EmptyEventSource>(); |
| 56 | + } |
| 57 | + |
| 58 | + [Fact] |
| 59 | + public void ValidateEventSourceIds_PassesForEventSourceWithMultipleParameterTypes() |
| 60 | + { |
| 61 | + EventSourceValidator.ValidateEventSourceIds<MultiParamEventSource>(); |
| 62 | + } |
| 63 | + |
| 64 | + // -- Test-only EventSource implementations -- |
| 65 | + |
| 66 | + [EventSource(Name = "Test-Correct")] |
| 67 | + private sealed class CorrectEventSource : EventSource |
| 68 | + { |
| 69 | + [Event(1, Level = EventLevel.Informational)] |
| 70 | + public void EventOne(string message) => WriteEvent(1, message); |
| 71 | + |
| 72 | + [Event(2, Level = EventLevel.Verbose)] |
| 73 | + public void EventTwo(int count) => WriteEvent(2, count); |
| 74 | + |
| 75 | + [Event(3, Level = EventLevel.Warning)] |
| 76 | + public void EventThree() => WriteEvent(3); |
| 77 | + } |
| 78 | + |
| 79 | + [EventSource(Name = "Test-MismatchedId")] |
| 80 | + private sealed class MismatchedIdEventSource : EventSource |
| 81 | + { |
| 82 | + [Event(1, Level = EventLevel.Informational)] |
| 83 | + public void EventOne(int value) => WriteEvent(99, value); |
| 84 | + } |
| 85 | + |
| 86 | + [EventSource(Name = "Test-DuplicateId")] |
| 87 | + private sealed class DuplicateIdEventSource : EventSource |
| 88 | + { |
| 89 | + [Event(1, Level = EventLevel.Informational)] |
| 90 | + public void EventAlpha(string message) => WriteEvent(1, message); |
| 91 | + |
| 92 | + [Event(1, Level = EventLevel.Informational)] |
| 93 | + public void EventBeta(int count) => WriteEvent(1, count); |
| 94 | + } |
| 95 | + |
| 96 | + [EventSource(Name = "Test-Empty")] |
| 97 | + private sealed class EmptyEventSource : EventSource |
| 98 | + { |
| 99 | + } |
| 100 | + |
| 101 | + [EventSource(Name = "Test-MultiParam")] |
| 102 | + private sealed class MultiParamEventSource : EventSource |
| 103 | + { |
| 104 | + [Event(1, Level = EventLevel.Informational)] |
| 105 | + public void EventWithString(string value) => WriteEvent(1, value); |
| 106 | + |
| 107 | + [Event(2, Level = EventLevel.Informational)] |
| 108 | + public void EventWithInt(int value) => WriteEvent(2, value); |
| 109 | + |
| 110 | + [Event(3, Level = EventLevel.Informational)] |
| 111 | + public void EventWithLong(long value) => WriteEvent(3, value); |
| 112 | + |
| 113 | + [Event(4, Level = EventLevel.Informational)] |
| 114 | + public void EventWithMultiple(string name, int count) => WriteEvent(4, name, count); |
| 115 | + |
| 116 | + [Event(5, Level = EventLevel.Informational)] |
| 117 | + public void EventWithNoArgs() => WriteEvent(5); |
| 118 | + } |
| 119 | +} |
0 commit comments