ThreadLocalTests.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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. [Category ("MultiThreaded")]
  50. public void ThreadedTest ()
  51. {
  52. AssertThreadLocal ();
  53. Thread t = new Thread ((object o) => { Interlocked.Decrement (ref nTimes); AssertThreadLocal (); });
  54. t.Start ();
  55. t.Join ();
  56. }
  57. [Test]
  58. public void InitializeThrowingTest ()
  59. {
  60. int callTime = 0;
  61. threadLocal = new ThreadLocal<int> (() => {
  62. Interlocked.Increment (ref callTime);
  63. throw new ApplicationException ("foo");
  64. });
  65. Exception exception = null;
  66. try {
  67. var foo = threadLocal.Value;
  68. } catch (Exception e) {
  69. exception = e;
  70. }
  71. Assert.IsNotNull (exception, "#1");
  72. Assert.That (exception, Is.TypeOf (typeof (ApplicationException)), "#2");
  73. Assert.AreEqual (1, callTime, "#3");
  74. exception = null;
  75. try {
  76. var foo = threadLocal.Value;
  77. } catch (Exception e) {
  78. exception = e;
  79. }
  80. Assert.IsNotNull (exception, "#4");
  81. Assert.That (exception, Is.TypeOf (typeof (ApplicationException)), "#5");
  82. Assert.AreEqual (2, callTime, "#6");
  83. }
  84. [Category ("NotDotNet")] // nunit results in stack overflow
  85. public void MultipleReferenceToValueTest ()
  86. {
  87. try {
  88. threadLocal = new ThreadLocal<int> (() => threadLocal.Value + 1);
  89. var v = threadLocal.Value;
  90. Assert.Fail ("#1");
  91. } catch (InvalidOperationException e) {
  92. }
  93. }
  94. [Test]
  95. public void DefaultThreadLocalInitTest ()
  96. {
  97. var local = new ThreadLocal<DateTime> ();
  98. var local2 = new ThreadLocal<object> ();
  99. Assert.AreEqual (default (DateTime), local.Value);
  100. Assert.AreEqual (default (object), local2.Value);
  101. }
  102. [Test, ExpectedException (typeof (ObjectDisposedException))]
  103. public void DisposedOnValueTest ()
  104. {
  105. var tl = new ThreadLocal<int> ();
  106. tl.Dispose ();
  107. var value = tl.Value;
  108. }
  109. [Test, ExpectedException (typeof (ObjectDisposedException))]
  110. public void DisposedOnIsValueCreatedTest ()
  111. {
  112. var tl = new ThreadLocal<int> ();
  113. tl.Dispose ();
  114. var value = tl.IsValueCreated;
  115. }
  116. [Test]
  117. [Category ("MultiThreaded")]
  118. public void PerThreadException ()
  119. {
  120. int callTime = 0;
  121. threadLocal = new ThreadLocal<int> (() => {
  122. if (callTime == 1)
  123. throw new ApplicationException ("foo");
  124. Interlocked.Increment (ref callTime);
  125. return 43;
  126. });
  127. Exception exception = null;
  128. var foo = threadLocal.Value;
  129. bool thread_value_created = false;
  130. Assert.AreEqual (43, foo, "#3");
  131. Thread t = new Thread ((object o) => {
  132. try {
  133. var foo2 = threadLocal.Value;
  134. } catch (Exception e) {
  135. exception = e;
  136. }
  137. // should be false and not throw
  138. thread_value_created = threadLocal.IsValueCreated;
  139. });
  140. t.Start ();
  141. t.Join ();
  142. Assert.AreEqual (false, thread_value_created, "#4");
  143. Assert.IsNotNull (exception, "#5");
  144. Assert.That (exception, Is.TypeOf (typeof (ApplicationException)), "#6");
  145. }
  146. void AssertThreadLocal ()
  147. {
  148. Assert.IsFalse (threadLocal.IsValueCreated, "#1");
  149. Assert.AreEqual (42, threadLocal.Value, "#2");
  150. Assert.IsTrue (threadLocal.IsValueCreated, "#3");
  151. Assert.AreEqual (42, threadLocal.Value, "#4");
  152. Assert.AreEqual (1, nTimes, "#5");
  153. }
  154. class SetMreOnFinalize
  155. {
  156. ManualResetEventSlim m_mres;
  157. public SetMreOnFinalize (ManualResetEventSlim mres)
  158. {
  159. m_mres = mres;
  160. }
  161. ~SetMreOnFinalize()
  162. {
  163. m_mres.Set();
  164. }
  165. }
  166. [Test]
  167. [Category ("NotWorking")] // Finalizers aren't guaranteed
  168. #if MONOTOUCH
  169. [Category ("NotWorking")] // https://bugzilla.xamarin.com/show_bug.cgi?id=34617
  170. #endif
  171. public void DisposeOnThreadExit ()
  172. {
  173. var threadLocal = new ThreadLocal<SetMreOnFinalize>();
  174. var mres = new ManualResetEventSlim(false);
  175. var thread = new Thread (() => { threadLocal.Value = new SetMreOnFinalize (mres); });
  176. thread.Start ();
  177. thread.Join ();
  178. SpinWait.SpinUntil (() => {
  179. GC.Collect();
  180. GC.WaitForPendingFinalizers();
  181. GC.Collect();
  182. return mres.IsSet;
  183. }, 500);
  184. if (!mres.IsSet)
  185. Assert.Fail ("Finalizer didn't run after thread termination");
  186. }
  187. }
  188. }