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 ();
71 CallContext.FreeNamedDataSlot ("testlc");
75 [Category("MobileNotWorking")]
76 public void LogicalGetData_SetData()
80 CallContext.SetData ("testlc", value);
81 var capturedValue = CallContext.LogicalGetData ("testlc");
83 Assert.IsNull (capturedValue);
87 [Category("MobileNotWorking")]
88 public void LogicalGetData_SetDataLogicalThreadAffinative()
90 var value = new CallContextValue ("a");
92 CallContext.SetData ("testlc", value);
93 var capturedValue = CallContext.LogicalGetData ("testlc");
95 Assert.AreEqual (value, capturedValue);
99 [Category("MobileNotWorking")]
100 public void GetData_SetLogicalData()
104 CallContext.LogicalSetData ("testlc", value);
105 var capturedValue = CallContext.GetData ("testlc");
107 Assert.AreEqual (value, capturedValue);
111 [Category("MobileNotWorking")]
112 public void CaptureLogicalCallContext()
114 var value = "Tester";
115 object capturedValue = null;
117 CallContext.LogicalSetData ("testlc", value);
119 ExecutionContext ec = ExecutionContext.Capture ();
120 Assert.IsNotNull (ec, "Capture");
121 Assert.AreEqual (value, CallContext.LogicalGetData ("testlc"));
122 CallContext.LogicalSetData ("testlc", null);
124 ExecutionContext.Run (ec, new ContextCallback (new Action<object> ((data) => {
125 capturedValue = CallContext.LogicalGetData ("testlc");
128 Assert.AreEqual (value, capturedValue);
129 Assert.AreNotEqual (value, CallContext.LogicalGetData ("testlc"));
133 [Category ("MobileNotWorking")]
134 public void CaptureCallContext ()
136 var value = new CallContextValue (true);
137 object capturedValue = null;
139 CallContext.SetData ("testlc", value);
141 ExecutionContext ec = ExecutionContext.Capture ();
142 Assert.IsNotNull (ec, "Capture");
143 Assert.AreEqual (value, CallContext.GetData ("testlc"));
144 CallContext.SetData ("testlc", null);
146 ExecutionContext.Run (ec, new ContextCallback (new Action<object> ((data) => {
147 capturedValue = CallContext.GetData ("testlc");
150 Assert.AreEqual (value, capturedValue);
151 Assert.AreNotEqual (value, CallContext.GetData ("testlc"));
155 [Category("MobileNotWorking")]
156 public void Capture ()
158 ExecutionContext ec = ExecutionContext.Capture ();
159 Assert.IsNotNull (ec, "Capture");
161 AsyncFlowControl afc = ExecutionContext.SuppressFlow ();
162 Assert.IsTrue (ExecutionContext.IsFlowSuppressed (), "IsFlowSuppressed-1");
164 ec = ExecutionContext.Capture ();
165 Assert.IsNull (ec, "Capture with SuppressFlow");
173 [Category("MobileNotWorking")]
176 ExecutionContext ec = ExecutionContext.Capture ();
177 Assert.IsNotNull (ec, "Capture");
179 ExecutionContext copy = ec.CreateCopy ();
180 Assert.IsNotNull (copy, "Copy of Capture");
182 Assert.IsFalse (ec.Equals (copy));
183 Assert.IsFalse (copy.Equals (ec));
184 Assert.IsFalse (Object.ReferenceEquals (ec, copy));
186 ExecutionContext copy2nd = copy.CreateCopy ();
187 Assert.IsNotNull (copy2nd, "2nd level copy of Capture");
191 [ExpectedException (typeof (InvalidOperationException))]
192 // The context might be the result of capture so no exception is thrown
193 [Category ("NotWorking")]
194 public void Copy_FromThread ()
196 ExecutionContext ec = Thread.CurrentThread.ExecutionContext;
197 Assert.IsNotNull (ec, "Thread.CurrentThread.ExecutionContext");
199 ExecutionContext copy = ec.CreateCopy ();
203 [Category("MobileNotWorking")]
204 public void IsFlowSuppressed ()
206 Assert.IsFalse (ExecutionContext.IsFlowSuppressed (), "IsFlowSuppressed-1");
208 AsyncFlowControl afc = ExecutionContext.SuppressFlow ();
209 Assert.IsTrue (ExecutionContext.IsFlowSuppressed (), "IsFlowSuppressed-2");
212 Assert.IsFalse (ExecutionContext.IsFlowSuppressed (), "IsFlowSuppressed-3");
216 [ExpectedException (typeof (InvalidOperationException))]
217 [Category("MobileNotWorking")]
218 public void RestoreFlow_None ()
220 ExecutionContext.RestoreFlow ();
224 [Category("MobileNotWorking")]
225 public void RestoreFlow_SuppressFlow ()
227 Assert.IsFalse (ExecutionContext.IsFlowSuppressed (), "IsFlowSuppressed-1");
228 ExecutionContext.SuppressFlow ();
229 Assert.IsTrue (ExecutionContext.IsFlowSuppressed (), "IsFlowSuppressed-2");
230 ExecutionContext.RestoreFlow ();
231 Assert.IsFalse (ExecutionContext.IsFlowSuppressed (), "IsFlowSuppressed-3");
235 [Category ("CAS")] // since r60298 the SecurityContext is only captured if the security manager is active
236 public void Run () // see bug #78306 for details
238 Assert.IsFalse (success, "pre-check");
239 ExecutionContext.Run (ExecutionContext.Capture (), new ContextCallback (Callback), true);
240 Assert.IsTrue (success, "post-check");
244 [ExpectedException (typeof (InvalidOperationException))]
245 [Category("MobileNotWorking")]
246 public void Run_SuppressFlow ()
248 Assert.IsFalse (ExecutionContext.IsFlowSuppressed ());
249 AsyncFlowControl afc = ExecutionContext.SuppressFlow ();
250 Assert.IsTrue (ExecutionContext.IsFlowSuppressed ());
252 ExecutionContext.Run (ExecutionContext.Capture (), new ContextCallback (Callback), "Hello world.");
260 [Category("MobileNotWorking")]
261 public void SuppressFlow ()
263 Assert.IsFalse (ExecutionContext.IsFlowSuppressed (), "IsFlowSuppressed-1");
265 AsyncFlowControl afc = ExecutionContext.SuppressFlow ();
266 Assert.IsTrue (ExecutionContext.IsFlowSuppressed (), "IsFlowSuppressed-3");
269 Assert.IsFalse (ExecutionContext.IsFlowSuppressed (), "IsFlowSuppressed-4");
273 [ExpectedException (typeof (InvalidOperationException))]
274 [Category("MobileNotWorking")]
275 public void SuppressFlow_Two_Undo ()
277 Assert.IsFalse (ExecutionContext.IsFlowSuppressed (), "IsFlowSuppressed-1");
279 AsyncFlowControl afc = ExecutionContext.SuppressFlow ();
280 Assert.IsTrue (ExecutionContext.IsFlowSuppressed (), "IsFlowSuppressed-2");
282 AsyncFlowControl afc2 = ExecutionContext.SuppressFlow ();
283 Assert.IsTrue (ExecutionContext.IsFlowSuppressed (), "IsFlowSuppressed-3");
286 // note: afc2 Undo return to the original (not the previous) state
287 Assert.IsFalse (ExecutionContext.IsFlowSuppressed (), "IsFlowSuppressed-4");
289 // we can't use the first AsyncFlowControl