예제 #1
0
        public void IsDoneIsTrueWhenTaskCompletes()
        {
            FutureTask <T> task = new FutureTask <T>(_noOpCall);

            task.Run();
            Assert.IsTrue(task.IsDone);
            Assert.IsFalse(task.IsCancelled);
        }
예제 #2
0
        public void CancelFailsOnCompletedTask([Values(true, false)] bool mayInterruptIfRunning)
        {
            FutureTask <T> task = new FutureTask <T>(_noOpCall);

            task.Run();
            Assert.IsFalse(task.Cancel(mayInterruptIfRunning));
            Assert.IsTrue(task.IsDone);
            Assert.IsFalse(task.IsCancelled);
        }
예제 #3
0
        public void CancelSecceedsBeforeRun([Values(true, false)] bool mayInterruptIfRunning)
        {
            FutureTask <T> task = new FutureTask <T>(_noOpCall);

            Assert.IsTrue(task.Cancel(mayInterruptIfRunning));
            task.Run();
            Assert.IsTrue(task.IsDone);
            Assert.IsTrue(task.IsCancelled);
        }
예제 #4
0
        public void CancelInterruptsRunningTask()
        {
            var t = ThreadManager.GetManagedAction(
                delegate
            {
                Assert.Throws <ThreadInterruptedException>(() => Thread.Sleep(Delays.Medium));
            });
            FutureTask <T> task = new FutureTask <T>(t, default(T));

            new Thread(task.Run).Start();

            Thread.Sleep(Delays.Short);
            Assert.IsTrue(task.Cancel(true));
            ThreadManager.JoinAndVerify();
            Assert.IsTrue(task.IsDone);
            Assert.IsTrue(task.IsCancelled);
        }
예제 #5
0
        public void TimedGetResultRetrievesValueSetFromAnotherThread()
        {
            T result          = TestData <T> .One;
            FutureTask <T> ft = new FutureTask <T>(() => result);

            Assert.IsFalse(ft.IsDone);
            Assert.IsFalse(ft.IsCancelled);
            ThreadManager.StartAndAssertRegistered(
                "T1",
                delegate
            {
                Assert.That(ft.GetResult(Delays.Medium), Is.EqualTo(result));
            });
            ft.Run();
            ThreadManager.JoinAndVerify();
            Assert.IsTrue(ft.IsDone);
            Assert.IsFalse(ft.IsCancelled);
        }
예제 #6
0
        public void GetChokesWhenInterrupted([Values(true, false)] bool isTimed)
        {
            FutureTask <T> ft = new FutureTask <T>(_noOpCall);
            var            t  = ThreadManager.StartAndAssertRegistered(
                "T1",
                () => Assert.Throws <ThreadInterruptedException>(
                    delegate
            {
                if (isTimed)
                {
                    ft.GetResult(Delays.Medium);
                }
                else
                {
                    ft.GetResult();
                }
            }));

            Thread.Sleep(Delays.Short);
            t.Interrupt();
            ThreadManager.JoinAndVerify();
        }
예제 #7
0
        public void GetChokesWhenTaskChokes([Values(true, false)] bool isTimed)
        {
            FutureTask <T> ft = new FutureTask <T>(
                delegate
            {
                int z = 0;
                return(TestData <T> .MakeData(5 / z));
            });

            ft.Run();
            Assert.Throws <ExecutionException>(
                delegate
            {
                if (isTimed)
                {
                    ft.GetResult(Delays.Medium);
                }
                else
                {
                    ft.GetResult();
                }
            });
        }
예제 #8
0
        public void GetChokesWhenTaskIsCancelled([Values(true, false)] bool isTimed)
        {
            FutureTask <T> ft = new FutureTask <T>(
                () => Thread.Sleep(Delays.Medium), default(T));

            ThreadStart getter = () => Assert.Throws <CancellationException>(
                delegate
            {
                if (isTimed)
                {
                    ft.GetResult(Delays.Medium);
                }
                else
                {
                    ft.GetResult();
                }
            });

            ThreadManager.StartAndAssertRegistered("T", getter, ft.Run);
            Thread.Sleep(Delays.Short);
            ft.Cancel(true);
            ThreadManager.JoinAndVerify();
        }
예제 #9
0
        public void CancelDoesNotInterruptRunningTask()
        {
            var            called = false;
            FutureTask <T> task   = new FutureTask <T>(
                ThreadManager.GetManagedAction(
                    delegate
            {
                Thread.Sleep(Delays.Small);
                called = true;
            }),
                default(T));

            ThreadManager.StartAndAssertRegistered(new Thread(task.Run)
            {
                Name = "T1"
            });
            Thread.Sleep(Delays.Short);
            Assert.IsTrue(task.Cancel());
            ThreadManager.JoinAndVerify();
            Assert.IsTrue(task.IsDone);
            Assert.IsTrue(task.IsCancelled);
            Assert.IsTrue(called);
        }
예제 #10
0
        public void GetChokesWhenTimeout()
        {
            FutureTask <T> ft = new FutureTask <T>(_noOpCall);

            Assert.Throws <TimeoutException>(() => ft.GetResult(TimeSpan.FromMilliseconds(1)));
        }