annotate Implab.Test/AsyncTests.cs @ 0:279591fb4df3

initial commit promises async model
author user@factory.site.local
date Fri, 23 Aug 2013 04:38:46 +0400
parents
children 381095ad0a69
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
1 using System;
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
2 using Microsoft.VisualStudio.TestTools.UnitTesting;
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
3 using Implab;
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
4 using System.Reflection;
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
5 using System.Threading;
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
6
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
7 namespace Implab.Tests
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
8 {
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
9 [TestClass]
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
10 public class AsyncTests
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
11 {
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
12 [TestMethod]
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
13 public void ResolveTest ()
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
14 {
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
15 int res = -1;
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
16 var p = new Promise<int> ();
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
17 p.Then (x => res = x);
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
18 p.Resolve (100);
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
19
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
20 Assert.AreEqual (res, 100);
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
21 }
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
22
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
23 [TestMethod]
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
24 public void RejectTest ()
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
25 {
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
26 int res = -1;
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
27 Exception err = null;
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
28
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
29 var p = new Promise<int> ();
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
30 p.Then (x => res = x, e => err = e);
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
31 p.Reject (new ApplicationException ("error"));
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
32
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
33 Assert.AreEqual (res, -1);
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
34 Assert.AreEqual (err.Message, "error");
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
35
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
36 }
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
37
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
38 [TestMethod]
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
39 public void JoinSuccessTest ()
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
40 {
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
41 var p = new Promise<int> ();
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
42 p.Resolve (100);
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
43 Assert.AreEqual (p.Join (), 100);
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
44 }
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
45
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
46 [TestMethod]
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
47 public void JoinFailTest ()
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
48 {
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
49 var p = new Promise<int> ();
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
50 p.Reject (new ApplicationException ("failed"));
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
51
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
52 try {
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
53 p.Join ();
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
54 throw new ApplicationException ("WRONG!");
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
55 } catch (TargetInvocationException err) {
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
56 Assert.AreEqual (err.InnerException.Message, "failed");
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
57 } catch {
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
58 Assert.Fail ("Got wrong excaption");
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
59 }
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
60 }
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
61
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
62 [TestMethod]
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
63 public void MapTest ()
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
64 {
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
65 var p = new Promise<int> ();
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
66
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
67 var p2 = p.Map (x => x.ToString ());
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
68 p.Resolve (100);
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
69
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
70 Assert.AreEqual (p2.Join (), "100");
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
71 }
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
72
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
73 [TestMethod]
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
74 public void ChainTest ()
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
75 {
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
76 var p1 = new Promise<int> ();
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
77
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
78 var p3 = p1.Chain (x => {
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
79 var p2 = new Promise<string> ();
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
80 p2.Resolve (x.ToString ());
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
81 return p2;
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
82 });
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
83
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
84 p1.Resolve (100);
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
85
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
86 Assert.AreEqual (p3.Join (), "100");
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
87 }
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
88
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
89 [TestMethod]
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
90 public void PoolTest ()
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
91 {
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
92 var pid = Thread.CurrentThread.ManagedThreadId;
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
93 var p = AsyncPool.Invoke (() => {
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
94 return Thread.CurrentThread.ManagedThreadId;
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
95 });
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
96
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
97 Assert.AreNotEqual (pid, p.Join ());
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
98 }
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
99 }
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
100 }
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
101