コード例 #1
0
        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(), continueOnCapturedContext: 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));
            }
        }
コード例 #2
0
        private static void AssertUsesSynchronizationContext(int arity, bool?configureAwait, bool shouldUseSynchronizationContext)
        {
            var source = new TaskCompletionSource <object>();

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

            var copyableContext = new CopyableSynchronizationContext();

            using (TempSyncContext(copyableContext))
            {
                var actualContinuationContextByAdapterIndex = new SynchronizationContext[adapters.Count];

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

                source.SetResult(null);

                for (var i = 0; i < adapters.Count; i++)
                {
                    if (shouldUseSynchronizationContext)
                    {
                        Assert.Same(expected: copyableContext, actual: actualContinuationContextByAdapterIndex[i]);
                    }
                    else
                    {
                        Assert.Null(actualContinuationContextByAdapterIndex[i]);
                    }
                }
            }
        }
コード例 #3
0
        public static void CompletesSynchronouslyIfAllTasksWereCompletedSynchronously(int arity)
        {
            var tasks = Enumerable.Repeat(CompletedTask, arity).ToArray();

            var adapters = AwaiterAdapter.CreateAllAdapters(tasks);

            AssertAllAdapters(adapters, adapter => adapter.IsCompleted);
        }
コード例 #4
0
        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)));
        }
コード例 #5
0
        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);
        }
コード例 #6
0
        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);
        }
コード例 #7
0
        public static void ResultsAreInCorrectOrder(int arity)
        {
            var tasks = (
                from index in Enumerable.Range(0, arity)
                select 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(result[i]))
                    {
                        return(false);
                    }
                }

                return(true);
            });
        }
コード例 #8
0
 public AwaiterAdapterAwaiter(AwaiterAdapter awaiterAdapter)
 {
     this.awaiterAdapter = awaiterAdapter;
 }