File: TestAgency.cs

package info (click to toggle)
mono 4.6.2.7+dfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 778,148 kB
  • ctags: 914,052
  • sloc: cs: 5,779,509; xml: 2,773,713; ansic: 432,645; sh: 14,749; makefile: 12,361; perl: 2,488; python: 1,434; cpp: 849; asm: 531; sql: 95; sed: 16; php: 1
file content (353 lines) | stat: -rw-r--r-- 7,810 bytes parent folder | download | duplicates (2)
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
using System;
using System.IO;
using System.Threading;
using System.Diagnostics;
using System.Collections;
using System.Collections.Specialized;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Services;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
using NUnit.Core;

namespace NUnit.Util
{
	/// <summary>
	/// Enumeration of agent types used to request agents
	/// </summary>
	[Flags]
	public enum AgentType
	{
		Default = 0,
		DomainAgent = 1, // NYI
		ProcessAgent = 2
	}

	/// <summary>
	/// Enumeration used to report AgentStatus
	/// </summary>
	public enum AgentStatus
	{
		Unknown,
		Starting,
		Ready,
		Busy,
		Stopping
	}

	/// <summary>
	/// The TestAgency class provides RemoteTestAgents
	/// on request and tracks their status. Agents
	/// are wrapped in an instance of the TestAgent
	/// class. Multiple agent types are supported
	/// but only one, ProcessAgent is implemented
	/// at this time.
	/// </summary>
	public class TestAgency : ServerBase, IService
	{
		#region Private Fields
		private AgentDataBase agentData = new AgentDataBase();

		private AgentType supportedAgentTypes = AgentType.ProcessAgent;

		private AgentType defaultAgentType = AgentType.ProcessAgent;
		#endregion

		#region Constructors
		public TestAgency() : this( "TestAgency", 9100 ) { }

		public TestAgency( string uri, int port ) : base( uri, port ) { }
		#endregion

		#region Static Property - TestAgentExePath
		public static string TestAgentExePath
		{
			get
			{
				string agentPath = "nunit-agent.exe";
			
				if ( !File.Exists(agentPath) )
				{
					DirectoryInfo dir = new DirectoryInfo( Environment.CurrentDirectory );
					if ( dir.Parent.Name == "bin" )
						dir = dir.Parent.Parent.Parent.Parent;
				
					string path = PathUtils.Combine( dir.FullName, "NUnitTestServer", "nunit-agent-exe", 
						"bin", NUnitFramework.BuildConfiguration, "nunit-agent.exe" );
					if( File.Exists( path ) )
						agentPath = path;
				}

				return agentPath;
			}
		}
		#endregion

		#region ServerBase Overrides
		public override void Stop()
		{
			foreach( AgentRecord r in agentData )
			{
				if ( !r.Process.HasExited )
				{
					if ( r.Agent != null )
						r.Agent.Stop();

					//r.Process.Kill();
				}
			}

			agentData.Clear();

			base.Stop ();
		}
		#endregion

		#region Public Methods - Called by Agents
		public void Register( RemoteTestAgent agent, int pid )
		{
			AgentRecord r = agentData[pid];
			if ( r == null )
				throw new ArgumentException( "Specified process is not in the agency database", "pid" );
			r.Agent = agent;
		}

		public void ReportStatus( int pid, AgentStatus status )
		{
			AgentRecord r = agentData[pid];

			if ( r == null )
				throw new ArgumentException( "Specified process is not in the agency database", "pid" );

			r.Status = status;
		}
		#endregion

		#region Public Methods - Called by Clients
		public TestAgent GetAgent()
		{
			return GetAgent( AgentType.Default, 5000 );
		}

		public TestAgent GetAgent( AgentType type )
		{
			return GetAgent( type, 5000 );
		}

		public TestAgent GetAgent(AgentType type, int waitTime)
		{
			if ( type == AgentType.Default )
				type = defaultAgentType;

			if ( (type & supportedAgentTypes) == 0 )
				throw new ArgumentException( 
					string.Format( "AgentType {0} is not supported by this agency", type ),
					"type" );

			AgentRecord r = FindAvailableRemoteAgent(type);
			if ( r == null )
				r = CreateRemoteAgent(type, waitTime);

			return new TestAgent( this, r.Process.Id, r.Agent );
		}

		public void ReleaseAgent( TestAgent agent )
		{
			AgentRecord r = agentData[agent.Id];
			if ( r == null )
				NTrace.Error( string.Format( "Unable to release agent {0} - not in database", agent.Id ) );
			else
			{
				r.Status = AgentStatus.Ready;
				NTrace.Debug( "Releasing agent " + agent.Id.ToString() );
			}
		}

		public void DestroyAgent( TestAgent agent )
		{
			AgentRecord r = agentData[agent.Id];
			if ( r != null )
			{
				if( !r.Process.HasExited )
					r.Agent.Stop();
				agentData[r.Process.Id] = null;
			}
		}
		#endregion

		#region Helper Methods
		private int LaunchAgentProcess()
		{
			//ProcessStartInfo startInfo = new ProcessStartInfo( TestAgentExePath, ServerUtilities.MakeUrl( this.uri, this.port ) );
			//startInfo.CreateNoWindow = true;
			Process p = new Process();
			if ( Type.GetType( "Mono.Runtime", false ) != null )
			{
				p.StartInfo.FileName = @"C:\Program Files\mono-1.2.5\bin\mono.exe";
				p.StartInfo.Arguments = TestAgentExePath + " " + ServerUtilities.MakeUrl( this.uri, this.port );
			}
			else
			{
				p.StartInfo.FileName = TestAgentExePath;
				p.StartInfo.Arguments = ServerUtilities.MakeUrl( this.uri, this.port );
			}
			
			//NTrace.Debug( "Launching {0}" p.StartInfo.FileName );
			p.Start();
			agentData.Add( new AgentRecord( p.Id, p, null, AgentStatus.Starting ) );
			return p.Id;
		}

		private AgentRecord FindAvailableRemoteAgent(AgentType type)
		{
			foreach( AgentRecord r in agentData )
				if ( r.Status == AgentStatus.Ready )
				{
					NTrace.DebugFormat( "Reusing agent {0}", r.Id );
					r.Status = AgentStatus.Busy;
					return r;
				}

			return null;
		}

		private AgentRecord CreateRemoteAgent(AgentType type, int waitTime)
		{
			int pid = LaunchAgentProcess();

			NTrace.DebugFormat( "Waiting for agent {0} to register", pid );
			while( waitTime > 0 )
			{
				int pollTime = Math.Min( 200, waitTime );
				Thread.Sleep( pollTime );
				waitTime -= pollTime;
				if ( agentData[pid].Agent != null )
				{
					NTrace.DebugFormat( "Returning new agent record {0}", pid ); 
					return agentData[pid];
				}
			}

			return null;
		}
		#endregion

		#region IService Members

		public void UnloadService()
		{
			this.Stop();
		}

		public void InitializeService()
		{
			this.Start();
		}

		#endregion

		#region Nested Class - AgentRecord
		private class AgentRecord
		{
			public int Id;
			public Process Process;
			public RemoteTestAgent Agent;
			public AgentStatus Status;

			public AgentRecord( int id, Process p, RemoteTestAgent a, AgentStatus s )
			{
				this.Id = id;
				this.Process = p;
				this.Agent = a;
				this.Status = s;
			}

		}
		#endregion

		#region Nested Class - AgentDataBase
		/// <summary>
		///  A simple class that tracks data about this
		///  agencies active and available agents
		/// </summary>
		private class AgentDataBase : IEnumerable
		{
			private ListDictionary agentData = new ListDictionary();

			public AgentRecord this[int id]
			{
				get { return (AgentRecord)agentData[id]; }
				set
				{
					if ( value == null )
						agentData.Remove( id );
					else
						agentData[id] = value;
				}
			}

			public AgentRecord this[RemoteTestAgent agent]
			{
				get
				{
					foreach( System.Collections.DictionaryEntry entry in agentData )
					{
						AgentRecord r = (AgentRecord)entry.Value;
						if ( r.Agent == agent )
							return r;
					}

					return null;
				}
			}

			public void Add( AgentRecord r )
			{
				agentData[r.Id] = r;
			}

			public void Clear()
			{
				agentData.Clear();
			}

			#region IEnumerable Members
			public IEnumerator GetEnumerator()
			{
				return new AgentDataEnumerator( agentData );
			}
			#endregion

			#region Nested Class - AgentDataEnumerator
			public class AgentDataEnumerator : IEnumerator
			{
				IEnumerator innerEnum;

				public AgentDataEnumerator( IDictionary list )
				{
					innerEnum = list.GetEnumerator();
				}

				#region IEnumerator Members
				public void Reset()
				{
					innerEnum.Reset();
				}

				public object Current
				{
					get { return ((DictionaryEntry)innerEnum.Current).Value; }
				}

				public bool MoveNext()
				{
					return innerEnum.MoveNext();
				}
				#endregion
			}
			#endregion
		}

		#endregion
	}
}