2 // ExecutionContextTest.cs - NUnit tests for ExecutionContext
5 // Sebastien Pouliot <sebastien@ximian.com>
7 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
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:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
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.
32 using System.Security;
33 using System.Threading;
35 using NUnit.Framework;
37 namespace MonoTests.System.Threading {
40 public class ExecutionContextTest {
44 static void Callback (object o)
56 public void TearDown ()
58 if (ExecutionContext.IsFlowSuppressed ())
59 ExecutionContext.RestoreFlow ();
63 public void Capture ()
65 ExecutionContext ec = ExecutionContext.Capture ();
66 Assert.IsNotNull (ec, "Capture");
68 AsyncFlowControl afc = ExecutionContext.SuppressFlow ();
69 Assert.IsTrue (ExecutionContext.IsFlowSuppressed (), "IsFlowSuppressed-1");
71 ec = ExecutionContext.Capture ();
72 Assert.IsNull (ec, "Capture with SuppressFlow");
82 ExecutionContext ec = ExecutionContext.Capture ();
83 Assert.IsNotNull (ec, "Capture");
85 ExecutionContext copy = ec.CreateCopy ();
86 Assert.IsNotNull (copy, "Copy of Capture");
88 Assert.IsFalse (ec.Equals (copy));
89 Assert.IsFalse (copy.Equals (ec));
90 Assert.IsFalse (Object.ReferenceEquals (ec, copy));
92 ExecutionContext copy2nd = copy.CreateCopy ();
93 Assert.IsNotNull (copy2nd, "2nd level copy of Capture");
97 [ExpectedException (typeof (InvalidOperationException))]
98 public void Copy_FromThread ()
100 ExecutionContext ec = Thread.CurrentThread.ExecutionContext;
101 Assert.IsNotNull (ec, "Thread.CurrentThread.ExecutionContext");
103 ExecutionContext copy = ec.CreateCopy ();
107 public void IsFlowSuppressed ()
109 Assert.IsFalse (ExecutionContext.IsFlowSuppressed (), "IsFlowSuppressed-1");
111 AsyncFlowControl afc = ExecutionContext.SuppressFlow ();
112 Assert.IsTrue (ExecutionContext.IsFlowSuppressed (), "IsFlowSuppressed-2");
115 Assert.IsFalse (ExecutionContext.IsFlowSuppressed (), "IsFlowSuppressed-3");
119 [ExpectedException (typeof (InvalidOperationException))]
120 public void RestoreFlow_None ()
122 ExecutionContext.RestoreFlow ();
126 public void RestoreFlow_SuppressFlow ()
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");
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
139 Assert.IsFalse (success, "pre-check");
140 ExecutionContext.Run (ExecutionContext.Capture (), new ContextCallback (Callback), true);
141 Assert.IsTrue (success, "post-check");
145 [ExpectedException (typeof (InvalidOperationException))]
146 public void Run_SuppressFlow ()
148 Assert.IsFalse (ExecutionContext.IsFlowSuppressed ());
149 AsyncFlowControl afc = ExecutionContext.SuppressFlow ();
150 Assert.IsTrue (ExecutionContext.IsFlowSuppressed ());
152 ExecutionContext.Run (ExecutionContext.Capture (), new ContextCallback (Callback), "Hello world.");
160 public void SuppressFlow ()
162 Assert.IsFalse (ExecutionContext.IsFlowSuppressed (), "IsFlowSuppressed-1");
164 AsyncFlowControl afc = ExecutionContext.SuppressFlow ();
165 Assert.IsTrue (ExecutionContext.IsFlowSuppressed (), "IsFlowSuppressed-3");
168 Assert.IsFalse (ExecutionContext.IsFlowSuppressed (), "IsFlowSuppressed-4");
172 [ExpectedException (typeof (InvalidOperationException))]
173 public void SuppressFlow_Two_Undo ()
175 Assert.IsFalse (ExecutionContext.IsFlowSuppressed (), "IsFlowSuppressed-1");
177 AsyncFlowControl afc = ExecutionContext.SuppressFlow ();
178 Assert.IsTrue (ExecutionContext.IsFlowSuppressed (), "IsFlowSuppressed-2");
180 AsyncFlowControl afc2 = ExecutionContext.SuppressFlow ();
181 Assert.IsTrue (ExecutionContext.IsFlowSuppressed (), "IsFlowSuppressed-3");
184 // note: afc2 Undo return to the original (not the previous) state
185 Assert.IsFalse (ExecutionContext.IsFlowSuppressed (), "IsFlowSuppressed-4");
187 // we can't use the first AsyncFlowControl