diff Implab.Test/CancelationTests.cs @ 145:706fccb85524 v2

RC: cancellation support for promises + tests
author cin
date Sun, 08 Mar 2015 02:52:27 +0300
parents
children eb793fbbe4ea
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Implab.Test/CancelationTests.cs	Sun Mar 08 02:52:27 2015 +0300
@@ -0,0 +1,144 @@
+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) {
+            }
+        }
+    }
+}
+