Skip to content

Commit

Permalink
improve eventsource test (Azure#30643)
Browse files Browse the repository at this point in the history
  • Loading branch information
TimothyMothra authored Aug 19, 2022
1 parent ea68740 commit ec4e562
Showing 1 changed file with 18 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -48,13 +48,11 @@ private void Test(Action<string, string> 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);
Expand All @@ -67,13 +65,11 @@ private void TestException(Action<string, Exception> 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);
Expand All @@ -86,13 +82,11 @@ private void TestAggregateException(Action<string, Exception> 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);
Expand All @@ -101,27 +95,18 @@ private void TestAggregateException(Action<string, Exception> writeAction, int e
Assert.Equal($"{name} - System.Exception: hello world_1", message);
}

private static EventWrittenEventArgs FindEvent(List<EventWrittenEventArgs> 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<EventSource> eventSources = new();
private readonly Guid guid = Guid.NewGuid();

public List<EventWrittenEventArgs> Events = new();

public TestListener()
{
EventSource.SetCurrentThreadActivityId(guid);
}

public override void Dispose()
{
foreach (EventSource eventSource in this.eventSources)
Expand All @@ -138,15 +123,18 @@ 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);
}

protected override void OnEventWritten(EventWrittenEventArgs eventData)
{
this.Events.Add(eventData);
if (eventData.ActivityId == this.guid)
{
this.Events.Add(eventData);
}
}
}
}
Expand Down

0 comments on commit ec4e562

Please sign in to comment.