Exemplo n.º 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);

            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);
        }
        public void Cancel時にRegisterで登録したデリゲートが呼ばれる()
        {
            var runner = new SampleTaskRunner.TaskRunner();

            {
                // キャンセルしない場合
                var cts = new CancellationTokenSource();
                var t = new Task<string>(c => Cancelで戻り値が切り替わるコルーチン(10, c, cts.Token));
                t.Start(runner);
                runner.Update(20);

                Assert.IsTrue(t.IsCompleted);
                Assert.AreEqual(CompletedMessage, t.Result);
            }

            {
                // キャンセルする場合
                var cts = new CancellationTokenSource();
                var t = new Task<string>(c => Cancelで戻り値が切り替わるコルーチン(10, c, cts.Token));
                t.Start(runner);
                runner.Update(5);
                cts.Cancel();
                runner.Update(5);

                Assert.IsTrue(t.IsCompleted);
                Assert.AreEqual(CanceledMessage, t.Result);
            }
        }
Exemplo n.º 3
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();
        }
Exemplo n.º 4
0
        public void Cancel時にRegisterで登録したデリゲートが呼ばれる()
        {
            var runner = new SampleTaskRunner.TaskRunner();

            {
                // キャンセルしない場合
                var cts = new CancellationTokenSource();
                var t   = new Task <string>(c => Cancelで戻り値が切り替わるコルーチン(10, c, cts.Token));
                t.Start(runner);
                runner.Update(20);

                Assert.IsTrue(t.IsCompleted);
                Assert.AreEqual(CompletedMessage, t.Result);
            }

            {
                // キャンセルする場合
                var cts = new CancellationTokenSource();
                var t   = new Task <string>(c => Cancelで戻り値が切り替わるコルーチン(10, c, cts.Token));
                t.Start(runner);
                runner.Update(5);
                cts.Cancel();
                runner.Update(5);

                Assert.IsTrue(t.IsCompleted);
                Assert.AreEqual(CanceledMessage, t.Result);
            }
        }
Exemplo n.º 5
0
        public void タスク中で例外が出たらErrorプロパティに例外が入る()
        {
            var task = new Task <int>(Coroutines.FErrorAsync)
                       .OnComplete(t =>
            {
                Assert.IsTrue(t.IsFaulted);
                Assert.IsNotNull(t.Error);
            })
                       .ContinueWith(呼ばれてはいけない);

            var runner = new SampleTaskRunner.TaskRunner();

            task.Start(runner);
            runner.Update(20);
        }
Exemplo n.º 6
0
        public void キャンセルトークンを渡しても_Cancelを呼ばなければ正常終了()
        {
            var x      = 10;
            var runner = new SampleTaskRunner.TaskRunner();

            var t = new Task <double>(c => Coroutines.F1Cancelable(x, 20, c, CancellationToken.None));

            t.Start(runner);
            while (!t.IsCompleted)
            {
                runner.Update();
            }

            Assert.AreEqual(Coroutines.F1(x), t.Result);
        }
Exemplo n.º 7
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);
        }
Exemplo n.º 8
0
        public static void Run()
        {
            var runner = new SampleTaskRunner.TaskRunner();

            Common.ShowFrameTask(50).Start(runner);

            new Task<string>(Worker1)
                .OnComplete(t => Console.WriteLine("Worker 1 Done: " + t.Result))
                .Start(runner);

            new Task<int>(Worker2)
                .OnComplete(t => Console.WriteLine("Worker 2 Done: " + t.Result))
                .Start(runner);

            runner.Update(200);
        }
Exemplo n.º 9
0
        public static void Run()
        {
            var runner = new SampleTaskRunner.TaskRunner();

            Common.ShowFrameTask(50).Start(runner);

            new Task <string>(Worker1)
            .OnComplete(t => Console.WriteLine("Worker 1 Done: " + t.Result))
            .Start(runner);

            new Task <int>(Worker2)
            .OnComplete(t => Console.WriteLine("Worker 2 Done: " + t.Result))
            .Start(runner);

            runner.Update(200);
        }
Exemplo n.º 10
0
        public void タスク中で例外が出たらOnErrorが呼ばれる_特定の型の例外だけ拾う()
        {
            var notSupportedCalled = false;
            var outOfRangeCalled   = false;

            var task = new Task(Coroutines.FErrorAsync)
                       .OnError <NotSupportedException>(e => notSupportedCalled  = true)
                       .OnError <IndexOutOfRangeException>(e => outOfRangeCalled = true);

            var runner = new SampleTaskRunner.TaskRunner();

            task.Start(runner);
            runner.Update(20);

            Assert.IsTrue(notSupportedCalled);
            Assert.IsFalse(outOfRangeCalled);
        }
Exemplo n.º 11
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);
        }
        public void TaskにCancellationTokenSourceを渡しておいて_TaskのCancelメソッド経由でキャンセルできる()
        {
            var x = 10;
            var runner = new SampleTaskRunner.TaskRunner();

            var cts = new CancellationTokenSource();
            var t = new Task<double>(c => Coroutines.F1Cancelable(x, 20, c, cts.Token));

            t.Cancellation = cts;

            t.Start(runner);
            runner.Update(5);
            t.Cancel(); // Task.Cancel の中で1度 MoveNext して、即座にキャンセル処理が動くようにする

            // 挙動自体は cts.Cancel(); と同じ
            Assert.IsTrue(t.IsFaulted);
            Assert.AreEqual(typeof(TaskCanceledException), t.Error.Exceptions.Single().GetType());
        }
Exemplo n.º 13
0
        public void ContinueWithは前段が正常終了したときにだけ呼ばれる()
        {
            var onCompletedCalled = false;

            var task = new Task(Coroutines.FErrorAsync)
                .OnComplete(t =>
                    {
                        Assert.IsTrue(t.IsFaulted);
                        onCompletedCalled = true;
                    })
                .ContinueWith(呼ばれてはいけない);

            var runner = new SampleTaskRunner.TaskRunner();
            task.Start(runner);
            runner.Update(20);

            Assert.IsTrue(onCompletedCalled);
        }
Exemplo n.º 14
0
        public void Nフレーム実行するイテレーターが丁度NフレームIsCompleted_falseになってることを確認()
        {
            const int N = 50;

            var runnner = new SampleTaskRunner.TaskRunner();
            var task = Coroutines.NFrameTask(N);
            task.Start(runnner);

            for (int i = 0; i < 2 * N; i++)
            {
                runnner.Update();

                if (i < N)
                    Assert.IsFalse(task.IsCompleted);
                else
                    Assert.IsTrue(task.IsCompleted);
            }
        }
Exemplo n.º 15
0
        public void TaskにCancellationTokenSourceを渡しておいて_TaskのCancelメソッド経由でキャンセルできる()
        {
            var x      = 10;
            var runner = new SampleTaskRunner.TaskRunner();

            var cts = new CancellationTokenSource();
            var t   = new Task <double>(c => Coroutines.F1Cancelable(x, 20, c, cts.Token));

            t.Cancellation = cts;

            t.Start(runner);
            runner.Update(5);
            t.Cancel();             // Task.Cancel の中で1度 MoveNext して、即座にキャンセル処理が動くようにする

            // 挙動自体は cts.Cancel(); と同じ
            Assert.IsTrue(t.IsFaulted);
            Assert.AreEqual(typeof(TaskCanceledException), t.Error.Exceptions.Single().GetType());
        }
Exemplo n.º 16
0
        public void ContinueWithは前段が正常終了したときにだけ呼ばれる()
        {
            var onCompletedCalled = false;

            var task = new Task(Coroutines.FErrorAsync)
                       .OnComplete(t =>
            {
                Assert.IsTrue(t.IsFaulted);
                onCompletedCalled = true;
            })
                       .ContinueWith(呼ばれてはいけない);

            var runner = new SampleTaskRunner.TaskRunner();

            task.Start(runner);
            runner.Update(20);

            Assert.IsTrue(onCompletedCalled);
        }
Exemplo n.º 17
0
        public void OnCompleteは_直前のタスク完了時_エラーも正常終了も_どちらも呼ばれる()
        {
            var errorTaskCalled  = false;
            var normalTaskCalled = false;

            var normalTask = new Task(() => Coroutines.NFrame(5))
                             .OnComplete(t => normalTaskCalled = true);
            var errorTask = new Task <int>(Coroutines.FErrorAsync)
                            .OnComplete(t => errorTaskCalled = true);

            var runner = new SampleTaskRunner.TaskRunner();

            errorTask.Start(runner);
            normalTask.Start(runner);
            runner.Update(20);

            Assert.IsTrue(normalTaskCalled);
            Assert.IsTrue(errorTaskCalled);
        }
Exemplo n.º 18
0
        public void キャンセルしたときにOperationCanceld例外発生()
        {
            var x      = 10;
            var runner = new SampleTaskRunner.TaskRunner();

            var cts = new CancellationTokenSource();
            var t   = new Task <double>(c => Coroutines.F1Cancelable(x, 20, c, cts.Token));

            t.Start(runner);
            runner.Update(5);
            cts.Cancel();

            // 次の1回の実行でタスクが終わるはず
            runner.Update();

            // この場合は IsCanceled にならない
            Assert.IsTrue(t.IsFaulted);
            Assert.AreEqual(typeof(TaskCanceledException), t.Error.Exceptions.Single().GetType());
        }
Exemplo n.º 19
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);
        }
Exemplo n.º 20
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);
        }
        public static void Run()
        {
            var x = 1.41421356;

            // 同期処理
            var result = F3(F2(F1(x)));
            Console.WriteLine("同期処理の結果: " + result);

            // イテレーター非同期処理
            var task = new Task<double>(c => F1Async(x, c))
                .ContinueWith<string>(F2Async)
                .ContinueWith<int>(F3Async)
                .OnComplete(t => Console.WriteLine("非同期処理の結果: " + t.Result));

            var runner = new SampleTaskRunner.TaskRunner();

            task.Start(runner);
            Common.ShowFrameTask(50).Start(runner);

            runner.Update(20);
        }
Exemplo n.º 22
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);
        }
        public void TaskのForceCancelで強制的にタスクを止めたときはOnCompleteも呼ばれない()
        {
            var x = 10;
            var runner = new SampleTaskRunner.TaskRunner();

            var cts = new CancellationTokenSource();
            var t = new Task<double>(c => Coroutines.F1Cancelable(x, 20, c, cts.Token));

            t.OnComplete(_ =>
            {
                Assert.Fail();
            });

            t.Start(runner);
            runner.Update(5);
            t.ForceCancel();

            runner.Update();

            // この場合は IsCanceled に
            Assert.IsTrue(t.IsCanceled);
        }
Exemplo n.º 24
0
        public void WhenAllでタスクの並行動作できる()
        {
            var t1 = new Task(() => Coroutines.NFrame(3));
            var t2 = new Task(() => Coroutines.NFrame(5));
            var t3 = new Task(() => Coroutines.NFrame(7));

            var task = Task.WhenAllTask(t1, t2, t3)
                       .OnComplete(t =>
            {
                Assert.IsTrue(t1.IsCompleted);
                Assert.IsTrue(t2.IsCompleted);
                Assert.IsTrue(t3.IsCompleted);
            });

            var runner = new SampleTaskRunner.TaskRunner();

            task.Start(runner);

            runner.Update(20);

            Assert.IsTrue(task.IsCompleted);
        }
Exemplo n.º 25
0
        public static void Run()
        {
            var x = 1.41421356;

            // 同期処理
            var result = F3(F2(F1(x)));

            Console.WriteLine("同期処理の結果: " + result);

            // イテレーター非同期処理
            var task = new Task <double>(c => F1Async(x, c))
                       .ContinueWith <string>(F2Async)
                       .ContinueWith <int>(F3Async)
                       .OnComplete(t => Console.WriteLine("非同期処理の結果: " + t.Result));

            var runner = new SampleTaskRunner.TaskRunner();

            task.Start(runner);
            Common.ShowFrameTask(50).Start(runner);

            runner.Update(20);
        }
Exemplo n.º 26
0
        public void TaskのForceCancelで強制的にタスクを止めたときはOnCompleteも呼ばれない()
        {
            var x      = 10;
            var runner = new SampleTaskRunner.TaskRunner();

            var cts = new CancellationTokenSource();
            var t   = new Task <double>(c => Coroutines.F1Cancelable(x, 20, c, cts.Token));

            t.OnComplete(_ =>
            {
                Assert.Fail();
            });

            t.Start(runner);
            runner.Update(5);
            t.ForceCancel();

            runner.Update();

            // この場合は IsCanceled に
            Assert.IsTrue(t.IsCanceled);
        }
Exemplo n.º 27
0
        public void Nフレーム実行するイテレーターが丁度NフレームIsCompleted_falseになってることを確認()
        {
            const int N = 50;

            var runnner = new SampleTaskRunner.TaskRunner();
            var task    = Coroutines.NFrameTask(N);

            task.Start(runnner);

            for (int i = 0; i < 2 * N; i++)
            {
                runnner.Update();

                if (i < N)
                {
                    Assert.IsFalse(task.IsCompleted);
                }
                else
                {
                    Assert.IsTrue(task.IsCompleted);
                }
            }
        }
Exemplo n.º 28
0
        public void Progressでは_Reportが呼ばれるたびにProgressChangedイベントが起こる()
        {
            var progress = new Progress<int>();
            var reportedItems = new List<int>();

            progress.ProgressChanged += i =>
            {
                reportedItems.Add(i);
            };

            var t = new Task<int>(c => 進捗報告付きのコルーチン(c, progress));

            var runner = new SampleTaskRunner.TaskRunner();
            t.Start(runner);

            for (int i = 0; i < 100; i++)
            {
                Assert.AreEqual(i, reportedItems.Count);
                runner.Update();
                Assert.AreEqual(i + 1, reportedItems.Count);
                Assert.AreEqual(i, reportedItems.Last());
            }
        }
Exemplo n.º 29
0
        public void タスク中で例外が出たときにResultをとろうとすると例外再スロー()
        {
            var task = new Task <int>(Coroutines.FErrorAsync)
                       .OnComplete(t =>
            {
                Assert.IsTrue(t.IsFaulted);

                try
                {
                    var result = t.Result;
                }
                catch
                {
                    return;
                }
                Assert.Fail();
            })
                       .ContinueWith(呼ばれてはいけない);

            var runner = new SampleTaskRunner.TaskRunner();

            task.Start(runner);
            runner.Update(20);
        }
Exemplo n.º 30
0
        public void Progressでは_Reportが呼ばれるたびにProgressChangedイベントが起こる()
        {
            var progress      = new Progress <int>();
            var reportedItems = new List <int>();

            progress.ProgressChanged += i =>
            {
                reportedItems.Add(i);
            };

            var t = new Task <int>(c => 進捗報告付きのコルーチン(c, progress));

            var runner = new SampleTaskRunner.TaskRunner();

            t.Start(runner);

            for (int i = 0; i < 100; i++)
            {
                Assert.AreEqual(i, reportedItems.Count);
                runner.Update();
                Assert.AreEqual(i + 1, reportedItems.Count);
                Assert.AreEqual(i, reportedItems.Last());
            }
        }
Exemplo n.º 31
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);
        }
Exemplo n.º 32
0
        public void OnCompleteは_直前のタスク完了時_エラーも正常終了も_どちらも呼ばれる()
        {
            var errorTaskCalled = false;
            var normalTaskCalled = false;

            var normalTask = new Task(() => Coroutines.NFrame(5))
                .OnComplete(t => normalTaskCalled = true);
            var errorTask = new Task<int>(Coroutines.FErrorAsync)
                .OnComplete(t => errorTaskCalled = true);

            var runner = new SampleTaskRunner.TaskRunner();
            errorTask.Start(runner);
            normalTask.Start(runner);
            runner.Update(20);

            Assert.IsTrue(normalTaskCalled);
            Assert.IsTrue(errorTaskCalled);
        }
Exemplo n.º 33
0
        public void タスク中で例外が出たらErrorプロパティに例外が入る()
        {
            var task = new Task<int>(Coroutines.FErrorAsync)
                .OnComplete(t =>
                {
                    Assert.IsTrue(t.IsFaulted);
                    Assert.IsNotNull(t.Error);
                })
                .ContinueWith(呼ばれてはいけない);

            var runner = new SampleTaskRunner.TaskRunner();
            task.Start(runner);
            runner.Update(20);
        }
Exemplo n.º 34
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);
        }
Exemplo n.º 35
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();
        }
Exemplo n.º 36
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);
        }
Exemplo n.º 37
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);
        }
Exemplo n.º 38
0
        public void タスク中で例外が出たらOnErrorが呼ばれる_特定の型の例外だけ拾う()
        {
            var notSupportedCalled = false;
            var outOfRangeCalled = false;

            var task = new Task(Coroutines.FErrorAsync)
                .OnError<NotSupportedException>(e => notSupportedCalled = true)
                .OnError<IndexOutOfRangeException>(e => outOfRangeCalled = true);

            var runner = new SampleTaskRunner.TaskRunner();
            task.Start(runner);
            runner.Update(20);

            Assert.IsTrue(notSupportedCalled);
            Assert.IsFalse(outOfRangeCalled);
        }
Exemplo n.º 39
0
        public void WhenAllでタスクの並行動作できる()
        {
            var t1 = new Task(() => Coroutines.NFrame(3));
            var t2 = new Task(() => Coroutines.NFrame(5));
            var t3 = new Task(() => Coroutines.NFrame(7));

            var task = Task.WhenAllTask(t1, t2, t3)
                .OnComplete(t =>
                {
                    Assert.IsTrue(t1.IsCompleted);
                    Assert.IsTrue(t2.IsCompleted);
                    Assert.IsTrue(t3.IsCompleted);
                });

            var runner = new SampleTaskRunner.TaskRunner();
            task.Start(runner);

            runner.Update(20);

            Assert.IsTrue(task.IsCompleted);
        }
        public void キャンセルしたときにOperationCanceld例外発生()
        {
            var x = 10;
            var runner = new SampleTaskRunner.TaskRunner();

            var cts = new CancellationTokenSource();
            var t = new Task<double>(c => Coroutines.F1Cancelable(x, 20, c, cts.Token));

            t.Start(runner);
            runner.Update(5);
            cts.Cancel();

            // 次の1回の実行でタスクが終わるはず
            runner.Update();

            // この場合は IsCanceled にならない
            Assert.IsTrue(t.IsFaulted);
            Assert.AreEqual(typeof(TaskCanceledException), t.Error.Exceptions.Single().GetType());
        }
        public void キャンセルトークンを渡しても_Cancelを呼ばなければ正常終了()
        {
            var x = 10;
            var runner = new SampleTaskRunner.TaskRunner();

            var t = new Task<double>(c => Coroutines.F1Cancelable(x, 20, c, CancellationToken.None));

            t.Start(runner);
            while (!t.IsCompleted)
                runner.Update();

            Assert.AreEqual(Coroutines.F1(x), t.Result);
        }
Exemplo n.º 42
0
        public void タスク中で例外が出たときにResultをとろうとすると例外再スロー()
        {
            var task = new Task<int>(Coroutines.FErrorAsync)
                .OnComplete(t =>
                {
                    Assert.IsTrue(t.IsFaulted);

                    try
                    {
                        var result = t.Result;
                    }
                    catch
                    {
                        return;
                    }
                    Assert.Fail();
                })
                .ContinueWith(呼ばれてはいけない);

            var runner = new SampleTaskRunner.TaskRunner();
            task.Start(runner);
            runner.Update(20);
        }