From ec4e562389a7b2b9902d2a378d74125572516e95 Mon Sep 17 00:00:00 2001 From: Timothy Mothra Date: Fri, 19 Aug 2022 14:53:59 -0700 Subject: [PATCH] improve eventsource test (#30643) --- .../AzureMonitorExporterEventSourceTests.cs | 48 +++++++------------ 1 file changed, 18 insertions(+), 30 deletions(-) diff --git a/sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/tests/Azure.Monitor.OpenTelemetry.Exporter.Tests/AzureMonitorExporterEventSourceTests.cs b/sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/tests/Azure.Monitor.OpenTelemetry.Exporter.Tests/AzureMonitorExporterEventSourceTests.cs index 418e7c8d0616d..3508eca9ae921 100644 --- a/sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/tests/Azure.Monitor.OpenTelemetry.Exporter.Tests/AzureMonitorExporterEventSourceTests.cs +++ b/sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/tests/Azure.Monitor.OpenTelemetry.Exporter.Tests/AzureMonitorExporterEventSourceTests.cs @@ -4,7 +4,7 @@ using System; using System.Collections.Generic; using System.Diagnostics.Tracing; - +using System.Linq; using Azure.Core.Shared; using Azure.Monitor.OpenTelemetry.Exporter.Internals; @@ -48,13 +48,11 @@ private void Test(Action writeAction, int expectedId, string exp { using var listener = new TestListener(); - // When running tests parallel, it's possible for one test to collect the messages from another test. - // We use a guid here to be able to find the specific message created by this test. - var name = $"{nameof(AzureMonitorExporterEventSourceTests)}.{Guid.NewGuid()}"; + var name = nameof(AzureMonitorExporterEventSourceTests); writeAction(name, "hello world"); - var eventData = FindEvent(listener.Events, name); + var eventData = listener.Events.Single(); Assert.Equal(AzureMonitorExporterEventSource.EventSourceName, eventData.EventSource.Name); Assert.Equal(expectedId, eventData.EventId); Assert.Equal(expectedName, eventData.EventName); @@ -67,13 +65,11 @@ private void TestException(Action writeAction, int expectedId { using var listener = new TestListener(); - // When running tests parallel, it's possible for one test to collect the messages from another test. - // We use a guid here to be able to find the specific message created by this test. - var name = $"{nameof(AzureMonitorExporterEventSourceTests)}.{Guid.NewGuid()}"; + var name = nameof(AzureMonitorExporterEventSourceTests); writeAction(name, new Exception("hello world")); - var eventData = FindEvent(listener.Events, name); + var eventData = listener.Events.Single(); Assert.Equal(AzureMonitorExporterEventSource.EventSourceName, eventData.EventSource.Name); Assert.Equal(expectedId, eventData.EventId); Assert.Equal(expectedName, eventData.EventName); @@ -86,13 +82,11 @@ private void TestAggregateException(Action writeAction, int e { using var listener = new TestListener(); - // When running tests parallel, it's possible for one test to collect the messages from another test. - // We use a guid here to be able to find the specific message created by this test. - var name = $"{nameof(AzureMonitorExporterEventSourceTests)}.{Guid.NewGuid()}"; + var name = nameof(AzureMonitorExporterEventSourceTests); writeAction(name, new AggregateException(new Exception("hello world_1"), new Exception("hello world_2)"))); - var eventData = FindEvent(listener.Events, name); + var eventData = listener.Events.Single(); Assert.Equal(AzureMonitorExporterEventSource.EventSourceName, eventData.EventSource.Name); Assert.Equal(expectedId, eventData.EventId); Assert.Equal(expectedName, eventData.EventName); @@ -101,27 +95,18 @@ private void TestAggregateException(Action writeAction, int e Assert.Equal($"{name} - System.Exception: hello world_1", message); } - private static EventWrittenEventArgs FindEvent(List list, string name) - { - // Note: cannot use Linq here. If the listener grabs another event, Linq will throw InvalidOperationException. - - for (int i = 0; i < list.Count; i++) - { - if (list[i].Payload.Contains(name)) - { - return list[i]; - } - } - - throw new Exception("not found"); - } - public class TestListener : EventListener { private readonly List eventSources = new(); + private readonly Guid guid = Guid.NewGuid(); public List Events = new(); + public TestListener() + { + EventSource.SetCurrentThreadActivityId(guid); + } + public override void Dispose() { foreach (EventSource eventSource in this.eventSources) @@ -138,7 +123,7 @@ protected override void OnEventSourceCreated(EventSource eventSource) if (eventSource?.Name == AzureMonitorExporterEventSource.EventSourceName) { this.eventSources.Add(eventSource); - this.EnableEvents(eventSource, EventLevel.Verbose, (EventKeywords)(-1)); + this.EnableEvents(eventSource, EventLevel.Verbose, EventKeywords.All); } base.OnEventSourceCreated(eventSource); @@ -146,7 +131,10 @@ protected override void OnEventSourceCreated(EventSource eventSource) protected override void OnEventWritten(EventWrittenEventArgs eventData) { - this.Events.Add(eventData); + if (eventData.ActivityId == this.guid) + { + this.Events.Add(eventData); + } } } }