public static void CompletesSynchronouslyIfAllTasksWereCompletedSynchronously(int arity)
        {
            var tasks = Enumerable.Repeat(CompletedTask, arity).ToArray();

            var adapters = AwaiterAdapter.CreateAllAdapters(tasks);

            AssertAllAdapters(adapters, adapter => adapter.IsCompleted);
        }
        public static void FirstExceptionIsThrown(int arity)
        {
            var sources = (
                from index in Enumerable.Range(0, arity)
                select new TaskCompletionSource <object>()).ToArray();

            var adapters = AwaiterAdapter.CreateAllAdapters(sources.Select(source => source.Task).ToArray());

            for (var i = sources.Length - 1; i >= 0; i--)
            {
                sources[i].SetException(new DummyException());
            }

            AssertAllAdapters(adapters, adapter =>
                              ReferenceEquals(
                                  sources[0].Task.Exception.InnerException,
                                  Assert.ThrowsAny <DummyException>(adapter.GetResult)));
        }
        public static async Task WaitsForAllTasksToCompleteWhenAllFail(int arity, int whichToWaitFor)
        {
            var source = new TaskCompletionSource <object>();

            var tasks = (
                from index in Enumerable.Range(0, arity)
                select index == whichToWaitFor ? source.Task : FailedTask).ToArray();

            var adapters = AwaiterAdapter.CreateAllAdapters(tasks);

            AssertAllAdapters(adapters, adapter => !adapter.IsCompleted);

            source.SetException(new DummyException());

            foreach (var adapter in adapters)
            {
                await Assert.ThrowsAnyAsync <DummyException>(async() => await adapter);
            }

            AssertAllAdapters(adapters, adapter => adapter.IsCompleted);
        }
        public static async Task WaitsForAllTasksToCompleteWhenAllSucceed(int arity, int whichToWaitFor)
        {
            var source = new TaskCompletionSource <object>();

            var tasks = (
                from index in Enumerable.Range(0, arity)
                select index == whichToWaitFor ? source.Task : CompletedTask).ToArray();

            var adapters = AwaiterAdapter.CreateAllAdapters(tasks);

            AssertAllAdapters(adapters, adapter => !adapter.IsCompleted);

            source.SetResult(null);

            foreach (var adapter in adapters)
            {
                await adapter;
            }

            AssertAllAdapters(adapters, adapter => adapter.IsCompleted);
        }