Debian Codegraph @

external/rx/Rx/NET/Source/Tests.System.Reactive/Tests/Concurrency/AsyncLockTest.cs

raw
1 // Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information.
2
3 #if !SILVERLIGHT // MethodAccessException
4 using System;
5 using System.Reactive.Concurrency;
6 using System.Reflection;
7 #if NUNIT
8 using NUnit.Framework;
9 using TestClassAttribute = NUnit.Framework.TestFixtureAttribute;
10 using TestMethodAttribute = NUnit.Framework.TestAttribute;
11 using TestInitializeAttribute = NUnit.Framework.SetUpAttribute;
12 #else
13 using Microsoft.VisualStudio.TestTools.UnitTesting;
14 #endif
15
16 namespace ReactiveTests.Tests
17 {
18 [TestClass]
19 public class AsyncLockTest
20 {
21 [TestMethod, ExpectedException(typeof(ArgumentNullException))]
22 public void Wait_ArgumentChecking()
23 {
24 var asyncLock = new AsyncLock();
25 asyncLock.Wait(null);
26 }
27
28 [TestMethod]
29 public void Wait_Graceful()
30 {
31 var ok = false;
32 new AsyncLock().Wait(() => { ok = true; });
33 Assert.IsTrue(ok);
34 }
35
36 [TestMethod]
37 public void Wait_Fail()
38 {
39 var l = new AsyncLock();
40
41 var ex = new Exception();
42 try
43 {
44 l.Wait(() => { throw ex; });
45 Assert.Fail();
46 }
47 catch (Exception e)
48 {
49 Assert.AreSame(ex, e);
50 }
51
52 // has faulted; should not run
53 l.Wait(() => { Assert.Fail(); });
54 }
55
56 [TestMethod]
57 public void Wait_QueuesWork()
58 {
59 var l = new AsyncLock();
60
61 var l1 = false;
62 var l2 = false;
63 l.Wait(() => { l.Wait(() => { Assert.IsTrue(l1); l2 = true; }); l1 = true; });
64 Assert.IsTrue(l2);
65 }
66
67 [TestMethod]
68 public void Dispose()
69 {
70 var l = new AsyncLock();
71
72 var l1 = false;
73 var l2 = false;
74 var l3 = false;
75 var l4 = false;
76
77 l.Wait(() =>
78 {
79 l.Wait(() =>
80 {
81 l.Wait(() =>
82 {
83 l3 = true;
84 });
85
86 l2 = true;
87
88 l.Dispose();
89
90 l.Wait(() =>
91 {
92 l4 = true;
93 });
94 });
95
96 l1 = true;
97 });
98
99 Assert.IsTrue(l1);
100 Assert.IsTrue(l2);
101 Assert.IsFalse(l3);
102 Assert.IsFalse(l4);
103 }
104
105 public class AsyncLock
106 {
107 object instance;
108
109 public AsyncLock()
110 {
111 instance = typeof(Scheduler).Assembly.GetType("System.Reactive.Concurrency.AsyncLock").GetConstructor(new Type[] { }).Invoke(new object[] { });
112 }
113
114 public void Wait(Action action)
115 {
116 try
117 {
118 instance.GetType().GetMethod("Wait").Invoke(instance, new object[] { action });
119 }
120 catch (TargetInvocationException ex)
121 {
122 throw ex.InnerException;
123 }
124 }
125
126 public void Dispose()
127 {
128 try
129 {
130 instance.GetType().GetMethod("Dispose").Invoke(instance, new object[0]);
131 }
132 catch (TargetInvocationException ex)
133 {
134 throw ex.InnerException;
135 }
136 }
137 }
138 }
139 }
140 #endif