ThreadLocalTests.cs 5.6 KB

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