ThreadLocalTests.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. //
  2. // ThreadLazyTests.cs
  3. //
  4. // Author:
  5. // Jérémie "Garuma" Laval <[email protected]>
  6. //
  7. // Copyright (c) 2009 Jérémie "Garuma" Laval
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining a copy
  10. // of this software and associated documentation files (the "Software"), to deal
  11. // in the Software without restriction, including without limitation the rights
  12. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. // copies of the Software, and to permit persons to whom the Software is
  14. // furnished to do so, subject to the following conditions:
  15. //
  16. // The above copyright notice and this permission notice shall be included in
  17. // all copies or substantial portions of the Software.
  18. //
  19. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. // THE SOFTWARE.
  26. using System;
  27. using System.Threading;
  28. using NUnit;
  29. using NUnit.Framework;
  30. namespace MonoTests.System.Threading
  31. {
  32. [TestFixtureAttribute]
  33. public class ThreadLocalTests
  34. {
  35. ThreadLocal<int> threadLocal;
  36. int nTimes;
  37. [SetUp]
  38. public void Setup ()
  39. {
  40. nTimes = 0;
  41. threadLocal = new ThreadLocal<int> (() => { Interlocked.Increment (ref nTimes); return 42; });
  42. }
  43. [Test]
  44. public void SingleThreadTest ()
  45. {
  46. AssertThreadLocal ();
  47. }
  48. [Test]
  49. public void ThreadedTest ()
  50. {
  51. AssertThreadLocal ();
  52. Thread t = new Thread ((object o) => { Interlocked.Decrement (ref nTimes); AssertThreadLocal (); });
  53. t.Start ();
  54. t.Join ();
  55. }
  56. [Test]
  57. public void InitializeThrowingTest ()
  58. {
  59. int callTime = 0;
  60. threadLocal = new ThreadLocal<int> (() => {
  61. Interlocked.Increment (ref callTime);
  62. throw new ApplicationException ("foo");
  63. });
  64. Exception exception = null;
  65. try {
  66. var foo = threadLocal.Value;
  67. } catch (Exception e) {
  68. exception = e;
  69. }
  70. Assert.IsNotNull (exception, "#1");
  71. Assert.That (exception, Is.TypeOf (typeof (ApplicationException)), "#2");
  72. Assert.AreEqual (1, callTime, "#3");
  73. exception = null;
  74. try {
  75. var foo = threadLocal.Value;
  76. } catch (Exception e) {
  77. exception = e;
  78. }
  79. Assert.IsNotNull (exception, "#4");
  80. Assert.That (exception, Is.TypeOf (typeof (ApplicationException)), "#5");
  81. Assert.AreEqual (2, callTime, "#6");
  82. }
  83. [Category ("NotDotNet")] // nunit results in stack overflow
  84. public void MultipleReferenceToValueTest ()
  85. {
  86. try {
  87. threadLocal = new ThreadLocal<int> (() => threadLocal.Value + 1);
  88. var v = threadLocal.Value;
  89. Assert.Fail ("#1");
  90. } catch (InvalidOperationException e) {
  91. }
  92. }
  93. [Test]
  94. public void DefaultThreadLocalInitTest ()
  95. {
  96. var local = new ThreadLocal<DateTime> ();
  97. var local2 = new ThreadLocal<object> ();
  98. Assert.AreEqual (default (DateTime), local.Value);
  99. Assert.AreEqual (default (object), local2.Value);
  100. }
  101. [Test, ExpectedException (typeof (ObjectDisposedException))]
  102. public void DisposedOnValueTest ()
  103. {
  104. var tl = new ThreadLocal<int> ();
  105. tl.Dispose ();
  106. var value = tl.Value;
  107. }
  108. [Test, ExpectedException (typeof (ObjectDisposedException))]
  109. public void DisposedOnIsValueCreatedTest ()
  110. {
  111. var tl = new ThreadLocal<int> ();
  112. tl.Dispose ();
  113. var value = tl.IsValueCreated;
  114. }
  115. [Test]
  116. public void PerThreadException ()
  117. {
  118. int callTime = 0;
  119. threadLocal = new ThreadLocal<int> (() => {
  120. if (callTime == 1)
  121. throw new ApplicationException ("foo");
  122. Interlocked.Increment (ref callTime);
  123. return 43;
  124. });
  125. Exception exception = null;
  126. var foo = threadLocal.Value;
  127. bool thread_value_created = false;
  128. Assert.AreEqual (43, foo, "#3");
  129. Thread t = new Thread ((object o) => {
  130. try {
  131. var foo2 = threadLocal.Value;
  132. } catch (Exception e) {
  133. exception = e;
  134. }
  135. // should be false and not throw
  136. thread_value_created = threadLocal.IsValueCreated;
  137. });
  138. t.Start ();
  139. t.Join ();
  140. Assert.AreEqual (false, thread_value_created, "#4");
  141. Assert.IsNotNull (exception, "#5");
  142. Assert.That (exception, Is.TypeOf (typeof (ApplicationException)), "#6");
  143. }
  144. void AssertThreadLocal ()
  145. {
  146. Assert.IsFalse (threadLocal.IsValueCreated, "#1");
  147. Assert.AreEqual (42, threadLocal.Value, "#2");
  148. Assert.IsTrue (threadLocal.IsValueCreated, "#3");
  149. Assert.AreEqual (42, threadLocal.Value, "#4");
  150. Assert.AreEqual (1, nTimes, "#5");
  151. }
  152. class SetMreOnFinalize
  153. {
  154. ManualResetEventSlim m_mres;
  155. public SetMreOnFinalize (ManualResetEventSlim mres)
  156. {
  157. m_mres = mres;
  158. }
  159. ~SetMreOnFinalize()
  160. {
  161. m_mres.Set();
  162. }
  163. }
  164. [Test]
  165. [Category ("NotWorking")] // Finalizers aren't guaranteed
  166. #if MONOTOUCH
  167. [Category ("NotWorking")] // https://bugzilla.xamarin.com/show_bug.cgi?id=34617
  168. #endif
  169. public void DisposeOnThreadExit ()
  170. {
  171. var threadLocal = new ThreadLocal<SetMreOnFinalize>();
  172. var mres = new ManualResetEventSlim(false);
  173. var thread = new Thread (() => { threadLocal.Value = new SetMreOnFinalize (mres); });
  174. thread.Start ();
  175. thread.Join ();
  176. SpinWait.SpinUntil (() => {
  177. GC.Collect();
  178. GC.WaitForPendingFinalizers();
  179. GC.Collect();
  180. return mres.IsSet;
  181. }, 500);
  182. if (!mres.IsSet)
  183. Assert.Fail ("Finalizer didn't run after thread termination");
  184. }
  185. }
  186. }