-
Notifications
You must be signed in to change notification settings - Fork 729
/
EventPump.cs
205 lines (177 loc) · 6.24 KB
/
EventPump.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
// Copyright (c) Charlie Poole, Rob Prouse and Contributors. MIT License - see LICENSE.txt
using System;
using System.Threading;
using NUnit.Framework.Interfaces;
namespace NUnit.Framework.Internal.Execution
{
/// <summary>
/// The EventPumpState enum represents the state of an
/// EventPump.
/// </summary>
public enum EventPumpState
{
/// <summary>
/// The pump is stopped
/// </summary>
Stopped,
/// <summary>
/// The pump is pumping events with no stop requested
/// </summary>
Pumping,
/// <summary>
/// The pump is pumping events but a stop has been requested
/// </summary>
Stopping
}
/// <summary>
/// EventPump pulls Event instances out of an EventQueue and sends
/// them to a ITestListener. It is used to send these events back to
/// the client without using the CallContext of the test
/// runner thread.
/// </summary>
public sealed class EventPump : EventPump<Event, ITestListener>, IDisposable
{
/// <summary>
/// Constructor for standard EventPump
/// </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<Event> events)
: base(eventListener, events, "Standard")
{
}
}
/// <summary>
/// EventPump base class pulls events of any type out of an EventQueue and sends
/// them to any listener. It is used to send events back to
/// the client without using the CallContext of the test
/// runner thread.
/// </summary>
public abstract class EventPump<TEvent, TListener> : IDisposable
where TEvent : IEvent<TListener>
{
private static readonly Logger Log = InternalTrace.GetLogger("EventPump");
#region Instance Variables
/// <summary>
/// The downstream listener to which we send events
/// </summary>
private readonly TListener _eventListener;
/// <summary>
/// The queue that holds our events
/// </summary>
private readonly EventQueue<TEvent> _events;
/// <summary>
/// Thread to do the pumping
/// </summary>
private Thread? _pumpThread;
/// <summary>
/// The current state of the event pump
/// </summary>
private int _pumpState = (int)EventPumpState.Stopped;
#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>
/// <param name="name">Name of the thread and pump</param>
protected EventPump(TListener eventListener, EventQueue<TEvent> events, string name = "Standard")
{
_eventListener = eventListener;
_events = events;
Name = name;
}
#endregion
#region Properties
/// <summary>
/// Gets or sets the current state of the pump
/// </summary>
public EventPumpState PumpState => (EventPumpState)_pumpState;
/// <summary>
/// Gets or sets the name of this EventPump
/// (used only internally and for testing).
/// </summary>
public string? Name { get; set; }
#endregion
#region Public Methods
/// <summary>
/// Dispose stops the pump
/// Disposes the used WaitHandle, too.
/// </summary>
public void Dispose()
{
Stop();
}
/// <summary>
/// Start the pump
/// </summary>
public void Start()
{
if (Interlocked.CompareExchange(ref _pumpState, (int)EventPumpState.Pumping, (int)EventPumpState.Stopped) == (int)EventPumpState.Stopped) // Ignore if already started
{
_pumpThread = new Thread(PumpThreadProc)
{
Name = $"{Name}EventPumpThread",
Priority = ThreadPriority.Highest
};
_pumpThread.Start();
}
}
/// <summary>
/// Tell the pump to stop after emptying the queue.
/// </summary>
public void Stop()
{
if (Interlocked.CompareExchange(ref _pumpState, (int)EventPumpState.Stopping, (int)EventPumpState.Pumping) == (int)EventPumpState.Pumping)
{
_events.Stop();
_pumpThread?.Join();
}
}
#endregion
#region PumpThreadProc
/// <summary>
/// Our thread proc for removing items from the event
/// queue and sending them on. Note that this would
/// need to do more locking if any other thread were
/// removing events from the queue.
/// </summary>
private void PumpThreadProc()
{
Log.Debug($"Starting {Name}");
//ITestListener hostListeners = CoreExtensions.Host.Listeners;
try
{
while (true)
{
var e = _events.Dequeue(PumpState == EventPumpState.Pumping);
if (e is null)
break;
try
{
e.Send(_eventListener);
//e.Send(hostListeners);
}
catch (Exception ex)
{
Log.Error("Exception in event handler {0}", ExceptionHelper.BuildStackTrace(ex));
}
}
Log.Debug("EventPump Terminating");
}
catch (Exception ex)
{
Log.Error("Exception in pump thread {0}", ExceptionHelper.BuildStackTrace(ex));
}
finally
{
_pumpState = (int)EventPumpState.Stopped;
//pumpThread = null;
if (_events.Count > 0)
Log.Error("Event pump thread exiting with {0} events remaining");
}
}
#endregion
}
}