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

            mrvts.SetResult(42);
            Assert.Throws <InvalidOperationException>(() => mrvts.SetResult(42));
            Assert.Throws <InvalidOperationException>(() => mrvts.SetException(new Exception()));

            mrvts.Reset();
            mrvts.SetException(new Exception());
            Assert.Throws <InvalidOperationException>(() => mrvts.SetResult(42));
            Assert.Throws <InvalidOperationException>(() => mrvts.SetException(new Exception()));
        }
Пример #2
0
        public async Task SetException_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);

            var e = new FormatException();

            mrvts.SetException(e);

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

            await onCompletedRan.Task;

            Assert.Equal(2, mrvts.Version);
        }
Пример #3
0
        public void SetException_OperationCanceledException_StatusIsCanceled()
        {
            var mrvts = new ManualResetValueTaskSource <int>();
            var e     = new OperationCanceledException();

            mrvts.SetException(e);
            Assert.Equal(ValueTaskSourceStatus.Canceled, mrvts.GetStatus(0));
            Assert.Same(e, Assert.Throws <OperationCanceledException>(() => mrvts.GetResult(0)));
        }
        public static ManualResetValueTaskSource <T> Completed <T>(T result, Exception error = null)
        {
            var vts = new ManualResetValueTaskSource <T>();

            if (error != null)
            {
                vts.SetException(error);
            }
            else
            {
                vts.SetResult(result);
            }
            return(vts);
        }
Пример #5
0
        /// <summary>
        /// Complete with or without an error.
        /// </summary>
        public static void SetExceptionOrResult(this ManualResetValueTaskSource ts, Exception exceptionOrNot)
        {
            if (ts is null)
            {
                throw new ArgumentNullException(nameof(ts));
            }

            if (exceptionOrNot is null)
            {
                ts.SetResult();
            }
            else
            {
                ts.SetException(exceptionOrNot);
            }
        }
        public static ManualResetValueTaskSource <T> Delay <T>(int delayMs, T result, Exception error = null)
        {
            var vts = new ManualResetValueTaskSource <T>();

            Task.Delay(delayMs).ContinueWith(_ =>
            {
                if (error != null)
                {
                    vts.SetException(error);
                }
                else
                {
                    vts.SetResult(result);
                }
            });
            return(vts);
        }
Пример #7
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);
        }
Пример #8
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);
        }