Merge pull request #2454 from tastywheattasteslikechicken/FixVtableAbort
[mono.git] / mcs / class / corlib / Test / System.Threading / ThreadTest.cs
1 // ThreadTest.cs - NUnit Test Cases for the System.Threading.Thread class
2 //
3 // Authors
4 //      Eduardo Garcia Cebollero (kiwnix@yahoo.es)
5 //      Sebastien Pouliot  <sebastien@ximian.com>
6 //
7 // (C) Eduardo Garcia Cebollero.
8 // (C) Ximian, Inc.  http://www.ximian.com
9 // (C) 2004 Novell (http://www.novell.com)
10 //
11
12 using System;
13 using System.Globalization;
14 using System.Security.Principal;
15 using System.Threading;
16 using System.Reflection;
17 using System.Collections.Generic;
18 using SD = System.Diagnostics;
19
20 using NUnit.Framework;
21
22 namespace MonoTests.System.Threading
23 {
24         // These tests seem to hang the 2.0 framework. So they are disabled for now
25         // Don't reenable them until you can run a few thousand times on an SMP box.
26         [Category ("NotWorking")]
27         public class ThreadedPrincipalTest
28         {
29                 public static void NoPrincipal () 
30                 {
31                         AppDomain.CurrentDomain.SetPrincipalPolicy (PrincipalPolicy.NoPrincipal);
32                         IPrincipal p = Thread.CurrentPrincipal;
33                         Assert.IsNull (p, "#1");
34
35                         Thread.CurrentPrincipal = new GenericPrincipal (new GenericIdentity ("mono"), null);
36                         Assert.IsNotNull (Thread.CurrentPrincipal, "#2");
37
38                         Thread.CurrentPrincipal = null;
39                         Assert.IsNull (Thread.CurrentPrincipal, "#3");
40                         // in this case we can return to null
41                 }
42
43                 public static void UnauthenticatedPrincipal () 
44                 {
45                         AppDomain.CurrentDomain.SetPrincipalPolicy (PrincipalPolicy.UnauthenticatedPrincipal);
46                         IPrincipal p = Thread.CurrentPrincipal;
47                         Assert.IsNotNull (p, "#1");
48                         Assert.IsTrue ((p is GenericPrincipal), "#2");
49                         Assert.AreEqual (String.Empty, p.Identity.Name, "#3");
50                         Assert.AreEqual (String.Empty, p.Identity.AuthenticationType, "#4");
51                         Assert.IsFalse (p.Identity.IsAuthenticated, "#5");
52
53                         Thread.CurrentPrincipal = new GenericPrincipal (new GenericIdentity ("mono"), null);
54                         Assert.IsNotNull (Thread.CurrentPrincipal, "#6");
55
56                         Thread.CurrentPrincipal = null;
57                         Assert.IsNotNull (Thread.CurrentPrincipal, "#7");
58                         // in this case we can't return to null
59                 }
60
61                 public static void WindowsPrincipal () 
62                 {
63                         AppDomain.CurrentDomain.SetPrincipalPolicy (PrincipalPolicy.WindowsPrincipal);
64                         IPrincipal p = Thread.CurrentPrincipal;
65                         Assert.IsNotNull (p, "#1");
66                         Assert.IsTrue ((p is WindowsPrincipal), "#2");
67                         Assert.IsNotNull (p.Identity.Name, "#3");
68                         Assert.IsNotNull (p.Identity.AuthenticationType, "#4");
69                         Assert.IsTrue (p.Identity.IsAuthenticated, "#5");
70
71                         // note: we can switch from a WindowsPrincipal to a GenericPrincipal
72                         Thread.CurrentPrincipal = new GenericPrincipal (new GenericIdentity ("mono"), null);
73                         Assert.IsNotNull (Thread.CurrentPrincipal, "#6");
74
75                         Thread.CurrentPrincipal = null;
76                         Assert.IsNotNull (Thread.CurrentPrincipal, "#7");
77                         // in this case we can't return to null
78                 }
79
80                 public static void CopyOnNewThread ()
81                 {
82                         Assert.IsNotNull (Thread.CurrentPrincipal, "#1");
83                         Assert.AreEqual ("good", Thread.CurrentPrincipal.Identity.Name, "#2");
84                 }
85         }
86
87         [TestFixture]
88         [Category("MobileNotWorking")] // Abort #10240
89         public class ThreadTest
90         {
91                 //TimeSpan Infinite = new TimeSpan (-10000);    // -10000 ticks == -1 ms
92                 TimeSpan SmallNegative = new TimeSpan (-2);     // between 0 and -1.0 (infinite) ms
93                 TimeSpan Negative = new TimeSpan (-20000);      // really negative
94                 //TimeSpan MaxValue = TimeSpan.FromMilliseconds ((long) Int32.MaxValue);
95                 TimeSpan TooLarge = TimeSpan.FromMilliseconds ((long) Int32.MaxValue + 1);
96
97                 static bool is_win32;
98                 static bool is_mono;
99
100                 static ThreadTest ()
101                 {
102                         switch (Environment.OSVersion.Platform) {
103                         case PlatformID.Win32NT:
104                         case PlatformID.Win32S:
105                         case PlatformID.Win32Windows:
106                         case PlatformID.WinCE:
107                                 is_win32 = true;
108                                 break;
109                         }
110
111                         // check a class in mscorlib to determine if we're running on Mono
112                         if (Type.GetType ("System.MonoType", false) != null)
113                                 is_mono = true;
114                 }
115
116                 //Some Classes to test as threads
117                 private class C1Test
118                 {
119                         public int cnt;
120                         public Thread thread1;
121                         public bool endm1;
122                         public bool endm2;
123
124                         public C1Test()
125                         {
126                                 thread1 = (Thread)null;
127                                 this.cnt = 0;
128                                 endm1 = endm2 = false;
129                         }
130                         
131                         public void TestMethod()
132                         {
133                                 while (cnt < 10)
134                                 {
135                                         cnt++;
136                                 }
137                                 endm1 = true;
138                         }
139                         public void TestMethod2()
140                         {
141                                 if (!(thread1==(Thread)null) )
142                                 {
143                                         thread1.Join();
144                                 }
145                                 endm2 = true;
146                         }
147                 }
148
149                 private class C2Test
150                 {
151                         public int cnt;
152                         public bool run = false;
153                         
154                         public C2Test()
155                         {
156                                 this.cnt = 0;
157                         }
158
159                         public void TestMethod()
160                         {
161                                 run = true;
162                                 while (true)
163                                 {
164                                         if (cnt < 1000)
165                                                 cnt++;
166                                         else
167                                                 cnt = 0;
168                                 }
169                         }
170                 }
171                 
172                 private class C3Test
173                 {
174                         public C1Test sub_class;
175                         public Thread sub_thread;
176
177                         public C3Test()
178                         {
179                                 sub_class = new C1Test();
180                                 sub_thread = new Thread(new ThreadStart(sub_class.TestMethod));
181                         }
182
183                         public void TestMethod1()
184                         {
185                                 sub_thread.Start();
186                                 Thread.Sleep (100);
187 #if MONO_FEATURE_THREAD_ABORT
188                                 sub_thread.Abort();
189 #else
190                                 sub_thread.Interrupt ();
191 #endif
192                         }
193                 }
194                 
195                 private class C4Test
196                 {
197                         public C1Test class1;
198                         public C1Test class2;
199                         public Thread thread1;
200                         public Thread thread2;
201                         public bool T1ON ;
202                         public bool T2ON ;
203
204                         public C4Test()
205                         {
206                                 T1ON = false;
207                                 T2ON = false;
208                                 class1 = new C1Test();
209                                 class2 = new C1Test();
210                                 thread1 = new Thread(new ThreadStart(class1.TestMethod));
211                                 thread2 = new Thread(new ThreadStart(class2.TestMethod));
212                         }
213
214                         public void TestMethod1()
215                         {
216                                 thread1.Start();
217                                 TestUtil.WaitForAlive (thread1, "wait1");
218                                 T1ON = true;
219                                 thread2.Start();
220                                 TestUtil.WaitForAlive (thread2, "wait2");
221                                 T2ON = true;
222 #if MONO_FEATURE_THREAD_ABORT
223                                 thread1.Abort();
224 #else
225                                 thread1.Interrupt ();
226 #endif
227                                 TestUtil.WaitForNotAlive (thread1, "wait3");
228                                 T1ON = false;
229 #if MONO_FEATURE_THREAD_ABORT
230                                 thread2.Abort();
231 #else
232                                 thread2.Interrupt ();
233 #endif
234                                 TestUtil.WaitForNotAlive (thread2, "wait4");
235                                 T2ON = false;
236                         }
237                         
238                         public void TestMethod2()
239                         {
240                                 thread1.Start();
241                                 thread1.Join();
242                         }
243                 }
244
245                 [Test]
246                 public void TestCtor1()
247                 {
248                         C1Test test1 = new C1Test();
249                         Thread t = new Thread (new ThreadStart (test1.TestMethod));
250
251                         Assert.IsTrue (t.CurrentCulture.IsReadOnly, "CurrentCulture.IsReadOnly");
252                         Assert.IsFalse (t.IsAlive, "IsAlive");
253                         Assert.IsFalse (t.IsBackground, "IsBackground");
254                         Assert.IsNull (t.Name, "Name");
255                         Assert.AreEqual (ThreadState.Unstarted, t.ThreadState, "ThreadState");
256                 }
257
258                 [Test]
259                 [Category ("NotWorking")] // we're not sharing (read-only) CultureInfo
260                 public void CultureInfo_Shared_Across_Threads ()
261                 {
262                         Thread t = new Thread (TestCtor1);
263                         Assert.AreSame (t.CurrentCulture, t.CurrentUICulture, "Culture");
264
265                         Assert.AreSame (t.CurrentCulture, CultureInfo.CurrentCulture, "CultureInfo.CurrentCulture");
266                         Assert.AreSame (t.CurrentUICulture, CultureInfo.CurrentUICulture, "CultureInfo.CurrentUICulture");
267
268                         Assert.AreSame (t.CurrentCulture, Thread.CurrentThread.CurrentCulture, "Thread.CurrentThread.CurrentCulture");
269                         Assert.AreSame (t.CurrentUICulture, Thread.CurrentThread.CurrentUICulture, "Thread.CurrentThread.CurrentUICulture");
270                 }
271
272                 [Test] // bug #325566
273                 public void GetHashCodeTest ()
274                 {
275                         C1Test test1 = new C1Test ();
276                         Thread tA = new Thread (new ThreadStart (test1.TestMethod));
277                         int hA1 = tA.GetHashCode ();
278                         Assert.IsTrue (hA1 > 0, "#A1");
279                         tA.Start ();
280                         int hA2 = tA.GetHashCode ();
281                         Assert.AreEqual (hA1, hA2, "#A2");
282                         tA.Join ();
283                         int hA3 = tA.GetHashCode ();
284                         Assert.AreEqual (hA1, hA3, "#A3");
285                         Assert.AreEqual (hA1, tA.ManagedThreadId, "#A4");
286
287                         test1 = new C1Test ();
288                         Thread tB = new Thread (new ThreadStart (test1.TestMethod));
289                         int hB1 = tB.GetHashCode ();
290                         Assert.IsTrue (hB1 > 0, "#B1");
291                         tB.Start ();
292                         int hB2 = tB.GetHashCode ();
293                         Assert.AreEqual (hB1, hB2, "#B2");
294                         tB.Join ();
295                         int hB3 = tB.GetHashCode ();
296                         Assert.AreEqual (hB1, hB3, "#B3");
297                         Assert.AreEqual (hB1, tB.ManagedThreadId, "#B4");
298                         Assert.IsFalse (hA2 == hB2, "#B5");
299                 }
300
301                 [Test] // bug #82700
302                 public void ManagedThreadId ()
303                 {
304                         C1Test test1 = new C1Test ();
305                         Thread t1 = new Thread (new ThreadStart (test1.TestMethod));
306                         int mtA1 = t1.ManagedThreadId;
307                         t1.Start ();
308                         int mtA2 = t1.ManagedThreadId;
309                         t1.Join ();
310                         int mtA3 = t1.ManagedThreadId;
311                         Assert.AreEqual (mtA1, mtA2, "#A1");
312                         Assert.AreEqual (mtA2, mtA3, "#A2");
313
314                         test1 = new C1Test ();
315                         Thread t2 = new Thread (new ThreadStart (test1.TestMethod));
316                         int mtB1 = t2.ManagedThreadId;
317                         t2.Start ();
318                         int mtB2 = t2.ManagedThreadId;
319                         t2.Join ();
320                         int mtB3 = t2.ManagedThreadId;
321                         Assert.AreEqual (mtB1, mtB2, "#B1");
322                         Assert.AreEqual (mtB2, mtB3, "#B2");
323                         Assert.IsFalse (mtB1 == mtA1, "#B3");
324                 }
325
326                 [Test]
327                 [Category ("NotDotNet")] // it hangs.
328                 public void TestStart()
329                 {
330                         if (is_win32 && is_mono)
331                                 Assert.Fail ("This test fails on Win32. The test should be fixed.");
332                 {
333                         C1Test test1 = new C1Test();
334                         Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
335                         TestThread.Start();
336                         TestThread.Join();
337                         Assert.AreEqual (10, test1.cnt, "#1");
338                 }
339                 {
340                         C2Test test1 = new C2Test();
341                         Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
342                         TestThread.Start();
343 #if MONO_FEATURE_THREAD_ABORT
344                         TestThread.Abort();
345 #else
346                         TestThread.Interrupt ();
347 #endif
348                         try {
349                                 TestThread.Start();
350                                 Assert.Fail ("#2");
351                         } catch (ThreadStateException) {
352                         }
353                 }
354                 {
355                         C2Test test1 = new C2Test();
356                         Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
357                         TestThread.Start();
358                         while (!test1.run) {
359                         }
360                         bool started = (TestThread.ThreadState == ThreadState.Running);
361                         Assert.AreEqual (started, test1.run, "#15 Thread Is not in the correct state: ");
362 #if MONO_FEATURE_THREAD_ABORT
363                         TestThread.Abort();
364 #else
365                         TestThread.Interrupt ();
366 #endif
367                 }
368                 }
369
370                 [Test]
371                 public void TestApartmentState ()
372                 {
373                         if (is_win32 && is_mono)
374                                 Assert.Fail ("This test fails on mono on win32. Our runtime should be fixed.");
375
376                         C2Test test1 = new C2Test();
377                         Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
378                         Assert.AreEqual (ApartmentState.Unknown, TestThread.ApartmentState, "#1");
379                         TestThread.Start();
380                         TestUtil.WaitForAlive (TestThread, "wait5");
381                         Assert.AreEqual (ApartmentState.MTA, TestThread.ApartmentState, "#2");
382 #if MONO_FEATURE_THREAD_ABORT
383                         TestThread.Abort();
384 #else
385                         TestThread.Interrupt ();
386 #endif
387                 }
388
389                 [Test]
390                 [Category ("NotWorking")] // setting the priority of a Thread before it is started isn't implemented in Mono yet
391                 public void TestPriority1()
392                 {
393                         if (is_win32 && is_mono)
394                                 Assert.Fail ("This test fails on mono on Win32. Our runtime should be fixed.");
395
396                         C2Test test1 = new C2Test();
397                         Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
398                         try {
399                                 TestThread.Priority=ThreadPriority.BelowNormal;
400                                 ThreadPriority before = TestThread.Priority;
401                                 Assert.AreEqual (ThreadPriority.BelowNormal, before, "#40 Unexpected priority before thread start: ");
402                                 TestThread.Start();
403                                 TestUtil.WaitForAlive (TestThread, "wait7");
404                                 ThreadPriority after = TestThread.Priority;
405                                 Assert.AreEqual (before, after, "#41 Unexpected Priority Change: ");
406                         } finally {
407 #if MONO_FEATURE_THREAD_ABORT
408                                 TestThread.Abort();
409 #else
410                                 TestThread.Interrupt ();
411 #endif
412                         }
413                 }
414
415 #if MONO_FEATURE_THREAD_ABORT
416                 [Test]
417                 [Category ("NotDotNet")] // on MS, Thread is still in AbortRequested state when Start is invoked
418                 public void AbortUnstarted ()
419                 {
420                         C2Test test1 = new C2Test();
421                         Thread th = new Thread (new ThreadStart (test1.TestMethod));
422                         th.Abort ();
423                         th.Start ();
424                 }
425 #endif
426
427                 [Test]
428                 [Category ("NotDotNet")] // on MS, ThreadState is immediately Stopped after Abort
429                 [Category ("NotWorking")] // this is a MonoTODO -> no support for Priority
430                 public void TestPriority2()
431                 {
432                         C2Test test1 = new C2Test();
433                         Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
434                         try {
435                                 Assert.AreEqual (ThreadPriority.Normal, TestThread.Priority, "#42 Incorrect Priority in New thread: ");
436                                 TestThread.Start();
437                                 TestUtil.WaitForAliveOrStop (TestThread, "wait8");
438                                 Assert.AreEqual (ThreadPriority.Normal, TestThread.Priority, "#43 Incorrect Priority in Started thread: ");
439                         } finally {
440 #if MONO_FEATURE_THREAD_ABORT
441                                 TestThread.Abort();
442 #else
443                                 TestThread.Interrupt ();
444 #endif
445                         }
446                         Assert.AreEqual (ThreadPriority.Normal, TestThread.Priority, "#44 Incorrect Priority in Aborted thread: ");
447                 }
448
449                 [Test]
450                 [Category ("NotWorking")] // this is a MonoTODO -> no support for Priority
451                 public void TestPriority3()
452                 {
453                         C2Test test1 = new C2Test();
454                         Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
455                         try {
456                                 TestThread.Start();
457                                 TestThread.Priority = ThreadPriority.Lowest;
458                                 Assert.AreEqual (ThreadPriority.Lowest, TestThread.Priority, "#45A Incorrect Priority:");
459                                 TestThread.Priority = ThreadPriority.BelowNormal;
460                                 Assert.AreEqual (ThreadPriority.BelowNormal, TestThread.Priority, "#45B Incorrect Priority:");
461                                 TestThread.Priority = ThreadPriority.Normal;
462                                 Assert.AreEqual (ThreadPriority.Normal, TestThread.Priority, "#45C Incorrect Priority:");
463                                 TestThread.Priority = ThreadPriority.AboveNormal;
464                                 Assert.AreEqual (ThreadPriority.AboveNormal, TestThread.Priority, "#45D Incorrect Priority:");
465                                 TestThread.Priority = ThreadPriority.Highest;
466                                 Assert.AreEqual (ThreadPriority.Highest, TestThread.Priority, "#45E Incorrect Priority:");
467                         }
468                         finally {
469 #if MONO_FEATURE_THREAD_ABORT
470                                 TestThread.Abort();
471 #else
472                                 TestThread.Interrupt ();
473 #endif
474                         }
475                 }
476
477                 [Test]
478                 public void TestUndivisibleByPageSizeMaxStackSize ()
479                 {
480                         const int undivisible_stacksize = 1048573;
481
482                         var thread = new Thread (new ThreadStart (delegate {}), undivisible_stacksize);
483                         thread.Start ();
484                         thread.Join ();
485                 }
486
487                 [Test]
488                 public void TestIsBackground1 ()
489                 {
490                         if (is_win32 && is_mono)
491                                 Assert.Fail ("This test fails on mono on Win32. Our runtime should be fixed.");
492
493                         C2Test test1 = new C2Test();
494                         Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
495                         try {
496                                 TestThread.Start();
497                                 TestUtil.WaitForAlive (TestThread, "wait9");
498                                 bool state = TestThread.IsBackground;
499                                 Assert.IsFalse (state, "#51 IsBackground not set at the default state: ");
500                         } finally {
501 #if MONO_FEATURE_THREAD_ABORT
502                                 TestThread.Abort();
503 #else
504                                 TestThread.Interrupt ();
505 #endif
506                         }
507                 }
508
509                 [Test]
510                 public void TestIsBackground2 ()
511                 {
512                         C2Test test1 = new C2Test();
513                         Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
514                         TestThread.IsBackground = true;
515                         try {
516                                 TestThread.Start();
517                         } finally {
518 #if MONO_FEATURE_THREAD_ABORT
519                                 TestThread.Abort();
520 #else
521                                 TestThread.Interrupt ();
522 #endif
523                         }
524                         
525                         if (TestThread.IsAlive) {
526                                 try {
527                                         Assert.IsTrue (TestThread.IsBackground, "#52 Is Background Changed to Start ");
528                                 } catch (ThreadStateException) {
529                                         // Ignore if thread died meantime
530                                 }
531                         }
532                 }
533
534                 [Test]
535                 public void TestName()
536                 {
537                         if (is_win32 && is_mono)
538                                 Assert.Fail ("This test fails on mono on Win32. Our runtime should be fixed.");
539
540                         C2Test test1 = new C2Test();
541                         Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
542                         try {
543                                 TestThread.Start();
544                                 TestUtil.WaitForAlive (TestThread, "wait10");
545                                 string name = TestThread.Name;
546                                 Assert.IsNull (name, "#61 Name set when mustn't be set: ");
547                                 string newname = "Testing....";
548                                 TestThread.Name = newname;
549                                 Assert.AreEqual (newname, TestThread.Name, "#62 Name not set when must be set: ");
550                         } finally {
551 #if MONO_FEATURE_THREAD_ABORT
552                                 TestThread.Abort();
553 #else
554                                 TestThread.Interrupt ();
555 #endif
556                         }
557                 }
558
559                 [Test]
560                 public void Name ()
561                 {
562                         Thread t = new Thread (new ThreadStart (Name));
563                         Assert.IsNull (t.Name, "Name-1");
564                         t.Name = null;
565                         Assert.IsNull (t.Name, "Name-2");
566                 }
567
568                 [Test]
569                 [ExpectedException (typeof (InvalidOperationException))]
570                 public void Rename ()
571                 {
572                         Thread t = new Thread (new ThreadStart (Rename));
573                         t.Name = "a";
574                         t.Name = "b";
575                 }
576
577                 bool rename_finished;
578                 bool rename_failed;
579
580                 [Test]
581                 public void RenameTpThread ()
582                 {
583                         object monitor = new object ();
584                         ThreadPool.QueueUserWorkItem (new WaitCallback (Rename_callback), monitor);
585                         lock (monitor) {
586                                 if (!rename_finished)
587                                         Monitor.Wait (monitor);
588                         }
589                         Assert.IsTrue (!rename_failed);
590                 }
591
592                 void Rename_callback (object o) {
593                         Thread.CurrentThread.Name = "a";
594                         try {
595                                 Thread.CurrentThread.Name = "b";
596                                 //Console.WriteLine ("Thread name is: {0}", Thread.CurrentThread.Name);
597                         } catch (Exception e) {
598                                 //Console.Error.WriteLine (e);
599                                 rename_failed = true;
600                         }
601                         object monitor = o;
602                         lock (monitor) {
603                                 rename_finished = true;
604                                 Monitor.Pulse (monitor);
605                         }
606                 }
607
608                 [Test]
609                 public void TestNestedThreads1()
610                 {
611                         C3Test test1 = new C3Test();
612                         Thread TestThread = new Thread(new ThreadStart(test1.TestMethod1));
613                         try {
614                                 TestThread.Start();
615                                 TestUtil.WaitForAlive (TestThread, "wait11");
616                         } finally {
617 #if MONO_FEATURE_THREAD_ABORT
618                                 TestThread.Abort();
619 #else
620                                 TestThread.Interrupt ();
621 #endif
622                         }
623                 }
624
625                 [Test]
626                 public void TestNestedThreads2()
627                 {
628                         C4Test test1 = new C4Test();
629                         Thread TestThread = new Thread(new ThreadStart(test1.TestMethod1));
630                         try {
631                                 TestThread.Start();
632                         } finally {
633 #if MONO_FEATURE_THREAD_ABORT
634                                 TestThread.Abort();
635 #else
636                                 TestThread.Interrupt ();
637 #endif
638                         }
639                 }
640
641                 [Test]
642                 public void TestJoin1()
643                 {
644                         C1Test test1 = new C1Test();
645                         C1Test test2 = new C1Test();
646                         Thread thread1 = new Thread(new ThreadStart(test1.TestMethod));
647                         Thread thread2 = new Thread(new ThreadStart(test1.TestMethod2));
648                         try {
649                                 thread1.Start();
650                                 thread2.Start();
651                                 thread2.Join();
652                         } finally {
653 #if MONO_FEATURE_THREAD_ABORT
654                                 thread1.Abort();
655                                 thread2.Abort();
656 #else
657                                 thread1.Interrupt ();
658                                 thread2.Interrupt ();
659 #endif
660                         }
661                 }
662
663                 [Test]
664                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
665                 public void Join_Int32_Negative ()
666                 {
667                         // -1 is Timeout.Infinite
668                         Thread.CurrentThread.Join (-2);
669                 }
670
671                 [Test]
672                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
673                 public void Join_TimeSpan_Negative ()
674                 {
675                         Thread.CurrentThread.Join (Negative);
676                 }
677
678                 [Test]
679                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
680                 public void Join_TimeSpan_TooLarge ()
681                 {
682                         Thread.CurrentThread.Join (TooLarge);
683                 }
684
685                 [Test]
686                 public void Join_TimeSpan_SmallNegative ()
687                 {
688                         Thread.CurrentThread.Join (SmallNegative);
689                 }
690
691                 [Test]
692                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
693                 public void Sleep_Int32_Negative ()
694                 {
695                         // -1 is Timeout.Infinite
696                         Thread.Sleep (-2);
697                 }
698
699                 [Test]
700                 public void Sleep_TimeSpan_SmallNegative ()
701                 {
702                         Thread.Sleep (SmallNegative);
703                 }
704
705                 [Test]
706                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
707                 public void Sleep_TimeSpan_Negative ()
708                 {
709                         Thread.Sleep (Negative);
710                 }
711
712                 [Test]
713                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
714                 public void Sleep_TimeSpan_TooLarge ()
715                 {
716                         Thread.Sleep (TooLarge);
717                 }
718
719                 [Test]
720                 public void SpinWait ()
721                 {
722                         // no exception for negative numbers
723                         Thread.SpinWait (Int32.MinValue);
724                         Thread.SpinWait (0);
725                 }
726
727                 [Test]
728                 public void TestThreadState ()
729                 {
730                         if (is_win32 && is_mono)
731                                 Assert.Fail ("This test fails on mono on Win32. Our runtime should be fixed.");
732
733                         //TODO: Test The rest of the possible transitions
734                         C2Test test1 = new C2Test();
735                         Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
736                         Assert.AreEqual (ThreadState.Unstarted, TestThread.ThreadState, "#101 Wrong Thread State");
737                         try {
738                                 TestThread.Start();
739                                 //while(!TestThread.IsAlive); //In the MS Documentation this is not necessary
740                                                                                           //but in the MS SDK it is
741                                 Assert.IsTrue (TestThread.ThreadState == ThreadState.Running || (TestThread.ThreadState & ThreadState.Unstarted) != 0,
742                                         "#102 Wrong Thread State: " + TestThread.ThreadState.ToString ());
743                         } finally {
744 #if MONO_FEATURE_THREAD_ABORT
745                                 TestThread.Abort();
746 #else
747                                 TestThread.Interrupt ();
748 #endif
749                         }
750                         
751                         TestUtil.WaitForNotAlive (TestThread, "wait12");
752                         // Docs say state will be Stopped, but Aborted happens sometimes (?)
753                         Assert.IsTrue ((ThreadState.Stopped & TestThread.ThreadState) != 0 || (ThreadState.Aborted & TestThread.ThreadState) != 0,
754                                 "#103 Wrong Thread State: " + TestThread.ThreadState.ToString ());
755                 }
756
757                 [Test]
758                 [Ignore ("see comment below.")]
759                 public void CurrentPrincipal_PrincipalPolicy_NoPrincipal () 
760                 {
761                         // note: switching from PrincipalPolicy won't work inside the same thread
762                         // because as soon as a Principal object is created the Policy doesn't matter anymore
763                         Thread t = new Thread (new ThreadStart (ThreadedPrincipalTest.NoPrincipal));
764                         try {
765                                 t.Start ();
766                                 t.Join ();
767                         } catch {
768 #if MONO_FEATURE_THREAD_ABORT
769                                 t.Abort ();
770 #else
771                                 t.Interrupt ();
772 #endif
773                         }
774                 }
775
776                 [Test]
777                 [Ignore ("see comment below.")]
778                 public void CurrentPrincipal_PrincipalPolicy_UnauthenticatedPrincipal () 
779                 {
780                         // note: switching from PrincipalPolicy won't work inside the same thread
781                         // because as soon as a Principal object is created the Policy doesn't matter anymore
782                         Thread t = new Thread (new ThreadStart (ThreadedPrincipalTest.UnauthenticatedPrincipal));
783                         try {
784                                 t.Start ();
785                                 t.Join ();
786                         } catch {
787 #if MONO_FEATURE_THREAD_ABORT
788                                 t.Abort ();
789 #else
790                                 t.Interrupt ();
791 #endif
792                         }
793                 }
794
795                 [Test]
796                 public void CurrentPrincipal_PrincipalPolicy_WindowsPrincipal () 
797                 {
798                         // note: switching from PrincipalPolicy won't work inside the same thread
799                         // because as soon as a Principal object is created the Policy doesn't matter anymore
800                         Thread t = new Thread (new ThreadStart (ThreadedPrincipalTest.WindowsPrincipal));
801                         try {
802                                 t.Start ();
803                                 t.Join ();
804                         } catch {
805 #if MONO_FEATURE_THREAD_ABORT
806                                 t.Abort ();
807 #else
808                                 t.Interrupt ();
809 #endif
810                         }
811                 }
812                 
813                 [Test]
814                 public void IPrincipal_CopyOnNewThread () 
815                 {
816                         Thread.CurrentPrincipal = new GenericPrincipal (new GenericIdentity ("bad"), null);
817                         Thread t = new Thread (new ThreadStart (ThreadedPrincipalTest.CopyOnNewThread));
818                         try {
819                                 Thread.CurrentPrincipal = new GenericPrincipal (new GenericIdentity ("good"), null);
820                                 t.Start ();
821                                 t.Join ();
822                         } catch {
823 #if MONO_FEATURE_THREAD_ABORT
824                                 t.Abort ();
825 #else
826                                 t.Interrupt ();
827 #endif
828                         }
829                 }
830
831                 int counter = 0;
832
833 #if MONO_FEATURE_THREAD_SUSPEND_RESUME
834                 [Test]
835                 public void TestSuspend ()
836                 {
837                         Thread t = new Thread (new ThreadStart (DoCount));
838                         t.IsBackground = true;
839                         t.Start ();
840                         
841                         CheckIsRunning ("t1", t);
842                         
843                         t.Suspend ();
844                         WaitSuspended ("t2", t);
845                         
846                         CheckIsNotRunning ("t3", t);
847                         
848                         t.Resume ();
849                         WaitResumed ("t4", t);
850                         
851                         CheckIsRunning ("t5", t);
852                         
853                         t.Abort ();
854                         TestUtil.WaitForNotAlive (t, "wait13");
855                         CheckIsNotRunning ("t6", t);
856                 }
857 #endif
858                 
859 #if MONO_FEATURE_THREAD_SUSPEND_RESUME && MONO_FEATURE_THREAD_ABORT
860                 [Test]
861                 [Category("NotDotNet")] // On MS, ThreadStateException is thrown on Abort: "Thread is suspended; attempting to abort"
862                 public void TestSuspendAbort ()
863                 {
864                         if (is_win32 && is_mono)
865                                 Assert.Fail ("This test fails on Win32. The test should be fixed.");
866
867                         Thread t = new Thread (new ThreadStart (DoCount));
868                         t.IsBackground = true;
869                         t.Start ();
870                         
871                         CheckIsRunning ("t1", t);
872                         
873                         t.Suspend ();
874                         WaitSuspended ("t2", t);
875                         
876                         CheckIsNotRunning ("t3", t);
877                         
878                         t.Abort ();
879                         
880                         int n=0;
881                         while (t.IsAlive && n < 200) {
882                                 Thread.Sleep (10);
883                                 n++;
884                         }
885
886                         Assert.IsTrue (n < 200, "Timeout while waiting for abort");
887                         
888                         CheckIsNotRunning ("t6", t);
889                 }
890 #endif
891
892                 [Test]
893                 public void Test_Interrupt ()
894                 {
895                         ManualResetEvent mre = new ManualResetEvent (false);
896                         bool interruptedExceptionThrown = false;
897
898                         ThreadPool.QueueUserWorkItem (Test_Interrupt_Worker, Thread.CurrentThread);
899
900                         try {
901                                 try {
902                                         mre.WaitOne (3000);
903                                 } finally {
904                                         try {
905                                                 mre.WaitOne (0);
906                                         } catch (ThreadInterruptedException) {
907                                                 Assert.Fail ("ThreadInterruptedException thrown twice");
908                                         }
909                                 }
910                         } catch (ThreadInterruptedException) {
911                                 interruptedExceptionThrown = true;
912                         }
913
914                         Assert.IsTrue (interruptedExceptionThrown, "ThreadInterruptedException expected.");
915                 }
916
917                 [Test]
918                 [ExpectedException (typeof (ArgumentNullException))]
919                 public void TestQueueUserWorkItemNullCallback ()
920                 {
921                         ThreadPool.QueueUserWorkItem (null, null);
922                 }
923
924                 private void Test_Interrupt_Worker (object o)
925                 {
926                         Thread t = o as Thread;
927                         Thread.Sleep (100);
928                         t.Interrupt ();
929                 }
930                 
931                 [Test]
932                 [Category ("NotDotNet")] // it crashes nunit.
933                 public void Test_InterruptCurrentThread ()
934                 {
935                         ManualResetEvent mre = new ManualResetEvent (false);
936                         bool interruptedExceptionThrown = false;
937
938                         Thread.CurrentThread.Interrupt ();
939                         try {
940                                 mre.WaitOne (0);
941                                 Assert.Fail ();
942                         } catch (ThreadInterruptedException) {
943                         }
944                 }
945
946                 [Test]
947                 public void GetNamedDataSlotTest ()
948                 {
949                         Assert.IsNotNull (Thread.GetNamedDataSlot ("te#st"), "#1");
950                         Assert.AreSame (Thread.GetNamedDataSlot ("te#st"), Thread.GetNamedDataSlot ("te#st"), "#2");
951                 }
952
953                 class DomainClass : MarshalByRefObject {
954                         Thread m_thread;
955                         bool success;
956
957                         public bool Run () {
958                                 m_thread = new Thread(ThreadProc);
959                                 m_thread.Start(Thread.CurrentThread);
960                                 m_thread.Join();
961                                 return success;
962                         }
963
964                         public void ThreadProc (object arg) {
965                                 success = m_thread == Thread.CurrentThread;
966                         }
967                 }
968
969 #if MONO_FEATURE_MULTIPLE_APPDOMAINS
970                 [Test]
971                 public void CurrentThread_Domains ()
972                 {
973                         AppDomain ad = AppDomain.CreateDomain ("foo");
974                         ad.Load (typeof (DomainClass).Assembly.GetName ());
975                         var o = (DomainClass)ad.CreateInstanceAndUnwrap (typeof (DomainClass).Assembly.FullName, typeof (DomainClass).FullName);
976                         Assert.IsTrue (o.Run ());
977                         AppDomain.Unload (ad);
978                 }
979 #endif // MONO_FEATURE_MULTIPLE_APPDOMAINS
980
981                 void CheckIsRunning (string s, Thread t)
982                 {
983                         int c = counter;
984                         Thread.Sleep (100);
985                         Assert.IsTrue (counter > c, s);
986                 }
987                 
988                 void CheckIsNotRunning (string s, Thread t)
989                 {
990                         int c = counter;
991                         Thread.Sleep (100);
992                         Assert.AreEqual (counter, c, s);
993                 }
994                 
995                 void WaitSuspended (string s, Thread t)
996                 {
997                         int n=0;
998                         ThreadState state = t.ThreadState;
999                         while ((state & ThreadState.Suspended) == 0) {
1000                                 Assert.IsTrue ((state & ThreadState.SuspendRequested) != 0, s + ": expected SuspendRequested state");
1001                                 Thread.Sleep (10);
1002                                 n++;
1003                                 Assert.IsTrue (n < 100, s + ": failed to suspend");
1004                                 state = t.ThreadState;
1005                         }
1006                         Assert.IsTrue ((state & ThreadState.SuspendRequested) == 0, s + ": SuspendRequested state not expected");
1007                 }
1008                 
1009                 void WaitResumed (string s, Thread t)
1010                 {
1011                         int n=0;
1012                         while ((t.ThreadState & ThreadState.Suspended) != 0) {
1013                                 Thread.Sleep (10);
1014                                 n++;
1015                                 Assert.IsTrue (n < 100, s + ": failed to resume");
1016                         }
1017                 }
1018                 
1019                 public void DoCount ()
1020                 {
1021                         while (true) {
1022                                 counter++;
1023                                 Thread.Sleep (1);
1024                         }
1025                 }
1026         }
1027
1028         [TestFixture]
1029         public class ThreadStateTest {
1030                 void Start ()
1031                 {
1032                 }
1033
1034                 [Test] // bug #81720
1035                 public void IsBackGround ()
1036                 {
1037                         Thread t1 = new Thread (new ThreadStart (Start));
1038                         Assert.AreEqual (ThreadState.Unstarted, t1.ThreadState, "#A1");
1039                         Assert.IsFalse (t1.IsBackground, "#A2");
1040                         t1.Start ();
1041                         t1.Join ();
1042                         Assert.AreEqual (ThreadState.Stopped, t1.ThreadState, "#A3");
1043
1044                         try {
1045                                 bool isBackGround = t1.IsBackground;
1046                                 Assert.Fail ("#A4: " + isBackGround.ToString ());
1047                         } catch (ThreadStateException ex) {
1048                                 Assert.AreEqual (typeof (ThreadStateException), ex.GetType (), "#A5");
1049                                 Assert.IsNull (ex.InnerException, "#A6");
1050                                 Assert.IsNotNull (ex.Message, "#A7");
1051                         }
1052
1053                         Thread t2 = new Thread (new ThreadStart (Start));
1054                         Assert.AreEqual (ThreadState.Unstarted, t2.ThreadState, "#B1");
1055                         t2.IsBackground = true;
1056                         Assert.AreEqual (ThreadState.Unstarted | ThreadState.Background, t2.ThreadState, "#B2");
1057                         Assert.IsTrue (t2.IsBackground, "#B3");
1058                         t2.Start ();
1059                         t2.Join ();
1060                         Assert.AreEqual (ThreadState.Stopped, t2.ThreadState, "#B4");
1061
1062                         try {
1063                                 bool isBackGround = t2.IsBackground;
1064                                 Assert.Fail ("#B5: " + isBackGround.ToString ());
1065                         } catch (ThreadStateException ex) {
1066                                 Assert.AreEqual (typeof (ThreadStateException), ex.GetType (), "#B6");
1067                                 Assert.IsNull (ex.InnerException, "#B7");
1068                                 Assert.IsNotNull (ex.Message, "#B8");
1069                         }
1070                 }
1071         }
1072
1073         [TestFixture]
1074         [Serializable]
1075         public class ThreadTest_ManagedThreadId
1076         {
1077                 AppDomain ad1;
1078                 AppDomain ad2;
1079                 MBRO mbro = new MBRO ();
1080
1081                 class MBRO : MarshalByRefObject {
1082                         public int id_a1;
1083                         public int id_b1;
1084                         public int id_b2;
1085                         public string ad_a1;
1086                         public string ad_b1;
1087                         public string ad_b2;
1088                         public string message;
1089                 }
1090 #if !MOBILE
1091                 [Test]
1092                 public void ManagedThreadId_AppDomains ()
1093                 {
1094                         AppDomain currentDomain = AppDomain.CurrentDomain;
1095                         ad1 = AppDomain.CreateDomain ("AppDomain 1", currentDomain.Evidence, currentDomain.SetupInformation);
1096                         ad2 = AppDomain.CreateDomain ("AppDomain 2", currentDomain.Evidence, currentDomain.SetupInformation);
1097
1098                         Thread a = new Thread (ThreadA);
1099                         Thread b = new Thread (ThreadB);
1100                         // execute on AppDomain 1 thread A
1101                         // execute on AppDomain 2 thread B
1102                         // execute on AppDomain 1 thread B - must have same ManagedThreadId as Ad 2 on thread B
1103                         a.Start ();
1104                         a.Join ();
1105                         b.Start ();
1106                         b.Join ();
1107
1108                         AppDomain.Unload (ad1);
1109                         AppDomain.Unload (ad2);
1110
1111                         if (mbro.message != null)
1112                                 Assert.Fail (mbro.message);
1113
1114                         // Console.WriteLine ("Done id_a1: {0} id_b1: {1} id_b2: {2} ad_a1: {3} ad_b1: {4} ad_b2: {5}", mbro.id_a1, mbro.id_b1, mbro.id_b2, mbro.ad_a1, mbro.ad_b1, mbro.ad_b2);
1115
1116                         Assert.AreEqual ("AppDomain 1", mbro.ad_a1, "Name #1");
1117                         Assert.AreEqual ("AppDomain 1", mbro.ad_b1, "Name #2");
1118                         Assert.AreEqual ("AppDomain 2", mbro.ad_b2, "Name #3");
1119
1120                         Assert.AreNotEqual (mbro.id_a1, mbro.id_b1, "Id #1");
1121                         Assert.AreNotEqual (mbro.id_a1, mbro.id_b2, "Id #2");
1122                         Assert.AreEqual (mbro.id_b1, mbro.id_b2, "Id #3");
1123
1124                         Assert.AreNotEqual (mbro.id_a1, Thread.CurrentThread.ManagedThreadId, "Id #4");
1125                         Assert.AreNotEqual (mbro.id_b1, Thread.CurrentThread.ManagedThreadId, "Id #5");
1126                         Assert.AreNotEqual (mbro.id_b2, Thread.CurrentThread.ManagedThreadId, "Id #6");
1127                         Assert.AreNotEqual (mbro.ad_a1, AppDomain.CurrentDomain.FriendlyName, "Name #4");
1128                         Assert.AreNotEqual (mbro.ad_b1, AppDomain.CurrentDomain.FriendlyName, "Name #5");
1129                         Assert.AreNotEqual (mbro.ad_b2, AppDomain.CurrentDomain.FriendlyName, "Name #6");
1130                 }
1131 #endif
1132                 void A1 ()
1133                 {
1134                         mbro.id_a1 = Thread.CurrentThread.ManagedThreadId;
1135                         mbro.ad_a1 = AppDomain.CurrentDomain.FriendlyName;
1136                 }
1137                 
1138                 void B2 ()
1139                 {
1140                         mbro.id_b2 = Thread.CurrentThread.ManagedThreadId;
1141                         mbro.ad_b2 = AppDomain.CurrentDomain.FriendlyName;
1142                 }
1143
1144                 void B1 ()
1145                 {
1146                         mbro.id_b1 = Thread.CurrentThread.ManagedThreadId;
1147                         mbro.ad_b1 = AppDomain.CurrentDomain.FriendlyName;
1148                 }
1149
1150                 void ThreadA (object obj)
1151                 {
1152                         // Console.WriteLine ("ThreadA");
1153                         try {
1154                                 ad1.DoCallBack (A1);
1155                         } catch (Exception ex) {
1156                                 mbro.message = string.Format ("ThreadA exception: {0}", ex);
1157                         }
1158                         // Console.WriteLine ("ThreadA Done");
1159                 }
1160
1161                 void ThreadB (object obj)
1162                 {
1163                         // Console.WriteLine ("ThreadB");
1164                         try {
1165                                 ad2.DoCallBack (B2);
1166                                 ad1.DoCallBack (B1);
1167                         } catch (Exception ex) {
1168                                 mbro.message = string.Format ("ThreadB exception: {0}", ex);
1169                         }
1170                         // Console.WriteLine ("ThreadB Done");
1171                 }
1172         }
1173
1174         [TestFixture]
1175         public class ThreadApartmentTest
1176         {
1177                 void Start ()
1178                 {
1179                 }
1180
1181                 [Test] // bug #81658
1182                 public void ApartmentState_StoppedThread ()
1183                 {
1184                         Thread t1 = new Thread (new ThreadStart (Start));
1185                         t1.Start ();
1186                         t1.Join ();
1187                         try {
1188                                 ApartmentState state = t1.ApartmentState;
1189                                 Assert.Fail ("#A1: " + state.ToString ());
1190                         } catch (ThreadStateException ex) {
1191                                 Assert.AreEqual (typeof (ThreadStateException), ex.GetType (), "#A2");
1192                                 Assert.IsNull (ex.InnerException, "#A3");
1193                                 Assert.IsNotNull (ex.Message, "#A4");
1194                         }
1195
1196                         Thread t2 = new Thread (new ThreadStart (Start));
1197                         t2.IsBackground = true;
1198                         t2.Start ();
1199                         t2.Join ();
1200                         try {
1201                                 ApartmentState state = t2.ApartmentState;
1202                                 Assert.Fail ("#B1: " + state.ToString ());
1203                         } catch (ThreadStateException ex) {
1204                                 Assert.AreEqual (typeof (ThreadStateException), ex.GetType (), "#B2");
1205                                 Assert.IsNull (ex.InnerException, "#B3");
1206                                 Assert.IsNotNull (ex.Message, "#B4");
1207                         }
1208                 }
1209
1210                 [Test]
1211                 public void ApartmentState_BackGround ()
1212                 {
1213                         Thread t1 = new Thread (new ThreadStart (Start));
1214                         t1.IsBackground = true;
1215                         Assert.AreEqual (ApartmentState.Unknown, t1.ApartmentState, "#1");
1216                         t1.ApartmentState = ApartmentState.STA;
1217                         Assert.AreEqual (ApartmentState.STA, t1.ApartmentState, "#2");
1218                 }
1219
1220                 [Test]
1221                 public void TestApartmentState ()
1222                 {
1223                         Thread t1 = new Thread (new ThreadStart (Start));
1224                         Thread t2 = new Thread (new ThreadStart (Start));
1225                         Thread t3 = new Thread (new ThreadStart (Start));
1226
1227                         Assert.AreEqual (ApartmentState.Unknown, t1.ApartmentState, "Thread1 Default");
1228                         Assert.AreEqual (ApartmentState.Unknown, t2.ApartmentState, "Thread2 Default");
1229                         Assert.AreEqual (ApartmentState.Unknown, t3.ApartmentState, "Thread3 Default");
1230
1231                         t1.ApartmentState = ApartmentState.STA;
1232                         Assert.AreEqual (ApartmentState.STA, t1.ApartmentState, "Thread1 Set Once");
1233                         t1.ApartmentState = ApartmentState.MTA;
1234                         Assert.AreEqual (ApartmentState.STA, t1.ApartmentState, "Thread1 Set Twice");
1235
1236                         t2.ApartmentState = ApartmentState.MTA;
1237                         Assert.AreEqual (ApartmentState.MTA, t2.ApartmentState, "Thread2 Set Once");
1238                         t2.ApartmentState = ApartmentState.STA;
1239                         Assert.AreEqual (ApartmentState.MTA, t2.ApartmentState, "Thread2 Set Twice");
1240
1241                         bool exception_occured = false;
1242                         try {
1243                                 t3.ApartmentState = ApartmentState.Unknown;
1244                         }
1245                         catch (Exception) {
1246                                 exception_occured = true;
1247                         }
1248                         Assert.AreEqual (ApartmentState.Unknown, t3.ApartmentState, "Thread3 Set Invalid");
1249                         Assert.IsFalse (exception_occured, "Thread3 Set Invalid Exception Occured");
1250
1251                         t1.Start ();
1252                         exception_occured = false;
1253                         try {
1254                                 t1.ApartmentState = ApartmentState.STA;
1255                         }
1256                         catch (Exception) {
1257                                 exception_occured = true;
1258                         }
1259                         Assert.IsTrue (exception_occured, "Thread1 Started Invalid Exception Occured");
1260                 }
1261
1262                 [Test]
1263                 public void TestSetApartmentStateSameState ()
1264                 {
1265                         Thread t1 = new Thread (new ThreadStart (Start));
1266                         t1.SetApartmentState (ApartmentState.STA);
1267                         Assert.AreEqual (ApartmentState.STA, t1.ApartmentState, "Thread1 Set Once");
1268
1269                         t1.SetApartmentState (ApartmentState.STA);
1270                         Assert.AreEqual (ApartmentState.STA, t1.ApartmentState, "Thread1 Set twice");
1271                 }
1272
1273                 [Test]
1274                 [ExpectedException(typeof(InvalidOperationException))]
1275                 public void TestSetApartmentStateDiffState ()
1276                 {
1277                         Thread t1 = new Thread (new ThreadStart (Start));
1278                         t1.SetApartmentState (ApartmentState.STA);
1279                         Assert.AreEqual (ApartmentState.STA, t1.ApartmentState, "Thread1 Set Once");
1280
1281                         t1.SetApartmentState (ApartmentState.MTA);
1282                 }
1283
1284                 [Test]
1285                 public void TestTrySetApartmentState ()
1286                 {
1287                         Thread t1 = new Thread (new ThreadStart (Start));
1288                         t1.SetApartmentState (ApartmentState.STA);
1289                         Assert.AreEqual (ApartmentState.STA, t1.ApartmentState, "#1");
1290
1291                         bool result = t1.TrySetApartmentState (ApartmentState.MTA);
1292                         Assert.IsFalse (result, "#2");
1293
1294                         result = t1.TrySetApartmentState (ApartmentState.STA);
1295                         Assert.IsTrue (result, "#3");
1296                 }
1297
1298                 [Test]
1299                 public void TestTrySetApartmentStateRunning ()
1300                 {
1301                         Thread t1 = new Thread (new ThreadStart (Start));
1302                         t1.SetApartmentState (ApartmentState.STA);
1303                         Assert.AreEqual (ApartmentState.STA, t1.ApartmentState, "#1");
1304
1305                         t1.Start ();
1306
1307                         try {
1308                                 t1.TrySetApartmentState (ApartmentState.STA);
1309                                 Assert.Fail ("#2");
1310                         } catch (ThreadStateException) {
1311                         }
1312
1313                         t1.Join ();
1314                 }
1315
1316                 [Test]
1317                 public void Volatile () {
1318                         double v3 = 55667;
1319                         Thread.VolatileWrite (ref v3, double.MaxValue);
1320                         Assert.AreEqual (v3, double.MaxValue);
1321
1322                         float v4 = 1;
1323                         Thread.VolatileWrite (ref v4, float.MaxValue);
1324                         Assert.AreEqual (v4, float.MaxValue);
1325                 }
1326
1327                 [Test]
1328                 public void Culture ()
1329                 {
1330                         Assert.IsNotNull (Thread.CurrentThread.CurrentCulture, "CurrentCulture");
1331                         Assert.IsNotNull (Thread.CurrentThread.CurrentUICulture, "CurrentUICulture");
1332                 }
1333
1334                 [Test]
1335                 public void ThreadStartSimple ()
1336                 {
1337                         int i = 0;
1338                         Thread t = new Thread (delegate () {
1339                                 // ensure the NSAutoreleasePool works
1340                                 i++;
1341                         });
1342                         t.Start ();
1343                         t.Join ();
1344                         Assert.AreEqual (1, i, "ThreadStart");
1345                 }
1346
1347                 [Test]
1348                 public void ParametrizedThreadStart ()
1349                 {
1350                         int i = 0;
1351                         object arg = null;
1352                         Thread t = new Thread (delegate (object obj) {
1353                                 // ensure the NSAutoreleasePool works
1354                                 i++;
1355                                 arg = obj;
1356                         });
1357                         t.Start (this);
1358                         t.Join ();
1359
1360                         Assert.AreEqual (1, i, "ParametrizedThreadStart");
1361                         Assert.AreEqual (this, arg, "obj");     
1362                 }               
1363
1364                 [Test]
1365                 public void SetNameTpThread () {
1366                         ThreadPool.QueueUserWorkItem(new WaitCallback(ThreadProc));
1367                 }
1368
1369                 static void ThreadProc(Object stateInfo) {
1370                         Thread.CurrentThread.Name = "My Worker";
1371                 }
1372
1373                 [Test]
1374                 public void GetStackTraces () {
1375                         var m = typeof (Thread).GetMethod ("Mono_GetStackTraces", BindingFlags.NonPublic|BindingFlags.Static);
1376                         if (m != null) {
1377                                 var res = (Dictionary<Thread,SD.StackTrace>)typeof (Thread).GetMethod ("Mono_GetStackTraces", BindingFlags.NonPublic|BindingFlags.Static).Invoke (null, null);
1378                                 foreach (var t in res.Keys) {
1379                                         var st = res [t].ToString ();
1380                                 }
1381                         }
1382                 }
1383         }
1384
1385         public class TestUtil
1386         {
1387                 public static void WaitForNotAlive (Thread t, string s)
1388                 {
1389                         WhileAlive (t, true, s);
1390                 }
1391                 
1392                 public static void WaitForAlive (Thread t, string s)
1393                 {
1394                         WhileAlive (t, false, s);
1395                 }
1396                 
1397                 public static bool WaitForAliveOrStop (Thread t, string s)
1398                 {
1399                         return WhileAliveOrStop (t, false, s);
1400                 }
1401                 
1402                 public static void WhileAlive (Thread t, bool alive, string s)
1403                 {
1404                         DateTime ti = DateTime.Now;
1405                         while (t.IsAlive == alive) {
1406                                 if ((DateTime.Now - ti).TotalSeconds > 10) {
1407                                         if (alive) Assert.Fail ("Timeout while waiting for not alive state. " + s);
1408                                         else Assert.Fail ("Timeout while waiting for alive state. " + s);
1409                                 }
1410                         }
1411                 }
1412
1413                 public static bool WhileAliveOrStop (Thread t, bool alive, string s)
1414                 {
1415                         DateTime ti = DateTime.Now;
1416                         while (t.IsAlive == alive) {
1417                                 if (t.ThreadState == ThreadState.Stopped)
1418                                         return false;
1419
1420                                 if ((DateTime.Now - ti).TotalSeconds > 10) {
1421                                         if (alive) Assert.Fail ("Timeout while waiting for not alive state. " + s);
1422                                         else Assert.Fail ("Timeout while waiting for alive state. " + s);
1423                                 }
1424                         }
1425
1426                         return true;
1427                 }
1428         }
1429 }