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.
28 using System.Runtime.Remoting.Messaging;
33 using System.Security;
34 using System.Threading;
36 using NUnit.Framework;
38 namespace MonoTests.System.Threading {
41 public class ExecutionContextTest {
45 static void Callback (object o)
50 public class CallContextValue : ILogicalThreadAffinative {
51 public object Value { get; set; }
53 public CallContextValue (object value)
66 public void TearDown ()
68 if (ExecutionContext.IsFlowSuppressed ())
69 ExecutionContext.RestoreFlow ();
73 [Category ("MobileNotWorking")]
74 public void CaptureCallContext ()
76 var value = new CallContextValue (true);
77 object capturedValue = null;
79 CallContext.SetData ("testlc", value);
81 ExecutionContext ec = ExecutionContext.Capture ();
82 Assert.IsNotNull (ec, "Capture");
83 Assert.AreEqual (value, CallContext.GetData ("testlc"));
84 CallContext.SetData ("testlc", null);
86 ExecutionContext.Run (ec, new ContextCallback (new Action<object> ((data) => {
87 capturedValue = CallContext.GetData ("testlc");
90 Assert.AreEqual (value, capturedValue);
91 Assert.AreNotEqual (value, CallContext.GetData ("testlc"));
95 public void Capture ()
97 ExecutionContext ec = ExecutionContext.Capture ();
98 Assert.IsNotNull (ec, "Capture");
100 AsyncFlowControl afc = ExecutionContext.SuppressFlow ();
101 Assert.IsTrue (ExecutionContext.IsFlowSuppressed (), "IsFlowSuppressed-1");
103 ec = ExecutionContext.Capture ();
104 Assert.IsNull (ec, "Capture with SuppressFlow");
114 ExecutionContext ec = ExecutionContext.Capture ();
115 Assert.IsNotNull (ec, "Capture");
117 ExecutionContext copy = ec.CreateCopy ();
118 Assert.IsNotNull (copy, "Copy of Capture");
120 Assert.IsFalse (ec.Equals (copy));
121 Assert.IsFalse (copy.Equals (ec));
122 Assert.IsFalse (Object.ReferenceEquals (ec, copy));
124 ExecutionContext copy2nd = copy.CreateCopy ();
125 Assert.IsNotNull (copy2nd, "2nd level copy of Capture");
129 [ExpectedException (typeof (InvalidOperationException))]
130 // The context might be the result of capture so no exception is thrown
131 [Category ("NotWorking")]
132 public void Copy_FromThread ()
134 ExecutionContext ec = Thread.CurrentThread.ExecutionContext;
135 Assert.IsNotNull (ec, "Thread.CurrentThread.ExecutionContext");
137 ExecutionContext copy = ec.CreateCopy ();
141 public void IsFlowSuppressed ()
143 Assert.IsFalse (ExecutionContext.IsFlowSuppressed (), "IsFlowSuppressed-1");
145 AsyncFlowControl afc = ExecutionContext.SuppressFlow ();
146 Assert.IsTrue (ExecutionContext.IsFlowSuppressed (), "IsFlowSuppressed-2");
149 Assert.IsFalse (ExecutionContext.IsFlowSuppressed (), "IsFlowSuppressed-3");
153 [ExpectedException (typeof (InvalidOperationException))]
154 public void RestoreFlow_None ()
156 ExecutionContext.RestoreFlow ();
160 public void RestoreFlow_SuppressFlow ()
162 Assert.IsFalse (ExecutionContext.IsFlowSuppressed (), "IsFlowSuppressed-1");
163 ExecutionContext.SuppressFlow ();
164 Assert.IsTrue (ExecutionContext.IsFlowSuppressed (), "IsFlowSuppressed-2");
165 ExecutionContext.RestoreFlow ();
166 Assert.IsFalse (ExecutionContext.IsFlowSuppressed (), "IsFlowSuppressed-3");
170 [Category ("CAS")] // since r60298 the SecurityContext is only captured if the security manager is active
171 public void Run () // see bug #78306 for details
173 Assert.IsFalse (success, "pre-check");
174 ExecutionContext.Run (ExecutionContext.Capture (), new ContextCallback (Callback), true);
175 Assert.IsTrue (success, "post-check");
179 [ExpectedException (typeof (InvalidOperationException))]
180 public void Run_SuppressFlow ()
182 Assert.IsFalse (ExecutionContext.IsFlowSuppressed ());
183 AsyncFlowControl afc = ExecutionContext.SuppressFlow ();
184 Assert.IsTrue (ExecutionContext.IsFlowSuppressed ());
186 ExecutionContext.Run (ExecutionContext.Capture (), new ContextCallback (Callback), "Hello world.");
194 public void SuppressFlow ()
196 Assert.IsFalse (ExecutionContext.IsFlowSuppressed (), "IsFlowSuppressed-1");
198 AsyncFlowControl afc = ExecutionContext.SuppressFlow ();
199 Assert.IsTrue (ExecutionContext.IsFlowSuppressed (), "IsFlowSuppressed-3");
202 Assert.IsFalse (ExecutionContext.IsFlowSuppressed (), "IsFlowSuppressed-4");
206 [ExpectedException (typeof (InvalidOperationException))]
207 public void SuppressFlow_Two_Undo ()
209 Assert.IsFalse (ExecutionContext.IsFlowSuppressed (), "IsFlowSuppressed-1");
211 AsyncFlowControl afc = ExecutionContext.SuppressFlow ();
212 Assert.IsTrue (ExecutionContext.IsFlowSuppressed (), "IsFlowSuppressed-2");
214 AsyncFlowControl afc2 = ExecutionContext.SuppressFlow ();
215 Assert.IsTrue (ExecutionContext.IsFlowSuppressed (), "IsFlowSuppressed-3");
218 // note: afc2 Undo return to the original (not the previous) state
219 Assert.IsFalse (ExecutionContext.IsFlowSuppressed (), "IsFlowSuppressed-4");
221 // we can't use the first AsyncFlowControl