[PreventExecutionContextLeaks] // Workaround for https://github.com/nunit/nunit/issues/3283
        public static void Passed_synchronization_context_is_used_for_post([Values] PostOverload overload)
        {
            using (SynchronizationContextAssert.ExpectSinglePost(postedAction => { }))
            {
                var contextToUse = SynchronizationContext.Current !;

                using (SynchronizationContextAssert.ExpectNoPost())
                {
                    switch (overload)
                    {
                    case PostOverload.Action:
                        AmbientTasks.Post(contextToUse, () => { });
                        break;

                    case PostOverload.SendOrPostCallback:
                        AmbientTasks.Post(contextToUse, state => { }, state: null);
                        break;

                    case PostOverload.AsyncAction:
                        AmbientTasks.Post(contextToUse, () => Task.CompletedTask);
                        break;

                    default:
                        throw new NotImplementedException();
                    }
                }
            }
        }
        [PreventExecutionContextLeaks] // Workaround for https://github.com/nunit/nunit/issues/3283
        public static void Passed_delegate_may_be_null([Values] PostOverload overload)
        {
            switch (overload)
            {
            case PostOverload.Action:
                AmbientTasks.Post((Action?)null);
                break;

            case PostOverload.SendOrPostCallback:
                AmbientTasks.Post(null, state: null);
                break;

            case PostOverload.AsyncAction:
                AmbientTasks.Post((Func <Task>?)null);
                break;

            default:
                throw new NotImplementedException();
            }
        }
        [PreventExecutionContextLeaks] // Workaround for https://github.com/nunit/nunit/issues/3283
        public static void WaitAllAsync_does_not_complete_until_async_task_returned_by_user_delegate_completes()
        {
            var source       = new TaskCompletionSource <object?>();
            var postedAction = (Action?)null;

            using (SynchronizationContextAssert.ExpectSinglePost(p => postedAction = p))
            {
                AmbientTasks.Post(() => source.Task);
            }

            var waitAllTask = AmbientTasks.WaitAllAsync();

            waitAllTask.IsCompleted.ShouldBeFalse();

            postedAction.ShouldNotBeNull();
            postedAction !.Invoke();

            waitAllTask.IsCompleted.ShouldBeFalse();

            source.SetResult(null);

            waitAllTask.Status.ShouldBe(TaskStatus.RanToCompletion);
        }
        private static void AmbientTasksPost(PostOverload overload, Action action)
        {
            switch (overload)
            {
            case PostOverload.Action:
                AmbientTasks.Post(action);
                break;

            case PostOverload.SendOrPostCallback:
                AmbientTasks.Post(state => ((Action)state !).Invoke(), state: action);
                break;

            case PostOverload.AsyncAction:
                AmbientTasks.Post(() =>
                {
                    action.Invoke();
                    return(Task.CompletedTask);
                });
                break;

            default:
                throw new NotImplementedException();
            }
        }