view Implab.Test/CancelationTests.cs @ 148:e6d4b41f0101 v2

fixed timeout handling in promises
author cin
date Wed, 15 Apr 2015 07:30:20 +0300
parents 706fccb85524
children eb793fbbe4ea
line wrap: on
line source

using System;
using Implab.Parallels;

#if MONO

using NUnit.Framework;
using TestClassAttribute = NUnit.Framework.TestFixtureAttribute;
using TestMethodAttribute = NUnit.Framework.TestAttribute;

#else

using Microsoft.VisualStudio.TestTools.UnitTesting;

#endif

namespace Implab.Test {
    [TestClass]
    public class CancelationTests {

        [TestMethod]
        public void PromiseCancelTest() {
            var p = new Promise();
            bool requested = false;
            var reason = new Exception("Test");

            // request cancelation
            p.Cancel(reason);

            Assert.IsTrue(p.IsCancellationRequested);
            Assert.AreSame(reason, p.CancellationReason);
            Assert.IsFalse(p.IsCancelled);

            p.CancellationRequested(r => {
                Assert.AreSame(reason, r);
                requested = true;
            });

            Assert.IsTrue(requested);

            // cancel the promise
            Assert.IsTrue(p.CancelOperationIfRequested());
            Assert.IsTrue(p.IsCancelled);
            Assert.AreSame(reason, p.Error);
        }

        [TestMethod]
        public void CancelActionBeforeStartTask() {
            bool run = false;
            var task = new ActionTask(() => {
                run = true;
            }, null, null);

            // request cancelation
            task.Cancel();
            Assert.IsTrue(task.IsCancelled);
            task.Resolve();
            Assert.IsFalse(run);
        }

        [TestMethod]
        public void CancelActionAfterTaskStarted() {
            var finish = new Signal();
            var started = new Signal();

            var task = new ActionTask(() => {
                started.Set();
                finish.Wait();
            }, null, null);

            AsyncPool.RunThread(() => {
                task.Resolve();
            });

            started.Wait(1000);

            task.Cancel();
            Assert.IsTrue(task.IsCancellationRequested);
            Assert.IsFalse(task.IsCancelled);
            Assert.IsFalse(task.IsResolved);

            finish.Set();
            task.Join(1000);

        }

        [TestMethod]
        public void CancelTaskChainFromBottom() {
            var check1 = new Signal();
            var requested = false;
            var p1 = AsyncPool.RunThread(token => {
                token.CancellationRequested(reason => requested = true);
                check1.Wait();
                token.CancelOperationIfRequested();
            });

            var p2 = p1.Then(() => {
            });

            Assert.IsFalse(p1.IsResolved);
            Assert.IsFalse(p2.IsResolved);

            p2.Cancel();

            Assert.IsFalse(p2.IsCancelled);
            Assert.IsFalse(p1.IsCancelled);
            Assert.IsTrue(requested);

            check1.Set();

            try {
                p2.Join(1000);
                Assert.Fail("The chain isn't cancelled");
            } catch(OperationCanceledException){
            }

            Assert.IsTrue(p1.IsCancelled);
            Assert.IsTrue(p2.IsCancelled);
        }



        [TestMethod]
        public void CancellableAsyncTask() {
            var finish = new Signal();
            var started = new Signal();

            var p = AsyncPool.RunThread(token => {
                token.CancellationRequested(r => finish.Set());
                started.Set();
                finish.Wait();
                Assert.IsTrue(token.CancelOperationIfRequested());
            });

            started.Wait(1000);
            Assert.IsFalse(p.IsResolved);
            p.Cancel();
            try {
                p.Join(1000);
            } catch (OperationCanceledException) {
            }
        }
    }
}