/// <summary> /// 执行测试,并返回测试结果。 /// </summary> /// <param name="config"></param> public void Test(TestingData data) { var thread = new Thread(new ParameterizedThreadStart((obj) => { var watch = new Stopwatch(); try { var item = (obj as TestingItem); watch.Start(); item.TestDelegate(data); watch.Stop(); } catch (ThreadAbortException ex) { // 超时。 watch.Stop(); data.Exception = ex; data.OK = false; data.ErrorType = ErrorType.Timeout; } catch (Exception ex) { // 测试引发了异常。 watch.Stop(); data.Exception = ex; data.OK = false; data.ErrorType = ErrorType.TestingOccurException; } finally { data.EndTime = DateTime.Now; data.TotalTime = new TimeSpan((long)(((double)watch.ElapsedTicks / Stopwatch.Frequency) * 1000 * 10000)); data.Completed = true; } })); data.StartTime = DateTime.Now; thread.Start(this); if (!thread.Join(data.Timeout)) { thread.Abort(); thread.Join(); } }
private void OnTestingItemEnd(TestingData data) { if (this.TestingItemEnd != null) this.TestingItemEnd(this, data); }
private void OnTestingItemBegin(TestingData e) { if (this.TestingItemBegin != null) this.TestingItemBegin(this, e); }
/// <summary> /// 开始测试。 /// </summary> /// <param name="groupName">要进行测试的测试组名。</param> /// <param name="keep">指示只执行一次测试,还是持续进行测试。</param> public void BeginTest(string groupName, bool keep) { // 开始进行测试 if (this.Config == null) throw new ArgumentNullException("Config"); var group = this.Config[ConfigKeys.TestingGroup] as TestingGroup; if (group == null) throw new ArgumentException("配置项缺乏 ConfigKeys.TestingGroup"); this.Status = TestingStatus.Running; ThreadPool.QueueUserWorkItem(status => { TestingConfiguration input = new TestingConfiguration(); foreach (var point in group.Points) { for (var i = 0; i < point.Times; ++i) { var item = Activator.CreateInstance(Type.GetType(point.Type)) as TestingItem; var data = new TestingData() { Config = this.Config, InputData = input != null ? new TestingConfiguration(point.Config.Concat(input).ToList()) : point.Config, Point = point, Time = i, Timeout = new TimeSpan(point.Timeout * 10000), StartTime = DateTime.Now }; this.OnTestingItemBegin(data); item.Test(data); this.OnTestingItemEnd(data); if (data.OutputData != null) { input.AddRange(data.OutputData); } } } this.Status = TestingStatus.Stop; }); }