private static async Task AssertUsesSynchronizationContext(int arity, bool?configureAwait, bool shouldUseSynchronizationContext)
    {
        var source = new TaskCompletionSource <object>();

        var adapters = AwaiterAdapter.CreateAdapters(Enumerable.Repeat(source.Task, arity).ToArray(), configureAwait);

        var copyableContext = new CopyableSynchronizationContext();

        using (TempSyncContext(copyableContext))
        {
            var resultSourcesByAdapterIndex =
                adapters.Select(_ => new TaskCompletionSource <SynchronizationContext>()).ToArray();

            for (var i = 0; i < adapters.Count; i++)
            {
                var adapterIndex = i;
                adapters[i].OnCompleted(() =>
                                        resultSourcesByAdapterIndex[adapterIndex].SetResult(SynchronizationContext.Current));
            }

            source.SetResult(null);

            var resultsByAdapterIndex = await Task.WhenAll(resultSourcesByAdapterIndex.Select(s => s.Task));

            var expected = shouldUseSynchronizationContext ? copyableContext : null;

            Assert.All(adapters.Zip(resultsByAdapterIndex, (adapter, result) => (Adapter: adapter, Result: result)), r => Assert.Same(expected, r.Result));
        }
    }
    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 = 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 = Enumerable.Range(0, arity)
                    .Select(index =>
                            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);
    }
    public static void ResultsAreInCorrectOrder(int arity)
    {
        var tasks = Enumerable.Range(0, arity)
                    .Select(index => Task.FromResult <object>(index)).ToArray();

        var adapters = AwaiterAdapter.CreateNonVoidResultAdapters(tasks);

        AssertAllAdapters(adapters, adapter =>
        {
            var result = adapter.GetResult();

            for (var i = 0; i < arity; i++)
            {
                if (!i.Equals((int)result[i]))
                {
                    return(false);
                }
            }

            return(true);
        });
    }
Beispiel #7
0
 public AwaiterAdapterAwaiter(AwaiterAdapter awaiterAdapter) =>