[Adapdev-commits] Adapdev/src/Adapdev.UnitTest.Core RunTestCommand.cs,NONE,1.1 RunTestIterationComma
Status: Beta
Brought to you by:
intesar66
From: Sean M. <int...@us...> - 2005-11-01 05:41:53
|
Update of /cvsroot/adapdev/Adapdev/src/Adapdev.UnitTest.Core In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv3474/src/Adapdev.UnitTest.Core Modified Files: Adapdev.UnitTest.Core.csproj RunTestFixtureCommand.cs Added Files: RunTestCommand.cs RunTestIterationCommand.cs Log Message: Added multi-threading for repeated tests --- NEW FILE: RunTestCommand.cs --- using System; using System.Diagnostics; using System.IO; using System.Reflection; using System.Threading; using Adapdev.Commands; using Adapdev.Diagnostics; using Adapdev.Threading; using log4net; namespace Adapdev.UnitTest.Core { /// <summary> /// Summary description for NonThreadedRunTestCommand. /// </summary> public class RunTestCommand : ICommand { // create the logger private static readonly ILog log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); // setup the logging levels private bool _debugMode = log.IsDebugEnabled; private bool _infoMode = log.IsInfoEnabled; private TestEventDispatcher _dispatcher = null; private Test _test = null; private TestFixture _testFixture = null; private Type _type = null; private object o = null; private TestFixtureIteration _testFixtureIteration = null; public RunTestCommand(TestEventDispatcher dispatcher, Test test, TestFixture tf, Type type, object o, TestFixtureIteration tfi) { this._dispatcher = dispatcher; this._test = test; this._testFixture = tf; this.o = o; this._testFixtureIteration = tfi; this._type = type; } public void Execute() { try { MethodInfo m = _type.GetMethod(_test.Method); TestResult tr = new TestResult(_test); tr.TestId = _test.Id; tr.Description = _test.Description; if(this._debugMode) log.Debug("Running T " + _test.Name); if (!_test.Ignore && _test.ShouldRun) { if(_dispatcher != null)_dispatcher.OnTestStarted(new TestEventArgs(_test)); this.RunTestIteration(tr, m); } else { TestIteration ti = new TestIteration(); ti.Name = _test.Name; ti.State = TestState.Ignore; ti.Result = _test.IgnoreReason; tr.AddIteration(ti); } if (_testFixture.ShouldShow) _testFixtureIteration.AddTestResult(tr); if(_dispatcher != null)_dispatcher.OnTestCompleted(new TestResultEventArgs(tr)); } catch (Exception e) { Console.WriteLine(e.Message + " " + e.StackTrace); } finally { } } private void RunTestIteration(TestResult testResult, MethodInfo method) { if(this._testFixture.IsMultiThreaded) { using(ThreadPoolWait threadPool = new ThreadPoolWait()) { for (int i = 1; i <= _test.RepeatCount; i++) { RunTestIterationCommand command = new RunTestIterationCommand(this._dispatcher, this._test, this._testFixture, this._type, this.o, this._testFixtureIteration, method, i, testResult); threadPool.QueueUserWorkItem(new WaitCallback(command.Execute)); } threadPool.WaitOne(); } } else { for (int i = 1; i <= _test.RepeatCount; i++) { new RunTestIterationCommand(this._dispatcher, this._test, this._testFixture, this._type, this.o, this._testFixtureIteration, method, i, testResult).Execute(); } } } public void Execute(object o){this.Execute();} } } Index: Adapdev.UnitTest.Core.csproj =================================================================== RCS file: /cvsroot/adapdev/Adapdev/src/Adapdev.UnitTest.Core/Adapdev.UnitTest.Core.csproj,v retrieving revision 1.5 retrieving revision 1.6 diff -C2 -d -r1.5 -r1.6 *** Adapdev.UnitTest.Core.csproj 26 Oct 2005 05:27:45 -0000 1.5 --- Adapdev.UnitTest.Core.csproj 1 Nov 2005 05:41:40 -0000 1.6 *************** *** 182,186 **** /> <File ! RelPath = "RunNonThreadedTestCommand.cs" SubType = "Code" BuildAction = "Compile" --- 182,186 ---- /> <File ! RelPath = "RunTestCommand.cs" SubType = "Code" BuildAction = "Compile" *************** *** 192,196 **** /> <File ! RelPath = "RunThreadedTestCommand.cs" SubType = "Code" BuildAction = "Compile" --- 192,196 ---- /> <File ! RelPath = "RunTestIterationCommand.cs" SubType = "Code" BuildAction = "Compile" Index: RunTestFixtureCommand.cs =================================================================== RCS file: /cvsroot/adapdev/Adapdev/src/Adapdev.UnitTest.Core/RunTestFixtureCommand.cs,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** RunTestFixtureCommand.cs 26 Oct 2005 05:27:45 -0000 1.1 --- RunTestFixtureCommand.cs 1 Nov 2005 05:41:40 -0000 1.2 *************** *** 96,114 **** if(this._testFixture.IsMultiThreaded) { - // ArrayList threads = new ArrayList(); - // foreach (Test test in this._testFixture.GetTests()) - // { - // RunNonThreadedTestCommand command = new RunNonThreadedTestCommand(this._dispatcher, test, this._testFixture, this._type, this._o, this._testFixtureIteration); - // Thread thread = new Thread(new ThreadStart(command.Execute)); - // threads.Add(thread); - // thread.Start(); - // } - // foreach(Thread t in threads) t.Join(); - using(ThreadPoolWait threadPool = new ThreadPoolWait()) { foreach (Test test in this._testFixture.GetTests()) { ! RunNonThreadedTestCommand command = new RunNonThreadedTestCommand(this._dispatcher, test, this._testFixture, this._type, this._o, this._testFixtureIteration); threadPool.QueueUserWorkItem(new WaitCallback(command.Execute)); } --- 96,104 ---- if(this._testFixture.IsMultiThreaded) { using(ThreadPoolWait threadPool = new ThreadPoolWait()) { foreach (Test test in this._testFixture.GetTests()) { ! RunTestCommand command = new RunTestCommand(this._dispatcher, test, this._testFixture, this._type, this._o, this._testFixtureIteration); threadPool.QueueUserWorkItem(new WaitCallback(command.Execute)); } *************** *** 120,124 **** foreach (Test test in this._testFixture.GetTests()) { ! new RunNonThreadedTestCommand(this._dispatcher, test, this._testFixture, this._type, this._o, this._testFixtureIteration).Execute(); } } --- 110,114 ---- foreach (Test test in this._testFixture.GetTests()) { ! new RunTestCommand(this._dispatcher, test, this._testFixture, this._type, this._o, this._testFixtureIteration).Execute(); } } --- NEW FILE: RunTestIterationCommand.cs --- using System; using System.Diagnostics; using System.IO; using System.Reflection; using System.Threading; using Adapdev.Diagnostics; namespace Adapdev.UnitTest.Core { /// <summary> /// Summary description for RunTestIterationCommand. /// </summary> public class RunTestIterationCommand { private TestEventDispatcher _dispatcher = null; private Test _test = null; private TestFixture _testFixture = null; private object o = null; private int i = 0; private Type _type = null; private TestFixtureIteration _testFixtureIteration = null; private MethodInfo _method = null; private IPerfTimer timer = PerfTimerFactory.GetPerfTimer(PerfTimerType.HIRESSECONDS); private TestResult tr = null; public RunTestIterationCommand(TestEventDispatcher dispatcher, Test test, TestFixture tf, Type type, object o, TestFixtureIteration tfi, MethodInfo method, int iteration, TestResult tr) { this._dispatcher = dispatcher; this._test = test; this._testFixture = tf; this.o = o; this.i = iteration; this._type = type; this._testFixtureIteration = tfi; this._method = method; this.tr = tr; } public void Execute() { TextWriter consoleOut = Console.Out; TextWriter errorOut = Console.Error; StringWriter consoleWriter = new StringWriter(); StringWriter errorWriter = new StringWriter(); StringWriter debugWriter = new StringWriter(); StringWriter traceWriter = new StringWriter(); Console.SetOut(consoleWriter); Console.SetError(errorWriter); TextWriterTraceListener debug = new TextWriterTraceListener(debugWriter); TextWriterTraceListener error = new TextWriterTraceListener(traceWriter); Debug.Listeners.Add(debug); Trace.Listeners.Add(error); if (_test.RepeatDelay > 0) { Thread.Sleep(_test.RepeatDelay); Console.WriteLine("Sleeping..." + _test.RepeatDelay); } TestIteration ti = new TestIteration(); ti.Name = _test.Name; ti.Iteration = i; ti.Thread = AppDomain.GetCurrentThreadId().ToString(); long kStart = 0; long kEnd = 0; if(_dispatcher != null)_dispatcher.OnTestIterationStarted(new TestEventArgs(_test)); try { this.RunTestSetUps(_dispatcher, _testFixture, _test.Name, o, _type); kStart = Process.GetCurrentProcess().WorkingSet; timer.Start(); new RunMethodCommand(this._method, o).Execute(); timer.Stop(); ti.Duration = timer.Duration; kEnd = Process.GetCurrentProcess().WorkingSet; ti.MemoryUsed = (kEnd - kStart)/1024; if (_test.MaxKMemory > 0 && _test.MaxKMemory < ti.MemoryUsed) { ti.State = TestState.Fail; ti.Result = "Memory usage exceeded MaxK limit of " + _test.MaxKMemory; } else if (_test.MinOperationsPerSecond > 0 && ti.GetOpsPerSecond() < _test.MinOperationsPerSecond) { ti.State = TestState.Fail; ti.Result = "Ops per second was less than minimum limit of " + _test.MinOperationsPerSecond; } else if (_test.ExpectedExceptionType != null && _test.ExpectedExceptionType.Length > 0) { ti.State = TestState.Fail; ti.Result = "ExceptedException type " + _test.ExpectedExceptionType + " was not thrown."; } else { ti.State = TestState.Pass; } this.RunTestTearDowns(_dispatcher, _testFixture, _test.Name, o, _type); } catch (Exception e) { timer.Stop(); ti.Duration = timer.Duration; kEnd = Process.GetCurrentProcess().WorkingSet; if (_test.ExpectedExceptionType != null && _test.ExpectedExceptionType.Length > 0) { Type t = null; foreach(Assembly ass in AppDomain.CurrentDomain.GetAssemblies()) { try { t = ass.GetType(_test.ExpectedExceptionType, true, true); break; } catch(Exception){} } if(t == null) throw new TypeLoadException("Unable to locate " + _test.ExpectedExceptionType + ". Please make sure it is correct."); if (e.InnerException.GetType().IsSubclassOf(t) || e.InnerException.GetType() == t) { if(_test.ExpectedExceptionMessage != null && _test.ExpectedExceptionMessage.Length > 0) { if(_test.ExpectedExceptionMessage.ToLower() == e.InnerException.Message.ToLower()) { ti.Result = "Expected Exception: " + _test.ExpectedExceptionType + " was thrown. Message: " + e.InnerException.Message; ti.State = TestState.Pass; } else { ti.Result = "Expected Exception: " + _test.ExpectedExceptionType + " was thrown, but wrong message. Message: " + e.InnerException.Message + " - Expected: " + _test.ExpectedExceptionMessage; ti.State = TestState.Fail; } } else { ti.Result = "Expected Exception: " + _test.ExpectedExceptionType + " was thrown. Message: " + e.InnerException.Message; ti.State = TestState.Pass; } } else { ti.Result = "Expected Exception: " + _test.ExpectedExceptionType + " was NOT thrown. Message: " + e.InnerException.Message; ti.State = TestState.Fail; } ti.ExceptionType = e.InnerException.GetType().FullName; ti.FullStackTrace = e.InnerException.StackTrace; ti.MemoryUsed = (kEnd - kStart)/1024; } // TODO : Fix incrementing of tests in GUI when IgnoreException is thrown else if(e.InnerException.GetType() == typeof(IgnoreException) || e.InnerException.GetType() == typeof(IgnoreException)) { ti.Result = e.InnerException.Message; ti.State = TestState.ForcedIgnore; ti.ExceptionType = e.InnerException.GetType().FullName; ti.FullStackTrace = e.InnerException.StackTrace; } else { ti.Result = e.InnerException.Message; ti.ExceptionType = e.InnerException.GetType().FullName; ti.FullStackTrace = e.InnerException.StackTrace; ti.State = TestState.Fail; } this.RunTestTearDowns(_dispatcher, _testFixture, _test.Name, o, _type); } ti.ConsoleOutput = consoleWriter.ToString(); ti.ConsoleError = errorWriter.ToString(); ti.DebugOutput = debugWriter.ToString(); ti.TraceOutput = traceWriter.ToString(); lock(this){tr.AddIteration(ti);} if(_dispatcher != null)_dispatcher.OnTestIterationCompleted(new TestIterationEventArgs(ti)); Console.SetOut(consoleOut); Console.SetError(errorOut); Debug.Listeners.Remove(debug); Trace.Listeners.Remove(error); debug.Dispose(); error.Dispose(); } protected void RunTestSetUps(TestEventDispatcher _dispatcher, TestFixture tf, string name, object instance, Type type) { foreach (TestHelper t in tf.GetTestSetUps()) { try { // Console.WriteLine(t.Method); if (t.Test.Length < 1 || t.Test.ToLower().Equals(name.ToLower())) { if(_dispatcher != null)_dispatcher.OnBaseTestHelperStarted(new BaseTestHelperEventArgs(t)); MethodInfo m = type.GetMethod(t.Method); m.Invoke(instance, null); } } catch (Exception e) { Console.Write(e); } } } protected void RunTestTearDowns(TestEventDispatcher _dispatcher, TestFixture tf, string name, object instance, Type type) { foreach (TestHelper t in tf.GetTestTearDowns()) { try { if (t.Test.Length < 1 || t.Test.ToLower().Equals(name.ToLower())) { if(_dispatcher != null)_dispatcher.OnBaseTestHelperStarted(new BaseTestHelperEventArgs(t)); MethodInfo m = type.GetMethod(t.Method); m.Invoke(instance, null); } } catch (Exception e) { Console.Write(e); } } } public void Execute(object o){this.Execute();} } } |