コード例 #1
0
        public void Cancel()
        {
            var runner = new CoroutineRunner();

            var delay = runner.Create(() => AwaitableCoroutine.DelayCount(5));

            var co = runner.Create(async() =>
            {
                await delay;
            });

            Assert.False(delay.IsCompleted);
            Assert.False(delay.IsCanceled);

            Assert.False(co.IsCompleted);
            Assert.False(co.IsCanceled);

            runner.Update();

            Assert.False(delay.IsCompleted);
            Assert.False(delay.IsCanceled);

            Assert.False(co.IsCompleted);
            Assert.False(co.IsCanceled);

            delay.Cancel();

            Assert.False(delay.IsCompleted);
            Assert.True(delay.IsCanceled);

            Assert.False(co.IsCompleted);
            Assert.True(co.IsCanceled);
        }
コード例 #2
0
        public void AwaitDelayCountTest()
        {
            var runner  = new CoroutineRunner();
            var counter = new Counter();

            var co = runner.Create(async() =>
            {
                counter.Inc();
                await AwaitableCoroutine.DelayCount(5);
                counter.Inc();
            });

            Assert.False(co.IsCompleted);
            Assert.Equal(0, counter.Count);
            Assert.False(co.IsCompleted);

            runner.Update();
            Assert.Equal(1, counter.Count);
            Assert.False(co.IsCompleted);

            for (var i = 0; i < 5; i++)
            {
                runner.Update();
                Assert.False(co.IsCompleted);
                Assert.Equal(1, counter.Count);
                Assert.False(co.IsCompleted);
            }

            runner.Update();
            Assert.Equal(2, counter.Count);
            Assert.True(co.IsCompleted);
        }
コード例 #3
0
        public void AwaitCoroutineWithAnotherRunner()
        {
            var runner1 = new CoroutineRunner();
            var runner2 = new CoroutineRunner();

            var coroutine1 = runner1.Create(() => AwaitableCoroutine.DelayCount(0));

            var coroutine2 = runner2.Create(() =>
                                            coroutine1.AndThen(() => AwaitableCoroutine.DelayCount(0))
                                            );

            Assert.False(coroutine1.IsCompleted);
            Assert.False(coroutine2.IsCompleted);

            runner1.Update();
            Assert.True(coroutine1.IsCompleted);
            Assert.False(coroutine2.IsCompleted);

            for (var i = 0; i < 5; i++)
            {
                runner1.Update();
                Assert.False(coroutine2.IsCompleted);
            }

            runner2.Update();
            runner2.Update();
            runner2.Update();
            Assert.True(coroutine2.IsCompleted);
        }
コード例 #4
0
        public void WaitAllWithValuesTest()
        {
            var runner = new CoroutineRunner();

            var waitAll = runner.Create(() =>
            {
                var coroutines = new AwaitableCoroutine <int> [4];
                for (var i = 0; i < 4; i++)
                {
                    coroutines[i] = AwaitableCoroutine.DelayCount(0).SelectTo(i);
                }
                return(AwaitableCoroutine.WaitAll <int>(coroutines));
            });

            Assert.False(waitAll.IsCompleted);

            runner.Update();

            Assert.True(waitAll.IsCompleted);

            var res = waitAll.Result;

            Assert.NotNull(res);

            for (var i = 0; i < 4; i++)
            {
                Assert.Equal(i, res[i]);
            }
        }
コード例 #5
0
        private async AwaitableCoroutine CreateAwaitDelayCount(Counter counter)
        {
            counter.Inc();
            await AwaitableCoroutine.DelayCount(5);

            counter.Inc();
            await AwaitableCoroutine.DelayCount(5);

            counter.Inc();
        }
コード例 #6
0
        public void WaitAll2Test()
        {
            var runner = new CoroutineRunner();

            var waitAll = runner.Create(() =>
                                        AwaitableCoroutine.WaitAll(
                                            AwaitableCoroutine.DelayCount(0),
                                            AwaitableCoroutine.DelayCount(0)
                                            )
                                        );

            Assert.False(waitAll.IsCompleted);

            // completes children
            runner.Update();

            Assert.True(waitAll.IsCompleted);
        }
コード例 #7
0
        public void WaitAllOfDelay0Test()
        {
            var runner = new CoroutineRunner();

            var waitAll = runner.Create(() =>
                                        AwaitableCoroutine.WaitAll(new AwaitableCoroutineBase[] {
                AwaitableCoroutine.DelayCount(0),
                AwaitableCoroutine.DelayCount(0),
                AwaitableCoroutine.DelayCount(0),
                AwaitableCoroutine.DelayCount(0),
            })
                                        );

            Assert.False(waitAll.IsCompleted);

            runner.Update();
            Assert.True(waitAll.IsCompleted);
        }
コード例 #8
0
        public void RunDelayCount()
        {
            var runner = new CoroutineRunner();

            var count = 5;

            bool flag = false;

            var coroutine = runner.Create(() => AwaitableCoroutine.DelayCount(count).OnCompleted(() => flag = true));

            for (var i = 0; i < count; i++)
            {
                runner.Update();
                Assert.False(coroutine.IsCompleted);
            }

            runner.Update();
            Assert.True(coroutine.IsCompleted);
            Assert.True(flag);
        }
コード例 #9
0
        public void WaitAllOfDelayTest()
        {
            var runner = new CoroutineRunner();

            var waitAll = runner.Create(() =>
                                        AwaitableCoroutine.WaitAll(new AwaitableCoroutineBase[] {
                AwaitableCoroutine.DelayCount(1),
                AwaitableCoroutine.DelayCount(2),
                AwaitableCoroutine.DelayCount(3),
                AwaitableCoroutine.DelayCount(4),
            })
                                        );

            Assert.False(waitAll.IsCompleted);

            for (var i = 0; i < 4; i++)
            {
                runner.Update();
            }
            runner.Update();
            Assert.True(waitAll.IsCompleted);
        }
コード例 #10
0
        public void RunAndThen()
        {
            var runner  = new CoroutineRunner();
            var counter = new Counter();
            var c       = runner.Create(() =>
                                        AwaitableCoroutine.DelayCount(2)
                                        .AndThen(() => AwaitableCoroutine.DelayCount(2).OnCompleted(() =>
            {
                counter.Inc();
                Log($"Count: {counter.Count}");
            }))
                                        );

            while (!c.IsCompleted)
            {
                runner.Update();
            }

            Assert.True(c.IsCompleted);

            Assert.Equal(1, counter.Count);
        }
コード例 #11
0
        public void RunUntilCompleted()
        {
            var runner1 = new CoroutineRunner();
            var runner2 = new CoroutineRunner();

            var counter = new Counter();

            var coroutine1 = runner1.Create(() => AwaitableCoroutine.DelayCount(3));
            var coroutine2 = runner2.Create(() => coroutine1.UntilCompleted(counter.Inc));

            Assert.False(coroutine1.IsCompleted);
            Assert.False(coroutine2.IsCompleted);

            runner2.Update();
            Assert.Equal(1, counter.Count);

            runner2.Update();
            Assert.Equal(2, counter.Count);

            for (var i = 0; i < 3; i++)
            {
                runner1.Update();
                Assert.Equal(2, counter.Count);
                Assert.False(coroutine1.IsCompleted);
                Assert.False(coroutine2.IsCompleted);
            }

            runner2.Update();
            Assert.Equal(3, counter.Count);

            runner1.Update();
            Assert.True(coroutine1.IsCompleted);
            Assert.False(coroutine2.IsCompleted);

            runner2.Update();
            Assert.True(coroutine2.IsCompleted);
            Assert.Equal(3, counter.Count);
        }
コード例 #12
0
        public void CancelWithCanceledException()
        {
            var runner = new CoroutineRunner();

            var delay = runner.Create(() => AwaitableCoroutine.DelayCount(5));

            var co = runner.Context <AwaitableCoroutine>(() => AwaitableCoroutine.WaitAll(delay, delay));

            Assert.False(delay.IsCompleted);
            Assert.False(delay.IsCanceled);

            Assert.False(co.IsCompleted);
            Assert.False(co.IsCanceled);

            runner.Update();

            Assert.False(delay.IsCompleted);
            Assert.False(delay.IsCanceled);

            Assert.False(co.IsCompleted);
            Assert.False(co.IsCanceled);

            delay.Cancel();

            Assert.False(delay.IsCompleted);
            Assert.True(delay.IsCanceled);

            Assert.False(co.IsCompleted);
            Assert.False(co.IsCanceled);

            runner.Update();

            Assert.False(co.IsCompleted);
            Assert.True(co.IsCanceled);
            Assert.True(co.Exception is ChildCanceledException <AwaitableCoroutineBase>);
        }
コード例 #13
0
        public void RunSelect()
        {
            var runner = new CoroutineRunner();

            var(c1, c2, c3) = runner.Context(() =>
            {
                var c1 = AwaitableCoroutine.DelayCount(0);
                var c2 = c1.SelectTo(2);
                var c3 = c2.Select(x => x * x);
                return(c1, c2, c3);
            });

            Assert.False(c1.IsCompleted);
            Assert.False(c2.IsCompleted);
            Assert.False(c3.IsCompleted);

            runner.Update();
            Assert.True(c1.IsCompleted);
            Assert.True(c2.IsCompleted);
            Assert.True(c3.IsCompleted);

            Assert.Equal(2, c2.Result);
            Assert.Equal(4, c3.Result);
        }