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();
            }
        }
        public void CancelWithTokenSimple()
        {
            var activity = new Delay() { Duration = TimeSpan.FromSeconds(10) };
            var workflow = new WorkflowP1(activity);
            var cancellationTokenSource = new CancellationTokenSource(Constants.Timeout);

            // Run until idle
            var task = workflow.Until.Idle.RunAsync(cancellationTokenSource.Token);

            // Cancel with the token source
            cancellationTokenSource.Cancel();

            // If you try to do anything with the task, you get an exception
            AssertHelper.Throws<AggregateException>(
                task.Wait,
                typeof(TaskCanceledException),
                "The task should be canceled");
            Assert.IsTrue(task.IsCanceled);
        }
Example #3
0
 /// <summary>
 ///   Creates and validates a description of the activity’s arguments, variables, child activities, and activity delegates.
 /// </summary>
 /// <param name = "metadata">The activity’s metadata that encapsulates the activity’s arguments, variables, child activities, and activity delegates.</param>
 protected override void CacheMetadata(NativeActivityMetadata metadata)
 {
     this.delay = new Delay { Duration = StubDuration };
     metadata.AddImplementationChild(this.delay);
     base.CacheMetadata(metadata);
 }
 /// <summary>
 ///   Initializes a new instance of the <see cref = "DelayUntilDateTime" /> class.
 /// </summary>
 public DelayUntilDateTime()
 {
     this.delay = new Delay { Duration = new InArgument<TimeSpan>(this.delayInterval) };
 }
Example #5
0
		protected PSActivity()
		{
			this.psActivityContextImplementationVariable = new Variable<PSActivityContext>("psActivityContextImplementationVariable");
			this._tracer = PowerShellTraceSourceFactory.GetTraceSource();
			this.noPersistHandle = new Variable<NoPersistHandle>("NoPersistHandle");
			this.bookmarking = new Variable<bool>("Bookmarking");
			TerminateWorkflow terminateWorkflow = new TerminateWorkflow();
			terminateWorkflow.Reason = Resources.RunningTimeExceeded;
			this.terminateActivity = terminateWorkflow;
			//base();
			Delay delay = new Delay();
			ParameterExpression parameterExpression = Expression.Parameter(typeof(ActivityContext), "context");
			Expression[] expressionArray = new Expression[3];
			expressionArray[0] = Expression.Constant(0, typeof(int));
			expressionArray[1] = Expression.Constant(0, typeof(int));
			Expression[] expressionArray1 = new Expression[1];
			expressionArray1[0] = Expression.Property(Expression.Constant(this, typeof(PSActivity)), (MethodInfo)MethodBase.GetMethodFromHandle(get_PSActionRunningTimeoutSec));
			expressionArray[2] = Expression.Convert(Expression.Call(parameterExpression, (MethodInfo)MethodBase.GetMethodFromHandle(GetValue<int?>), expressionArray1), typeof(int));
			ParameterExpression[] parameterExpressionArray = new ParameterExpression[1];
			parameterExpressionArray[0] = parameterExpression;
			//TODO: REVIEW: (ConstructorInfo)MethodBase.GetMethodFromHandle
			delay.Duration = new InArgument<TimeSpan>(Expression.Lambda<Func<ActivityContext, TimeSpan>>(Expression.New(typeof(TimeSpan).GetConstructor(new System.Type[] { typeof(int), typeof(int), typeof(int) }), expressionArray), parameterExpressionArray));
			this.cancelTimer = delay;
		}