Skip to content

Commit

Permalink
Add TestEventSource and use it in TestInvoker (#2884)
Browse files Browse the repository at this point in the history
  • Loading branch information
adamsitnik committed Feb 22, 2024
1 parent 4100ea2 commit 0b635ca
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 8 deletions.
30 changes: 30 additions & 0 deletions src/xunit.v3.core/Sdk/v3/EventSources/TestEventSource.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using System.Diagnostics.Tracing;

namespace Xunit.v3;

[EventSource(Name = sourceName, Guid = "ae399e80-45fc-4219-aacc-b73a458ad7e1")]
internal sealed class TestEventSource : EventSource
{
const string sourceName = "xUnit.TestEventSource";

internal static readonly TestEventSource Log = new();

TestEventSource() { }

[Event(Events.TestStart, Level = EventLevel.Informational, Task = Tasks.Test, Opcode = EventOpcode.Start)]
internal void TestStart(string testName) => WriteEvent(Events.TestStart, testName);

[Event(Events.TestStop, Level = EventLevel.Informational, Task = Tasks.Test, Opcode = EventOpcode.Stop)]
internal void TestStop(string testName) => WriteEvent(Events.TestStop, testName);

class Events
{
internal const int TestStart = 1;
internal const int TestStop = 2;
}

class Tasks
{
internal const EventTask Test = (EventTask)1;
}
}
29 changes: 21 additions & 8 deletions src/xunit.v3.core/Sdk/v3/Runners/TestInvoker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -185,15 +185,28 @@ public abstract class TestInvoker<TContext>
}
else
{
var result = CallTestMethod(ctxt, testClassInstance);
var valueTask = AsyncUtility.TryConvertToValueTask(result);
if (valueTask.HasValue)
await valueTask.Value;
else if (asyncSyncContext is not null)
var logEnabled = TestEventSource.Log.IsEnabled();
if (logEnabled)
TestEventSource.Log.TestStart(ctxt.Test.TestDisplayName);
try
{
var result = CallTestMethod(ctxt, testClassInstance);
var valueTask = AsyncUtility.TryConvertToValueTask(result);
if (valueTask.HasValue)
await valueTask.Value;
else if (asyncSyncContext is not null)
{
var ex = await asyncSyncContext.WaitForCompletionAsync();
if (ex is not null)
ctxt.Aggregator.Add(ex);
}
}
finally
{
var ex = await asyncSyncContext.WaitForCompletionAsync();
if (ex is not null)
ctxt.Aggregator.Add(ex);
if (logEnabled)
TestEventSource.Log.TestStop(ctxt.Test.TestDisplayName);
}
}
}
Expand Down

0 comments on commit 0b635ca

Please sign in to comment.