1 // ***********************************************************************
2 // Copyright (c) 2006-2016 Charlie Poole
4 // Permission is hereby granted, free of charge, to any person obtaining
5 // a copy of this software and associated documentation files (the
6 // "Software"), to deal in the Software without restriction, including
7 // without limitation the rights to use, copy, modify, merge, publish,
8 // distribute, sublicense, and/or sell copies of the Software, and to
9 // permit persons to whom the Software is furnished to do so, subject to
10 // the following conditions:
12 // The above copyright notice and this permission notice shall be
13 // included in all copies or substantial portions of the Software.
15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 // ***********************************************************************
25 #define NUNIT_FRAMEWORK
31 using System.Threading;
32 using NUnit.Framework.Interfaces;
34 namespace NUnit.Framework.Internal.Execution
37 /// The EventPumpState enum represents the state of an
40 public enum EventPumpState
43 /// The pump is stopped
48 /// The pump is pumping events with no stop requested
53 /// The pump is pumping events but a stop has been requested
59 /// EventPump pulls events out of an EventQueue and sends
60 /// them to a listener. It is used to send events back to
61 /// the client without using the CallContext of the test
64 public class EventPump : IDisposable
66 static readonly Logger log = InternalTrace.GetLogger("EventPump");
68 #region Instance Variables
71 /// The downstream listener to which we send events
73 private readonly ITestListener _eventListener;
76 /// The queue that holds our events
78 private readonly EventQueue _events;
81 /// Thread to do the pumping
83 private Thread _pumpThread;
86 /// The current state of the eventpump
88 private int _pumpState = (int)EventPumpState.Stopped;
96 /// <param name="eventListener">The EventListener to receive events</param>
97 /// <param name="events">The event queue to pull events from</param>
98 public EventPump( ITestListener eventListener, EventQueue events)
100 _eventListener = eventListener;
109 /// Gets or sets the current state of the pump
111 public EventPumpState PumpState
115 return (EventPumpState)_pumpState;
120 /// Gets or sets the name of this EventPump
121 /// (used only internally and for testing).
123 public string Name { get; set; }
127 #region Public Methods
130 /// Dispose stops the pump
131 /// Disposes the used WaitHandle, too.
133 public void Dispose()
143 if ( Interlocked.CompareExchange (ref _pumpState, (int)EventPumpState.Pumping, (int)EventPumpState.Stopped) == (int)EventPumpState.Stopped) // Ignore if already started
145 _pumpThread = new Thread (PumpThreadProc)
147 Name = "EventPumpThread" + Name,
148 //Priority = ThreadPriority.Highest
156 /// Tell the pump to stop after emptying the queue.
160 if (Interlocked.CompareExchange (ref _pumpState, (int)EventPumpState.Stopping, (int)EventPumpState.Pumping) == (int)EventPumpState.Pumping)
168 #region PumpThreadProc
171 /// Our thread proc for removing items from the event
172 /// queue and sending them on. Note that this would
173 /// need to do more locking if any other thread were
174 /// removing events from the queue.
176 private void PumpThreadProc()
178 //ITestListener hostListeners = CoreExtensions.Host.Listeners;
183 Event e = _events.Dequeue( PumpState == EventPumpState.Pumping );
188 e.Send(_eventListener);
189 //e.Send(hostListeners);
193 log.Error( "Exception in event handler\r\n {0}", ex );
199 log.Error( "Exception in pump thread", ex );
203 _pumpState = (int)EventPumpState.Stopped;
205 if (_events.Count > 0)
206 log.Error("Event pump thread exiting with {0} events remaining");