コード例 #1
0
        public void RunWithInvalidArgShouldThrow()
        {
            // Arrange
            TestTrace.Arrange();
            var activity = new EchoArg<int>();
            using (var workflow = new WorkflowP1(activity) { Extensions = { new ListTrackingParticipant() } })
            {
                // Intentionally used wrong name
                workflow.Inputs.Input1 = 123;

                try
                {
                    // Act 
                    TestTrace.Act();

                    // Assert
                    TestTrace.Assert();
                    AssertHelper.Throws<ArgumentException>(workflow.RunAsync());
                }
                finally
                {
                    TestTrace.Finally();
                    workflow.Trace();
                }
            }
        }
コード例 #2
0
        public void CancelWithTokenSource()
        {
            // Arrange
            TestTrace.Arrange();
            var activity = new Delay { Duration = TimeSpan.FromMilliseconds(200) };
            var workflow = new WorkflowP1(activity)
                {
                    Timeout = Constants.Timeout,
                    Extensions =
                        {
                            new ListTrackingParticipant()
                        }
                };
            var delayExecuting = new AutoResetEvent(false);
            var source = new CancellationTokenSource(Constants.Timeout);

            // When the workflow is idle set an event to trigger the test to cancel
            workflow.When.Idle += (sender, args) => delayExecuting.Set();

            try
            {
                // Act
                TestTrace.Act();

                // Start the workflow async
                var task = workflow.RunAsync(source.Token);

                TestTrace.Write("Waiting for Delay activity to start executing");
                var delayIsExecuting = delayExecuting.WaitOne(Constants.Timeout);


                if (delayIsExecuting)
                {
                    TestTrace.Write("Cancelling workflow via token source");
                    source.Cancel();
                }


                // Assert
                TestTrace.Assert();
                Assert.IsTrue(delayIsExecuting);

                AssertHelper.Throws<AggregateException>(task.Wait, typeof(TaskCanceledException));

                // Canceled tasks can run to completion
                Assert.IsTrue(task.IsCanceled, "The task was not canceled");
                Assert.IsTrue(task.IsCompleted, "Task was not completed, task status is " + task.Status.ToString());
            }
            finally
            {
                TestTrace.Finally();

                workflow.Trace();
            }
        }
コード例 #3
0
        public void CancelTaskAsyncActivityViaToken()
        {
            // Arrange
            TestTrace.Arrange();
            var spinExecuting = new ManualResetEvent(false);
            var completedEvent = new ManualResetEvent(false);
            var notify = new SpinNotify { LoopComplete = (loops, iterations) => spinExecuting.Set() };
            var cancellationHandlerClosed = new AutoResetEvent(false);
            var activity = CreateSequenceWithSpinWaiter(typeof(TaskSpinWaiter));

            var tracking = new ListTrackingParticipant();

            var workflow = new WorkflowP1(activity)
            {
                Timeout = Constants.Timeout,
                Extensions =
                        {
                            tracking, 
                            notify
                        }
            };

            workflow.When.Activity.Name("CancellationHandler").Closed +=
                (sender, args) => cancellationHandlerClosed.Set();

            workflow.When.Completed += (sender, args) => completedEvent.Set();

            var source = new CancellationTokenSource(Constants.Timeout);

            try
            {
                // Act
                TestTrace.Act();
                var task = workflow.RunAsync(source.Token);

                TestTrace.Write("Waiting for AsyncSpinWaiter to start executing");
                Assert.IsTrue(spinExecuting.WaitOne(Constants.Timeout));

                TestTrace.Write("Cancelling workflow with token");
                source.Cancel();

                TestTrace.Write("Waiting for workflow to execute cancellation handler");
                Assert.IsTrue(cancellationHandlerClosed.WaitOne(Constants.Timeout));

                TestTrace.Write("Waiting for workflow");

                // Assert
                TestTrace.Assert();
                AssertHelper.Throws<AggregateException>(task.Wait, typeof(TaskCanceledException));
                Assert.IsTrue(
                    tracking.Records.Any(ActivityInstanceState.Closed, "CancellationHandler"),
                    "The cancellation handler was not invoked");
                Assert.IsTrue(task.IsCanceled);
                Assert.IsTrue(task.IsCompleted);
            }
            finally
            {
                TestTrace.Finally();

                workflow.Trace();
            }
        }
コード例 #4
0
        public void RunShouldRunWorkflowToCompletion()
        {
            // Arrange
            TestTrace.Arrange();
            var activity = new Sequence();
            using (var workflow = new WorkflowP1(activity) { Timeout = Constants.Timeout })
            {
                try
                {
                    // Act
                    TestTrace.Act();
                    var result = workflow.RunAsync().Result as WorkflowCompletedResult;

                    // Assert
                    TestTrace.Assert();
                    Assert.IsNotNull(result);
                }
                finally
                {
                    TestTrace.Finally();
                    workflow.Trace();
                }
            }
        }
コード例 #5
0
        public void CancelWithAsyncActivityWillCancel()
        {
            // Arrange
            TestTrace.Arrange();
            var spinExecuting = new AutoResetEvent(false);
            var notify = new SpinNotify { LoopComplete = (loops, iterations) => spinExecuting.Set() };
            var cancellationHandlerClosed = new AutoResetEvent(false);
            var activity = CreateSequenceWithSpinWaiter(typeof(AsyncSpinWaiter));

            var tracking = new ListTrackingParticipant();

            var workflow = new WorkflowP1(activity)
            {
                Timeout = Constants.Timeout,
                Extensions =
                        {
                            tracking, 
                            notify
                        }
            };

            workflow.When.Activity.Name("CancellationHandler").Closed +=
                (sender, args) => cancellationHandlerClosed.Set();

            var source = new CancellationTokenSource(Constants.Timeout);

            try
            {
                // Act
                TestTrace.Act();
                workflow.Until.Idle.RunAsync(source.Token);

                TestTrace.Write("Waiting for AsyncSpinWaiter to start executing");
                var spinWait = spinExecuting.WaitOne(TimeSpan.FromSeconds(10));

                TestTrace.Write("Cancelling workflow");
                source.Cancel();

                var cancelInvoked = cancellationHandlerClosed.WaitOne(Constants.Timeout);

                // Assert
                TestTrace.Assert();
                Assert.IsTrue(spinWait, "The spinExecuting event was not set");
                Assert.IsTrue(cancelInvoked);
                Assert.IsTrue(
                    tracking.Records.Any(ActivityInstanceState.Closed, "CancellationHandler"),
                    "The cancellation handler was not invoked");
            }
            finally
            {
                TestTrace.Finally();

                workflow.Trace();
            }
        }