ExecutionContextTest.cs 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. //
  2. // ExecutionContextTest.cs - NUnit tests for ExecutionContext
  3. //
  4. // Author:
  5. // Sebastien Pouliot <[email protected]>
  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. using System.Runtime.Remoting.Messaging;
  29. using System;
  30. using System.Security;
  31. using System.Threading;
  32. using NUnit.Framework;
  33. namespace MonoTests.System.Threading {
  34. [TestFixture]
  35. public class ExecutionContextTest {
  36. static bool success;
  37. static void Callback (object o)
  38. {
  39. success = (bool)o;
  40. }
  41. public class CallContextValue : ILogicalThreadAffinative {
  42. public object Value { get; set; }
  43. public CallContextValue (object value)
  44. {
  45. this.Value = value;
  46. }
  47. }
  48. [SetUp]
  49. public void SetUp ()
  50. {
  51. success = false;
  52. }
  53. [TearDown]
  54. public void TearDown ()
  55. {
  56. if (ExecutionContext.IsFlowSuppressed ())
  57. ExecutionContext.RestoreFlow ();
  58. CallContext.FreeNamedDataSlot ("testlc");
  59. }
  60. [Test]
  61. [Category("MobileNotWorking")]
  62. public void LogicalGetData_SetData()
  63. {
  64. var value = "a";
  65. CallContext.SetData ("testlc", value);
  66. var capturedValue = CallContext.LogicalGetData ("testlc");
  67. Assert.IsNull (capturedValue);
  68. }
  69. [Test]
  70. [Category("MobileNotWorking")]
  71. public void LogicalGetData_SetDataLogicalThreadAffinative()
  72. {
  73. var value = new CallContextValue ("a");
  74. CallContext.SetData ("testlc", value);
  75. var capturedValue = CallContext.LogicalGetData ("testlc");
  76. Assert.AreEqual (value, capturedValue);
  77. }
  78. [Test]
  79. [Category("MobileNotWorking")]
  80. public void GetData_SetLogicalData()
  81. {
  82. var value = "a";
  83. CallContext.LogicalSetData ("testlc", value);
  84. var capturedValue = CallContext.GetData ("testlc");
  85. Assert.AreEqual (value, capturedValue);
  86. }
  87. [Test]
  88. [Category("MobileNotWorking")]
  89. public void CaptureLogicalCallContext()
  90. {
  91. var value = "Tester";
  92. object capturedValue = null;
  93. CallContext.LogicalSetData ("testlc", value);
  94. ExecutionContext ec = ExecutionContext.Capture ();
  95. Assert.IsNotNull (ec, "Capture");
  96. Assert.AreEqual (value, CallContext.LogicalGetData ("testlc"));
  97. CallContext.LogicalSetData ("testlc", null);
  98. ExecutionContext.Run (ec, new ContextCallback (new Action<object> ((data) => {
  99. capturedValue = CallContext.LogicalGetData ("testlc");
  100. })), null);
  101. Assert.AreEqual (value, capturedValue);
  102. Assert.AreNotEqual (value, CallContext.LogicalGetData ("testlc"));
  103. }
  104. [Test]
  105. [Category ("MobileNotWorking")]
  106. public void CaptureCallContext ()
  107. {
  108. var value = new CallContextValue (true);
  109. object capturedValue = null;
  110. CallContext.SetData ("testlc", value);
  111. ExecutionContext ec = ExecutionContext.Capture ();
  112. Assert.IsNotNull (ec, "Capture");
  113. Assert.AreEqual (value, CallContext.GetData ("testlc"));
  114. CallContext.SetData ("testlc", null);
  115. ExecutionContext.Run (ec, new ContextCallback (new Action<object> ((data) => {
  116. capturedValue = CallContext.GetData ("testlc");
  117. })), null);
  118. Assert.AreEqual (value, capturedValue);
  119. Assert.AreNotEqual (value, CallContext.GetData ("testlc"));
  120. }
  121. [Test]
  122. [Category("MobileNotWorking")]
  123. public void Capture ()
  124. {
  125. ExecutionContext ec = ExecutionContext.Capture ();
  126. Assert.IsNotNull (ec, "Capture");
  127. AsyncFlowControl afc = ExecutionContext.SuppressFlow ();
  128. Assert.IsTrue (ExecutionContext.IsFlowSuppressed (), "IsFlowSuppressed-1");
  129. try {
  130. ec = ExecutionContext.Capture ();
  131. Assert.IsNull (ec, "Capture with SuppressFlow");
  132. }
  133. finally {
  134. afc.Undo ();
  135. }
  136. }
  137. [Test]
  138. [Category("MobileNotWorking")]
  139. public void Copy ()
  140. {
  141. ExecutionContext ec = ExecutionContext.Capture ();
  142. Assert.IsNotNull (ec, "Capture");
  143. ExecutionContext copy = ec.CreateCopy ();
  144. Assert.IsNotNull (copy, "Copy of Capture");
  145. Assert.IsFalse (ec.Equals (copy));
  146. Assert.IsFalse (copy.Equals (ec));
  147. Assert.IsFalse (Object.ReferenceEquals (ec, copy));
  148. ExecutionContext copy2nd = copy.CreateCopy ();
  149. Assert.IsNotNull (copy2nd, "2nd level copy of Capture");
  150. }
  151. [Test]
  152. [ExpectedException (typeof (InvalidOperationException))]
  153. // The context might be the result of capture so no exception is thrown
  154. [Category ("NotWorking")]
  155. public void Copy_FromThread ()
  156. {
  157. ExecutionContext ec = Thread.CurrentThread.ExecutionContext;
  158. Assert.IsNotNull (ec, "Thread.CurrentThread.ExecutionContext");
  159. ExecutionContext copy = ec.CreateCopy ();
  160. }
  161. [Test]
  162. [Category("MobileNotWorking")]
  163. public void IsFlowSuppressed ()
  164. {
  165. Assert.IsFalse (ExecutionContext.IsFlowSuppressed (), "IsFlowSuppressed-1");
  166. AsyncFlowControl afc = ExecutionContext.SuppressFlow ();
  167. Assert.IsTrue (ExecutionContext.IsFlowSuppressed (), "IsFlowSuppressed-2");
  168. afc.Undo ();
  169. Assert.IsFalse (ExecutionContext.IsFlowSuppressed (), "IsFlowSuppressed-3");
  170. }
  171. [Test]
  172. [ExpectedException (typeof (InvalidOperationException))]
  173. [Category("MobileNotWorking")]
  174. public void RestoreFlow_None ()
  175. {
  176. ExecutionContext.RestoreFlow ();
  177. }
  178. [Test]
  179. [Category("MobileNotWorking")]
  180. public void RestoreFlow_SuppressFlow ()
  181. {
  182. Assert.IsFalse (ExecutionContext.IsFlowSuppressed (), "IsFlowSuppressed-1");
  183. ExecutionContext.SuppressFlow ();
  184. Assert.IsTrue (ExecutionContext.IsFlowSuppressed (), "IsFlowSuppressed-2");
  185. ExecutionContext.RestoreFlow ();
  186. Assert.IsFalse (ExecutionContext.IsFlowSuppressed (), "IsFlowSuppressed-3");
  187. }
  188. [Test]
  189. [Category ("CAS")] // since r60298 the SecurityContext is only captured if the security manager is active
  190. public void Run () // see bug #78306 for details
  191. {
  192. Assert.IsFalse (success, "pre-check");
  193. ExecutionContext.Run (ExecutionContext.Capture (), new ContextCallback (Callback), true);
  194. Assert.IsTrue (success, "post-check");
  195. }
  196. [Test]
  197. [ExpectedException (typeof (InvalidOperationException))]
  198. [Category("MobileNotWorking")]
  199. public void Run_SuppressFlow ()
  200. {
  201. Assert.IsFalse (ExecutionContext.IsFlowSuppressed ());
  202. AsyncFlowControl afc = ExecutionContext.SuppressFlow ();
  203. Assert.IsTrue (ExecutionContext.IsFlowSuppressed ());
  204. try {
  205. ExecutionContext.Run (ExecutionContext.Capture (), new ContextCallback (Callback), "Hello world.");
  206. }
  207. finally {
  208. afc.Undo ();
  209. }
  210. }
  211. [Test]
  212. [Category("MobileNotWorking")]
  213. public void SuppressFlow ()
  214. {
  215. Assert.IsFalse (ExecutionContext.IsFlowSuppressed (), "IsFlowSuppressed-1");
  216. AsyncFlowControl afc = ExecutionContext.SuppressFlow ();
  217. Assert.IsTrue (ExecutionContext.IsFlowSuppressed (), "IsFlowSuppressed-3");
  218. afc.Undo ();
  219. Assert.IsFalse (ExecutionContext.IsFlowSuppressed (), "IsFlowSuppressed-4");
  220. }
  221. [Test]
  222. [ExpectedException (typeof (InvalidOperationException))]
  223. [Category("MobileNotWorking")]
  224. public void SuppressFlow_Two_Undo ()
  225. {
  226. Assert.IsFalse (ExecutionContext.IsFlowSuppressed (), "IsFlowSuppressed-1");
  227. AsyncFlowControl afc = ExecutionContext.SuppressFlow ();
  228. Assert.IsTrue (ExecutionContext.IsFlowSuppressed (), "IsFlowSuppressed-2");
  229. AsyncFlowControl afc2 = ExecutionContext.SuppressFlow ();
  230. Assert.IsTrue (ExecutionContext.IsFlowSuppressed (), "IsFlowSuppressed-3");
  231. afc2.Undo ();
  232. // note: afc2 Undo return to the original (not the previous) state
  233. Assert.IsFalse (ExecutionContext.IsFlowSuppressed (), "IsFlowSuppressed-4");
  234. // we can't use the first AsyncFlowControl
  235. afc.Undo ();
  236. }
  237. }
  238. }