PageRenderTime 1750ms CodeModel.GetById 62ms RepoModel.GetById 40ms app.codeStats 0ms

/src/NUnit/util/RemoteTestAgent.cs

#
C# | 127 lines | 86 code | 22 blank | 19 comment | 0 complexity | 31c91afa0b941a633bb00cbbc2e30a1a MD5 | raw file
Possible License(s): GPL-2.0
  1. // ****************************************************************
  2. // Copyright 2008, Charlie Poole
  3. // This is free software licensed under the NUnit license. You may
  4. // obtain a copy of the license at http://nunit.org
  5. // ****************************************************************
  6. using System;
  7. using System.Threading;
  8. using System.Collections;
  9. using System.IO;
  10. using System.Reflection;
  11. using System.Runtime.Remoting;
  12. using System.Runtime.Remoting.Channels;
  13. using System.Runtime.Remoting.Channels.Tcp;
  14. using NUnit.Core;
  15. namespace NUnit.Util
  16. {
  17. /// <summary>
  18. /// RemoteTestAgent represents a remote agent executing in another process
  19. /// and communicating with NUnit by TCP. Although it is similar to a
  20. /// TestServer, it does not publish a Uri at which clients may connect
  21. /// to it. Rather, it reports back to the sponsoring TestAgency upon
  22. /// startup so that the agency may in turn provide it to clients for use.
  23. /// </summary>
  24. public class RemoteTestAgent : TestAgent
  25. {
  26. static Logger log = InternalTrace.GetLogger(typeof(RemoteTestAgent));
  27. #region Fields
  28. private ManualResetEvent stopSignal = new ManualResetEvent(false);
  29. #endregion
  30. #region Constructor
  31. /// <summary>
  32. /// Construct a RemoteTestAgent
  33. /// </summary>
  34. public RemoteTestAgent( Guid agentId, TestAgency agency )
  35. : base(agentId, agency) { }
  36. #endregion
  37. #region Properties
  38. public int ProcessId
  39. {
  40. get { return System.Diagnostics.Process.GetCurrentProcess().Id; }
  41. }
  42. #endregion
  43. #region Public Methods
  44. public override TestRunner CreateRunner(int runnerID)
  45. {
  46. return new AgentRunner(runnerID);
  47. }
  48. public override bool Start()
  49. {
  50. log.Info("Agent starting");
  51. try
  52. {
  53. this.Agency.Register( this );
  54. log.Debug( "Registered with TestAgency" );
  55. }
  56. catch( Exception ex )
  57. {
  58. log.Error( "RemoteTestAgent: Failed to register with TestAgency", ex );
  59. return false;
  60. }
  61. return true;
  62. }
  63. public override void Stop()
  64. {
  65. log.Info( "Stopping" );
  66. // This causes an error in the client because the agent
  67. // database is not thread-safe.
  68. //if ( agency != null )
  69. // agency.ReportStatus(this.ProcessId, AgentStatus.Stopping);
  70. stopSignal.Set();
  71. }
  72. public void WaitForStop()
  73. {
  74. stopSignal.WaitOne();
  75. }
  76. #endregion
  77. #region Nested AgentRunner class
  78. class AgentRunner : ProxyTestRunner
  79. {
  80. private ITestRunnerFactory factory;
  81. public AgentRunner(int runnerID)
  82. : base(runnerID)
  83. {
  84. this.factory = new InProcessTestRunnerFactory();
  85. }
  86. public override bool Load(TestPackage package)
  87. {
  88. this.TestRunner = factory.MakeTestRunner(package);
  89. return base.Load(package);
  90. }
  91. public override IList AssemblyInfo
  92. {
  93. get
  94. {
  95. IList result = base.AssemblyInfo;
  96. string name = Path.GetFileName(Assembly.GetEntryAssembly().Location);
  97. foreach( TestAssemblyInfo info in result )
  98. info.ModuleName = name;
  99. return result;
  100. }
  101. }
  102. }
  103. #endregion
  104. }
  105. }