annotate 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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
4
381095ad0a69 Implab.Fx: implemented animation object
cin
parents: 0
diff changeset
1 using System;
381095ad0a69 Implab.Fx: implemented animation object
cin
parents: 0
diff changeset
2 using Microsoft.VisualStudio.TestTools.UnitTesting;
381095ad0a69 Implab.Fx: implemented animation object
cin
parents: 0
diff changeset
3 using System.Reflection;
381095ad0a69 Implab.Fx: implemented animation object
cin
parents: 0
diff changeset
4 using System.Threading;
381095ad0a69 Implab.Fx: implemented animation object
cin
parents: 0
diff changeset
5
10
aa33d0bb8c0c implemeted new cancellable promises concept
cin
parents: 4
diff changeset
6 namespace Implab.Test
4
381095ad0a69 Implab.Fx: implemented animation object
cin
parents: 0
diff changeset
7 {
381095ad0a69 Implab.Fx: implemented animation object
cin
parents: 0
diff changeset
8 [TestClass]
381095ad0a69 Implab.Fx: implemented animation object
cin
parents: 0
diff changeset
9 public class AsyncTests
381095ad0a69 Implab.Fx: implemented animation object
cin
parents: 0
diff changeset
10 {
381095ad0a69 Implab.Fx: implemented animation object
cin
parents: 0
diff changeset
11 [TestMethod]
381095ad0a69 Implab.Fx: implemented animation object
cin
parents: 0
diff changeset
12 public void ResolveTest ()
381095ad0a69 Implab.Fx: implemented animation object
cin
parents: 0
diff changeset
13 {
381095ad0a69 Implab.Fx: implemented animation object
cin
parents: 0
diff changeset
14 int res = -1;
381095ad0a69 Implab.Fx: implemented animation object
cin
parents: 0
diff changeset
15 var p = new Promise<int> ();
381095ad0a69 Implab.Fx: implemented animation object
cin
parents: 0
diff changeset
16 p.Then (x => res = x);
381095ad0a69 Implab.Fx: implemented animation object
cin
parents: 0
diff changeset
17 p.Resolve (100);
381095ad0a69 Implab.Fx: implemented animation object
cin
parents: 0
diff changeset
18
381095ad0a69 Implab.Fx: implemented animation object
cin
parents: 0
diff changeset
19 Assert.AreEqual (res, 100);
0
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
20 }
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
21
4
381095ad0a69 Implab.Fx: implemented animation object
cin
parents: 0
diff changeset
22 [TestMethod]
381095ad0a69 Implab.Fx: implemented animation object
cin
parents: 0
diff changeset
23 public void RejectTest ()
381095ad0a69 Implab.Fx: implemented animation object
cin
parents: 0
diff changeset
24 {
381095ad0a69 Implab.Fx: implemented animation object
cin
parents: 0
diff changeset
25 int res = -1;
381095ad0a69 Implab.Fx: implemented animation object
cin
parents: 0
diff changeset
26 Exception err = null;
381095ad0a69 Implab.Fx: implemented animation object
cin
parents: 0
diff changeset
27
381095ad0a69 Implab.Fx: implemented animation object
cin
parents: 0
diff changeset
28 var p = new Promise<int> ();
381095ad0a69 Implab.Fx: implemented animation object
cin
parents: 0
diff changeset
29 p.Then (x => res = x, e => err = e);
381095ad0a69 Implab.Fx: implemented animation object
cin
parents: 0
diff changeset
30 p.Reject (new ApplicationException ("error"));
381095ad0a69 Implab.Fx: implemented animation object
cin
parents: 0
diff changeset
31
381095ad0a69 Implab.Fx: implemented animation object
cin
parents: 0
diff changeset
32 Assert.AreEqual (res, -1);
381095ad0a69 Implab.Fx: implemented animation object
cin
parents: 0
diff changeset
33 Assert.AreEqual (err.Message, "error");
381095ad0a69 Implab.Fx: implemented animation object
cin
parents: 0
diff changeset
34
0
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
35 }
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
36
4
381095ad0a69 Implab.Fx: implemented animation object
cin
parents: 0
diff changeset
37 [TestMethod]
381095ad0a69 Implab.Fx: implemented animation object
cin
parents: 0
diff changeset
38 public void JoinSuccessTest ()
381095ad0a69 Implab.Fx: implemented animation object
cin
parents: 0
diff changeset
39 {
381095ad0a69 Implab.Fx: implemented animation object
cin
parents: 0
diff changeset
40 var p = new Promise<int> ();
381095ad0a69 Implab.Fx: implemented animation object
cin
parents: 0
diff changeset
41 p.Resolve (100);
381095ad0a69 Implab.Fx: implemented animation object
cin
parents: 0
diff changeset
42 Assert.AreEqual (p.Join (), 100);
0
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
43 }
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
44
4
381095ad0a69 Implab.Fx: implemented animation object
cin
parents: 0
diff changeset
45 [TestMethod]
381095ad0a69 Implab.Fx: implemented animation object
cin
parents: 0
diff changeset
46 public void JoinFailTest ()
381095ad0a69 Implab.Fx: implemented animation object
cin
parents: 0
diff changeset
47 {
381095ad0a69 Implab.Fx: implemented animation object
cin
parents: 0
diff changeset
48 var p = new Promise<int> ();
381095ad0a69 Implab.Fx: implemented animation object
cin
parents: 0
diff changeset
49 p.Reject (new ApplicationException ("failed"));
381095ad0a69 Implab.Fx: implemented animation object
cin
parents: 0
diff changeset
50
381095ad0a69 Implab.Fx: implemented animation object
cin
parents: 0
diff changeset
51 try {
381095ad0a69 Implab.Fx: implemented animation object
cin
parents: 0
diff changeset
52 p.Join ();
381095ad0a69 Implab.Fx: implemented animation object
cin
parents: 0
diff changeset
53 throw new ApplicationException ("WRONG!");
381095ad0a69 Implab.Fx: implemented animation object
cin
parents: 0
diff changeset
54 } catch (TargetInvocationException err) {
381095ad0a69 Implab.Fx: implemented animation object
cin
parents: 0
diff changeset
55 Assert.AreEqual (err.InnerException.Message, "failed");
381095ad0a69 Implab.Fx: implemented animation object
cin
parents: 0
diff changeset
56 } catch {
381095ad0a69 Implab.Fx: implemented animation object
cin
parents: 0
diff changeset
57 Assert.Fail ("Got wrong excaption");
381095ad0a69 Implab.Fx: implemented animation object
cin
parents: 0
diff changeset
58 }
0
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
59 }
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
60
4
381095ad0a69 Implab.Fx: implemented animation object
cin
parents: 0
diff changeset
61 [TestMethod]
381095ad0a69 Implab.Fx: implemented animation object
cin
parents: 0
diff changeset
62 public void MapTest ()
381095ad0a69 Implab.Fx: implemented animation object
cin
parents: 0
diff changeset
63 {
381095ad0a69 Implab.Fx: implemented animation object
cin
parents: 0
diff changeset
64 var p = new Promise<int> ();
381095ad0a69 Implab.Fx: implemented animation object
cin
parents: 0
diff changeset
65
381095ad0a69 Implab.Fx: implemented animation object
cin
parents: 0
diff changeset
66 var p2 = p.Map (x => x.ToString ());
381095ad0a69 Implab.Fx: implemented animation object
cin
parents: 0
diff changeset
67 p.Resolve (100);
381095ad0a69 Implab.Fx: implemented animation object
cin
parents: 0
diff changeset
68
381095ad0a69 Implab.Fx: implemented animation object
cin
parents: 0
diff changeset
69 Assert.AreEqual (p2.Join (), "100");
0
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
70 }
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
71
4
381095ad0a69 Implab.Fx: implemented animation object
cin
parents: 0
diff changeset
72 [TestMethod]
381095ad0a69 Implab.Fx: implemented animation object
cin
parents: 0
diff changeset
73 public void ChainTest ()
381095ad0a69 Implab.Fx: implemented animation object
cin
parents: 0
diff changeset
74 {
381095ad0a69 Implab.Fx: implemented animation object
cin
parents: 0
diff changeset
75 var p1 = new Promise<int> ();
381095ad0a69 Implab.Fx: implemented animation object
cin
parents: 0
diff changeset
76
381095ad0a69 Implab.Fx: implemented animation object
cin
parents: 0
diff changeset
77 var p3 = p1.Chain (x => {
381095ad0a69 Implab.Fx: implemented animation object
cin
parents: 0
diff changeset
78 var p2 = new Promise<string> ();
381095ad0a69 Implab.Fx: implemented animation object
cin
parents: 0
diff changeset
79 p2.Resolve (x.ToString ());
381095ad0a69 Implab.Fx: implemented animation object
cin
parents: 0
diff changeset
80 return p2;
381095ad0a69 Implab.Fx: implemented animation object
cin
parents: 0
diff changeset
81 });
381095ad0a69 Implab.Fx: implemented animation object
cin
parents: 0
diff changeset
82
381095ad0a69 Implab.Fx: implemented animation object
cin
parents: 0
diff changeset
83 p1.Resolve (100);
381095ad0a69 Implab.Fx: implemented animation object
cin
parents: 0
diff changeset
84
381095ad0a69 Implab.Fx: implemented animation object
cin
parents: 0
diff changeset
85 Assert.AreEqual (p3.Join (), "100");
0
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
86 }
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
87
4
381095ad0a69 Implab.Fx: implemented animation object
cin
parents: 0
diff changeset
88 [TestMethod]
381095ad0a69 Implab.Fx: implemented animation object
cin
parents: 0
diff changeset
89 public void PoolTest ()
381095ad0a69 Implab.Fx: implemented animation object
cin
parents: 0
diff changeset
90 {
381095ad0a69 Implab.Fx: implemented animation object
cin
parents: 0
diff changeset
91 var pid = Thread.CurrentThread.ManagedThreadId;
10
aa33d0bb8c0c implemeted new cancellable promises concept
cin
parents: 4
diff changeset
92 var p = AsyncPool.Invoke (() => Thread.CurrentThread.ManagedThreadId);
4
381095ad0a69 Implab.Fx: implemented animation object
cin
parents: 0
diff changeset
93
381095ad0a69 Implab.Fx: implemented animation object
cin
parents: 0
diff changeset
94 Assert.AreNotEqual (pid, p.Join ());
381095ad0a69 Implab.Fx: implemented animation object
cin
parents: 0
diff changeset
95 }
10
aa33d0bb8c0c implemeted new cancellable promises concept
cin
parents: 4
diff changeset
96
aa33d0bb8c0c implemeted new cancellable promises concept
cin
parents: 4
diff changeset
97 [TestMethod]
aa33d0bb8c0c implemeted new cancellable promises concept
cin
parents: 4
diff changeset
98 public void ComplexCase1Test() {
aa33d0bb8c0c implemeted new cancellable promises concept
cin
parents: 4
diff changeset
99 var flags = new bool[3];
aa33d0bb8c0c implemeted new cancellable promises concept
cin
parents: 4
diff changeset
100
aa33d0bb8c0c implemeted new cancellable promises concept
cin
parents: 4
diff changeset
101 // op1 (aync 200ms) => op2 (async 200ms) => op3 (sync map)
aa33d0bb8c0c implemeted new cancellable promises concept
cin
parents: 4
diff changeset
102
aa33d0bb8c0c implemeted new cancellable promises concept
cin
parents: 4
diff changeset
103 var p = PromiseHelper
aa33d0bb8c0c implemeted new cancellable promises concept
cin
parents: 4
diff changeset
104 .Sleep(200, "Alan")
aa33d0bb8c0c implemeted new cancellable promises concept
cin
parents: 4
diff changeset
105 .Cancelled(() => flags[0] = true)
aa33d0bb8c0c implemeted new cancellable promises concept
cin
parents: 4
diff changeset
106 .Chain(x =>
aa33d0bb8c0c implemeted new cancellable promises concept
cin
parents: 4
diff changeset
107 PromiseHelper
aa33d0bb8c0c implemeted new cancellable promises concept
cin
parents: 4
diff changeset
108 .Sleep(200, "Hi, " + x)
aa33d0bb8c0c implemeted new cancellable promises concept
cin
parents: 4
diff changeset
109 .Map( y => y )
aa33d0bb8c0c implemeted new cancellable promises concept
cin
parents: 4
diff changeset
110 .Cancelled(() => flags[1] = true)
aa33d0bb8c0c implemeted new cancellable promises concept
cin
parents: 4
diff changeset
111 )
aa33d0bb8c0c implemeted new cancellable promises concept
cin
parents: 4
diff changeset
112 .Cancelled(() => flags[2] = true);
aa33d0bb8c0c implemeted new cancellable promises concept
cin
parents: 4
diff changeset
113 Thread.Sleep(300);
aa33d0bb8c0c implemeted new cancellable promises concept
cin
parents: 4
diff changeset
114 p.Cancel();
aa33d0bb8c0c implemeted new cancellable promises concept
cin
parents: 4
diff changeset
115 try {
aa33d0bb8c0c implemeted new cancellable promises concept
cin
parents: 4
diff changeset
116 Assert.AreEqual(p.Join(), "Hi, Alan");
aa33d0bb8c0c implemeted new cancellable promises concept
cin
parents: 4
diff changeset
117 Assert.Fail("Shouldn't get here");
aa33d0bb8c0c implemeted new cancellable promises concept
cin
parents: 4
diff changeset
118 } catch(OperationCanceledException) {
aa33d0bb8c0c implemeted new cancellable promises concept
cin
parents: 4
diff changeset
119 }
aa33d0bb8c0c implemeted new cancellable promises concept
cin
parents: 4
diff changeset
120
aa33d0bb8c0c implemeted new cancellable promises concept
cin
parents: 4
diff changeset
121 Assert.IsFalse(flags[0]);
aa33d0bb8c0c implemeted new cancellable promises concept
cin
parents: 4
diff changeset
122 Assert.IsTrue(flags[1]);
aa33d0bb8c0c implemeted new cancellable promises concept
cin
parents: 4
diff changeset
123 Assert.IsTrue(flags[2]);
aa33d0bb8c0c implemeted new cancellable promises concept
cin
parents: 4
diff changeset
124 }
4
381095ad0a69 Implab.Fx: implemented animation object
cin
parents: 0
diff changeset
125 }
381095ad0a69 Implab.Fx: implemented animation object
cin
parents: 0
diff changeset
126 }
381095ad0a69 Implab.Fx: implemented animation object
cin
parents: 0
diff changeset
127