|
| 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.Collections.Generic; |
| 6 | +using System.Diagnostics.Tracing; |
| 7 | +using System.Linq; |
| 8 | +using System.Text; |
| 9 | +using System.Threading.Tasks; |
| 10 | +using Xunit; |
| 11 | + |
| 12 | +namespace BasicEventSourceTests |
| 13 | +{ |
| 14 | + public class ActivityTracking |
| 15 | + { |
| 16 | + [ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsNotBrowser))] |
| 17 | + public void StartStopCreatesActivity() |
| 18 | + { |
| 19 | + using ActivityEventListener l = new ActivityEventListener(); |
| 20 | + using ActivityEventSource es = new ActivityEventSource(); |
| 21 | + |
| 22 | + Assert.Equal(Guid.Empty, EventSource.CurrentThreadActivityId); |
| 23 | + es.ExampleStart(); |
| 24 | + Assert.NotEqual(Guid.Empty, EventSource.CurrentThreadActivityId); |
| 25 | + es.ExampleStop(); |
| 26 | + Assert.Equal(Guid.Empty, EventSource.CurrentThreadActivityId); |
| 27 | + } |
| 28 | + |
| 29 | + [ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsNotBrowser))] |
| 30 | + public async Task ActivityFlowsAsync() |
| 31 | + { |
| 32 | + using ActivityEventListener l = new ActivityEventListener(); |
| 33 | + using ActivityEventSource es = new ActivityEventSource(); |
| 34 | + |
| 35 | + Assert.Equal(Guid.Empty, EventSource.CurrentThreadActivityId); |
| 36 | + es.ExampleStart(); |
| 37 | + Assert.NotEqual(Guid.Empty, EventSource.CurrentThreadActivityId); |
| 38 | + await Task.Yield(); |
| 39 | + Assert.NotEqual(Guid.Empty, EventSource.CurrentThreadActivityId); |
| 40 | + es.ExampleStop(); |
| 41 | + Assert.Equal(Guid.Empty, EventSource.CurrentThreadActivityId); |
| 42 | + } |
| 43 | + |
| 44 | + [ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsNotBrowser))] |
| 45 | + public async Task ActivityIdIsZeroedOnThreadSwitchOut() |
| 46 | + { |
| 47 | + using ActivityEventListener l = new ActivityEventListener(); |
| 48 | + using ActivityEventSource es = new ActivityEventSource(); |
| 49 | + |
| 50 | + // Run tasks on many threads. If an activity id leaks it is likely |
| 51 | + // that the thread will be sheduled to run one of our other tasks |
| 52 | + // and we can detect the non-zero id at the start of the task |
| 53 | + List<Task> tasks = new List<Task>(); |
| 54 | + for (int i = 0; i < 100; i++) |
| 55 | + { |
| 56 | + tasks.Add(Task.Run(() => YieldTwoActivitiesDeep(es))); |
| 57 | + } |
| 58 | + |
| 59 | + await Task.WhenAll(tasks.ToArray()); |
| 60 | + } |
| 61 | + |
| 62 | + private async Task YieldTwoActivitiesDeep(ActivityEventSource es) |
| 63 | + { |
| 64 | + Assert.Equal(Guid.Empty, EventSource.CurrentThreadActivityId); |
| 65 | + es.ExampleStart(); |
| 66 | + es.Example2Start(); |
| 67 | + await Task.Yield(); |
| 68 | + es.Example2Stop(); |
| 69 | + es.ExampleStop(); |
| 70 | + Assert.Equal(Guid.Empty, EventSource.CurrentThreadActivityId); |
| 71 | + } |
| 72 | + |
| 73 | + // I don't know if this behavior is by-design or accidental. For now |
| 74 | + // I am attempting to preserve it to lower back compat risk, but in |
| 75 | + // the future we might decide it wasn't even desirable to begin with. |
| 76 | + // Compare with SetCurrentActivityIdAfterEventDoesNotFlowAsync below. |
| 77 | + [ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsNotBrowser))] |
| 78 | + public async Task SetCurrentActivityIdBeforeEventFlowsAsync() |
| 79 | + { |
| 80 | + using ActivityEventListener l = new ActivityEventListener(); |
| 81 | + using ActivityEventSource es = new ActivityEventSource(); |
| 82 | + try |
| 83 | + { |
| 84 | + Guid g = Guid.NewGuid(); |
| 85 | + EventSource.SetCurrentThreadActivityId(g); |
| 86 | + Assert.Equal(g, EventSource.CurrentThreadActivityId); |
| 87 | + es.ExampleStart(); |
| 88 | + await Task.Yield(); |
| 89 | + es.ExampleStop(); |
| 90 | + Assert.Equal(g, EventSource.CurrentThreadActivityId); |
| 91 | + } |
| 92 | + finally |
| 93 | + { |
| 94 | + EventSource.SetCurrentThreadActivityId(Guid.Empty); |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + // I don't know if this behavior is by-design or accidental. For now |
| 99 | + // I am attempting to preserve it to lower back compat risk, but in |
| 100 | + // the future we might decide it wasn't even desirable to begin with. |
| 101 | + // Compare with SetCurrentActivityIdBeforeEventFlowsAsync above. |
| 102 | + [ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsNotBrowser))] |
| 103 | + public async Task SetCurrentActivityIdAfterEventDoesNotFlowAsync() |
| 104 | + { |
| 105 | + using ActivityEventListener l = new ActivityEventListener(); |
| 106 | + using ActivityEventSource es = new ActivityEventSource(); |
| 107 | + try |
| 108 | + { |
| 109 | + es.ExampleStart(); |
| 110 | + Guid g = Guid.NewGuid(); |
| 111 | + EventSource.SetCurrentThreadActivityId(g); |
| 112 | + Assert.Equal(g, EventSource.CurrentThreadActivityId); |
| 113 | + await Task.Yield(); |
| 114 | + Assert.NotEqual(g, EventSource.CurrentThreadActivityId); |
| 115 | + es.ExampleStop(); |
| 116 | + } |
| 117 | + finally |
| 118 | + { |
| 119 | + EventSource.SetCurrentThreadActivityId(Guid.Empty); |
| 120 | + } |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + [EventSource(Name = "ActivityEventSource")] |
| 125 | + class ActivityEventSource : EventSource |
| 126 | + { |
| 127 | + [Event(1)] |
| 128 | + public void ExampleStart() => WriteEvent(1); |
| 129 | + |
| 130 | + [Event(2)] |
| 131 | + public void ExampleStop() => WriteEvent(2); |
| 132 | + |
| 133 | + [Event(3)] |
| 134 | + public void Example2Start() => WriteEvent(3); |
| 135 | + |
| 136 | + [Event(4)] |
| 137 | + public void Example2Stop() => WriteEvent(4); |
| 138 | + } |
| 139 | + |
| 140 | + class ActivityEventListener : EventListener |
| 141 | + { |
| 142 | + protected override void OnEventSourceCreated(EventSource eventSource) |
| 143 | + { |
| 144 | + if (eventSource.Name == "System.Threading.Tasks.TplEventSource") |
| 145 | + { |
| 146 | + EnableEvents(eventSource, EventLevel.LogAlways, (EventKeywords)0x80); |
| 147 | + } |
| 148 | + else if (eventSource.Name == "ActivityEventSource") |
| 149 | + { |
| 150 | + EnableEvents(eventSource, EventLevel.Informational); |
| 151 | + } |
| 152 | + } |
| 153 | + } |
| 154 | +} |
0 commit comments