PageRenderTime 50ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/src/NUnit/core/RemoteTestRunner.cs

#
C# | 148 lines | 99 code | 26 blank | 23 comment | 3 complexity | f80ddc3d9ae739d2695d86441f5fb91e MD5 | raw file
Possible License(s): GPL-2.0
  1. // ****************************************************************
  2. // Copyright 2007, 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. namespace NUnit.Core
  7. {
  8. using System;
  9. using System.Reflection;
  10. using System.Collections;
  11. using System.Diagnostics;
  12. /// <summary>
  13. /// RemoteTestRunner is tailored for use as the initial runner to
  14. /// receive control in a remote domain. It provides isolation for the return
  15. /// value by using a ThreadedTestRunner and for the events through use of
  16. /// an EventPump.
  17. /// </summary>
  18. public class RemoteTestRunner : ProxyTestRunner
  19. {
  20. /// <summary>
  21. /// Returns a RemoteTestRunner in the target domain. This method
  22. /// is used in the domain that wants to get a reference to
  23. /// a RemoteTestRunnner and not in the test domain itself.
  24. /// </summary>
  25. /// <param name="targetDomain">AppDomain in which to create the runner</param>
  26. /// <param name="ID">Id for the new runner to use</param>
  27. /// <returns></returns>
  28. public static RemoteTestRunner CreateInstance(AppDomain targetDomain, int ID)
  29. {
  30. #if NET_2_0
  31. System.Runtime.Remoting.ObjectHandle oh = Activator.CreateInstance(
  32. targetDomain,
  33. #else
  34. System.Runtime.Remoting.ObjectHandle oh = targetDomain.CreateInstance(
  35. #endif
  36. Assembly.GetExecutingAssembly().FullName,
  37. typeof(RemoteTestRunner).FullName,
  38. false, BindingFlags.Default, null, new object[] { ID }, null, null, null);
  39. object obj = oh.Unwrap();
  40. return (RemoteTestRunner)obj;
  41. }
  42. static Logger log = InternalTrace.GetLogger("RemoteTestRunner");
  43. #region Constructors
  44. public RemoteTestRunner() : this( 0 ) { }
  45. public RemoteTestRunner( int runnerID ) : base( runnerID ) { }
  46. #endregion
  47. #region Method Overrides
  48. public override bool Load(TestPackage package)
  49. {
  50. log.Info("Loading Test Package " + package.Name );
  51. // Initialize ExtensionHost if not already done
  52. if ( !CoreExtensions.Host.Initialized )
  53. CoreExtensions.Host.InitializeService();
  54. // Delayed creation of downstream runner allows us to
  55. // use a different runner type based on the package
  56. bool useThreadedRunner = package.GetSetting( "UseThreadedRunner", true );
  57. TestRunner runner = new SimpleTestRunner( this.runnerID );
  58. if ( useThreadedRunner )
  59. runner = new ThreadedTestRunner( runner );
  60. this.TestRunner = runner;
  61. if( base.Load (package) )
  62. {
  63. log.Info("Loaded package successfully" );
  64. return true;
  65. }
  66. else
  67. {
  68. log.Info("Package load failed" );
  69. return false;
  70. }
  71. }
  72. public override void Unload()
  73. {
  74. log.Info("Unloading test package");
  75. base.Unload();
  76. }
  77. public override TestResult Run(EventListener listener)
  78. {
  79. return Run( listener, TestFilter.Empty );
  80. }
  81. public override TestResult Run( EventListener listener, ITestFilter filter )
  82. {
  83. log.Debug("Run");
  84. QueuingEventListener queue = new QueuingEventListener();
  85. StartTextCapture( queue );
  86. using( EventPump pump = new EventPump( listener, queue.Events, true ) )
  87. {
  88. pump.Start();
  89. return base.Run( queue, filter );
  90. }
  91. }
  92. public override void BeginRun( EventListener listener )
  93. {
  94. BeginRun( listener, TestFilter.Empty );
  95. }
  96. public override void BeginRun( EventListener listener, ITestFilter filter )
  97. {
  98. log.Debug("BeginRun");
  99. QueuingEventListener queue = new QueuingEventListener();
  100. StartTextCapture( queue );
  101. EventPump pump = new EventPump( listener, queue.Events, true);
  102. pump.Start(); // Will run till RunFinished is received
  103. // TODO: Make sure the thread is cleaned up if we abort the run
  104. base.BeginRun( queue, filter );
  105. }
  106. private void StartTextCapture( EventListener queue )
  107. {
  108. TestExecutionContext.CurrentContext.Out = new EventListenerTextWriter(queue, TestOutputType.Out);
  109. TestExecutionContext.CurrentContext.Error = new EventListenerTextWriter(queue, TestOutputType.Error);
  110. TestExecutionContext.CurrentContext.TraceWriter = new EventListenerTextWriter(queue, TestOutputType.Trace);
  111. TestExecutionContext.CurrentContext.Tracing = true;
  112. TestExecutionContext.CurrentContext.LogWriter = new EventListenerTextWriter(queue, TestOutputType.Log);
  113. TestExecutionContext.CurrentContext.Logging = true;
  114. }
  115. #endregion
  116. private void CurrentDomain_DomainUnload(object sender, EventArgs e)
  117. {
  118. log.Debug(AppDomain.CurrentDomain.FriendlyName + " unloaded");
  119. InternalTrace.Flush();
  120. }
  121. }
  122. }