6 // Jérémie "Garuma" Laval <jeremie.laval@gmail.com>
8 // Copyright (c) 2009 Jérémie "Garuma" Laval
10 // Permission is hereby granted, free of charge, to any person obtaining a copy
11 // of this software and associated documentation files (the "Software"), to deal
12 // in the Software without restriction, including without limitation the rights
13 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14 // copies of the Software, and to permit persons to whom the Software is
15 // furnished to do so, subject to the following conditions:
17 // The above copyright notice and this permission notice shall be included in
18 // all copies or substantial portions of the Software.
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
29 using System.Threading;
32 using NUnit.Framework;
34 using NUnit.Framework.SyntaxHelpers;
37 namespace MonoTests.System.Threading
39 [TestFixtureAttribute]
40 public class ThreadLocalTests
42 ThreadLocal<int> threadLocal;
49 threadLocal = new ThreadLocal<int> (() => { Interlocked.Increment (ref nTimes); return 42; });
53 public void SingleThreadTest ()
59 public void ThreadedTest ()
63 Thread t = new Thread ((object o) => { Interlocked.Decrement (ref nTimes); AssertThreadLocal (); });
69 public void InitializeThrowingTest ()
72 threadLocal = new ThreadLocal<int> (() => {
73 Interlocked.Increment (ref callTime);
74 throw new ApplicationException ("foo");
77 Exception exception = null;
80 var foo = threadLocal.Value;
81 } catch (Exception e) {
85 Assert.IsNotNull (exception, "#1");
86 Assert.That (exception, Is.TypeOf (typeof (ApplicationException)), "#2");
87 Assert.AreEqual (1, callTime, "#3");
92 var foo = threadLocal.Value;
93 } catch (Exception e) {
97 Assert.IsNotNull (exception, "#4");
98 Assert.That (exception, Is.TypeOf (typeof (ApplicationException)), "#5");
99 Assert.AreEqual (1, callTime, "#6");
102 [Test, ExpectedException (typeof (InvalidOperationException))]
103 [Category ("NotDotNet")] // nunit results in stack overflow
104 public void MultipleReferenceToValueTest ()
106 threadLocal = new ThreadLocal<int> (() => threadLocal.Value + 1);
108 var value = threadLocal.Value;
112 public void DefaultThreadLocalInitTest ()
114 var local = new ThreadLocal<DateTime> ();
115 var local2 = new ThreadLocal<object> ();
117 Assert.AreEqual (default (DateTime), local.Value);
118 Assert.AreEqual (default (object), local2.Value);
121 [Test, ExpectedException (typeof (ObjectDisposedException))]
122 public void DisposedOnValueTest ()
124 var tl = new ThreadLocal<int> ();
126 var value = tl.Value;
129 [Test, ExpectedException (typeof (ObjectDisposedException))]
130 public void DisposedOnIsValueCreatedTest ()
132 var tl = new ThreadLocal<int> ();
134 var value = tl.IsValueCreated;
138 public void PerThreadException ()
141 threadLocal = new ThreadLocal<int> (() => {
143 throw new ApplicationException ("foo");
144 Interlocked.Increment (ref callTime);
148 Exception exception = null;
150 var foo = threadLocal.Value;
151 bool thread_value_created = false;
152 Assert.AreEqual (43, foo, "#3");
153 Thread t = new Thread ((object o) => {
155 var foo2 = threadLocal.Value;
156 } catch (Exception e) {
159 // should be false and not throw
160 thread_value_created = threadLocal.IsValueCreated;
164 Assert.AreEqual (false, thread_value_created, "#4");
165 Assert.IsNotNull (exception, "#5");
166 Assert.That (exception, Is.TypeOf (typeof (ApplicationException)), "#6");
169 void AssertThreadLocal ()
171 Assert.IsFalse (threadLocal.IsValueCreated, "#1");
172 Assert.AreEqual (42, threadLocal.Value, "#2");
173 Assert.IsTrue (threadLocal.IsValueCreated, "#3");
174 Assert.AreEqual (42, threadLocal.Value, "#4");
175 Assert.AreEqual (1, nTimes, "#5");