Exemplo n.º 1
0
        [PreventExecutionContextLeaks] // Workaround for https://github.com/nunit/nunit/issues/3283
        public static void Handler_should_be_called_one_additional_time_to_handle_its_own_exception()
        {
            var taskException    = new Exception();
            var handlerException = new Exception();
            var watcher          = new CallbackWatcher();

            AmbientTasks.BeginContext(ex =>
            {
                watcher.OnCallback(out var callCount);

                if (callCount == 1)
                {
                    throw handlerException;
                }

                ex.ShouldBe(handlerException);
            });

            using (watcher.ExpectCallback(count: 2))
                AmbientTasks.Add(Task.FromException(taskException));

            var waitAllTask = AmbientTasks.WaitAllAsync();

            waitAllTask.Status.ShouldBe(TaskStatus.Faulted);

            var aggregateException = waitAllTask.Exception !.InnerExceptions.ShouldHaveSingleItem().ShouldBeOfType <AggregateException>();

            aggregateException.InnerExceptions.ShouldHaveSingleItem().ShouldBeSameAs(taskException);
        }
        [PreventExecutionContextLeaks] // Workaround for https://github.com/nunit/nunit/issues/3283
        public static void Exception_from_user_delegate_is_in_task_from_next_call_to_WaitAllAsync_when_there_is_no_BeginContext_handler([Values] PostOverload overload)
        {
            var exception = new Exception();

            using (SynchronizationContextAssert.ExpectSinglePost(postedAction => postedAction.Invoke()))
            {
                Should.Throw <Exception>(() => AmbientTasksPost(overload, () => throw exception));
            }

            var waitAllTask = AmbientTasks.WaitAllAsync();

            waitAllTask.Status.ShouldBe(TaskStatus.Faulted);

            var aggregateException = waitAllTask.Exception !.InnerExceptions.ShouldHaveSingleItem().ShouldBeOfType <AggregateException>();

            aggregateException.InnerExceptions.ShouldHaveSingleItem().ShouldBeSameAs(exception);
        }
Exemplo n.º 3
0
        [PreventExecutionContextLeaks] // Workaround for https://github.com/nunit/nunit/issues/3283
        public static void WaitAllAsync_should_have_single_AggregateException_with_all_exceptions_from_each_task_all_faulted_synchronously()
        {
            var task1Exceptions = new[] { new Exception("Task 1 exception 1"), new Exception("Task 1 exception 2") };
            var task2Exceptions = new[] { new Exception("Task 2 exception 1"), new Exception("Task 2 exception 2") };
            var source1         = new TaskCompletionSource <object?>();
            var source2         = new TaskCompletionSource <object?>();

            AmbientTasks.Add(source1.Task);
            AmbientTasks.Add(source2.Task);

            source1.SetException(task1Exceptions);
            source2.SetException(task2Exceptions);

            var waitAllTask = AmbientTasks.WaitAllAsync();

            waitAllTask.Status.ShouldBe(TaskStatus.Faulted);

            var aggregateException = waitAllTask.Exception !.InnerExceptions.ShouldHaveSingleItem().ShouldBeOfType <AggregateException>();

            aggregateException.InnerExceptions.ShouldBe(task1Exceptions.Concat(task2Exceptions));
        }
Exemplo n.º 4
0
        [PreventExecutionContextLeaks] // Workaround for https://github.com/nunit/nunit/issues/3283
        public static void WaitAllAsync_should_have_single_AggregateException_with_all_three_exceptions_when_handler_throws_exception_twice()
        {
            var taskException     = new Exception();
            var handlerException1 = new Exception();
            var handlerException2 = new Exception();
            var watcher           = new CallbackWatcher();

            AmbientTasks.BeginContext(ex =>
            {
                watcher.OnCallback(out var callCount);

                throw callCount == 1 ? handlerException1 : handlerException2;
            });

            using (watcher.ExpectCallback(count: 2))
                AmbientTasks.Add(Task.FromException(taskException));

            var waitAllTask = AmbientTasks.WaitAllAsync();

            waitAllTask.Status.ShouldBe(TaskStatus.Faulted);
            var aggregateException = waitAllTask.Exception !.InnerExceptions.ShouldHaveSingleItem().ShouldBeOfType <AggregateException>();

            aggregateException.InnerExceptions.ShouldBe(new[] { taskException, handlerException1, handlerException2 });
        }