示例#1
0
        public void ImmediateException()
        {
            var scheduler = new SingleThreadedTaskScheduler();
            Mock<IDisposable> mockDisposable = new Mock<IDisposable>();
            var exception = new Exception("oops");

            Task task = null;
            Exception caughtException = null;

            scheduler.Start();
            try
            {
                var block = ImmediateExceptionBlock(exception, mockDisposable.Object);

                task = block.AsCoroutine<string>(scheduler);
            }
            catch (Exception e)
            {
                caughtException = e;
            }
            finally
            {
                scheduler.Dispose();
            }

            mockDisposable.Verify(d => d.Dispose(), Times.Once(), "Disposable not disposed.");
            Assert.AreEqual(exception, caughtException, "Exceptions differ.");
        }
示例#2
0
        public void DeferredException()
        {
            var scheduler = new SingleThreadedTaskScheduler();
            Mock<IDisposable> mockDisposable = new Mock<IDisposable>();
            var exception = new Exception("oops");

            Task task = null;
            Exception caughtException = null;

            scheduler.Start();
            try
            {
                task = DeferredExceptionBlock(exception, mockDisposable.Object).CreateCoroutine<string>(scheduler);
            }
            catch (Exception e)
            {
                caughtException = e;
            }
            finally
            {
                scheduler.Dispose();
            }

            mockDisposable.Verify(d => d.Dispose(), Times.Once(), "Disposable not disposed.");
            Assert.IsNull(caughtException, "Coroutine constructor threw up.");
            Assert.IsNotNull(task.Exception, "Coroutine didn't have exception.");
            Assert.AreEqual(exception, task.Exception.InnerException, "Exceptions differ.");
        }
示例#3
0
        public void PropagatesExceptionFromNestedCoroutine()
        {
            var scheduler = new SingleThreadedTaskScheduler();
            Exception e = new Exception("Boo.");

            scheduler.Start();
            var task = PropagatesExceptionFromNestedCoroutineBlock(e).AsCoroutine<int>();
            Thread.SpinWait(10000);
            scheduler.Dispose();

            Assert.IsTrue(task.IsCompleted);
            Assert.IsTrue(task.IsFaulted);
            Assert.AreEqual(e, task.Exception.InnerException);
        }
示例#4
0
        public void SetsSchedulerImplicitly()
        {
            var scheduler = new SingleThreadedTaskScheduler();

            scheduler.Start();
            var task = SetsSchedulerImplicitlyBlock(scheduler).CreateCoroutine<TaskScheduler>(scheduler);
            Thread.SpinWait(10000);
            scheduler.Dispose();

            Assert.IsTrue(task.IsCompleted, "Not IsCompleted");
            Assert.IsFalse(task.IsFaulted, "IsFaulted");
        }
示例#5
0
        public void RunsTask()
        {
            var scheduler = new SingleThreadedTaskScheduler();
            Mock<IDisposable> mockDisposable = new Mock<IDisposable>();

            bool taskRun = false;
            Action subTask = () =>
                {
                    Console.WriteLine("Sub task run.");
                    taskRun = true;
                };

            scheduler.Start();
            var task = RunsTaskBlock(subTask, mockDisposable.Object).CreateCoroutine<int>(scheduler);
            Thread.SpinWait(10000);
            scheduler.Dispose();

            mockDisposable.Verify(d => d.Dispose(), Times.Once());
            Assert.IsTrue(task.IsCompleted, "Not IsCompleted");
            Assert.IsFalse(task.IsFaulted, "IsFaulted");
            Assert.IsTrue(taskRun, "Task was not run.");
        }
示例#6
0
        public void ReturnsResultFromNestedCoroutine()
        {
            var scheduler = new SingleThreadedTaskScheduler();

            scheduler.Start();
            var task = ReturnsResultFromNestedCoroutineBlock().CreateCoroutine<int>(scheduler);
            Thread.SpinWait(10000);
            scheduler.Dispose();

            Assert.IsTrue(task.IsCompleted);
            Assert.IsFalse(task.IsFaulted);
            Assert.AreEqual(52, task.Result);
        }
示例#7
0
        public void ReturnsResult()
        {
            var scheduler = new SingleThreadedTaskScheduler();
            Mock<IDisposable> mockDisposable = new Mock<IDisposable>();

            scheduler.Start();
            var task = ReturnsResultBlock(mockDisposable.Object).AsCoroutine<string>(TaskCreationOptions.None, scheduler);
            scheduler.Dispose();

            mockDisposable.Verify(d => d.Dispose(), Times.Once(), "Disposable not disposed.");
            Assert.IsTrue(task.IsCompleted, "Not IsCompleted");
            Assert.IsFalse(task.IsFaulted, "IsFaulted");
            Assert.AreEqual("result", task.Result);
        }
示例#8
0
        public void PropagatesValueFromTask()
        {
            var scheduler = new SingleThreadedTaskScheduler();
            Mock<IDisposable> mockDisposable = new Mock<IDisposable>();

            int value = 42;

            Func<int> subTask = () => { return value; };

            scheduler.Start();
            var task = PropagatesValuesFromTaskBlock(subTask, mockDisposable.Object).CreateCoroutine<int>(scheduler);
            Thread.SpinWait(10000);
            scheduler.Dispose();

            mockDisposable.Verify(d => d.Dispose(), Times.Once());
            Assert.IsTrue(task.IsCompleted, "Not IsCompleted");
            Assert.IsFalse(task.IsFaulted, "IsFaulted");
            Assert.AreEqual(value, task.Result, "Values differ.");
        }
示例#9
0
        // disabled, no good if we want to be able to catch errors.
        //[Test]
        public void PropagatesExceptionFromTask()
        {
            var scheduler = new SingleThreadedTaskScheduler();
            Mock<IDisposable> mockDisposable = new Mock<IDisposable>();

            Exception exception = null;

            Action subTask = () => {
                exception = new Exception("oops.");
                throw exception;
            };

            scheduler.Start();
            var task = PropagatesExceptionFromTaskBlock(subTask, mockDisposable.Object).CreateCoroutine<int>(scheduler);
            Thread.SpinWait(10000);
            scheduler.Dispose();

            mockDisposable.Verify(d => d.Dispose(), Times.Once());
            Assert.IsTrue(task.IsCompleted, "Not IsCompleted");
            Assert.IsTrue(task.IsFaulted, "Not IsFaulted");

            //                              Aggregate Aggregate      exception
            Assert.AreEqual(exception, task.Exception.InnerException.InnerException);
        }