Skip to content

Commit

Permalink
Enabling extension of events
Browse files Browse the repository at this point in the history
  • Loading branch information
OsirisTerje committed Apr 21, 2024
1 parent 7b9497d commit a79d198
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 20 deletions.
4 changes: 2 additions & 2 deletions src/NUnitFramework/framework/Api/NUnitTestAssemblyRunner.cs
Expand Up @@ -35,7 +35,7 @@ public class NUnitTestAssemblyRunner : ITestAssemblyRunner
private TextWriter? _savedErr;

// Event Pump
private EventPump? _pump;
private EventPump<Event, ITestListener>? _pump;

#region Constructors

Expand Down Expand Up @@ -263,7 +263,7 @@ private void StartRun(TestExecutionContext context, WorkItem topLevelWorkItem, I
QueuingEventListener queue = new QueuingEventListener();
context.Listener = queue;

_pump = new EventPump(listener, queue.Events);
_pump = new EventPump<Event, ITestListener>(listener, queue.Events);
_pump.Start();
}

Expand Down
20 changes: 12 additions & 8 deletions src/NUnitFramework/framework/Internal/Execution/EventPump.cs
Expand Up @@ -2,7 +2,6 @@

using System;
using System.Threading;
using NUnit.Framework.Interfaces;

namespace NUnit.Framework.Internal.Execution
{
Expand Down Expand Up @@ -34,7 +33,9 @@ public enum EventPumpState
/// the client without using the CallContext of the test
/// runner thread.
/// </summary>
public class EventPump : IDisposable
public class EventPump<TEvent, TListener>
where TEvent : IEvent<TListener>,
IDisposable
{
private static readonly Logger Log = InternalTrace.GetLogger("EventPump");

Expand All @@ -43,12 +44,12 @@ public class EventPump : IDisposable
/// <summary>
/// The downstream listener to which we send events
/// </summary>
private readonly ITestListener _eventListener;
private readonly TListener _eventListener;

/// <summary>
/// The queue that holds our events
/// </summary>
private readonly EventQueue _events;
private readonly EventQueue<TEvent> _events;

/// <summary>
/// Thread to do the pumping
Expand All @@ -63,15 +64,18 @@ public class EventPump : IDisposable
#endregion

#region Constructor

/// <summary>
/// Constructor
/// </summary>
/// <param name="eventListener">The EventListener to receive events</param>
/// <param name="events">The event queue to pull events from</param>
public EventPump(ITestListener eventListener, EventQueue events)
/// <param name="name">Name of the thread and pump</param>
public EventPump(TListener eventListener, EventQueue<TEvent> events, string name = "Standard")
{
_eventListener = eventListener;
_events = events;
Name = name;
}

#endregion
Expand Down Expand Up @@ -111,7 +115,7 @@ public void Start()
{
_pumpThread = new Thread(PumpThreadProc)
{
Name = "EventPumpThread" + Name,
Name = $"{Name}EventPumpThread",
Priority = ThreadPriority.Highest
};

Expand Down Expand Up @@ -142,14 +146,14 @@ public void Stop()
/// </summary>
private void PumpThreadProc()
{
Log.Debug("Starting EventPump");
Log.Debug($"Starting EventPump {Name}");

//ITestListener hostListeners = CoreExtensions.Host.Listeners;
try
{
while (true)
{
Event? e = _events.Dequeue(PumpState == EventPumpState.Pumping);
var e = _events.Dequeue(PumpState == EventPumpState.Pumping);
if (e is null)
break;
try
Expand Down
30 changes: 22 additions & 8 deletions src/NUnitFramework/framework/Internal/Execution/EventQueue.cs
@@ -1,11 +1,21 @@
// Copyright (c) Charlie Poole, Rob Prouse and Contributors. MIT License - see LICENSE.txt

using System;
using System.Collections.Concurrent;
using System.Threading;
using NUnit.Framework.Interfaces;

namespace NUnit.Framework.Internal.Execution
{
public interface IEvent<in TListener>

Check failure on line 10 in src/NUnitFramework/framework/Internal/Execution/EventQueue.cs

View workflow job for this annotation

GitHub Actions / Linux Build

Missing XML comment for publicly visible type or member 'IEvent<TListener>'

Check failure on line 10 in src/NUnitFramework/framework/Internal/Execution/EventQueue.cs

View workflow job for this annotation

GitHub Actions / Linux Build

Missing XML comment for publicly visible type or member 'IEvent<TListener>'

Check failure on line 10 in src/NUnitFramework/framework/Internal/Execution/EventQueue.cs

View workflow job for this annotation

GitHub Actions / Linux Build

Missing XML comment for publicly visible type or member 'IEvent<TListener>'

Check failure on line 10 in src/NUnitFramework/framework/Internal/Execution/EventQueue.cs

View workflow job for this annotation

GitHub Actions / Linux Build

Missing XML comment for publicly visible type or member 'IEvent<TListener>'

Check failure on line 10 in src/NUnitFramework/framework/Internal/Execution/EventQueue.cs

View workflow job for this annotation

GitHub Actions / Windows Build

Missing XML comment for publicly visible type or member 'IEvent<TListener>'

Check failure on line 10 in src/NUnitFramework/framework/Internal/Execution/EventQueue.cs

View workflow job for this annotation

GitHub Actions / Windows Build

Missing XML comment for publicly visible type or member 'IEvent<TListener>'

Check failure on line 10 in src/NUnitFramework/framework/Internal/Execution/EventQueue.cs

View workflow job for this annotation

GitHub Actions / Windows Build

Missing XML comment for publicly visible type or member 'IEvent<TListener>'

Check failure on line 10 in src/NUnitFramework/framework/Internal/Execution/EventQueue.cs

View workflow job for this annotation

GitHub Actions / Windows Build

Missing XML comment for publicly visible type or member 'IEvent<TListener>'

Check failure on line 10 in src/NUnitFramework/framework/Internal/Execution/EventQueue.cs

View workflow job for this annotation

GitHub Actions / MacOS Build

Missing XML comment for publicly visible type or member 'IEvent<TListener>'

Check failure on line 10 in src/NUnitFramework/framework/Internal/Execution/EventQueue.cs

View workflow job for this annotation

GitHub Actions / MacOS Build

Missing XML comment for publicly visible type or member 'IEvent<TListener>'

Check failure on line 10 in src/NUnitFramework/framework/Internal/Execution/EventQueue.cs

View workflow job for this annotation

GitHub Actions / MacOS Build

Missing XML comment for publicly visible type or member 'IEvent<TListener>'

Check failure on line 10 in src/NUnitFramework/framework/Internal/Execution/EventQueue.cs

View workflow job for this annotation

GitHub Actions / MacOS Build

Missing XML comment for publicly visible type or member 'IEvent<TListener>'
{
/// <summary>
/// The Send method is implemented by derived classes to send the event to the specified listener.
/// </summary>
/// <param name="listener">The listener.</param>
void Send(TListener listener);
}

#region Individual Event Classes

/// <summary>
Expand All @@ -15,13 +25,17 @@ namespace NUnit.Framework.Internal.Execution
/// or to queue them for forwarding on another thread or at
/// a later time.
/// </summary>
public abstract class Event
public abstract class Event : IEvent<ITestListener>, IDisposable
{
/// <summary>
/// The Send method is implemented by derived classes to send the event to the specified listener.
/// </summary>
/// <param name="listener">The listener.</param>
public abstract void Send(ITestListener listener);

public void Dispose()

Check failure on line 36 in src/NUnitFramework/framework/Internal/Execution/EventQueue.cs

View workflow job for this annotation

GitHub Actions / Linux Build

Missing XML comment for publicly visible type or member 'Event.Dispose()'

Check failure on line 36 in src/NUnitFramework/framework/Internal/Execution/EventQueue.cs

View workflow job for this annotation

GitHub Actions / Linux Build

Missing XML comment for publicly visible type or member 'Event.Dispose()'

Check failure on line 36 in src/NUnitFramework/framework/Internal/Execution/EventQueue.cs

View workflow job for this annotation

GitHub Actions / Linux Build

Missing XML comment for publicly visible type or member 'Event.Dispose()'

Check failure on line 36 in src/NUnitFramework/framework/Internal/Execution/EventQueue.cs

View workflow job for this annotation

GitHub Actions / Linux Build

Missing XML comment for publicly visible type or member 'Event.Dispose()'

Check failure on line 36 in src/NUnitFramework/framework/Internal/Execution/EventQueue.cs

View workflow job for this annotation

GitHub Actions / Windows Build

Missing XML comment for publicly visible type or member 'Event.Dispose()'

Check failure on line 36 in src/NUnitFramework/framework/Internal/Execution/EventQueue.cs

View workflow job for this annotation

GitHub Actions / Windows Build

Missing XML comment for publicly visible type or member 'Event.Dispose()'

Check failure on line 36 in src/NUnitFramework/framework/Internal/Execution/EventQueue.cs

View workflow job for this annotation

GitHub Actions / Windows Build

Missing XML comment for publicly visible type or member 'Event.Dispose()'

Check failure on line 36 in src/NUnitFramework/framework/Internal/Execution/EventQueue.cs

View workflow job for this annotation

GitHub Actions / Windows Build

Missing XML comment for publicly visible type or member 'Event.Dispose()'

Check failure on line 36 in src/NUnitFramework/framework/Internal/Execution/EventQueue.cs

View workflow job for this annotation

GitHub Actions / MacOS Build

Missing XML comment for publicly visible type or member 'Event.Dispose()'

Check failure on line 36 in src/NUnitFramework/framework/Internal/Execution/EventQueue.cs

View workflow job for this annotation

GitHub Actions / MacOS Build

Missing XML comment for publicly visible type or member 'Event.Dispose()'

Check failure on line 36 in src/NUnitFramework/framework/Internal/Execution/EventQueue.cs

View workflow job for this annotation

GitHub Actions / MacOS Build

Missing XML comment for publicly visible type or member 'Event.Dispose()'

Check failure on line 36 in src/NUnitFramework/framework/Internal/Execution/EventQueue.cs

View workflow job for this annotation

GitHub Actions / MacOS Build

Missing XML comment for publicly visible type or member 'Event.Dispose()'
{
}
}

/// <summary>
Expand Down Expand Up @@ -137,13 +151,13 @@ public override void Send(ITestListener listener)
/// Implements a queue of work items each of which
/// is queued as a WaitCallback.
/// </summary>
public class EventQueue
public class EventQueue<T>
{
private const int SpinCount = 5;

// static readonly Logger log = InternalTrace.GetLogger("EventQueue");

private readonly ConcurrentQueue<Event> _queue = new();
private readonly ConcurrentQueue<T> _queue = new();

/* This event is used solely for the purpose of having an optimized sleep cycle when
* we have to wait on an external event (Add or Remove for instance)
Expand Down Expand Up @@ -171,7 +185,7 @@ public class EventQueue
/// Enqueues the specified event
/// </summary>
/// <param name="e">The event to enqueue.</param>
public void Enqueue(Event e)
public void Enqueue(T e)
{
do
{
Expand Down Expand Up @@ -216,7 +230,7 @@ public void Enqueue(Event e)
/// </item>
/// </list>
/// </returns>
public Event? Dequeue(bool blockWhenEmpty)
public T? Dequeue(bool blockWhenEmpty)
{
SpinWait sw = new SpinWait();

Expand All @@ -229,7 +243,7 @@ public void Enqueue(Event e)
if (cachedRemoveId == cachedAddId)
{
if (!blockWhenEmpty || _stopped != 0)
return null;
return default(T);

// Spin a few times to see if something changes
if (sw.Count <= SpinCount)
Expand Down Expand Up @@ -261,11 +275,11 @@ public void Enqueue(Event e)
continue;

// Dequeue our work item
Event? e;
T? e;
while (!_queue.TryDequeue(out e))
{
if (!blockWhenEmpty || _stopped != 0)
return null;
return default(T);
}

return e;
Expand Down
Expand Up @@ -13,14 +13,14 @@ public class QueuingEventListener : ITestListener
/// <summary>
/// The EventQueue created and filled by this listener
/// </summary>
public EventQueue Events { get; }
public EventQueue<Event> Events { get; }

/// <summary>
/// Construct a QueuingEventListener
/// </summary>
public QueuingEventListener()
{
Events = new EventQueue();
Events = new EventQueue<Event>();
}

#region EventListener Methods
Expand Down

0 comments on commit a79d198

Please sign in to comment.