| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658 | // Licensed to the .NET Foundation under one or more agreements.// The .NET Foundation licenses this file to you under the MIT License.// See the LICENSE file in the project root for more information. using System;using System.Linq;using System.Reactive;using System.Reactive.Concurrency;using System.Reactive.Linq;using System.Runtime.CompilerServices;using System.Security.Cryptography;using System.Threading;using System.Threading.Tasks;using Microsoft.Reactive.Testing;using Microsoft.VisualStudio.TestTools.UnitTesting;using Tests.System.Reactive;using Assert = Xunit.Assert;namespace ReactiveTests.Tests{    [TestClass]    public class StartAsyncTest : ReactiveTest    {        private readonly Task<int> _doneTask;        public StartAsyncTest()        {            var tcs = new TaskCompletionSource<int>();            tcs.SetResult(42);            _doneTask = tcs.Task;        }        #region Func        [TestMethod]        public void StartAsync_Func_ArgumentChecking()        {            var s = Scheduler.Immediate;            ReactiveAssert.Throws<ArgumentNullException>(() => Observable.StartAsync(default(Func<Task<int>>)));            ReactiveAssert.Throws<ArgumentNullException>(() => Observable.StartAsync(default(Func<CancellationToken, Task<int>>)));            ReactiveAssert.Throws<ArgumentNullException>(() => Observable.StartAsync(default(Func<Task<int>>), s));            ReactiveAssert.Throws<ArgumentNullException>(() => Observable.StartAsync(default(Func<CancellationToken, Task<int>>), s));            ReactiveAssert.Throws<ArgumentNullException>(() => Observable.StartAsync(() => _doneTask, default(IScheduler)));            ReactiveAssert.Throws<ArgumentNullException>(() => Observable.StartAsync(ct => _doneTask, default(IScheduler)));        }        [TestMethod]        public void StartAsync_Func_Success()        {            var n = 42;            var i = 0;            var xs = Observable.StartAsync(() =>            {                i++;                return Task.Factory.StartNew(() => n);            });            Assert.Equal(n, xs.Single());            Assert.Equal(1, i);            Assert.Equal(n, xs.Single());            Assert.Equal(1, i);        }        [TestMethod]        public void StartAsync_Func_Throw_Synchronous()        {            var ex = new Exception();            var xs = Observable.StartAsync<int>(() =>            {                throw ex;            });            ReactiveAssert.Throws(ex, () => xs.Single());        }        [TestMethod]        public void StartAsync_Func_Throw_Asynchronous()        {            var ex = new Exception();            var xs = Observable.StartAsync(() =>                Task.Factory.StartNew<int>(() =>                {                    throw ex;                })            );            ReactiveAssert.Throws(ex, () => xs.Single());        }        [TestMethod]        public void StartAsync_FuncWithCancel_Success()        {            var n = 42;            var i = 0;            var xs = Observable.StartAsync(ct =>            {                i++;                return Task.Factory.StartNew(() => n);            });            Assert.Equal(n, xs.Single());            Assert.Equal(1, i);            Assert.Equal(n, xs.Single());            Assert.Equal(1, i);        }        [TestMethod]        public void StartAsync_FuncWithCancel_Throw_Synchronous()        {            var ex = new Exception();            var xs = Observable.StartAsync<int>(ct =>            {                throw ex;            });            ReactiveAssert.Throws(ex, () => xs.Single());        }        [TestMethod]        public void StartAsync_FuncWithCancel_Throw_Asynchronous()        {            var ex = new Exception();            var xs = Observable.StartAsync(ct =>                Task.Factory.StartNew<int>(() => { throw ex; })            );            ReactiveAssert.Throws(ex, () => xs.Single());        }        [TestMethod]        public void StartAsync_FuncWithCancel_Cancel()        {            var N = 10;            for (var n = 0; n < N; n++)            {                var e = new ManualResetEvent(false);                var f = new ManualResetEvent(false);                var t = default(Task<int>);                var xs = Observable.StartAsync(ct =>                    t = Task.Factory.StartNew<int>(() =>                    {                        try                        {                            e.Set();                            while (true)                            {                                ct.ThrowIfCancellationRequested();                            }                        }                        finally                        {                            f.Set();                        }                    })                );                e.WaitOne();                var d = xs.Subscribe(_ => { });                d.Dispose();                f.WaitOne();                while (!t.IsCompleted)                {                    ;                }                ReactiveAssert.Throws<OperationCanceledException>(() => xs.Single());            }        }        [TestMethod]        public void Start_Func_UnsubscribeThenError_ErrorReportedAsUnobserved()        {            Start_Func_ErrorAfterUnsubscribeReportedAsUnobserved_Core(                createTask => Observable.StartAsync(createTask),                errorObservation =>                {                    errorObservation.AssertExceptionReportedAsUnobserved();                });        }        [TestMethod]        public void Start_FuncWithCancel_UnsubscribeThenError_ErrorReportedAsUnobserved()        {            Start_Func_ErrorAfterUnsubscribeReportedAsUnobserved_Core(                createTask => Observable.StartAsync(_ => createTask()),                errorObservation =>                {                    errorObservation.AssertExceptionReportedAsUnobserved();                });        }        [TestMethod]        public void Start_Func_WithScheduler_UnsubscribeThenError_ErrorReportedAsUnobserved()        {            Start_Func_ErrorAfterUnsubscribeReportedAsUnobserved_Core(                createTask => Observable.StartAsync(createTask, TaskPoolScheduler.Default),                errorObservation =>                {                    errorObservation.AssertExceptionReportedAsUnobserved();                });        }        [TestMethod]        public void Start_FuncWithCancel_WithScheduler_UnsubscribeThenError_ErrorReportedAsUnobserved()        {            Start_Func_ErrorAfterUnsubscribeReportedAsUnobserved_Core(                createTask => Observable.StartAsync(_ => createTask(), TaskPoolScheduler.Default),                errorObservation =>                {                    errorObservation.AssertExceptionReportedAsUnobserved();                });        }        [TestMethod]        public void Start_Func_IgnorePostUnsubscribeErrors_UnsubscribeThenError_ErrorNotReportedAsUnobserved()        {            Start_Func_ErrorAfterUnsubscribeReportedAsUnobserved_Core(                createTask => Observable.StartAsync(createTask, new TaskObservationOptions(null, ignoreExceptionsAfterUnsubscribe: true)),                errorObservation =>                {                    errorObservation.AssertExceptionNotReportedAsUnobserved();                });        }        [TestMethod]        public void Start_FuncWithCancel_IgnorePostUnsubscribeErrors_UnsubscribeThenError_ErrorNotReportedAsUnobserved()        {            Start_Func_ErrorAfterUnsubscribeReportedAsUnobserved_Core(                createTask => Observable.StartAsync(_ => createTask(), new TaskObservationOptions(null, ignoreExceptionsAfterUnsubscribe: true)),                errorObservation =>                {                    errorObservation.AssertExceptionNotReportedAsUnobserved();                });        }        [TestMethod]        public void Start_Func_WithScheduler_IgnorePostUnsubscribeErrors_UnsubscribeThenError_ErrorNotReportedAsUnobserved()        {            Start_Func_ErrorAfterUnsubscribeReportedAsUnobserved_Core(                createTask => Observable.StartAsync(createTask, new TaskObservationOptions(TaskPoolScheduler.Default, ignoreExceptionsAfterUnsubscribe: true)),                errorObservation =>                {                    errorObservation.AssertExceptionNotReportedAsUnobserved();                });        }        [TestMethod]        public void Start_FuncWithCancel_WithScheduler_IgnorePostUnsubscribeErrors_UnsubscribeThenError_ErrorNotReportedAsUnobserved()        {            Start_Func_ErrorAfterUnsubscribeReportedAsUnobserved_Core(                createTask => Observable.StartAsync(_ => createTask(), new TaskObservationOptions(TaskPoolScheduler.Default, ignoreExceptionsAfterUnsubscribe: true)),                errorObservation =>                {                    errorObservation.AssertExceptionNotReportedAsUnobserved();                });        }#if DESKTOPCLR        [TestMethod]        public void StartAsync_Func_Scheduler1()        {            var tcs = new TaskCompletionSource<int>();            var e = new ManualResetEvent(false);            var x = default(int);            var t = default(int);            var xs = Observable.StartAsync(() => tcs.Task, Scheduler.Immediate);            xs.Subscribe(res =>            {                x = res;                t = Environment.CurrentManagedThreadId;                e.Set();            });            tcs.SetResult(42);            e.WaitOne();            Assert.Equal(42, x);            Assert.Equal(Environment.CurrentManagedThreadId, t);        }        [TestMethod]        public void StartAsync_Func_Scheduler2()        {            var tcs = new TaskCompletionSource<int>();            var e = new ManualResetEvent(false);            var x = default(int);            var t = default(int);            var xs = Observable.StartAsync(ct => tcs.Task, Scheduler.Immediate);            xs.Subscribe(res =>            {                x = res;                t = Environment.CurrentManagedThreadId;                e.Set();            });            tcs.SetResult(42);            e.WaitOne();            Assert.Equal(42, x);            Assert.Equal(Environment.CurrentManagedThreadId, t);        }#endif        #endregion        #region Action        [TestMethod]        public void StartAsync_Action_ArgumentChecking()        {            var s = Scheduler.Immediate;            ReactiveAssert.Throws<ArgumentNullException>(() => Observable.StartAsync(default(Func<Task>)));            ReactiveAssert.Throws<ArgumentNullException>(() => Observable.StartAsync(default(Func<CancellationToken, Task>)));            ReactiveAssert.Throws<ArgumentNullException>(() => Observable.StartAsync(default(Func<Task>), s));            ReactiveAssert.Throws<ArgumentNullException>(() => Observable.StartAsync(default(Func<CancellationToken, Task>), s));            ReactiveAssert.Throws<ArgumentNullException>(() => Observable.StartAsync(() => (Task)_doneTask, default(IScheduler)));            ReactiveAssert.Throws<ArgumentNullException>(() => Observable.StartAsync(ct => (Task)_doneTask, default(IScheduler)));        }        [TestMethod]        public void StartAsync_Action_Success()        {            var i = 0;            var xs = Observable.StartAsync(() =>            {                i++;                return Task.Factory.StartNew(() => { });            });            Assert.Equal(Unit.Default, xs.Single());            Assert.Equal(1, i);            Assert.Equal(Unit.Default, xs.Single());            Assert.Equal(1, i);        }        [TestMethod]        public void StartAsync_Action_Throw_Synchronous()        {            var ex = new Exception();            var xs = Observable.StartAsync(() =>            {                throw ex;            });            ReactiveAssert.Throws(ex, () => xs.Single());        }        [TestMethod]        public void StartAsync_Action_Throw_Asynchronous()        {            var ex = new Exception();            var xs = Observable.StartAsync(() =>                Task.Factory.StartNew(() => { throw ex; })            );            ReactiveAssert.Throws(ex, () => xs.Single());        }        [TestMethod]        public void StartAsync_ActionWithCancel_Success()        {            var i = 0;            var xs = Observable.StartAsync(ct =>            {                i++;                return Task.Factory.StartNew(() => { }, CancellationToken.None); // Not forwarding ct because we always want this task to run to completion in this test.            });            Assert.Equal(Unit.Default, xs.Single());            Assert.Equal(1, i);            Assert.Equal(Unit.Default, xs.Single());            Assert.Equal(1, i);        }        [TestMethod]        public void StartAsync_ActionWithCancel_Throw_Synchronous()        {            var ex = new Exception();            var xs = Observable.StartAsync(ct =>            {                throw ex;            });            ReactiveAssert.Throws(ex, () => xs.Single());        }        [TestMethod]        public void StartAsync_ActionWithCancel_Throw_Asynchronous()        {            var ex = new Exception();            var xs = Observable.StartAsync(ct =>                Task.Factory.StartNew(() => { throw ex; }, CancellationToken.None) // Not forwarding ct because we always want this task to run and then fail in this test            );            ReactiveAssert.Throws(ex, () => xs.Single());        }        [TestMethod]        public void StartAsync_ActionWithCancel_Cancel()        {            var N = 10;            for (var n = 0; n < N; n++)            {                var e = new ManualResetEvent(false);                var f = new ManualResetEvent(false);                var t = default(Task);                var xs = Observable.StartAsync(ct =>                    t = Task.Factory.StartNew(() =>                    {                        try                        {                            e.Set();                            while (true)                            {                                ct.ThrowIfCancellationRequested();                            }                        }                        finally                        {                            f.Set();                        }                    },                    CancellationToken.None) // Not forwarding ct because we are testing the case where the task is already running by the time cancellation is detected                );                e.WaitOne();                var d = xs.Subscribe(_ => { });                d.Dispose();                f.WaitOne();                while (!t.IsCompleted)                {                    ;                }                ReactiveAssert.Throws<OperationCanceledException>(() => xs.Single());            }        }        [TestMethod]        public void Start_Action_UnsubscribeThenError_ErrorReportedAsUnobserved()        {            Start_Action_ErrorAfterUnsubscribeReportedAsUnobserved_Core(                createTask => Observable.StartAsync(createTask),                errorObservation =>                {                    errorObservation.AssertExceptionReportedAsUnobserved();                });        }        [TestMethod]        public void Start_ActionWithCancel_UnsubscribeThenError_ErrorReportedAsUnobserved()        {            Start_Action_ErrorAfterUnsubscribeReportedAsUnobserved_Core(                createTask => Observable.StartAsync(_ => createTask()),                errorObservation =>                {                    errorObservation.AssertExceptionReportedAsUnobserved();                });        }        [TestMethod]        public void Start_Action_WithScheduler_UnsubscribeThenError_ErrorReportedAsUnobserved()        {            Start_Action_ErrorAfterUnsubscribeReportedAsUnobserved_Core(                createTask => Observable.StartAsync(createTask, TaskPoolScheduler.Default),                errorObservation =>                {                    errorObservation.AssertExceptionReportedAsUnobserved();                });        }        [TestMethod]        public void Start_ActionWithCancel_WithScheduler_UnsubscribeThenError_ErrorReportedAsUnobserved()        {            Start_Action_ErrorAfterUnsubscribeReportedAsUnobserved_Core(                createTask => Observable.StartAsync(_ => createTask(), TaskPoolScheduler.Default),                errorObservation =>                {                    errorObservation.AssertExceptionReportedAsUnobserved();                });        }        [TestMethod]        public void Start_Action_IgnorePostUnsubscribeErrors_UnsubscribeThenError_ErrorNotReportedAsUnobserved()        {            Start_Action_ErrorAfterUnsubscribeReportedAsUnobserved_Core(                createTask => Observable.StartAsync(createTask, new TaskObservationOptions(null, ignoreExceptionsAfterUnsubscribe: true)),                errorObservation =>                {                    errorObservation.AssertExceptionNotReportedAsUnobserved();                });        }        [TestMethod]        public void Start_ActionWithCancel_IgnorePostUnsubscribeErrors_UnsubscribeThenError_ErrorNotReportedAsUnobserved()        {            Start_Action_ErrorAfterUnsubscribeReportedAsUnobserved_Core(                createTask => Observable.StartAsync(_ => createTask(), new TaskObservationOptions(null, ignoreExceptionsAfterUnsubscribe: true)),                errorObservation =>                {                    errorObservation.AssertExceptionNotReportedAsUnobserved();                });        }        [TestMethod]        public void Start_Action_WithScheduler_IgnorePostUnsubscribeErrors_UnsubscribeThenError_ErrorNotReportedAsUnobserved()        {            Start_Action_ErrorAfterUnsubscribeReportedAsUnobserved_Core(                createTask => Observable.StartAsync(createTask, new TaskObservationOptions(TaskPoolScheduler.Default, ignoreExceptionsAfterUnsubscribe: true)),                errorObservation =>                {                    errorObservation.AssertExceptionNotReportedAsUnobserved();                });        }        [TestMethod]        public void Start_ActionWithCancel_WithScheduler_IgnorePostUnsubscribeErrors_UnsubscribeThenError_ErrorNotReportedAsUnobserved()        {            Start_Action_ErrorAfterUnsubscribeReportedAsUnobserved_Core(                createTask => Observable.StartAsync(_ => createTask(), new TaskObservationOptions(TaskPoolScheduler.Default, ignoreExceptionsAfterUnsubscribe: true)),                errorObservation =>                {                    errorObservation.AssertExceptionNotReportedAsUnobserved();                });        }#if DESKTOPCLR        [TestMethod]        public void StartAsync_Action_Scheduler1()        {            var tcs = new TaskCompletionSource<int>();            var e = new ManualResetEvent(false);            var t = default(int);            var xs = Observable.StartAsync(() => (Task)tcs.Task, Scheduler.Immediate);            xs.Subscribe(res =>            {                t = Environment.CurrentManagedThreadId;                e.Set();            });            tcs.SetResult(42);            e.WaitOne();            Assert.Equal(Environment.CurrentManagedThreadId, t);        }        [TestMethod]        public void StartAsync_Action_Scheduler2()        {            var tcs = new TaskCompletionSource<int>();            var e = new ManualResetEvent(false);            var t = default(int);            var xs = Observable.StartAsync(ct => (Task)tcs.Task, Scheduler.Immediate);            xs.Subscribe(res =>            {                t = Environment.CurrentManagedThreadId;                e.Set();            });            tcs.SetResult(42);            e.WaitOne();            Assert.Equal(Environment.CurrentManagedThreadId, t);        }#endif        #endregion        private void Start_Func_ErrorAfterUnsubscribeReportedAsUnobserved_Core(            Func<Func<Task<int>>, IObservable<int>> createObservable,            Action<TaskErrorObservation> testResults)        {            Start_Func_ErrorAfterUnsubscribeReportedAsUnobserved_Core<int>(createObservable, testResults);        }        private void Start_Action_ErrorAfterUnsubscribeReportedAsUnobserved_Core(            Func<Func<Task>, IObservable<Unit>> createObservable,            Action<TaskErrorObservation> testResults)        {            Start_Func_ErrorAfterUnsubscribeReportedAsUnobserved_Core<Unit>(createObservable, testResults);        }        private void Start_Func_ErrorAfterUnsubscribeReportedAsUnobserved_Core<T>(            Func<Func<Task<T>>, IObservable<T>> createObservable,            Action<TaskErrorObservation> testResults)        {            using Barrier gate = new(2);            using TaskErrorObservation errorObservation = new();            var sub = errorObservation.SuscribeWithoutKeepingSourceReachable<T>(                (setTask, exception) => createObservable(                    () => setTask(Task.Factory.StartNew<T>(                        () =>                        {                            // 1: Notify that task execution has begun                            gate.SignalAndWait();                            // 2: Wait until unsubscribe Dispose has returned                            gate.SignalAndWait();                            throw exception;                        })))                    .Subscribe());            // 1: wait until task execution has begun            gate.SignalAndWait();            sub.Dispose();            //sub = null;            // 2: Notify that unsubscribe Dispose has returned            gate.SignalAndWait();            testResults(errorObservation);        }    }}
 |