我在对一个触发线程开始和结束事件的类进行单元测试时遇到了问题。以下是有问题的源码简化版本:
public class ThreadRunner
{
private bool keepRunning;
public event EventHandler Started;
public event EventHandler Finished;
public void StartThreadTest()
{
this.keepRunning = true;
var thread = new Thread(new ThreadStart(this.LongRunningMethod));
thread.Start();
}
public void FinishThreadTest()
{
this.keepRunning = false;
}
protected void OnStarted()
{
if (this.Started != null)
this.Started(this, new EventArgs());
}
protected void OnFinished()
{
if (this.Finished != null)
this.Finished(this, new EventArgs());
}
private void LongRunningMethod()
{
this.OnStarted();
while (this.keepRunning)
Thread.Sleep(100);
this.OnFinished();
}
}
然后我有一个测试来检查LongRunningMethod
完成后是否会触发Finished
事件,如下所示:
[TestClass]
public class ThreadRunnerTests
{
[TestMethod]
public void CheckFinishedEventFiresTest()
{
var threadTest = new ThreadRunner();
bool finished = false;
object locker = new object();
threadTest.Finished += delegate(object sender, EventArgs e)
{
lock (locker)
{
finished = true;
Monitor.Pulse(locker);
}
};
threadTest.StartThreadTest();
threadTest.FinishThreadTest();
lock (locker)
{
Monitor.Wait(locker, 1000);
Assert.IsTrue(finished);
}
}
}
这里的想法是测试将阻塞最多一秒钟 - 或者直到触发Finish
事件 - 然后检查finished
标志是否设置。
显然,我做错了什么,有时测试会通过,有时不会。调试似乎非常困难,因为我期望被命中的断点(例如OnFinished
方法)并不总是出现。
我假设这只是我对线程工作方式的误解,希望有人能启发我。