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 namespace MonoTests.System.Threading
36 [TestFixtureAttribute]
37 public class ThreadLocalTests
39 ThreadLocal<int> threadLocal;
46 threadLocal = new ThreadLocal<int> (() => { Interlocked.Increment (ref nTimes); return 42; });
50 public void SingleThreadTest ()
56 public void ThreadedTest ()
60 Thread t = new Thread ((object o) => { Interlocked.Decrement (ref nTimes); AssertThreadLocal (); });
66 public void InitializeThrowingTest ()
69 threadLocal = new ThreadLocal<int> (() => {
70 Interlocked.Increment (ref callTime);
71 throw new ApplicationException ("foo");
75 Exception exception = null;
78 var foo = threadLocal.Value;
79 } catch (Exception e) {
83 Assert.IsNotNull (exception, "#1");
84 Assert.IsInstanceOfType (typeof (ApplicationException), exception, "#2");
85 Assert.AreEqual (1, callTime, "#3");
90 var foo = threadLocal.Value;
91 } catch (Exception e) {
95 Assert.IsNotNull (exception, "#4");
96 Assert.IsInstanceOfType (typeof (ApplicationException), exception, "#5");
97 Assert.AreEqual (1, callTime, "#6");
100 [Test, ExpectedException (typeof (InvalidOperationException))]
101 [Category ("NotDotNet")] // nunit results in stack overflow
102 public void MultipleReferenceToValueTest ()
104 threadLocal = new ThreadLocal<int> (() => threadLocal.Value + 1);
106 var value = threadLocal.Value;
110 public void DefaultThreadLocalInitTest ()
112 var local = new ThreadLocal<DateTime> ();
113 var local2 = new ThreadLocal<object> ();
115 Assert.AreEqual (default (DateTime), local.Value);
116 Assert.AreEqual (default (object), local2.Value);
119 [Test, ExpectedException (typeof (ObjectDisposedException))]
120 public void DisposedOnValueTest ()
122 var tl = new ThreadLocal<int> ();
124 var value = tl.Value;
127 [Test, ExpectedException (typeof (ObjectDisposedException))]
128 public void DisposedOnIsValueCreatedTest ()
130 var tl = new ThreadLocal<int> ();
132 var value = tl.IsValueCreated;
136 public void PerThreadException ()
139 threadLocal = new ThreadLocal<int> (() => {
141 throw new ApplicationException ("foo");
142 Interlocked.Increment (ref callTime);
146 Exception exception = null;
148 var foo = threadLocal.Value;
149 bool thread_value_created = false;
150 Assert.AreEqual (43, foo, "#3");
151 Thread t = new Thread ((object o) => {
153 var foo2 = threadLocal.Value;
154 } catch (Exception e) {
157 // should be false and not throw
158 thread_value_created = threadLocal.IsValueCreated;
162 Assert.AreEqual (false, thread_value_created, "#4");
163 Assert.IsNotNull (exception, "#5");
164 Assert.IsInstanceOfType (typeof (ApplicationException), exception, "#6");
167 void AssertThreadLocal ()
169 Assert.IsFalse (threadLocal.IsValueCreated, "#1");
170 Assert.AreEqual (42, threadLocal.Value, "#2");
171 Assert.IsTrue (threadLocal.IsValueCreated, "#3");
172 Assert.AreEqual (42, threadLocal.Value, "#4");
173 Assert.AreEqual (1, nTimes, "#5");