5 // Jérémie "Garuma" Laval <jeremie.laval@gmail.com>
7 // Copyright (c) 2009 Jérémie "Garuma" Laval
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:
16 // The above copyright notice and this permission notice shall be included in
17 // all copies or substantial portions of the Software.
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
28 using System.Threading;
31 using NUnit.Framework;
33 using NUnit.Framework.SyntaxHelpers;
36 namespace MonoTests.System.Threading
38 [TestFixtureAttribute]
39 public class ThreadLocalTests
41 ThreadLocal<int> threadLocal;
48 threadLocal = new ThreadLocal<int> (() => { Interlocked.Increment (ref nTimes); return 42; });
52 public void SingleThreadTest ()
58 public void ThreadedTest ()
62 Thread t = new Thread ((object o) => { Interlocked.Decrement (ref nTimes); AssertThreadLocal (); });
68 public void InitializeThrowingTest ()
71 threadLocal = new ThreadLocal<int> (() => {
72 Interlocked.Increment (ref callTime);
73 throw new ApplicationException ("foo");
76 Exception exception = null;
79 var foo = threadLocal.Value;
80 } catch (Exception e) {
84 Assert.IsNotNull (exception, "#1");
85 Assert.That (exception, Is.TypeOf (typeof (ApplicationException)), "#2");
86 Assert.AreEqual (1, callTime, "#3");
91 var foo = threadLocal.Value;
92 } catch (Exception e) {
96 Assert.IsNotNull (exception, "#4");
97 Assert.That (exception, Is.TypeOf (typeof (ApplicationException)), "#5");
98 Assert.AreEqual (2, callTime, "#6");
101 [Category ("NotDotNet")] // nunit results in stack overflow
102 public void MultipleReferenceToValueTest ()
105 threadLocal = new ThreadLocal<int> (() => threadLocal.Value + 1);
106 var v = threadLocal.Value;
109 } catch (InvalidOperationException e) {
114 public void DefaultThreadLocalInitTest ()
116 var local = new ThreadLocal<DateTime> ();
117 var local2 = new ThreadLocal<object> ();
119 Assert.AreEqual (default (DateTime), local.Value);
120 Assert.AreEqual (default (object), local2.Value);
123 [Test, ExpectedException (typeof (ObjectDisposedException))]
124 public void DisposedOnValueTest ()
126 var tl = new ThreadLocal<int> ();
128 var value = tl.Value;
131 [Test, ExpectedException (typeof (ObjectDisposedException))]
132 public void DisposedOnIsValueCreatedTest ()
134 var tl = new ThreadLocal<int> ();
136 var value = tl.IsValueCreated;
140 public void PerThreadException ()
143 threadLocal = new ThreadLocal<int> (() => {
145 throw new ApplicationException ("foo");
146 Interlocked.Increment (ref callTime);
150 Exception exception = null;
152 var foo = threadLocal.Value;
153 bool thread_value_created = false;
154 Assert.AreEqual (43, foo, "#3");
155 Thread t = new Thread ((object o) => {
157 var foo2 = threadLocal.Value;
158 } catch (Exception e) {
161 // should be false and not throw
162 thread_value_created = threadLocal.IsValueCreated;
166 Assert.AreEqual (false, thread_value_created, "#4");
167 Assert.IsNotNull (exception, "#5");
168 Assert.That (exception, Is.TypeOf (typeof (ApplicationException)), "#6");
171 void AssertThreadLocal ()
173 Assert.IsFalse (threadLocal.IsValueCreated, "#1");
174 Assert.AreEqual (42, threadLocal.Value, "#2");
175 Assert.IsTrue (threadLocal.IsValueCreated, "#3");
176 Assert.AreEqual (42, threadLocal.Value, "#4");
177 Assert.AreEqual (1, nTimes, "#5");
180 class SetMreOnFinalize
182 ManualResetEventSlim m_mres;
184 public SetMreOnFinalize (ManualResetEventSlim mres)
196 public void DisposeOnThreadExit ()
198 var threadLocal = new ThreadLocal<SetMreOnFinalize>();
199 var mres = new ManualResetEventSlim(false);
200 var thread = new Thread (() => { threadLocal.Value = new SetMreOnFinalize (mres); });
205 SpinWait.SpinUntil (() => {
207 GC.WaitForPendingFinalizers();