확장 메서드가 있습니다. 여기있다 :이를 테스트하려면비 결정적 다중 스레드 테스트
public static void BeginInvokeWithAutoEnd(this EventHandler handler, object sender, EventArgs eventArgs)
{
var buffer = handler;
buffer.BeginInvoke(sender, eventArgs, buffer.EndInvoke, null);
}
는 다음과 같은 방법이있다 : 자체가 잘 작동
[TestMethod]
public void BeginInvokeWithAutoEnd_SubscribedMethodThrowsException_ExceptionCanBeCaught()
{
var multiThreadTest = new MultiThreadTest(2);//Class that helps to test asynchronous stuff
var thrown = false;
var ex = new Exception("OOoooOOo!");
EventHandler onHandler = (s, a) => { throw ex; };
UnhandledExceptionEventHandler currentDomainOnUnhandledException = (s, args) =>
{
thrown = args.ExceptionObject.Equals(ex);
multiThreadTest.Notify();
};
AppDomain.CurrentDomain.UnhandledException += currentDomainOnUnhandledException;
//Invokes Action from the parameter and waits for multiThreadTest.Notify(); method to be called, otherwise 2 seconds
multiThreadTest.Act(() => onHandler.BeginInvokeWithAutoEnd(this, EventArgs.Empty));
AppDomain.CurrentDomain.UnhandledException -= currentDomainOnUnhandledException;
Assert.IsTrue(thrown);
}
테스트,하지만 무작위로 내가 가진 다른 테스트 중 하나 나옵니다. 내가 부러진 테스트를 볼 때 그것은 쓰여진다 : The agent process was stopped while the test was running.
. 즉, 깨진 테스트가 실행되는 동안 다른 스레드에서 처리되지 않은 예외가 있음을 의미합니다.
나는 어떻게 이런 일이 일어날 지 모른다. 도움을 주시면 감사하겠습니다.
스레드 풀에서 작업을 시작하고 완료 될 때까지 기다리지 않으므로. – usr
@usr 좀 더 정확하게 설명 할 수 있습니까? 나는'multiThreadTest.Notify();'를 실제로 기다린다. 그것이 당신이 의미하는 바라면. – dmigo
BeginInvokeWithAutoEnd는 대기하지 않고 즉시 반환합니다. 즉'Act '가 호출 된 줄도 즉시 반환됩니다. 너가 원하는게 그거야? – usr