Пример #1
0
        public void OnCompleted_UsedTwiceBeforeCompletion_Throws()
        {
            var mrvts = new ManualResetValueTaskSource <int>();

            mrvts.OnCompleted(_ => { }, null, 0, ValueTaskSourceOnCompletedFlags.None);
            Assert.Throws <InvalidOperationException>(() => mrvts.OnCompleted(_ => { }, null, 0, ValueTaskSourceOnCompletedFlags.None));
        }
Пример #2
0
        public void AccessWrongVersion_Fails()
        {
            var mrvts = new ManualResetValueTaskSource <int>();

            mrvts.Reset();

            Assert.Throws <InvalidOperationException>(() => mrvts.GetResult(0));
            Assert.Throws <InvalidOperationException>(() => mrvts.GetStatus(0));
            Assert.Throws <InvalidOperationException>(() => mrvts.OnCompleted(_ => { }, new object(), 0, ValueTaskSourceOnCompletedFlags.None));

            Assert.Throws <InvalidOperationException>(() => mrvts.GetResult(2));
            Assert.Throws <InvalidOperationException>(() => mrvts.GetStatus(2));
            Assert.Throws <InvalidOperationException>(() => mrvts.OnCompleted(_ => { }, new object(), 2, ValueTaskSourceOnCompletedFlags.None));
        }
Пример #3
0
        public async Task SetResult_AfterOnCompleted_ResultAvailableAsynchronously()
        {
            var mrvts = new ManualResetValueTaskSource <int>();

            mrvts.Reset();
            mrvts.Reset();
            Assert.Equal(2, mrvts.Version);

            Assert.Equal(ValueTaskSourceStatus.Pending, mrvts.GetStatus(2));
            Assert.Throws <InvalidOperationException>(() => mrvts.GetResult(2));

            var onCompletedRan = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously);

            mrvts.OnCompleted(s => ((TaskCompletionSource)s).SetResult(), onCompletedRan, 2, ValueTaskSourceOnCompletedFlags.None);

            Assert.False(onCompletedRan.Task.IsCompleted);
            await Task.Delay(1);

            Assert.False(onCompletedRan.Task.IsCompleted);

            mrvts.SetResult(42);
            Assert.Equal(ValueTaskSourceStatus.Succeeded, mrvts.GetStatus(2));
            Assert.Equal(42, mrvts.GetResult(2));

            await onCompletedRan.Task;

            Assert.Equal(2, mrvts.Version);
        }
Пример #4
0
        public async Task SynchronizationContext_CaptureIfRequested(
            bool runContinuationsAsynchronously, bool captureSyncCtx, bool setBeforeOnCompleted)
        {
            await Task.Run(async() =>  // escape xunit sync ctx
            {
                var mrvts = new ManualResetValueTaskSource <int>()
                {
                    RunContinuationsAsynchronously = runContinuationsAsynchronously
                };

                if (setBeforeOnCompleted)
                {
                    mrvts.SetResult(42);
                }

                var tcs = new TaskCompletionSource();
                var sc  = new TrackingSynchronizationContext();
                SynchronizationContext.SetSynchronizationContext(sc);
                Assert.Equal(0, sc.Posts);
                mrvts.OnCompleted(
                    _ => tcs.SetResult(),
                    null,
                    0,
                    captureSyncCtx ? ValueTaskSourceOnCompletedFlags.UseSchedulingContext : ValueTaskSourceOnCompletedFlags.None);
                SynchronizationContext.SetSynchronizationContext(null);

                if (!setBeforeOnCompleted)
                {
                    mrvts.SetResult(42);
                }

                await tcs.Task;
                Assert.Equal(captureSyncCtx ? 1 : 0, sc.Posts);
            });
        }
Пример #5
0
        public void SetResult_RunContinuationsAsynchronously_ContinuationInvokedAccordingly(bool runContinuationsAsynchronously)
        {
            var mres  = new ManualResetEventSlim();
            var mrvts = new ManualResetValueTaskSource <int>()
            {
                RunContinuationsAsynchronously = runContinuationsAsynchronously
            };

            for (short i = 0; i < 10; i++)
            {
                int threadId = Environment.CurrentManagedThreadId;
                mrvts.OnCompleted(
                    _ =>
                {
                    Assert.Equal(!runContinuationsAsynchronously, threadId == Environment.CurrentManagedThreadId);
                    mres.Set();
                },
                    null,
                    i,
                    ValueTaskSourceOnCompletedFlags.None);
                mrvts.SetResult(42);
                mres.Wait();
                mrvts.Reset();
                mres.Reset();
            }
        }
Пример #6
0
        public async Task SetResult_RunContinuationsAsynchronously_ContinuationInvokedAccordingly(bool runContinuationsAsynchronously)
        {
            var mrvts = new ManualResetValueTaskSource <int>()
            {
                RunContinuationsAsynchronously = runContinuationsAsynchronously
            };

            for (short i = 0; i < 10; i++)
            {
                var tcs = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously);

                var tl = new ThreadLocal <int> {
                    Value = 42
                };
                mrvts.OnCompleted(
                    _ =>
                {
                    Assert.Equal(!runContinuationsAsynchronously, tl.Value == 42);
                    tcs.SetResult();
                },
                    null,
                    i,
                    ValueTaskSourceOnCompletedFlags.None);
                mrvts.SetResult(42);
                mrvts.Reset();

                tl.Value = 0;

                await tcs.Task;
            }
        }
Пример #7
0
        public void SetResult_BeforeOnCompleted_ResultAvailableSynchronously()
        {
            var mrvts = new ManualResetValueTaskSource <int>();

            mrvts.Reset();
            mrvts.Reset();
            Assert.Equal(2, mrvts.Version);

            mrvts.SetResult(42);

            Assert.Equal(ValueTaskSourceStatus.Succeeded, mrvts.GetStatus(2));
            Assert.Equal(42, mrvts.GetResult(2));

            var mres = new ManualResetEventSlim();

            mrvts.OnCompleted(s => ((ManualResetEventSlim)s).Set(), mres, 2, ValueTaskSourceOnCompletedFlags.None);
            mres.Wait();

            Assert.Equal(2, mrvts.Version);
        }
Пример #8
0
        public void FlowContext_SetAfterOnCompleted_FlowsIfExpected(bool flowContext)
        {
            var mres  = new ManualResetEventSlim();
            var mrvts = new ManualResetValueTaskSource <int>();

            mrvts.RunContinuationsAsynchronously = true;

            var al = new AsyncLocal <int>();

            al.Value = 42;
            mrvts.OnCompleted(
                _ => { Assert.Equal(flowContext ? 42 : 0, al.Value); mres.Set(); },
                null,
                0,
                flowContext ? ValueTaskSourceOnCompletedFlags.FlowExecutionContext : ValueTaskSourceOnCompletedFlags.None);

            mrvts.SetResult(1);

            mres.Wait();
        }
Пример #9
0
        public async Task SetResult_BeforeOnCompleted_ResultAvailableSynchronously()
        {
            var mrvts = new ManualResetValueTaskSource <int>();

            mrvts.Reset();
            mrvts.Reset();
            Assert.Equal(2, mrvts.Version);

            mrvts.SetResult(42);

            Assert.Equal(ValueTaskSourceStatus.Succeeded, mrvts.GetStatus(2));
            Assert.Equal(42, mrvts.GetResult(2));

            var tcs = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously);

            mrvts.OnCompleted(s => ((TaskCompletionSource)s).SetResult(), tcs, 2, ValueTaskSourceOnCompletedFlags.None);
            await tcs.Task;

            Assert.Equal(2, mrvts.Version);
        }
Пример #10
0
        public async Task FlowContext_SetAfterOnCompleted_FlowsIfExpected(bool flowContext)
        {
            var tcs   = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously);
            var mrvts = new ManualResetValueTaskSource <int>();

            mrvts.RunContinuationsAsynchronously = true;

            var al = new AsyncLocal <int>();

            al.Value = 42;
            mrvts.OnCompleted(
                _ => { Assert.Equal(flowContext ? 42 : 0, al.Value); tcs.SetResult(); },
                null,
                0,
                flowContext ? ValueTaskSourceOnCompletedFlags.FlowExecutionContext : ValueTaskSourceOnCompletedFlags.None);

            mrvts.SetResult(1);

            await tcs.Task;
        }
Пример #11
0
        public void SetException_BeforeOnCompleted_ResultAvailableSynchronously()
        {
            var mrvts = new ManualResetValueTaskSource <int>();

            mrvts.Reset();
            mrvts.Reset();
            Assert.Equal(2, mrvts.Version);

            var e = new FormatException();

            mrvts.SetException(e);

            Assert.Equal(ValueTaskSourceStatus.Faulted, mrvts.GetStatus(2));
            Assert.Same(e, Assert.Throws <FormatException>(() => mrvts.GetResult(2)));

            var mres = new ManualResetEventSlim();

            mrvts.OnCompleted(s => ((ManualResetEventSlim)s).Set(), mres, 2, ValueTaskSourceOnCompletedFlags.None);
            mres.Wait();

            Assert.Equal(2, mrvts.Version);
        }
Пример #12
0
        public async Task SetException_BeforeOnCompleted_ResultAvailableSynchronously()
        {
            var mrvts = new ManualResetValueTaskSource <int>();

            mrvts.Reset();
            mrvts.Reset();
            Assert.Equal(2, mrvts.Version);

            var e = new FormatException();

            mrvts.SetException(e);

            Assert.Equal(ValueTaskSourceStatus.Faulted, mrvts.GetStatus(2));
            Assert.Same(e, Assert.Throws <FormatException>(() => mrvts.GetResult(2)));

            var tcs = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously);

            mrvts.OnCompleted(s => ((TaskCompletionSource)s).SetResult(), tcs, 2, ValueTaskSourceOnCompletedFlags.None);
            await tcs.Task;

            Assert.Equal(2, mrvts.Version);
        }
Пример #13
0
        public async Task TaskScheduler_CaptureIfRequested(
            bool runContinuationsAsynchronously, bool captureTaskScheduler, bool setBeforeOnCompleted)
        {
            await Task.Run(async() =>  // escape xunit sync ctx
            {
                var mrvts = new ManualResetValueTaskSource <int>()
                {
                    RunContinuationsAsynchronously = runContinuationsAsynchronously
                };

                if (setBeforeOnCompleted)
                {
                    mrvts.SetResult(42);
                }

                var tcs = new TaskCompletionSource();
                var ts  = new TrackingTaskScheduler();
                Assert.Equal(0, ts.QueueTasks);
                await Task.Factory.StartNew(() =>
                {
                    mrvts.OnCompleted(
                        _ => tcs.SetResult(),
                        null,
                        0,
                        captureTaskScheduler ? ValueTaskSourceOnCompletedFlags.UseSchedulingContext : ValueTaskSourceOnCompletedFlags.None);
                }, CancellationToken.None, TaskCreationOptions.None, ts);

                if (!setBeforeOnCompleted)
                {
                    mrvts.SetResult(42);
                }

                await tcs.Task;
                Assert.Equal(captureTaskScheduler ? 2 : 1, ts.QueueTasks);
            });
        }
Пример #14
0
        public void OnCompleted_UnknownFlagsIgnored()
        {
            var mrvts = new ManualResetValueTaskSource <int>();

            mrvts.OnCompleted(_ => { }, new object(), 0, (ValueTaskSourceOnCompletedFlags)int.MaxValue);
        }
Пример #15
0
        public void OnCompleted_NullDelegate_Throws()
        {
            var mrvts = new ManualResetValueTaskSource <int>();

            AssertExtensions.Throws <ArgumentNullException>("continuation", () => mrvts.OnCompleted(null, new object(), 0, ValueTaskSourceOnCompletedFlags.None));
        }