Merge pull request #1857 from slluis/fix-assembly-resolver
[mono.git] / mcs / class / corlib / Test / System.Threading / ThreadLocalTests.cs
1 #if NET_4_0
2 // 
3 // ThreadLazyTests.cs
4 //  
5 // Author:
6 //       Jérémie "Garuma" Laval <jeremie.laval@gmail.com>
7 // 
8 // Copyright (c) 2009 Jérémie "Garuma" Laval
9 // 
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:
16 // 
17 // The above copyright notice and this permission notice shall be included in
18 // all copies or substantial portions of the Software.
19 // 
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
26 // THE SOFTWARE.
27
28 using System;
29 using System.Threading;
30
31 using NUnit;
32 using NUnit.Framework;
33 #if !MOBILE
34 using NUnit.Framework.SyntaxHelpers;
35 #endif
36
37 namespace MonoTests.System.Threading
38 {
39         [TestFixtureAttribute]
40         public class ThreadLocalTests
41         {
42                 ThreadLocal<int> threadLocal;
43                 int nTimes;
44                 
45                 [SetUp]
46                 public void Setup ()
47                 {
48                         nTimes = 0;
49                         threadLocal = new ThreadLocal<int> (() => { Interlocked.Increment (ref nTimes); return 42; });
50                 }
51
52                 [Test]
53                 public void SingleThreadTest ()
54                 {
55                         AssertThreadLocal ();
56                 }
57                 
58                 [Test]
59                 public void ThreadedTest ()
60                 {
61                         AssertThreadLocal ();
62                         
63                         Thread t = new Thread ((object o) => { Interlocked.Decrement (ref nTimes); AssertThreadLocal (); });
64                         t.Start ();
65                         t.Join ();
66                 }
67
68                 [Test]
69                 public void InitializeThrowingTest ()
70                 {
71                         int callTime = 0;
72                         threadLocal = new ThreadLocal<int> (() => {
73                                         Interlocked.Increment (ref callTime);
74                                         throw new ApplicationException ("foo");
75                                 });
76
77                         Exception exception = null;
78
79                         try {
80                                 var foo = threadLocal.Value;
81                         } catch (Exception e) {
82                                 exception = e;
83                         }
84
85                         Assert.IsNotNull (exception, "#1");
86                         Assert.That (exception, Is.TypeOf (typeof (ApplicationException)), "#2");
87                         Assert.AreEqual (1, callTime, "#3");
88
89                         exception = null;
90
91                         try {
92                                 var foo = threadLocal.Value;
93                         } catch (Exception e) {
94                                 exception = e;
95                         }
96
97                         Assert.IsNotNull (exception, "#4");
98                         Assert.That (exception, Is.TypeOf (typeof (ApplicationException)), "#5");
99                         Assert.AreEqual (2, callTime, "#6");
100                 }
101
102                 [Category ("NotDotNet")] // nunit results in stack overflow
103                 public void MultipleReferenceToValueTest ()
104                 {
105                         try {
106                                 threadLocal = new ThreadLocal<int> (() => threadLocal.Value + 1);
107                                 var v = threadLocal.Value;
108
109                                 Assert.Fail ("#1");
110                         } catch (InvalidOperationException e) {
111                         }
112                 }
113
114                 [Test]
115                 public void DefaultThreadLocalInitTest ()
116                 {
117                         var local = new ThreadLocal<DateTime> ();
118                         var local2 = new ThreadLocal<object> ();
119
120                         Assert.AreEqual (default (DateTime), local.Value);
121                         Assert.AreEqual (default (object), local2.Value);
122                 }
123
124                 [Test, ExpectedException (typeof (ObjectDisposedException))]
125                 public void DisposedOnValueTest ()
126                 {
127                         var tl = new ThreadLocal<int> ();
128                         tl.Dispose ();
129                         var value = tl.Value;
130                 }
131
132                 [Test, ExpectedException (typeof (ObjectDisposedException))]
133                 public void DisposedOnIsValueCreatedTest ()
134                 {
135                         var tl = new ThreadLocal<int> ();
136                         tl.Dispose ();
137                         var value = tl.IsValueCreated;
138                 }
139
140                 [Test]
141                 public void PerThreadException ()
142                 {
143                         int callTime = 0;
144                         threadLocal = new ThreadLocal<int> (() => {
145                                         if (callTime == 1)
146                                                 throw new ApplicationException ("foo");
147                                         Interlocked.Increment (ref callTime);
148                                         return 43;
149                                 });
150
151                         Exception exception = null;
152
153                         var foo = threadLocal.Value;
154                         bool thread_value_created = false;
155                         Assert.AreEqual (43, foo, "#3");
156                         Thread t = new Thread ((object o) => {
157                                 try {
158                                         var foo2 = threadLocal.Value;
159                                 } catch (Exception e) {
160                                         exception = e;
161                                 }
162                                 // should be false and not throw
163                                 thread_value_created = threadLocal.IsValueCreated;
164                         });
165                         t.Start ();
166                         t.Join ();
167                         Assert.AreEqual (false, thread_value_created, "#4");
168                         Assert.IsNotNull (exception, "#5");
169                         Assert.That (exception, Is.TypeOf (typeof (ApplicationException)), "#6");
170                 }
171
172                 void AssertThreadLocal ()
173                 {
174                         Assert.IsFalse (threadLocal.IsValueCreated, "#1");
175                         Assert.AreEqual (42, threadLocal.Value, "#2");
176                         Assert.IsTrue (threadLocal.IsValueCreated, "#3");
177                         Assert.AreEqual (42, threadLocal.Value, "#4");
178                         Assert.AreEqual (1, nTimes, "#5");
179                 }
180
181                 class SetMreOnFinalize
182                 {
183                         ManualResetEventSlim m_mres;
184
185                         public SetMreOnFinalize (ManualResetEventSlim mres)
186                         {
187                                 m_mres = mres;
188                         }
189
190                         ~SetMreOnFinalize()
191                         {
192                                 m_mres.Set();
193                         }
194                 }
195
196                 [Test]
197                 public void DisposeOnThreadExit ()
198                 {
199                         var threadLocal = new ThreadLocal<SetMreOnFinalize>();
200                         var mres = new ManualResetEventSlim(false);
201                         var thread = new Thread (() => { threadLocal.Value = new SetMreOnFinalize (mres); });
202
203                         thread.Start ();
204                         thread.Join ();
205
206                         SpinWait.SpinUntil (() => {
207                                 GC.Collect();
208                                 GC.WaitForPendingFinalizers();
209                                 GC.Collect();
210                                 return mres.IsSet;
211                         }, 500);
212
213                         if (!mres.IsSet)
214                                 Assert.Fail ();
215                 }
216         }
217 }
218 #endif