ExecutionContextTest.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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. #if NET_2_0
  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. [SetUp]
  42. public void SetUp ()
  43. {
  44. success = false;
  45. }
  46. [TearDown]
  47. public void TearDown ()
  48. {
  49. if (ExecutionContext.IsFlowSuppressed ())
  50. ExecutionContext.RestoreFlow ();
  51. }
  52. [Test]
  53. public void Capture ()
  54. {
  55. ExecutionContext ec = ExecutionContext.Capture ();
  56. Assert.IsNotNull (ec, "Capture");
  57. AsyncFlowControl afc = ExecutionContext.SuppressFlow ();
  58. Assert.IsTrue (ExecutionContext.IsFlowSuppressed (), "IsFlowSuppressed-1");
  59. try {
  60. ec = ExecutionContext.Capture ();
  61. Assert.IsNull (ec, "Capture with SuppressFlow");
  62. }
  63. finally {
  64. afc.Undo ();
  65. }
  66. }
  67. [Test]
  68. public void Copy ()
  69. {
  70. ExecutionContext ec = ExecutionContext.Capture ();
  71. Assert.IsNotNull (ec, "Capture");
  72. ExecutionContext copy = ec.CreateCopy ();
  73. Assert.IsNotNull (copy, "Copy of Capture");
  74. Assert.IsFalse (ec.Equals (copy));
  75. Assert.IsFalse (copy.Equals (ec));
  76. Assert.IsFalse (Object.ReferenceEquals (ec, copy));
  77. ExecutionContext copy2nd = copy.CreateCopy ();
  78. Assert.IsNotNull (copy2nd, "2nd level copy of Capture");
  79. }
  80. [Test]
  81. [ExpectedException (typeof (InvalidOperationException))]
  82. // The context might be the result of capture so no exception is thrown
  83. [Category ("NotWorking")]
  84. public void Copy_FromThread ()
  85. {
  86. ExecutionContext ec = Thread.CurrentThread.ExecutionContext;
  87. Assert.IsNotNull (ec, "Thread.CurrentThread.ExecutionContext");
  88. ExecutionContext copy = ec.CreateCopy ();
  89. }
  90. [Test]
  91. public void IsFlowSuppressed ()
  92. {
  93. Assert.IsFalse (ExecutionContext.IsFlowSuppressed (), "IsFlowSuppressed-1");
  94. AsyncFlowControl afc = ExecutionContext.SuppressFlow ();
  95. Assert.IsTrue (ExecutionContext.IsFlowSuppressed (), "IsFlowSuppressed-2");
  96. afc.Undo ();
  97. Assert.IsFalse (ExecutionContext.IsFlowSuppressed (), "IsFlowSuppressed-3");
  98. }
  99. [Test]
  100. [ExpectedException (typeof (InvalidOperationException))]
  101. public void RestoreFlow_None ()
  102. {
  103. ExecutionContext.RestoreFlow ();
  104. }
  105. [Test]
  106. public void RestoreFlow_SuppressFlow ()
  107. {
  108. Assert.IsFalse (ExecutionContext.IsFlowSuppressed (), "IsFlowSuppressed-1");
  109. ExecutionContext.SuppressFlow ();
  110. Assert.IsTrue (ExecutionContext.IsFlowSuppressed (), "IsFlowSuppressed-2");
  111. ExecutionContext.RestoreFlow ();
  112. Assert.IsFalse (ExecutionContext.IsFlowSuppressed (), "IsFlowSuppressed-3");
  113. }
  114. [Test]
  115. [Category ("CAS")] // since r60298 the SecurityContext is only captured if the security manager is active
  116. public void Run () // see bug #78306 for details
  117. {
  118. Assert.IsFalse (success, "pre-check");
  119. ExecutionContext.Run (ExecutionContext.Capture (), new ContextCallback (Callback), true);
  120. Assert.IsTrue (success, "post-check");
  121. }
  122. [Test]
  123. [ExpectedException (typeof (InvalidOperationException))]
  124. public void Run_SuppressFlow ()
  125. {
  126. Assert.IsFalse (ExecutionContext.IsFlowSuppressed ());
  127. AsyncFlowControl afc = ExecutionContext.SuppressFlow ();
  128. Assert.IsTrue (ExecutionContext.IsFlowSuppressed ());
  129. try {
  130. ExecutionContext.Run (ExecutionContext.Capture (), new ContextCallback (Callback), "Hello world.");
  131. }
  132. finally {
  133. afc.Undo ();
  134. }
  135. }
  136. [Test]
  137. public void SuppressFlow ()
  138. {
  139. Assert.IsFalse (ExecutionContext.IsFlowSuppressed (), "IsFlowSuppressed-1");
  140. AsyncFlowControl afc = ExecutionContext.SuppressFlow ();
  141. Assert.IsTrue (ExecutionContext.IsFlowSuppressed (), "IsFlowSuppressed-3");
  142. afc.Undo ();
  143. Assert.IsFalse (ExecutionContext.IsFlowSuppressed (), "IsFlowSuppressed-4");
  144. }
  145. [Test]
  146. [ExpectedException (typeof (InvalidOperationException))]
  147. public void SuppressFlow_Two_Undo ()
  148. {
  149. Assert.IsFalse (ExecutionContext.IsFlowSuppressed (), "IsFlowSuppressed-1");
  150. AsyncFlowControl afc = ExecutionContext.SuppressFlow ();
  151. Assert.IsTrue (ExecutionContext.IsFlowSuppressed (), "IsFlowSuppressed-2");
  152. AsyncFlowControl afc2 = ExecutionContext.SuppressFlow ();
  153. Assert.IsTrue (ExecutionContext.IsFlowSuppressed (), "IsFlowSuppressed-3");
  154. afc2.Undo ();
  155. // note: afc2 Undo return to the original (not the previous) state
  156. Assert.IsFalse (ExecutionContext.IsFlowSuppressed (), "IsFlowSuppressed-4");
  157. // we can't use the first AsyncFlowControl
  158. afc.Undo ();
  159. }
  160. }
  161. }
  162. #endif