ExecutionContextTest.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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. public void Copy_FromThread ()
  83. {
  84. ExecutionContext ec = Thread.CurrentThread.ExecutionContext;
  85. Assert.IsNotNull (ec, "Thread.CurrentThread.ExecutionContext");
  86. ExecutionContext copy = ec.CreateCopy ();
  87. }
  88. [Test]
  89. public void IsFlowSuppressed ()
  90. {
  91. Assert.IsFalse (ExecutionContext.IsFlowSuppressed (), "IsFlowSuppressed-1");
  92. AsyncFlowControl afc = ExecutionContext.SuppressFlow ();
  93. Assert.IsTrue (ExecutionContext.IsFlowSuppressed (), "IsFlowSuppressed-2");
  94. afc.Undo ();
  95. Assert.IsFalse (ExecutionContext.IsFlowSuppressed (), "IsFlowSuppressed-3");
  96. }
  97. [Test]
  98. [ExpectedException (typeof (InvalidOperationException))]
  99. public void RestoreFlow_None ()
  100. {
  101. ExecutionContext.RestoreFlow ();
  102. }
  103. [Test]
  104. public void RestoreFlow_SuppressFlow ()
  105. {
  106. Assert.IsFalse (ExecutionContext.IsFlowSuppressed (), "IsFlowSuppressed-1");
  107. ExecutionContext.SuppressFlow ();
  108. Assert.IsTrue (ExecutionContext.IsFlowSuppressed (), "IsFlowSuppressed-2");
  109. ExecutionContext.RestoreFlow ();
  110. Assert.IsFalse (ExecutionContext.IsFlowSuppressed (), "IsFlowSuppressed-3");
  111. }
  112. [Test]
  113. public void Run ()
  114. {
  115. Assert.IsFalse (success, "pre-check");
  116. ExecutionContext.Run (ExecutionContext.Capture (), new ContextCallback (Callback), true);
  117. Assert.IsTrue (success, "post-check");
  118. }
  119. [Test]
  120. [ExpectedException (typeof (InvalidOperationException))]
  121. public void Run_SuppressFlow ()
  122. {
  123. Assert.IsFalse (ExecutionContext.IsFlowSuppressed ());
  124. AsyncFlowControl afc = ExecutionContext.SuppressFlow ();
  125. Assert.IsTrue (ExecutionContext.IsFlowSuppressed ());
  126. try {
  127. ExecutionContext.Run (ExecutionContext.Capture (), new ContextCallback (Callback), "Hello world.");
  128. }
  129. finally {
  130. afc.Undo ();
  131. }
  132. }
  133. [Test]
  134. public void SuppressFlow ()
  135. {
  136. Assert.IsFalse (ExecutionContext.IsFlowSuppressed (), "IsFlowSuppressed-1");
  137. AsyncFlowControl afc = ExecutionContext.SuppressFlow ();
  138. Assert.IsTrue (ExecutionContext.IsFlowSuppressed (), "IsFlowSuppressed-3");
  139. afc.Undo ();
  140. Assert.IsFalse (ExecutionContext.IsFlowSuppressed (), "IsFlowSuppressed-4");
  141. }
  142. [Test]
  143. [ExpectedException (typeof (InvalidOperationException))]
  144. public void SuppressFlow_Two_Undo ()
  145. {
  146. Assert.IsFalse (ExecutionContext.IsFlowSuppressed (), "IsFlowSuppressed-1");
  147. AsyncFlowControl afc = ExecutionContext.SuppressFlow ();
  148. Assert.IsTrue (ExecutionContext.IsFlowSuppressed (), "IsFlowSuppressed-2");
  149. AsyncFlowControl afc2 = ExecutionContext.SuppressFlow ();
  150. Assert.IsTrue (ExecutionContext.IsFlowSuppressed (), "IsFlowSuppressed-3");
  151. afc2.Undo ();
  152. // note: afc2 Undo return to the original (not the previous) state
  153. Assert.IsFalse (ExecutionContext.IsFlowSuppressed (), "IsFlowSuppressed-4");
  154. // we can't use the first AsyncFlowControl
  155. afc.Undo ();
  156. }
  157. }
  158. }
  159. #endif