diff Implab.Test/AsyncTests.cs @ 10:aa33d0bb8c0c promises

implemeted new cancellable promises concept
author cin
date Sun, 03 Nov 2013 18:07:38 +0400
parents 381095ad0a69
children 6ec82bf68c8e
line wrap: on
line diff
--- a/Implab.Test/AsyncTests.cs	Sat Nov 02 00:55:47 2013 +0400
+++ b/Implab.Test/AsyncTests.cs	Sun Nov 03 18:07:38 2013 +0400
@@ -1,10 +1,9 @@
 using System;
 using Microsoft.VisualStudio.TestTools.UnitTesting;
-using Implab;
 using System.Reflection;
 using System.Threading;
 
-namespace Implab.Tests
+namespace Implab.Test
 {
 	[TestClass]
 	public class AsyncTests
@@ -90,12 +89,39 @@
 		public void PoolTest ()
 		{
 			var pid = Thread.CurrentThread.ManagedThreadId;
-			var p = AsyncPool.Invoke (() => {
-				return Thread.CurrentThread.ManagedThreadId;
-			});
+			var p = AsyncPool.Invoke (() => Thread.CurrentThread.ManagedThreadId);
 
 			Assert.AreNotEqual (pid, p.Join ());
 		}
+
+        [TestMethod]
+        public void ComplexCase1Test() {
+            var flags = new bool[3];
+
+            // op1 (aync 200ms) => op2 (async 200ms) => op3 (sync map)
+
+            var p = PromiseHelper
+                .Sleep(200, "Alan")
+                .Cancelled(() => flags[0] = true)
+                .Chain(x =>
+                    PromiseHelper
+                        .Sleep(200, "Hi, " + x)
+                        .Map( y => y )
+                        .Cancelled(() => flags[1] = true)
+                )
+                .Cancelled(() => flags[2] = true);
+            Thread.Sleep(300);
+            p.Cancel();
+            try {
+                Assert.AreEqual(p.Join(), "Hi, Alan");
+                Assert.Fail("Shouldn't get here");
+            } catch(OperationCanceledException) {
+            }
+
+            Assert.IsFalse(flags[0]);
+            Assert.IsTrue(flags[1]);
+            Assert.IsTrue(flags[2]);
+        }
 	}
 }