New test.
[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                 [Category ("CAS")] // since r60298 the SecurityContext is only captured if the security manager is active
137                 public void Run () // see bug #78306 for details
138                 {
139                         Assert.IsFalse (success, "pre-check");
140                         ExecutionContext.Run (ExecutionContext.Capture (), new ContextCallback (Callback), true);
141                         Assert.IsTrue (success, "post-check");
142                 }
143
144                 [Test]
145                 [ExpectedException (typeof (InvalidOperationException))]
146                 public void Run_SuppressFlow ()
147                 {
148                         Assert.IsFalse (ExecutionContext.IsFlowSuppressed ());
149                         AsyncFlowControl afc = ExecutionContext.SuppressFlow ();
150                         Assert.IsTrue (ExecutionContext.IsFlowSuppressed ());
151                         try {
152                                 ExecutionContext.Run (ExecutionContext.Capture (), new ContextCallback (Callback), "Hello world.");
153                         }
154                         finally {
155                                 afc.Undo ();
156                         }
157                 }
158
159                 [Test]
160                 public void SuppressFlow ()
161                 {
162                         Assert.IsFalse (ExecutionContext.IsFlowSuppressed (), "IsFlowSuppressed-1");
163
164                         AsyncFlowControl afc = ExecutionContext.SuppressFlow ();
165                         Assert.IsTrue (ExecutionContext.IsFlowSuppressed (), "IsFlowSuppressed-3");
166                         afc.Undo ();
167
168                         Assert.IsFalse (ExecutionContext.IsFlowSuppressed (), "IsFlowSuppressed-4");
169                 }
170
171                 [Test]
172                 [ExpectedException (typeof (InvalidOperationException))]
173                 public void SuppressFlow_Two_Undo ()
174                 {
175                         Assert.IsFalse (ExecutionContext.IsFlowSuppressed (), "IsFlowSuppressed-1");
176
177                         AsyncFlowControl afc = ExecutionContext.SuppressFlow ();
178                         Assert.IsTrue (ExecutionContext.IsFlowSuppressed (), "IsFlowSuppressed-2");
179
180                         AsyncFlowControl afc2 = ExecutionContext.SuppressFlow ();
181                         Assert.IsTrue (ExecutionContext.IsFlowSuppressed (), "IsFlowSuppressed-3");
182                         afc2.Undo ();
183
184                         // note: afc2 Undo return to the original (not the previous) state
185                         Assert.IsFalse (ExecutionContext.IsFlowSuppressed (), "IsFlowSuppressed-4");
186
187                         // we can't use the first AsyncFlowControl
188                         afc.Undo ();
189                 }
190         }
191 }
192
193 #endif