Exemple #1
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);
        }
Exemple #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));
        }
Exemple #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)));
        }
Exemple #4
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);
        }
Exemple #5
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);
        }
Exemple #6
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);
        }
Exemple #7
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);
        }
Exemple #8
0
        public void GetResult_BeforeCompleted_Fails()
        {
            var mrvts = new ManualResetValueTaskSource <int>();

            Assert.Throws <InvalidOperationException>(() => mrvts.GetResult(0));
        }