0
|
1 using System;
|
|
2 using System.Diagnostics;
|
|
3
|
|
4 using NUnit.Framework;
|
|
5
|
|
6 using BLToolkit.Aspects;
|
|
7 using BLToolkit.Reflection;
|
|
8
|
|
9 namespace Aspects
|
|
10 {
|
|
11 [TestFixture]
|
|
12 public class AsyncAspectTest
|
|
13 {
|
|
14 private const int ExecutionTime = 200;
|
|
15
|
|
16 public abstract class TestObject
|
|
17 {
|
|
18 public int Test(int intVal, string strVal)
|
|
19 {
|
|
20 System.Threading.Thread.Sleep(ExecutionTime + 30);
|
|
21 return intVal;
|
|
22 }
|
|
23
|
|
24 [Async] public abstract IAsyncResult BeginTest(int intVal, string strVal);
|
|
25 [Async] public abstract IAsyncResult BeginTest(int intVal, string strVal, AsyncCallback callback);
|
|
26 [Async] public abstract IAsyncResult BeginTest(int intVal, string strVal, AsyncCallback callback, object state);
|
|
27 [Async] public abstract int EndTest(IAsyncResult asyncResult);
|
|
28
|
|
29 [Async("Test")]
|
|
30 public abstract IAsyncResult AnyName(int intVal, string strVal, AsyncCallback callback, object state);
|
|
31 [Async("Test", typeof(int), typeof(string))]
|
|
32 public abstract int AnyName(IAsyncResult asyncResult);
|
|
33 }
|
|
34
|
|
35 public abstract class TestObject<T>
|
|
36 {
|
|
37 public T Test(T intVal)
|
|
38 {
|
|
39 System.Threading.Thread.Sleep(ExecutionTime + 30);
|
|
40 return intVal;
|
|
41 }
|
|
42
|
|
43 [Async] public abstract IAsyncResult BeginTest(T intVal);
|
|
44 [Async] public abstract T EndTest(IAsyncResult asyncResult);
|
|
45 }
|
|
46
|
|
47 [Test]
|
|
48 public void AsyncTest()
|
|
49 {
|
|
50 var o = TypeAccessor<TestObject>.CreateInstanceEx();
|
|
51 var sw = Stopwatch.StartNew();
|
|
52
|
|
53 Assert.AreEqual(1, o.Test(1, null));
|
|
54 var mss = sw.ElapsedMilliseconds;
|
|
55 Assert.IsTrue(mss >= ExecutionTime);
|
|
56
|
|
57 sw.Reset();
|
|
58 sw.Start();
|
|
59
|
|
60 var ar = o.BeginTest(2, "12");
|
|
61 mss = sw.ElapsedMilliseconds;
|
|
62 Assert.IsTrue(mss <= ExecutionTime);
|
|
63
|
|
64 Assert.AreEqual(2, o.EndTest(ar));
|
|
65 mss = sw.ElapsedMilliseconds;
|
|
66 Assert.IsTrue(mss >= ExecutionTime);
|
|
67 }
|
|
68
|
|
69 [Test]
|
|
70 public void GenericTest()
|
|
71 {
|
|
72 var o = TypeAccessor<TestObject<DateTime>>.CreateInstanceEx();
|
|
73 var now = DateTime.Now;
|
|
74 var ar = o.BeginTest(now);
|
|
75
|
|
76 Assert.AreEqual(now, o.EndTest(ar));
|
|
77 }
|
|
78
|
|
79 private static void CallBack(IAsyncResult ar)
|
|
80 {
|
|
81 var o = (TestObject) ar.AsyncState;
|
|
82 Console.WriteLine("Callback");
|
|
83 o.EndTest(ar);
|
|
84 }
|
|
85
|
|
86 [Test]
|
|
87 public void CallbackTest()
|
|
88 {
|
|
89 var o = TypeAccessor<TestObject>.CreateInstanceEx();
|
|
90
|
|
91 o.BeginTest(2, null, CallBack, o);
|
|
92 }
|
|
93
|
|
94 [Test]
|
|
95 public void NoStateTest()
|
|
96 {
|
|
97 var o = TypeAccessor<TestObject>.CreateInstanceEx();
|
|
98
|
|
99 Assert.AreEqual(1, o.Test(1, null));
|
|
100
|
|
101 var ar = o.BeginTest(2, null, null);
|
|
102 Assert.AreEqual(2, o.EndTest(ar));
|
|
103 }
|
|
104
|
|
105 [Test]
|
|
106 public void NoCallbackTest()
|
|
107 {
|
|
108 var o = TypeAccessor<TestObject>.CreateInstanceEx();
|
|
109
|
|
110 Assert.AreEqual(1, o.Test(1, null));
|
|
111
|
|
112 var ar = o.BeginTest(2, "1234");
|
|
113 Assert.AreEqual(2, o.EndTest(ar));
|
|
114 }
|
|
115
|
|
116 [Test]
|
|
117 public void AnyNameTest()
|
|
118 {
|
|
119 var o = TypeAccessor<TestObject>.CreateInstanceEx();
|
|
120
|
|
121 var ar = o.AnyName(2, null, null, null);
|
|
122 Assert.AreEqual(2, o.AnyName(ar));
|
|
123 }
|
|
124 }
|
|
125 }
|