In .:
[mono.git] / mcs / class / corlib / Test / System.Threading / ExecutionContextTest.cs
1 //
2 // ExecutionContextTest.cs - NUnit tests for ExecutionContext
3 //
4 // Author:
5 //      Sebastien Pouliot  <sebastien@ximian.com>
6 //
7 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
8 //
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
16 // 
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 // 
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 //
28
29 #if NET_2_0
30
31 using System;
32 using System.Security;
33 using System.Threading;
34
35 using NUnit.Framework;
36
37 namespace MonoTests.System.Threading {
38
39         [TestFixture]
40         public class ExecutionContextTest {
41
42                 static bool success;
43
44                 static void Callback (object o)
45                 {
46                         success = (bool)o;
47                 }
48
49                 [SetUp]
50                 public void SetUp ()
51                 {
52                         success = false;
53                 }
54
55                 [TearDown]
56                 public void TearDown ()
57                 {
58                         if (ExecutionContext.IsFlowSuppressed ())
59                                 ExecutionContext.RestoreFlow ();
60                 }
61
62                 [Test]
63                 public void Capture ()
64                 {
65                         ExecutionContext ec = ExecutionContext.Capture ();
66                         Assert.IsNotNull (ec, "Capture");
67
68                         AsyncFlowControl afc = ExecutionContext.SuppressFlow ();
69                         Assert.IsTrue (ExecutionContext.IsFlowSuppressed (), "IsFlowSuppressed-1");
70                         try {
71                                 ec = ExecutionContext.Capture ();
72                                 Assert.IsNull (ec, "Capture with SuppressFlow");
73                         }
74                         finally {
75                                 afc.Undo ();
76                         }
77                 }
78
79                 [Test]
80                 public void Copy ()
81                 {
82                         ExecutionContext ec = ExecutionContext.Capture ();
83                         Assert.IsNotNull (ec, "Capture");
84
85                         ExecutionContext copy = ec.CreateCopy ();
86                         Assert.IsNotNull (copy, "Copy of Capture");
87
88                         Assert.IsFalse (ec.Equals (copy));
89                         Assert.IsFalse (copy.Equals (ec));
90                         Assert.IsFalse (Object.ReferenceEquals (ec, copy));
91
92                         ExecutionContext copy2nd = copy.CreateCopy ();
93                         Assert.IsNotNull (copy2nd, "2nd level copy of Capture");
94                 }
95
96                 [Test]
97                 [ExpectedException (typeof (InvalidOperationException))]
98                 public void Copy_FromThread ()
99                 {
100                         ExecutionContext ec = Thread.CurrentThread.ExecutionContext;
101                         Assert.IsNotNull (ec, "Thread.CurrentThread.ExecutionContext");
102
103                         ExecutionContext copy = ec.CreateCopy ();
104                 }
105
106                 [Test]
107                 public void IsFlowSuppressed ()
108                 {
109                         Assert.IsFalse (ExecutionContext.IsFlowSuppressed (), "IsFlowSuppressed-1");
110
111                         AsyncFlowControl afc = ExecutionContext.SuppressFlow ();
112                         Assert.IsTrue (ExecutionContext.IsFlowSuppressed (), "IsFlowSuppressed-2");
113                         afc.Undo ();
114
115                         Assert.IsFalse (ExecutionContext.IsFlowSuppressed (), "IsFlowSuppressed-3");
116                 }
117
118                 [Test]
119                 [ExpectedException (typeof (InvalidOperationException))]
120                 public void RestoreFlow_None ()
121                 {
122                         ExecutionContext.RestoreFlow ();
123                 }
124
125                 [Test]
126                 public void RestoreFlow_SuppressFlow ()
127                 {
128                         Assert.IsFalse (ExecutionContext.IsFlowSuppressed (), "IsFlowSuppressed-1");
129                         ExecutionContext.SuppressFlow ();
130                         Assert.IsTrue (ExecutionContext.IsFlowSuppressed (), "IsFlowSuppressed-2");
131                         ExecutionContext.RestoreFlow ();
132                         Assert.IsFalse (ExecutionContext.IsFlowSuppressed (), "IsFlowSuppressed-3");
133                 }
134
135                 [Test]
136                 public void Run ()
137                 {
138                         Assert.IsFalse (success, "pre-check");
139                         ExecutionContext.Run (ExecutionContext.Capture (), new ContextCallback (Callback), true);
140                         Assert.IsTrue (success, "post-check");
141                 }
142
143                 [Test]
144                 [ExpectedException (typeof (InvalidOperationException))]
145                 public void Run_SuppressFlow ()
146                 {
147                         Assert.IsFalse (ExecutionContext.IsFlowSuppressed ());
148                         AsyncFlowControl afc = ExecutionContext.SuppressFlow ();
149                         Assert.IsTrue (ExecutionContext.IsFlowSuppressed ());
150                         try {
151                                 ExecutionContext.Run (ExecutionContext.Capture (), new ContextCallback (Callback), "Hello world.");
152                         }
153                         finally {
154                                 afc.Undo ();
155                         }
156                 }
157
158                 [Test]
159                 public void SuppressFlow ()
160                 {
161                         Assert.IsFalse (ExecutionContext.IsFlowSuppressed (), "IsFlowSuppressed-1");
162
163                         AsyncFlowControl afc = ExecutionContext.SuppressFlow ();
164                         Assert.IsTrue (ExecutionContext.IsFlowSuppressed (), "IsFlowSuppressed-3");
165                         afc.Undo ();
166
167                         Assert.IsFalse (ExecutionContext.IsFlowSuppressed (), "IsFlowSuppressed-4");
168                 }
169
170                 [Test]
171                 [ExpectedException (typeof (InvalidOperationException))]
172                 public void SuppressFlow_Two_Undo ()
173                 {
174                         Assert.IsFalse (ExecutionContext.IsFlowSuppressed (), "IsFlowSuppressed-1");
175
176                         AsyncFlowControl afc = ExecutionContext.SuppressFlow ();
177                         Assert.IsTrue (ExecutionContext.IsFlowSuppressed (), "IsFlowSuppressed-2");
178
179                         AsyncFlowControl afc2 = ExecutionContext.SuppressFlow ();
180                         Assert.IsTrue (ExecutionContext.IsFlowSuppressed (), "IsFlowSuppressed-3");
181                         afc2.Undo ();
182
183                         // note: afc2 Undo return to the original (not the previous) state
184                         Assert.IsFalse (ExecutionContext.IsFlowSuppressed (), "IsFlowSuppressed-4");
185
186                         // we can't use the first AsyncFlowControl
187                         afc.Undo ();
188                 }
189         }
190 }
191
192 #endif