示例#1
0
 public void Test2()
 {
     var th = new TaskHelper<int>();
     var t = th.Task;
     Assert.IsNotNull(t);
     var e =new InvalidProgramException();
     th.RunTask(null, e);
     Assert.IsTrue(t.IsFaulted);
     Assert.AreEqual(t.Exception.InnerException, e);
 }
 public void AreEqual(Mock<ISciencePublisher> publisher, Mock<IExperimentSteps<string, string>> steps,
     Mock<IExperimentState> state, string name, string ctrlResult, string candResult,
     string candName, InvalidProgramException excp, string errMsg)
 {
     //Setup
     AreEqualDelegate<string> areEqual = (ctrl, cand) =>
     {
         throw excp;
     };
     bool excpPassed = false;
     Action<ErrorEventArgs> onError = (e) =>
     {
         excpPassed = e.ExperimentError.LastException == excp
             && e.ExperimentError.LastStep == Operations.AreEqual;
         e.Publisher.Publish(errMsg, e.State);
     };
     steps.DefaultValue = DefaultValue.Empty;
     steps.SetupAllProperties();
     steps.SetupGet(x => x.AreEqual).Returns(areEqual);
     steps.Setup(x => x.OnError(It.IsAny<ErrorEventArgs>()))
         .Callback(onError);
     SetupControlAndCandidate(steps, ctrlResult, candResult, candName);
     state.SetupAllProperties();
     SetupStateSnapshot(state);
     //Exercise
     var sut = new Experiment<string, string>(name, publisher.Object, state.Object, steps.Object, true);
     var result = sut.Run(); //no exception
     //Verify
     steps.Verify(x => x.AreEqual, Times.AtLeastOnce);
     steps.Verify(x => x.OnError(It.Is<ErrorEventArgs>(
         a => a.State.CurrentStep == Operations.OnError && a.ExperimentError.LastException == excp
         && a.ExperimentError.LastStep == Operations.AreEqual)), Times.AtLeastOnce);
     //Results correct
     publisher.Verify(x => x.Publish(It.Is<IExperimentResult<string>>(
         r => !r.Control.ExceptionThrown && r.Control.Value == ctrlResult
         && r.Candidates.All(kvp => kvp.Value.ExceptionThrown
         && kvp.Value.ExperimentError.LastException == excp
         && kvp.Value.ExperimentError.LastStep == Operations.AreEqual))), Times.Once);
     //Message published
     publisher.Verify(x => x.Publish(errMsg,
        It.Is<IExperimentState>(y => y.CurrentStep == Operations.OnError)), Times.AtLeastOnce);
     Assert.True(excpPassed);
 }
示例#3
0
        public void Should_execute_all_cleanup_steps_despite_async_exceptions_in_run_and_cleanup_and_complete_with_all_exceptions()
        {
            CleanupGuard guard = new CleanupGuard();
            List<int> steps = new List<int>();
            Func<int, Task, Task> doStepAsync = delegate(int i, Task t)
            {
                steps.Add(i);
                return t;
            };

            TaskCompletionSource<bool> cleanupTcs = new TaskCompletionSource<bool>();
            guard.Register(() => doStepAsync(1, Task.FromResult(false)));
            guard.Register(() => doStepAsync(2, cleanupTcs.Task));

            TaskCompletionSource<bool> runTcs = new TaskCompletionSource<bool>();
            Task task = guard.RunAsync(g => runTcs.Task);

            Assert.False(task.IsCompleted);

            InvalidProgramException expectedRunException = new InvalidProgramException("Expected (run).");
            runTcs.SetException(expectedRunException);

            InvalidTimeZoneException expectedCleanupException = new InvalidTimeZoneException("Expected (cleanup).");
            cleanupTcs.SetException(expectedCleanupException);

            Assert.Equal(TaskStatus.Faulted, task.Status);
            Assert.NotNull(task.Exception);
            AggregateException ae = Assert.IsType<AggregateException>(task.Exception).Flatten();
            Assert.Equal(2, ae.InnerExceptions.Count);
            Assert.Same(expectedRunException, ae.InnerExceptions[0]);
            Assert.Same(expectedCleanupException, ae.InnerExceptions[1]);
            Assert.Equal(new int[] { 2, 1 }, steps.ToArray());
        }
        public void First_exception_fails_operation_without_executing_remaining_calls_for_that_loop()
        {
            Queue<TaskCompletionSource<bool>> pending = new Queue<TaskCompletionSource<bool>>();
            Func<Task> doAsync = delegate
            {
                TaskCompletionSource<bool> tcs = new TaskCompletionSource<bool>();
                pending.Enqueue(tcs);
                return tcs.Task;
            };

            ParallelOperationManager manager = new ParallelOperationManager(2, doAsync);
            Task task = manager.RunAsync(4);

            Assert.False(task.IsCompleted);
            Assert.Equal(2, pending.Count);

            TaskCompletionSource<bool> current = pending.Dequeue();
            InvalidProgramException exception = new InvalidProgramException("expected");
            current.SetException(exception);

            Assert.False(task.IsCompleted);
            Assert.Equal(1, pending.Count);

            current = pending.Dequeue();
            current.SetResult(false);

            Assert.False(task.IsCompleted);
            Assert.Equal(1, pending.Count);

            current = pending.Dequeue();
            current.SetResult(false);

            Assert.True(task.IsCompleted);
            Assert.True(task.IsFaulted);
            Assert.NotNull(task.Exception);
            AggregateException ae = Assert.IsType<AggregateException>(task.Exception).Flatten();
            Assert.Equal(1, ae.InnerExceptions.Count);
            Assert.Same(exception, ae.InnerExceptions[0]);
        }
示例#5
0
        private void DispatchAsync(PipeInvocation invocation, MessageDispatch dispatch)
        {
            var invocationTask = invocation.RunAsync();
            invocationTask.ContinueWith(task => dispatch.SetHandled(invocation.Invoker, GetException(task)), TaskContinuationOptions.ExecuteSynchronously);

            if (invocationTask.Status != TaskStatus.Created)
                return;

            if (invocation.Invoker.ShouldCreateStartedTasks)
            {
                var exception = new InvalidProgramException(string.Format("{0}.Handle({1}) did not start the returned task", invocation.Invoker.MessageHandlerType.Name, invocation.Invoker.MessageType.Name));
                dispatch.SetHandled(invocation.Invoker, exception);
                return;
            }

            var taskScheduler = GetTaskScheduler(invocation.Invoker.DispatchQueueName);
            invocationTask.Start(taskScheduler);
        }
示例#6
0
        void OnError(string msg, int retval)
        {
            Console.WriteLine();
            Console.WriteLine("Fatal error: {0}", msg);
            Console.WriteLine("PC    = {0:X8} ", (PC - 1) * 4);

            Console.Write("R[0] = {0:X8}   ", R[0]);
            Console.Write("R[1] = {0:X8}   ", R[1]);
            Console.Write("R[2] = {0:X8}   ", R[2]);
            Console.WriteLine("R[3] = {0:X8}", R[3]);

            Console.Write("MT   = {0:X8}   ", R[12]);
            Console.Write("SB   = {0:X8}   ", R[13]);
            Console.Write("SP   = {0:X8}   ", R[14]);
            Console.WriteLine("LNK  = {0:X8} ", R[15]);

            Exception e = new InvalidProgramException(msg);
            e.Data[0] = retval;
            throw e;
        }
 public void Candidate(Mock<ISciencePublisher> publisher,
     Mock<IExperimentSteps<string, string>> steps, Mock<IExperimentState> state,
     string name, string ctrlResult, string candResult, string candName, InvalidProgramException excp,
     string errMsg)
 {
     //Setup
     steps.DefaultValue = DefaultValue.Empty;
     steps.SetupAllProperties();
     bool excpPassed = false;
     Action<ErrorEventArgs> onError = (e) =>
     {
         excpPassed = e.ExperimentError.LastException == excp
             && e.ExperimentError.LastStep == Operations.Candidate;
         e.Publisher.Publish(errMsg, e.State);
     };
     Func<string> candidate = () =>
     {
         throw excp;
     };
     SetupControlAndCandidate(steps, ctrlResult, candResult, candName);
     steps.Setup(x => x.GetCandidates())
         .Returns(new Dictionary<string, Func<string>> { { candName, candidate } });
     steps.Setup(x => x.OnError(It.IsAny<ErrorEventArgs>()))
         .Callback(onError);
     state.SetupAllProperties();
     SetupStateSnapshot(state);
     //Exercise
     var sut = new Experiment<string, string>(name, publisher.Object, state.Object, steps.Object, true);
     var result = sut.Run(); //No exceptions should be thrown
     //Verify
     steps.Verify(x => x.GetCandidates(), Times.AtLeastOnce);
     steps.Verify(x => x.OnError(It.Is<ErrorEventArgs>(
         a => a.State.CurrentStep == Operations.OnError && a.ExperimentError.LastException == excp
         && a.ExperimentError.LastStep == Operations.Candidate)), Times.AtLeastOnce);
     //Results published
     publisher.Verify(x => x.Publish(It.Is<IExperimentResult<string>>(
         r => r.Candidates.All(kvp => kvp.Key.Equals(candName) && kvp.Value.ExceptionThrown
         && kvp.Value.Name.Equals(candName) && kvp.Value.IsMismatched
         && kvp.Value.ExperimentError.LastException == excp
         && kvp.Value.ExperimentError.LastStep == Operations.Candidate))), Times.Once);
     publisher.Verify(x => x.Publish(errMsg,
        It.Is<IExperimentState>(y => y.CurrentStep == Operations.OnError)), Times.AtLeastOnce);
     Assert.True(excpPassed);
 }
 public void Control_Exception_Thrown(Mock<ISciencePublisher> publisher,
     Mock<IExperimentSteps<string, string>> steps, Mock<IExperimentState> state,
     string name, string ctrlResult, string candResult, string candName, InvalidProgramException excp)
 {
     //Setup
     steps.DefaultValue = DefaultValue.Empty;
     steps.SetupAllProperties();
     Func<string> control = () => { throw excp; };
     SetupControlAndCandidate(steps, ctrlResult, candResult, candName);
     steps.Setup(x => x.Control)
         .Returns(control);
     state.SetupAllProperties();
     SetupStateSnapshot(state);
     //Exercise
     var sut = new Experiment<string, string>(name, publisher.Object, state.Object, steps.Object, true);
     var thrown = Assert.Throws<InvalidProgramException>(() => sut.Run()); //Exceptions should be thrown
     //Verify
     steps.Verify(x => x.Control, Times.AtLeastOnce);
     steps.Verify(x => x.GetCandidates(), Times.AtLeastOnce); //GetCandidates STILL called
     steps.Verify(x => x.OnMismatch(It.Is<MismatchEventArgs<string>>(
         r => r.Candidate == candResult && r.ControlException == excp)),
         Times.AtLeastOnce);
     steps.Verify(x => x.OnError(It.Is<ErrorEventArgs>(a => a.State.CurrentStep == Operations.OnError)),
         Times.AtLeastOnce);
     //Results published
     publisher.Verify(x => x.Publish(It.Is<IExperimentResult<string>>(
         r => r.Control.ExceptionThrown && r.Control.ExperimentError.LastException == excp
         && r.Control.ExperimentError.LastStep == Operations.Control
         && r.Candidates.All(kvp => kvp.Value.IsMismatched))),
         Times.Once);
     Assert.Equal(thrown, excp);
 }
 public void Control_And_Candidate_Throw_Same_Exception(Mock<ISciencePublisher> publisher,
     Mock<IExperimentSteps<string, string>> steps, Mock<IExperimentState> state,
     string name, string candName, InvalidProgramException excp)
 {
     //Setup
     steps.DefaultValue = DefaultValue.Empty;
     steps.SetupAllProperties();
     Func<string> function = () => { throw excp; };
     steps.SetupGet(x => x.Control)
         .Returns(function);
     steps.Setup(x => x.GetCandidates())
         .Returns(new Dictionary<string, Func<string>> { { candName, function } });
     state.SetupAllProperties();
     SetupStateSnapshot(state);
     //Exercise
     var sut = new Experiment<string, string>(name, publisher.Object, state.Object, steps.Object, true);
     var thrown = Assert.Throws<InvalidProgramException>(() => sut.Run()); //Exceptions should be thrown
     //Verify
     steps.Verify(x => x.Control, Times.AtLeastOnce);
     steps.Verify(x => x.GetCandidates(), Times.AtLeastOnce); //GetCandidates STILL called
     steps.Verify(x => x.OnMismatch(It.IsAny<MismatchEventArgs<string>>()),
         Times.Never); //Same exception, they're not mismatched
     steps.Verify(x => x.OnError(It.Is<ErrorEventArgs>(a => a.State.CurrentStep == Operations.OnError)),
         Times.AtLeastOnce);
     //Results published
     publisher.Verify(x => x.Publish(It.Is<IExperimentResult<string>>(
         r => r.Control.ExceptionThrown && r.Control.ExperimentError.LastException == excp
         && r.Control.ExperimentError.LastStep == Operations.Control
         && r.Candidates.All(kvp => !kvp.Value.IsMismatched && kvp.Value.ExceptionThrown
         && kvp.Value.ExperimentError.LastException == excp
         && kvp.Value.ExperimentError.LastStep == Operations.Candidate))),
         Times.Once);
     Assert.Equal(thrown, excp);
 }
            public void Candidate_Exception_Thrown(Mock<ISciencePublisher> publisher,
                Mock<IExperimentSteps<string, string>> steps, Mock<IExperimentState> state,
                string name, string ctrlResult, string candResult, string candName, InvalidProgramException excp)
            {
                //Setup
                steps.DefaultValue = DefaultValue.Empty;
                steps.SetupAllProperties();

                Func<string> candidate = () =>
                {
                    throw excp;
                };
                SetupControlAndCandidate(steps, ctrlResult, candResult, candName);
                steps.Setup(x => x.GetCandidates())
                    .Returns(new Dictionary<string, Func<string>> { { candName, candidate } });
                state.SetupAllProperties();
                SetupStateSnapshot(state);
                //Exercise
                var sut = new Experiment<string, string>(name, publisher.Object, state.Object, steps.Object, true);
                var result = sut.Run(); //No exceptions should be thrown
                //Verify
                steps.Verify(x => x.GetCandidates(), Times.AtLeastOnce); //Setup called
                steps.Verify(x => x.OnMismatch(It.Is<MismatchEventArgs<string>>(
                    a => a.CandidateException == excp && a.Control == ctrlResult)), Times.AtLeastOnce);
                steps.Verify(x => x.OnError(It.Is<ErrorEventArgs>(r => r.ExperimentError.LastException == excp
                    && r.State.CurrentStep == Operations.OnError
                    && r.ExperimentError.LastStep == Operations.Candidate)),
                    Times.AtLeastOnce);
                //Results published
                publisher.Verify(x => x.Publish(It.Is<IExperimentResult<string>>(
                    r => r.Candidates.All(kvp => kvp.Key.Equals(candName) && kvp.Value.ExceptionThrown
                    && kvp.Value.Name.Equals(candName) && kvp.Value.IsMismatched
                    && kvp.Value.ExperimentError.LastException == excp
                    && kvp.Value.ExperimentError.LastStep == Operations.Candidate))), Times.Once);
            }
 public void OnMismatch_ThrowInternalExceptions_True(Mock<ISciencePublisher> publisher,
     Mock<IExperimentSteps<string, string>> steps, Mock<IExperimentState> state, string name,
     string ctrlResult, string candResult, string candName, InvalidProgramException excp, string errMsg)
 {
     //Setup
     Action<MismatchEventArgs<string>> onMismatch = (e) =>
     {
         throw excp;
     };
     bool excpPassed = false;
     Action<ErrorEventArgs> onError = (e) =>
     {
         excpPassed = e.ExperimentError.LastException == excp
             && e.ExperimentError.LastStep == Operations.OnMismatch;
         e.Publisher.Publish(errMsg, e.State);
     };
     steps.DefaultValue = DefaultValue.Empty;
     steps.SetupAllProperties();
     steps.SetupGet(x => x.AreEqual).Returns((x, y) => false); //Make sure to cause a mismatch
     steps.Setup(x => x.OnMismatch(It.IsAny<MismatchEventArgs<string>>())).Callback(onMismatch);
     steps.Setup(x => x.OnError(It.IsAny<ErrorEventArgs>()))
         .Callback(onError);
     SetupControlAndCandidate(steps, ctrlResult, candResult, candName);
     state.SetupAllProperties();
     SetupStateSnapshot(state);
     //Exercise
     var sut = new Experiment<string, string>(name, publisher.Object, state.Object, steps.Object, true);
     //Exception thrown because throwOnInternalExceptions = true!
     var result = Assert.Throws<StepFailedException>(() => sut.Run());
     //Verify
     steps.Verify(x => x.OnMismatch(It.IsAny<MismatchEventArgs<string>>()), Times.AtLeastOnce);
     steps.Verify(x => x.OnError(It.Is<ErrorEventArgs>(
         a => a.State.CurrentStep == Operations.OnError && a.ExperimentError.LastException == excp
         && a.ExperimentError.LastStep == Operations.OnMismatch)), Times.AtLeastOnce);
     //Final results NOT published
     publisher.Verify(x => x.Publish(It.IsAny<IExperimentResult<string>>()), Times.Never);
     //Message published
     publisher.Verify(x => x.Publish(errMsg,
        It.Is<IExperimentState>(y => y.CurrentStep == Operations.OnError)), Times.AtLeastOnce);
     Assert.True(excpPassed);
     Assert.Equal(result.InnerException, excp);
 }
 public void OnError_ThrowInternalExceptions_False(Mock<ISciencePublisher> publisher,
     Mock<IExperimentSteps<string, string>> steps, Mock<IExperimentState> state, string name,
     string ctrlResult, string candResult, string candName, InvalidProgramException excp,
     ApplicationException otherExcp)
 {
     //Setup
     Action<MismatchEventArgs<string>> onMismatch = (e) =>
     {
         throw otherExcp;
     };
     Action<ErrorEventArgs> onError = (error) =>
     {
         throw excp;
     };
     steps.DefaultValue = DefaultValue.Empty;
     steps.SetupAllProperties();
     steps.SetupGet(x => x.AreEqual).Returns((x, y) => false); //Make sure to cause a mismatch
     steps.Setup(x => x.OnMismatch(It.IsAny<MismatchEventArgs<string>>())).Callback(onMismatch);
     steps.Setup(x => x.OnError(It.IsAny<ErrorEventArgs>()))
         .Callback(onError);
     SetupControlAndCandidate(steps, ctrlResult, candResult, candName);
     state.SetupAllProperties();
     SetupStateSnapshot(state);
     //Exercise
     var sut = new Experiment<string, string>(name, publisher.Object, state.Object, steps.Object, false);
     var result = sut.Run(); //Exception swallowed because throwOnInternalExceptions = false!
     //Verify
     steps.Verify(x => x.OnMismatch(It.IsAny<MismatchEventArgs<string>>()), Times.AtLeastOnce);
     steps.Verify(x => x.OnError(It.Is<ErrorEventArgs>(
         a => a.State.CurrentStep == Operations.OnError && a.ExperimentError.LastException == otherExcp
         && a.ExperimentError.LastStep == Operations.OnMismatch)), Times.AtLeastOnce);
     //Final results NOT published
     publisher.Verify(x => x.Publish(It.IsAny<IExperimentResult<string>>()), Times.Never);
     //Message NOT published
     publisher.Verify(x => x.Publish(It.IsAny<string>(),
        It.Is<IExperimentState>(y => y.CurrentStep == Operations.OnError)), Times.Never);
     Assert.Equal(result, ctrlResult);
 }
 public void OnError_And_Control_ThrowInternalExceptions_True(Mock<ISciencePublisher> publisher,
     Mock<IExperimentSteps<string, string>> steps, Mock<IExperimentState> state, string name,
     string ctrlResult, string candResult, string candName, InvalidProgramException onErrorExcp,
     ApplicationException ctrlExcp)
 {
     //Setup
     Action<ErrorEventArgs> onError = (error) =>
     {
         throw onErrorExcp;
     };
     Func<string> control = () => { throw ctrlExcp; };
     steps.DefaultValue = DefaultValue.Empty;
     steps.SetupAllProperties();
     steps.Setup(x => x.OnError(It.IsAny<ErrorEventArgs>()))
         .Callback(onError);
     SetupControlAndCandidate(steps, ctrlResult, candResult, candName);
     steps.SetupGet(x => x.Control).Returns(control);
     state.SetupAllProperties();
     SetupStateSnapshot(state);
     //Exercise
     var sut = new Experiment<string, string>(name, publisher.Object, state.Object, steps.Object, true);
     // Exception thrown because throwOnInternalExceptions = true, but its a
     // StepFailedException, not an ApplicationException as would be expected
     var result = Assert.Throws<StepFailedException>(() => sut.Run());
     //Verify
     steps.Verify(x => x.OnError(It.Is<ErrorEventArgs>(
         a => a.State.CurrentStep == Operations.OnError && a.ExperimentError.LastException == ctrlExcp
         && a.ExperimentError.LastStep == Operations.Control)), Times.AtLeastOnce);
     steps.Verify(x => x.GetCandidates(), Times.Never); //Candidates don't run in this case
     //Final results NOT published
     publisher.Verify(x => x.Publish(It.IsAny<IExperimentResult<string>>()), Times.Never);
     //Message NOT published
     publisher.Verify(x => x.Publish(It.IsAny<string>(),
        It.Is<IExperimentState>(y => y.CurrentStep == Operations.OnError)), Times.Never);
     Assert.Equal(result.InnerException, onErrorExcp); // Inner comes from OnError, not Control
 }
 public void OnError_And_Control_ThrowInternalExceptions_False(Mock<ISciencePublisher> publisher,
     Mock<IExperimentSteps<string, string>> steps, Mock<IExperimentState> state, string name,
     string ctrlResult, string candResult, string candName, InvalidProgramException onErrorExcp,
     ApplicationException ctrlExcp)
 {
     //Setup
     Action<ErrorEventArgs> onError = (error) =>
     {
         throw onErrorExcp;
     };
     Func<string> control = () => { throw ctrlExcp; };
     steps.DefaultValue = DefaultValue.Empty;
     steps.SetupAllProperties();
     steps.Setup(x => x.OnError(It.IsAny<ErrorEventArgs>()))
         .Callback(onError);
     SetupControlAndCandidate(steps, ctrlResult, candResult, candName);
     steps.SetupGet(x => x.Control).Returns(control);
     state.SetupAllProperties();
     SetupStateSnapshot(state);
     //Exercise
     var sut = new Experiment<string, string>(name, publisher.Object, state.Object, steps.Object, false);
     // OnError Exception swallowed because throwOnInternalExceptions = false, but still
     // throws the control's Exception
     var result = Assert.Throws<ApplicationException>(() => sut.Run());
     //Verify
     steps.Verify(x => x.OnError(It.Is<ErrorEventArgs>(
         a => a.State.CurrentStep == Operations.OnError && a.ExperimentError.LastException == ctrlExcp
         && a.ExperimentError.LastStep == Operations.Control)), Times.AtLeastOnce);
     steps.Verify(x => x.GetCandidates(), Times.AtLeastOnce); //Candidates still run in this case
     //Final results ARE published, but the Control Observation is not properly filled out
     publisher.Verify(x => x.Publish(It.Is<IExperimentResult<string>>(
         r => r.Control == null)), Times.AtLeastOnce);
     //Error Message NOT published
     publisher.Verify(x => x.Publish(It.IsAny<string>(),
        It.Is<IExperimentState>(y => y.CurrentStep == Operations.OnError)), Times.Never);
     Assert.Equal(result, ctrlExcp); //Still throws the right exception
 }