[MSBuild] Fix minor assembly resolution issue
[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 (1, callTime, "#6");
100                 }
101
102                 [Test, ExpectedException (typeof (InvalidOperationException))]
103                 [Category ("NotDotNet")] // nunit results in stack overflow
104                 public void MultipleReferenceToValueTest ()
105                 {
106                         threadLocal = new ThreadLocal<int> (() => threadLocal.Value + 1);
107
108                         var value = threadLocal.Value;
109                 }
110
111                 [Test]
112                 public void DefaultThreadLocalInitTest ()
113                 {
114                         var local = new ThreadLocal<DateTime> ();
115                         var local2 = new ThreadLocal<object> ();
116
117                         Assert.AreEqual (default (DateTime), local.Value);
118                         Assert.AreEqual (default (object), local2.Value);
119                 }
120
121                 [Test, ExpectedException (typeof (ObjectDisposedException))]
122                 public void DisposedOnValueTest ()
123                 {
124                         var tl = new ThreadLocal<int> ();
125                         tl.Dispose ();
126                         var value = tl.Value;
127                 }
128
129                 [Test, ExpectedException (typeof (ObjectDisposedException))]
130                 public void DisposedOnIsValueCreatedTest ()
131                 {
132                         var tl = new ThreadLocal<int> ();
133                         tl.Dispose ();
134                         var value = tl.IsValueCreated;
135                 }
136
137                 [Test]
138                 public void PerThreadException ()
139                 {
140                         int callTime = 0;
141                         threadLocal = new ThreadLocal<int> (() => {
142                                         if (callTime == 1)
143                                                 throw new ApplicationException ("foo");
144                                         Interlocked.Increment (ref callTime);
145                                         return 43;
146                                 });
147
148                         Exception exception = null;
149
150                         var foo = threadLocal.Value;
151                         bool thread_value_created = false;
152                         Assert.AreEqual (43, foo, "#3");
153                         Thread t = new Thread ((object o) => {
154                                 try {
155                                         var foo2 = threadLocal.Value;
156                                 } catch (Exception e) {
157                                         exception = e;
158                                 }
159                                 // should be false and not throw
160                                 thread_value_created = threadLocal.IsValueCreated;
161                         });
162                         t.Start ();
163                         t.Join ();
164                         Assert.AreEqual (false, thread_value_created, "#4");
165                         Assert.IsNotNull (exception, "#5");
166                         Assert.That (exception, Is.TypeOf (typeof (ApplicationException)), "#6");
167                 }
168
169                 void AssertThreadLocal ()
170                 {
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");
176                 }
177         }
178 }
179 #endif