public void TestWorkCancelledException() { // prepare workAction to throw exception Exception currentException = null; Func <Task> workAction = () => { throw currentException; }; // initialize worker using (var worker = new MockWorker(workAction)) { var finishedEvent = new AutoResetEvent(false); worker.OnWorkFinished += () => { finishedEvent.Set(); }; bool wasError; worker.OnWorkError += e => wasError = true; // start worker worker.Start(); // throw OperationCanceledException wasError = false; currentException = new OperationCanceledException(); worker.NotifyWork(); // verify work finished Assert.IsTrue(finishedEvent.WaitOne(1000)); Assert.IsFalse(wasError); // throw Exception wasError = false; currentException = new Exception(); worker.NotifyWork(); // verify work errored Assert.IsTrue(finishedEvent.WaitOne()); Assert.IsTrue(wasError); // throw AggregateException of all OperationCanceledException wasError = false; currentException = new AggregateException(new OperationCanceledException(), new OperationCanceledException()); worker.NotifyWork(); // verify work finished Assert.IsTrue(finishedEvent.WaitOne()); Assert.IsFalse(wasError); // throw AggregateException of some OperationCanceledException wasError = false; currentException = new AggregateException(new OperationCanceledException(), new Exception()); worker.NotifyWork(); // verify work errored Assert.IsTrue(finishedEvent.WaitOne()); Assert.IsTrue(wasError); } }
public void TestOnWorkError() { // prepare workAction to throw exception var expectedException = new Exception(); Func<Task> workAction = () => { throw expectedException; }; // initialize worker using (var worker = new MockWorker(workAction)) { // prepare OnWorkError call tracking Exception actualException = null; var callEvent = new AutoResetEvent(false); worker.OnWorkError += e => { actualException = e; callEvent.Set(); }; // start worker worker.Start(); // notify worker worker.NotifyWork(); // verify OnWorkError was called with expected exception var wasCalled = callEvent.WaitOne(); Assert.IsTrue(wasCalled); Assert.AreSame(expectedException, actualException); } }
public void TestNotifyWork() { // prepare workAction call tracking var callEvent = new AutoResetEvent(false); Func<Task> workAction = () => { callEvent.Set(); return Task.FromResult(false); }; // initialize worker using (var worker = new MockWorker(workAction)) { // verify workAction has not been called var wasCalled = callEvent.WaitOne(1000); Assert.IsFalse(wasCalled); // start worker worker.Start(); // verify workAction has not been called wasCalled = callEvent.WaitOne(1000); Assert.IsFalse(wasCalled); // notify worker worker.NotifyWork(); // verify workAction has been called wasCalled = callEvent.WaitOne(); Assert.IsTrue(wasCalled); // stop worker worker.Stop(); // verify workAction has not been called wasCalled = callEvent.WaitOne(1000); Assert.IsFalse(wasCalled); } }
public void TestNotifyWork() { // prepare workAction call tracking var callEvent = new AutoResetEvent(false); Func <Task> workAction = () => { callEvent.Set(); return(Task.CompletedTask); }; // initialize worker using (var worker = new MockWorker(workAction)) { // verify workAction has not been called var wasCalled = callEvent.WaitOne(1000); Assert.IsFalse(wasCalled); // start worker worker.Start(); // verify workAction has not been called wasCalled = callEvent.WaitOne(1000); Assert.IsFalse(wasCalled); // notify worker worker.NotifyWork(); // verify workAction has been called wasCalled = callEvent.WaitOne(); Assert.IsTrue(wasCalled); // stop worker worker.Stop(); // verify workAction has not been called wasCalled = callEvent.WaitOne(1000); Assert.IsFalse(wasCalled); } }
public void TestOnWorkError() { // prepare workAction to throw exception var expectedException = new Exception(); Func <Task> workAction = () => { throw expectedException; }; // initialize worker using (var worker = new MockWorker(workAction)) { // prepare OnWorkError call tracking Exception actualException = null; var callEvent = new AutoResetEvent(false); worker.OnWorkError += e => { actualException = e; callEvent.Set(); }; // start worker worker.Start(); // notify worker worker.NotifyWork(); // verify OnWorkError was called with expected exception var wasCalled = callEvent.WaitOne(); Assert.IsTrue(wasCalled); Assert.AreSame(expectedException, actualException); } }
public void TestStartNotified() { // prepare workAction call tracking var callEvent = new AutoResetEvent(false); Action workAction = () => callEvent.Set(); // initialize worker using (var worker = new MockWorker(workAction)) { // verify workAction has not been called var wasCalled = callEvent.WaitOne(10); Assert.IsFalse(wasCalled); // notify worker before it has been started worker.NotifyWork(); // verify workAction has not been called wasCalled = callEvent.WaitOne(10); Assert.IsFalse(wasCalled); // start worker, it has already been notified worker.Start(); // verify workAction has been called wasCalled = callEvent.WaitOne(1000); Assert.IsTrue(wasCalled); // stop worker worker.Stop(); // verify workAction has not been called wasCalled = callEvent.WaitOne(10); Assert.IsFalse(wasCalled); } }
public void TestRestartNotified() { // prepare workAction call tracking var callEvent = new AutoResetEvent(false); Action workAction = () => callEvent.Set(); // initialize worker using (var worker = new MockWorker(workAction)) { // start worker worker.Start(); // verify workAction has not been called var wasCalled = callEvent.WaitOne(10); Assert.IsFalse(wasCalled); // notify worker worker.NotifyWork(); // verify workAction has been called wasCalled = callEvent.WaitOne(10); Assert.IsTrue(wasCalled); // stop worker worker.Stop(); // wait for worker to idle worker.WaitForIdle(); // notify worker again worker.NotifyWork(); //TODO calling callEvent.WaitOne(10) cancels the notify and fails the test, i'm not sure why //TODO calling Thread.Sleep(10) instead of callEvent.WaitOne has the same effect // verify workAction has not been called wasCalled = callEvent.WaitOne(10); Assert.IsFalse(wasCalled); // start worker again, it has already been notified worker.Start(); // verify workAction has been called wasCalled = callEvent.WaitOne(1000); Assert.IsTrue(wasCalled); } }
public void TestRestart() { // prepare workAction call tracking var callEvent = new AutoResetEvent(false); Action workAction = () => callEvent.Set(); // initialize worker using (var worker = new MockWorker(workAction)) { // start worker worker.Start(); // verify workAction has not been called var wasCalled = callEvent.WaitOne(1000); Assert.IsFalse(wasCalled); // notify worker worker.NotifyWork(); // verify workAction has been called wasCalled = callEvent.WaitOne(10); Assert.IsTrue(wasCalled); // stop worker worker.Stop(); // wait for worker to idle worker.WaitForIdle(); // verify workAction has not been called wasCalled = callEvent.WaitOne(10); Assert.IsFalse(wasCalled); // start worker again worker.Start(); // notify worker worker.NotifyWork(); // verify workAction has been called wasCalled = callEvent.WaitOne(10); Assert.IsTrue(wasCalled); } }
public void TestWorkAfterCancellation() { // create a work action that will throw a cancellation exception on its first call var workedEvent = new AutoResetEvent(false); var workCount = 0; Func <Task> workAction = () => { try { workCount++; if (workCount == 1) { throw new OperationCanceledException(); } return(Task.CompletedTask); } finally { workedEvent.Set(); } }; // initialize worker using (var worker = new MockWorker(workAction)) { worker.Start(); // notify and verify work was performed worker.NotifyWork(); Assert.IsTrue(workedEvent.WaitOne()); Assert.AreEqual(1, workCount); // the first work action cancelled, need to verify that a second work action can be performed // notify and verify work was performed worker.NotifyWork(); Assert.IsTrue(workedEvent.WaitOne()); Assert.AreEqual(2, workCount); } }
public void TestWorkCancelledException() { // prepare workAction to throw exception Exception currentException = null; Func<Task> workAction = () => { throw currentException; }; // initialize worker using (var worker = new MockWorker(workAction)) { var finishedEvent = new AutoResetEvent(false); worker.OnWorkFinished += () => { finishedEvent.Set(); }; bool wasError; worker.OnWorkError += e => wasError = true; // start worker worker.Start(); // throw OperationCanceledException wasError = false; currentException = new OperationCanceledException(); worker.NotifyWork(); // verify work finished Assert.IsTrue(finishedEvent.WaitOne(1000)); Assert.IsFalse(wasError); // throw Exception wasError = false; currentException = new Exception(); worker.NotifyWork(); // verify work errored Assert.IsTrue(finishedEvent.WaitOne()); Assert.IsTrue(wasError); // throw AggregateException of all OperationCanceledException wasError = false; currentException = new AggregateException(new OperationCanceledException(), new OperationCanceledException()); worker.NotifyWork(); // verify work finished Assert.IsTrue(finishedEvent.WaitOne()); Assert.IsFalse(wasError); // throw AggregateException of some OperationCanceledException wasError = false; currentException = new AggregateException(new OperationCanceledException(), new Exception()); worker.NotifyWork(); // verify work errored Assert.IsTrue(finishedEvent.WaitOne()); Assert.IsTrue(wasError); } }
public void TestWorkAfterCancellation() { // create a work action that will throw a cancellation exception on its first call var workedEvent = new AutoResetEvent(false); var workCount = 0; Func<Task> workAction = () => { try { workCount++; if (workCount == 1) throw new OperationCanceledException(); return Task.FromResult(false); } finally { workedEvent.Set(); } }; // initialize worker using (var worker = new MockWorker(workAction)) { worker.Start(); // notify and verify work was performed worker.NotifyWork(); Assert.IsTrue(workedEvent.WaitOne()); Assert.AreEqual(1, workCount); // the first work action cancelled, need to verify that a second work action can be performed // notify and verify work was performed worker.NotifyWork(); Assert.IsTrue(workedEvent.WaitOne()); Assert.AreEqual(2, workCount); } }