ExecutionContextTest.cs 6.5 KB

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