ビジュアル スタジオ テスト ツールと Rhino モックを使用しています。
現在、Parallel.ForEach を使用してデータを処理するメソッドの単体テストを作成しています。これは私がテストしたい方法です:
var exceptions = new ConcurrentQueue<Exception>();
Parallel.ForEach(messages, emailMessage =>
{
try
{
this.Insert(emailMessage)
}
catch (Exception exception)
{
exceptions.Enqueue(exception);
}
});
if (exceptions.Count > 0)
{
throw new AggregateException(exceptions);
}
そして、これは私のテストです:
[ExpectedException(typeof(AggregateException))]
public void MyTest()
{
this.target = new MyClassToTest();
// stub the throwing of an exception
this.target.Stub(
s => s.Insert(null)).IgnoreArguments().Throw(new ArgumentNullException());
// perform the test
this.target.Send();
}
私のテストでは、挿入メソッドをスタブし、強制的にスローして例外を発生させます。私のテストでの期待は、AggregateException がスローされることです。Visual Studio 内でテストを実行すると正常に動作しますが、継続的インテグレーション サーバーではテストが突然タイムアウトします。これは、ループの反復が並列で行われる Parallel.ForEach の性質に関する問題だと思います。
上記のどこが間違っているか、またはタイムアウトを防ぐためにテストを変更する方法を誰かが見ることができますか?