2
我正在对一个组件进行单元测试,该组件可以处理任意数量的线程并在这些线程上引发事件。我写了我使用断言,我期待的事件已经提出了一个简单的事件寄存器:多线程应用程序中的单元测试事件
public class EventRegister
{
private readonly object _lock = new object();
public int Count { get; private set; }
public void Increment()
{
lock (_lock)
{
Count++;
Monitor.Pulse(_lock);
}
}
public static EventRegister operator ++(EventRegister counter)
{
counter.Count++;
return counter;
}
public static implicit operator int(EventRegister counter)
{
return counter.Count;
}
public bool HasFired { get { return Count > 0; } }
public void Wait(int unitCount)
{
Wait(unitCount, TimeSpan.MinValue);
}
public void Wait(int unitCount, TimeSpan timeout)
{
lock (_lock)
{
var start = DateTime.Now;
while (Count < unitCount)
{
if (timeout != TimeSpan.MinValue && !Debugger.IsAttached)
{
Monitor.Wait(_lock, timeout);
if (DateTime.Now-start>timeout)
throw new TimeoutException();
}
else
Monitor.Wait(_lock);
}
}
}
}
这工作得很好,我足以与它高兴,但我重新发明轮子呢? NUnit或MOQ(或任何其他lib)是否已经包含一些其他机制来执行相同的操作?
你开始了,我会加入你,在BitBucket上见你? https://bitbucket.org/mylesmcdonnell – 2012-02-06 21:04:14