Esempio n. 1
0
        internal void RealRun()
        {
            if (_isPromise)
            {
                _promise = new TaskCompletionSource <int>();
            }
            else
            {
                _task = new Task(TaskRun, _taskCts.Token);
            }

            if (_testAction != TestAction.None)
            {
                try
                {
                    bool executeTask = false;

                    if (_isPromise)
                    {
                        switch (_testAction)
                        {
                        case TestAction.CompletedTask:
                            _promise.SetResult(1);
                            break;

                        case TestAction.FailedTask:
                            _promise.SetException(new StatusTestException());
                            break;
                        }
                    }
                    else
                    {
                        if (_testAction == TestAction.CancelScheduledTask)
                        {
                            CancelWaitingToRunTaskScheduler scheduler = new CancelWaitingToRunTaskScheduler();
                            CancellationTokenSource         cts       = new CancellationTokenSource();
                            scheduler.Cancellation = cts;

                            // Replace _task with a task that has a cutoms scheduler
                            _task = Task.Factory.StartNew(() => { }, cts.Token, TaskCreationOptions.None, scheduler);

                            try { _task.GetAwaiter().GetResult(); }
                            catch (Exception ex)
                            {
                                if (ex is OperationCanceledException)
                                {
                                    Debug.WriteLine("OperationCanceledException Exception was thrown as expected");
                                }
                                else
                                {
                                    Assert.True(false, string.Format("Unexpected exception was thrown: \n{0}", ex.ToString()));
                                }
                            }
                        }
                        else if (_testAction == TestAction.CancelCreatedTask)
                        {
                            _taskCts.Cancel();
                        }
                        else //When the TestAction is CompletedTask and IsPromise is false, the code will reach this point
                        {
                            executeTask = true;
                            _task.Start();
                        }
                    }

                    if (_task != null && executeTask)
                    {
                        _mre.Wait();
                        //
                        // Current Task status is WaitingForChildrenToComplete if Task didn't Cancel/Faulted and Child was created
                        // without Detached options and current status of the child isn't RanToCompletion or Faulted yet
                        //

                        Task.Delay(100).Wait();

                        if (_createChildTask &&
                            _childTask != null &&
                            _testAction != TestAction.CancelTask &&
                            _testAction != TestAction.CancelTaskAndAcknowledge &&
                            _testAction != TestAction.FailedTask &&
                            _childCreationOptions == MyTaskCreationOptions.AttachedToParent &&
                            _childTask.Status != TaskStatus.RanToCompletion &&
                            _childTask.Status != TaskStatus.Faulted)
                        {
                            //we may have reach this point too soon, let's keep spinning until the status changes.
                            while (_task.Status == TaskStatus.Running)
                            {
                                ;
                            }
                            //
                            // If we're still waiting for children our Status should reflect so
                            //
                            if (_task.Status != TaskStatus.WaitingForChildrenToComplete)
                            {
                                Assert.True(false, string.Format("Expecting currrent Task status to be WaitingForChildren but getting {0}", _task.Status.ToString()));
                            }
                        }
                        _task.Wait();
                    }
                }
                catch (AggregateException exp)
                {
                    if ((_testAction == TestAction.CancelTaskAndAcknowledge || _testAction == TestAction.CancelScheduledTask || _testAction == TestAction.CancelCreatedTask) &&
                        exp.Flatten().InnerException.GetType() == typeof(TaskCanceledException))
                    {
                        Debug.WriteLine("TaskCanceledException Exception was thrown as expected");
                    }
                    else if ((_testAction == TestAction.FailedTask || _testAction == TestAction.FailedChildTask) && _task.IsFaulted &&
                             exp.Flatten().InnerException.GetType() == typeof(StatusTestException))
                    {
                        Debug.WriteLine("StatusTestException Exception was thrown as expected");
                    }
                    else
                    {
                        Assert.True(false, string.Format("Unexpected exception was thrown: \n{0}", exp.ToString()));
                    }
                }

                try
                {
                    //
                    // Need to wait for Children task if it was created with Default option (Detached by default),
                    // or current task was either canceled or failed
                    //
                    if (_createChildTask &&
                        (_childCreationOptions == MyTaskCreationOptions.None ||
                         _testAction == TestAction.CancelTask ||
                         _testAction == TestAction.CancelTaskAndAcknowledge ||
                         _testAction == TestAction.FailedTask))
                    {
                        _childTask.Wait();
                    }
                }
                catch (AggregateException exp)
                {
                    if (((_testAction == TestAction.CancelTask || _testAction == TestAction.CancelTaskAndAcknowledge) &&
                         _childCreationOptions == MyTaskCreationOptions.RespectParentCancellation) &&
                        exp.Flatten().InnerException.GetType() == typeof(TaskCanceledException))
                    {
                        Debug.WriteLine("TaskCanceledException Exception was thrown as expected");
                    }
                    else if (_testAction == TestAction.FailedChildTask && _childTask.IsFaulted &&
                             exp.Flatten().InnerException.GetType() == typeof(StatusTestException))
                    {
                        Debug.WriteLine("StatusTestException Exception was thrown as expected");
                    }
                    else
                    {
                        Assert.True(false, string.Format("Unexpected exception was thrown: \n{0}", exp.ToString()));
                    }
                }
            }

            //
            // Verification
            //
            if (_finalTaskStatus != null && _finalTaskStatus.Value != _task.Status)
            {
                Assert.True(false, string.Format("Expecting Task final Status to be {0}, while getting {1}", _finalTaskStatus.Value, _task.Status));
            }
            if (_finalChildTaskStatus != null && _finalChildTaskStatus.Value != _childTask.Status)
            {
                Assert.True(false, string.Format("Expecting Child Task final Status to be {0}, while getting {1}", _finalChildTaskStatus.Value, _childTask.Status));
            }
            if (_finalPromiseStatus != null && _finalPromiseStatus.Value != _promise.Task.Status)
            {
                Assert.True(false, string.Format("Expecting Promise Status to be {0}, while getting {1}", _finalPromiseStatus.Value, _promise.Task.Status));
            }

            //
            // Extra verifications for Cancel Task
            //
            if (_task != null && _task.Status == TaskStatus.Canceled && _task.IsCanceled != true)
            {
                Assert.True(false, string.Format("Task final Status is Canceled, expecting IsCanceled property to be True as well"));
            }
            if (_childTask != null && _childTask.Status == TaskStatus.Canceled && _childTask.IsCanceled != true)
            {
                Assert.True(false, string.Format("Child Task final Status is Canceled, expecting IsCanceled property to be True as well"));
            }

            //
            // Extra verification for faulted Promise
            //
            if (_isPromise && _testAction == TestAction.FailedTask)
            {
                //
                // If promise with Exception, read the exception so we don't
                // crash on Finalizer
                //
                AggregateException exp = _promise.Task.Exception;
                if (!_promise.Task.IsFaulted || exp == null)
                {
                    Assert.True(false, string.Format("No Exception found on promise"));
                }
                else if (exp.Flatten().InnerException.GetType() == typeof(StatusTestException))
                {
                    Debug.WriteLine("StatusTestException Exception was thrown as expected");
                }
                else
                {
                    Assert.True(false, string.Format("Exception on promise has mismatched type, expecting StatusTestException, actual: {0}", exp.Flatten().InnerException.GetType()));
                }
            }
        }
Esempio n. 2
0
        internal void RealRun()
        {
            if (_isPromise)
            {
                _promise = new TaskCompletionSource<int>();
            }
            else
            {
                _task = new Task(TaskRun, _taskCts.Token);
            }

            if (_testAction != TestAction.None)
            {
                try
                {
                    bool executeTask = false;

                    if (_isPromise)
                    {
                        switch (_testAction)
                        {
                            case TestAction.CompletedTask:
                                _promise.SetResult(1);
                                break;
                            case TestAction.FailedTask:
                                _promise.SetException(new StatusTestException());
                                break;
                        }
                    }
                    else
                    {
                        if (_testAction == TestAction.CancelScheduledTask)
                        {
                            CancelWaitingToRunTaskScheduler scheduler = new CancelWaitingToRunTaskScheduler();
                            CancellationTokenSource cts = new CancellationTokenSource();
                            scheduler.Cancellation = cts;

                            // Replace _task with a task that has a cutoms scheduler
                            _task = Task.Factory.StartNew(() => { }, cts.Token, TaskCreationOptions.None, scheduler);

                            try { _task.GetAwaiter().GetResult(); }
                            catch (Exception ex)
                            {
                                if (ex is OperationCanceledException)
                                    Debug.WriteLine("OperationCanceledException Exception was thrown as expected");
                                else
                                    Assert.True(false, string.Format("Unexpected exception was thrown: \n{0}", ex.ToString()));
                            }
                        }
                        else if (_testAction == TestAction.CancelCreatedTask)
                        {
                            _taskCts.Cancel();
                        }
                        else //When the TestAction is CompletedTask and IsPromise is false, the code will reach this point
                        {
                            executeTask = true;
                            _task.Start();
                        }
                    }

                    if (_task != null && executeTask)
                    {
                        _mre.Wait();
                        //
                        // Current Task status is WaitingForChildrenToComplete if Task didn't Cancel/Faulted and Child was created 
                        // without Detached options and current status of the child isn't RanToCompletion or Faulted yet
                        //

                        Task.Delay(1).Wait();

                        if (_createChildTask &&
                            _childTask != null &&
                            _testAction != TestAction.CancelTask &&
                            _testAction != TestAction.CancelTaskAndAcknowledge &&
                            _testAction != TestAction.FailedTask &&
                            _childCreationOptions == MyTaskCreationOptions.AttachedToParent &&
                            _childTask.Status != TaskStatus.RanToCompletion &&
                            _childTask.Status != TaskStatus.Faulted)
                        {
                            //we may have reach this point too soon, let's keep spinning until the status changes.
                            while (_task.Status == TaskStatus.Running)
                                ;
                            //
                            // If we're still waiting for children our Status should reflect so
                            //
                            if (_task.Status != TaskStatus.WaitingForChildrenToComplete)
                            {
                                Assert.True(false, string.Format("Expecting currrent Task status to be WaitingForChildren but getting {0}", _task.Status.ToString()));
                            }
                        }
                        _task.Wait();
                    }
                }
                catch (AggregateException exp)
                {
                    if ((_testAction == TestAction.CancelTaskAndAcknowledge || _testAction == TestAction.CancelScheduledTask || _testAction == TestAction.CancelCreatedTask) &&
                        exp.Flatten().InnerException.GetType() == typeof(TaskCanceledException))
                    {
                        Debug.WriteLine("TaskCanceledException Exception was thrown as expected");
                    }
                    else if ((_testAction == TestAction.FailedTask || _testAction == TestAction.FailedChildTask) && _task.IsFaulted &&
                        exp.Flatten().InnerException.GetType() == typeof(StatusTestException))
                    {
                        Debug.WriteLine("StatusTestException Exception was thrown as expected");
                    }
                    else
                    {
                        Assert.True(false, string.Format("Unexpected exception was thrown: \n{0}", exp.ToString()));
                    }
                }

                try
                {
                    //
                    // Need to wait for Children task if it was created with Default option (Detached by default), 
                    // or current task was either canceled or failed
                    //
                    if (_createChildTask &&
                        (_childCreationOptions == MyTaskCreationOptions.None ||
                        _testAction == TestAction.CancelTask ||
                        _testAction == TestAction.CancelTaskAndAcknowledge ||
                        _testAction == TestAction.FailedTask))
                    {
                        _childTask.Wait();
                    }
                }
                catch (AggregateException exp)
                {
                    if (((_testAction == TestAction.CancelTask || _testAction == TestAction.CancelTaskAndAcknowledge) &&
                        _childCreationOptions == MyTaskCreationOptions.RespectParentCancellation) &&
                        exp.Flatten().InnerException.GetType() == typeof(TaskCanceledException))
                    {
                        Debug.WriteLine("TaskCanceledException Exception was thrown as expected");
                    }
                    else if (_testAction == TestAction.FailedChildTask && _childTask.IsFaulted &&
                        exp.Flatten().InnerException.GetType() == typeof(StatusTestException))
                    {
                        Debug.WriteLine("StatusTestException Exception was thrown as expected");
                    }
                    else
                    {
                        Assert.True(false, string.Format("Unexpected exception was thrown: \n{0}", exp.ToString()));
                    }
                }
            }

            //
            // Verification
            //
            if (_finalTaskStatus != null && _finalTaskStatus.Value != _task.Status)
            {
                Assert.True(false, string.Format("Expecting Task final Status to be {0}, while getting {1}", _finalTaskStatus.Value, _task.Status));
            }
            if (_finalChildTaskStatus != null && _finalChildTaskStatus.Value != _childTask.Status)
            {
                Assert.True(false, string.Format("Expecting Child Task final Status to be {0}, while getting {1}", _finalChildTaskStatus.Value, _childTask.Status));
            }
            if (_finalPromiseStatus != null && _finalPromiseStatus.Value != _promise.Task.Status)
            {
                Assert.True(false, string.Format("Expecting Promise Status to be {0}, while getting {1}", _finalPromiseStatus.Value, _promise.Task.Status));
            }

            //
            // Extra verifications for Cancel Task
            //
            if (_task != null && _task.Status == TaskStatus.Canceled && _task.IsCanceled != true)
            {
                Assert.True(false, string.Format("Task final Status is Canceled, expecting IsCanceled property to be True as well"));
            }
            if (_childTask != null && _childTask.Status == TaskStatus.Canceled && _childTask.IsCanceled != true)
            {
                Assert.True(false, string.Format("Child Task final Status is Canceled, expecting IsCanceled property to be True as well"));
            }

            //
            // Extra verification for faulted Promise
            //
            if (_isPromise && _testAction == TestAction.FailedTask)
            {
                //
                // If promise with Exception, read the exception so we don't
                // crash on Finalizer
                //
                AggregateException exp = _promise.Task.Exception;
                if (!_promise.Task.IsFaulted || exp == null)
                {
                    Assert.True(false, string.Format("No Exception found on promise"));
                }
                else if (exp.Flatten().InnerException.GetType() == typeof(StatusTestException))
                {
                    Debug.WriteLine("StatusTestException Exception was thrown as expected");
                }
                else
                {
                    Assert.True(false, string.Format("Exception on promise has mismatched type, expecting StatusTestException, actual: {0}", exp.Flatten().InnerException.GetType()));
                }
            }
        }