Mercurial > pub > ImplabNet
comparison 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 | 
   comparison
  equal
  deleted
  inserted
  replaced
| -1:000000000000 | 0:279591fb4df3 | 
|---|---|
| 1 using System; | |
| 2 using Microsoft.VisualStudio.TestTools.UnitTesting; | |
| 3 using Implab; | |
| 4 using System.Reflection; | |
| 5 using System.Threading; | |
| 6 | |
| 7 namespace Implab.Tests | |
| 8 { | |
| 9 [TestClass] | |
| 10 public class AsyncTests | |
| 11 { | |
| 12 [TestMethod] | |
| 13 public void ResolveTest () | |
| 14 { | |
| 15 int res = -1; | |
| 16 var p = new Promise<int> (); | |
| 17 p.Then (x => res = x); | |
| 18 p.Resolve (100); | |
| 19 | |
| 20 Assert.AreEqual (res, 100); | |
| 21 } | |
| 22 | |
| 23 [TestMethod] | |
| 24 public void RejectTest () | |
| 25 { | |
| 26 int res = -1; | |
| 27 Exception err = null; | |
| 28 | |
| 29 var p = new Promise<int> (); | |
| 30 p.Then (x => res = x, e => err = e); | |
| 31 p.Reject (new ApplicationException ("error")); | |
| 32 | |
| 33 Assert.AreEqual (res, -1); | |
| 34 Assert.AreEqual (err.Message, "error"); | |
| 35 | |
| 36 } | |
| 37 | |
| 38 [TestMethod] | |
| 39 public void JoinSuccessTest () | |
| 40 { | |
| 41 var p = new Promise<int> (); | |
| 42 p.Resolve (100); | |
| 43 Assert.AreEqual (p.Join (), 100); | |
| 44 } | |
| 45 | |
| 46 [TestMethod] | |
| 47 public void JoinFailTest () | |
| 48 { | |
| 49 var p = new Promise<int> (); | |
| 50 p.Reject (new ApplicationException ("failed")); | |
| 51 | |
| 52 try { | |
| 53 p.Join (); | |
| 54 throw new ApplicationException ("WRONG!"); | |
| 55 } catch (TargetInvocationException err) { | |
| 56 Assert.AreEqual (err.InnerException.Message, "failed"); | |
| 57 } catch { | |
| 58 Assert.Fail ("Got wrong excaption"); | |
| 59 } | |
| 60 } | |
| 61 | |
| 62 [TestMethod] | |
| 63 public void MapTest () | |
| 64 { | |
| 65 var p = new Promise<int> (); | |
| 66 | |
| 67 var p2 = p.Map (x => x.ToString ()); | |
| 68 p.Resolve (100); | |
| 69 | |
| 70 Assert.AreEqual (p2.Join (), "100"); | |
| 71 } | |
| 72 | |
| 73 [TestMethod] | |
| 74 public void ChainTest () | |
| 75 { | |
| 76 var p1 = new Promise<int> (); | |
| 77 | |
| 78 var p3 = p1.Chain (x => { | |
| 79 var p2 = new Promise<string> (); | |
| 80 p2.Resolve (x.ToString ()); | |
| 81 return p2; | |
| 82 }); | |
| 83 | |
| 84 p1.Resolve (100); | |
| 85 | |
| 86 Assert.AreEqual (p3.Join (), "100"); | |
| 87 } | |
| 88 | |
| 89 [TestMethod] | |
| 90 public void PoolTest () | |
| 91 { | |
| 92 var pid = Thread.CurrentThread.ManagedThreadId; | |
| 93 var p = AsyncPool.Invoke (() => { | |
| 94 return Thread.CurrentThread.ManagedThreadId; | |
| 95 }); | |
| 96 | |
| 97 Assert.AreNotEqual (pid, p.Join ()); | |
| 98 } | |
| 99 } | |
| 100 } | |
| 101 | 
