Beispiel #1
0
        public void 実行途中のタスクを再スタートしようとしたら例外を出す()
        {
            var x    = 10.0;
            var task = new Task <double>(c => Coroutines.F1Async(x, c));

            Assert.AreEqual(TaskStatus.Created, task.Status);

            var runnner = new SampleTaskRunner.TaskRunner();

            task.Start(runnner);

            runnner.Update();
            Assert.AreEqual(TaskStatus.Running, task.Status);

            try
            {
                task.Start(runnner);
            }
            catch (InvalidOperationException)
            {
                return;
            }

            Assert.Fail();
        }
Beispiel #2
0
        public void 開始前_実行中_正常終了_エラー終了_キャンセルされた_がわかる()
        {
            var x    = 10.0;
            var task = new Task <double>(c => Coroutines.F1Async(x, c));

            Assert.AreEqual(TaskStatus.Created, task.Status);

            var runnner = new SampleTaskRunner.TaskRunner();

            task.Start(runnner);

            runnner.Update();
            Assert.AreEqual(TaskStatus.Running, task.Status);

            runnner.Update(10);
            Assert.AreEqual(TaskStatus.RanToCompletion, task.Status);

            var errorTask = new Task(Coroutines.FErrorAsync);

            Assert.AreEqual(TaskStatus.Created, errorTask.Status);

            errorTask.Start(runnner);
            runnner.Update();
            Assert.AreEqual(TaskStatus.Running, errorTask.Status);

            runnner.Update(10);
            Assert.AreEqual(TaskStatus.Faulted, errorTask.Status);
        }
Beispiel #3
0
        public void Task_Tで正常終了するとResultに結果が入る()
        {
            var x = 10;
            var y = Coroutines.F1(x);

            var task = Task.Run <double>(c => Coroutines.F1Async(x, c))
                       .OnComplete(t => Assert.AreEqual(t.Result, y));

            var scheduler = Task.DefaultScheduler;

            scheduler.Update(10);

            Assert.AreEqual(y, task.Result);
        }
Beispiel #4
0
        public void Task_Tで正常終了するとResultに結果が入る()
        {
            var x = 10;
            var y = Coroutines.F1(x);

            var task = new Task <double>(c => Coroutines.F1Async(x, c))
                       .OnComplete(t => Assert.AreEqual(t.Result, y));

            var runnner = new SampleTaskRunner.TaskRunner();

            task.Start(runnner);
            runnner.Update(10);

            Assert.AreEqual(y, task.Result);
        }
Beispiel #5
0
        public void 一度完了したタスク_何度でも結果が取れる()
        {
            var x = 10;
            var y = Coroutines.F1(x);

            var task = Task.Run <double>(c => Coroutines.F1Async(x, c));

            var scheduler = Task.DefaultScheduler;

            scheduler.Update(10);

            Assert.AreEqual(y, task.Result);
            Assert.AreEqual(y, task.Result);
            Assert.AreEqual(y, task.Result);
            Assert.AreEqual(y, task.Result);
        }
Beispiel #6
0
        public void 一度完了したタスク_何度でも結果が取れる()
        {
            var x = 10;
            var y = Coroutines.F1(x);

            var task = new Task <double>(c => Coroutines.F1Async(x, c));

            var runnner = new SampleTaskRunner.TaskRunner();

            task.Start(runnner);
            runnner.Update(10);

            Assert.AreEqual(y, task.Result);
            Assert.AreEqual(y, task.Result);
            Assert.AreEqual(y, task.Result);
            Assert.AreEqual(y, task.Result);
        }
Beispiel #7
0
        public void タスク完了時にContinueWithが呼ばれる()
        {
            var x = 10;
            var y = Coroutines.F1(x);

            bool called = false;

            var task = Task.Run <double>(c => Coroutines.F1Async(x, c));

            task.ContinueWith(t => called = true);

            Assert.IsFalse(called);

            var scheduler = Task.DefaultScheduler;

            scheduler.Update(10);

            Assert.IsTrue(called);
        }
Beispiel #8
0
        public void ContinueWithで継続処理を実行できる()
        {
            var x  = 10.0;
            var x1 = Coroutines.F1(x);
            var x2 = Coroutines.F2(x1);
            var x3 = Coroutines.F3(x2);

            var task = Task.Run <double>(c => Coroutines.F1Async(x, c))
                       .OnComplete(t => Assert.AreEqual(t.Result, x1))
                       .ContinueWithIterator <string>((t, callback) => Coroutines.F2Async(t.Result, callback))
                       .OnComplete(t => Assert.AreEqual(t.Result, x2))
                       .ContinueWithIterator <int>((t, callback) => Coroutines.F3Async(t.Result, callback))
                       .OnComplete(t => Assert.AreEqual(t.Result, x2))
            ;

            var scheduler = Task.DefaultScheduler;

            scheduler.Update(30);
        }
Beispiel #9
0
        public void 完了済みのタスクでContinueWithすると_次のUpdateでコールバックが呼ばれる()
        {
            var x = 10;
            var y = Coroutines.F1(x);

            var task = Task.Run <double>(c => Coroutines.F1Async(x, c));

            var scheduler = Task.DefaultScheduler;

            scheduler.Update(10);

            Assert.IsTrue(task.IsCompleted);

            bool called = false;

            task.ContinueWith(t => called = true);
            scheduler.Update();

            Assert.IsTrue(called);
        }
Beispiel #10
0
        public void 完了済みのタスクでOnCompleteすると_即座にコールバックが呼ばれる()
        {
            var runnner = new SampleTaskRunner.TaskRunner();

            var x = 10;
            var y = Coroutines.F1(x);

            var task = new Task <double>(c => Coroutines.F1Async(x, c));

            task.Start(runnner);
            runnner.Update(10);

            Assert.IsTrue(task.IsCompleted);

            bool called = false;

            task.OnComplete(t => called = true);

            Assert.IsTrue(called);
        }
Beispiel #11
0
        public void 開始前_実行中_正常終了_エラー終了_がわかる()
        {
            var scheduler = Task.DefaultScheduler;

            var x    = 10.0;
            var task = Task.Run <double>(c => Coroutines.F1Async(x, c));

            Assert.AreEqual(TaskStatus.Running, task.Status);

            scheduler.Update(10);

            Assert.AreEqual(TaskStatus.RanToCompletion, task.Status);

            var errorTask = Task.Run(Coroutines.FErrorAsync);

            Assert.AreEqual(TaskStatus.Running, errorTask.Status);

            scheduler.Update(10);

            Assert.AreEqual(TaskStatus.Faulted, errorTask.Status);
        }
Beispiel #12
0
        public void ContinueWithで継続処理を実行できる()
        {
            var x  = 10.0;
            var x1 = Coroutines.F1(x);
            var x2 = Coroutines.F2(x1);
            var x3 = Coroutines.F3(x2);

            var task = new Task <double>(c => Coroutines.F1Async(x, c))
                       .OnComplete(t => Assert.AreEqual(t.Result, x1))
                       .ContinueWith <string>(Coroutines.F2Async)
                       .OnComplete(t => Assert.AreEqual(t.Result, x2))
                       .ContinueWith <int>(Coroutines.F3Async)
                       .OnComplete(t => Assert.AreEqual(t.Result, x2))
            ;

            var runner = new SampleTaskRunner.TaskRunner();

            task.Start(runner);

            runner.Update(20);
        }
Beispiel #13
0
        public void 実行途中のタスクを再スタートしようとしたら例外を出す()
        {
            var x    = 10.0;
            var task = Task.Run <double>(c => Coroutines.F1Async(x, c));

            var scheduler = Task.DefaultScheduler;

            scheduler.Update();

            Assert.AreEqual(TaskStatus.Running, task.Status);

            try
            {
                task.Start();
            }
            catch (InvalidOperationException)
            {
                return;
            }

            Assert.Fail();
        }
Beispiel #14
0
        public void new_Taskでコールドスタート_Task_Runでホットスタート()
        {
            var x = 10;

            var t1 = new Task <double>(c => Coroutines.F1Async(x, c));

            Assert.AreEqual(TaskStatus.Created, t1.Status);

            t1.Start();

            Assert.AreEqual(TaskStatus.Running, t1.Status);

            var t2 = Task.Run <double>(c => Coroutines.F1Async(x, c));

            Assert.AreEqual(TaskStatus.Running, t2.Status);

            var scheduler = Task.DefaultScheduler;

            scheduler.Update(10);

            Assert.AreEqual(TaskStatus.RanToCompletion, t1.Status);
            Assert.AreEqual(TaskStatus.RanToCompletion, t2.Status);
        }
Beispiel #15
0
        public void タスク中で例外が発生した時_UnhandledExceptionイベントが発生()
        {
            var scheduler = Task.DefaultScheduler;

            bool flag;

            scheduler.UnhandledException += _ => flag = true;

            // 例外が発生したタスクをほっておくと、UnhandledException イベントが発生。
            flag = false;
            var t1 = Task.Run(Coroutines.FErrorAsync);

            scheduler.Update(50);
            Assert.IsTrue(flag);

            // ContinueWith とかでの例がいも同様の扱い。
            flag = false;
            var t2 = Task.Run <double>(c => Coroutines.F1Async(10, c))
                     .ContinueWith(_ => { throw new Exception(); });

            scheduler.Update(50);
            Assert.IsTrue(flag);

            flag = false;
            var t3 = Task.Run <double>(c => Coroutines.F1Async(10, c))
                     .ContinueWithTask(_ => Task.Run(Coroutines.FErrorAsync));

            scheduler.Update(50);
            Assert.IsTrue(flag);

            // ContinueWith とかで Error の IsHandled を立てると UnhandledException は発生しない。
            flag = false;
            var t4 = Task.Run(Coroutines.FErrorAsync)
                     .ContinueWith(t => { var e = t.Exception; e.IsHandled = true; });

            Assert.IsFalse(flag);
        }
Beispiel #16
0
        public void タスクが正常終了した時だけThenが呼ばれる()
        {
            var value = 10.0;
            var t1    = Task.Run <double>(c => Coroutines.F1Async(value, c));

            double result1 = 0;
            int    result2 = 0;

            t1.Then(x => result1 = x); // 呼ばれる

            var t2 = Task.Run <int>(Coroutines.FErrorAsync);

            t2.Then(x => result2 = -1); // 呼ばれない

            Assert.AreEqual(0.0, result1);
            Assert.AreEqual(0, result2);

            var scheduler = Task.DefaultScheduler;

            scheduler.Update(20);

            Assert.AreEqual(Coroutines.F1(value), result1);
            Assert.AreEqual(0, result2);
        }