Remove Thread.[Abort|Suspend|Resume] from TvOS/WatchOS.
[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                 public void TestPriority1()
391                 {
392                         if (is_win32 && is_mono)
393                                 Assert.Fail ("This test fails on mono on Win32. Our runtime should be fixed.");
394
395                         C2Test test1 = new C2Test();
396                         Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
397                         try {
398                                 TestThread.Priority=ThreadPriority.BelowNormal;
399                                 ThreadPriority after = TestThread.Priority;
400                                 TestThread.Start();
401                                 TestUtil.WaitForAlive (TestThread, "wait7");
402                                 ThreadPriority before = TestThread.Priority;
403                                 Assert.AreEqual (before, after, "#41 Unexpected Priority Change: ");
404                         } finally {
405 #if MONO_FEATURE_THREAD_ABORT
406                                 TestThread.Abort();
407 #else
408                                 TestThread.Interrupt ();
409 #endif
410                         }
411                 }
412
413 #if MONO_FEATURE_THREAD_ABORT
414                 [Test]
415                 [Category ("NotDotNet")] // on MS, Thread is still in AbortRequested state when Start is invoked
416                 public void AbortUnstarted ()
417                 {
418                         C2Test test1 = new C2Test();
419                         Thread th = new Thread (new ThreadStart (test1.TestMethod));
420                         th.Abort ();
421                         th.Start ();
422                 }
423 #endif
424
425                 [Test]
426                 [Category ("NotDotNet")] // on MS, ThreadState is immediately Stopped after Abort
427                 [Category ("NotWorking")] // this is a MonoTODO -> no support for Priority
428                 public void TestPriority2()
429                 {
430                         C2Test test1 = new C2Test();
431                         Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
432                         try {
433                                 Assert.AreEqual (ThreadPriority.Normal, TestThread.Priority, "#42 Incorrect Priority in New thread: ");
434                                 TestThread.Start();
435                                 TestUtil.WaitForAliveOrStop (TestThread, "wait8");
436                                 Assert.AreEqual (ThreadPriority.Normal, TestThread.Priority, "#43 Incorrect Priority in Started thread: ");
437                         } finally {
438 #if MONO_FEATURE_THREAD_ABORT
439                                 TestThread.Abort();
440 #else
441                                 TestThread.Interrupt ();
442 #endif
443                         }
444                         Assert.AreEqual (ThreadPriority.Normal, TestThread.Priority, "#44 Incorrect Priority in Aborted thread: ");
445                 }
446
447                 [Test]
448                 [Category ("NotWorking")] // this is a MonoTODO -> no support for Priority
449                 public void TestPriority3()
450                 {
451                         C2Test test1 = new C2Test();
452                         Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
453                         try {
454                                 TestThread.Start();
455                                 TestThread.Priority = ThreadPriority.Lowest;
456                                 Assert.AreEqual (ThreadPriority.Lowest, TestThread.Priority, "#45A Incorrect Priority:");
457                                 TestThread.Priority = ThreadPriority.BelowNormal;
458                                 Assert.AreEqual (ThreadPriority.BelowNormal, TestThread.Priority, "#45B Incorrect Priority:");
459                                 TestThread.Priority = ThreadPriority.Normal;
460                                 Assert.AreEqual (ThreadPriority.Normal, TestThread.Priority, "#45C Incorrect Priority:");
461                                 TestThread.Priority = ThreadPriority.AboveNormal;
462                                 Assert.AreEqual (ThreadPriority.AboveNormal, TestThread.Priority, "#45D Incorrect Priority:");
463                                 TestThread.Priority = ThreadPriority.Highest;
464                                 Assert.AreEqual (ThreadPriority.Highest, TestThread.Priority, "#45E Incorrect Priority:");
465                         }
466                         finally {
467 #if MONO_FEATURE_THREAD_ABORT
468                                 TestThread.Abort();
469 #else
470                                 TestThread.Interrupt ();
471 #endif
472                         }
473                 }
474
475                 [Test]
476                 public void TestUndivisibleByPageSizeMaxStackSize ()
477                 {
478                         const int undivisible_stacksize = 1048573;
479
480                         var thread = new Thread (new ThreadStart (delegate {}), undivisible_stacksize);
481                         thread.Start ();
482                         thread.Join ();
483                 }
484
485                 [Test]
486                 public void TestIsBackground1 ()
487                 {
488                         if (is_win32 && is_mono)
489                                 Assert.Fail ("This test fails on mono on Win32. Our runtime should be fixed.");
490
491                         C2Test test1 = new C2Test();
492                         Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
493                         try {
494                                 TestThread.Start();
495                                 TestUtil.WaitForAlive (TestThread, "wait9");
496                                 bool state = TestThread.IsBackground;
497                                 Assert.IsFalse (state, "#51 IsBackground not set at the default state: ");
498                         } finally {
499 #if MONO_FEATURE_THREAD_ABORT
500                                 TestThread.Abort();
501 #else
502                                 TestThread.Interrupt ();
503 #endif
504                         }
505                 }
506
507                 [Test]
508                 public void TestIsBackground2 ()
509                 {
510                         C2Test test1 = new C2Test();
511                         Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
512                         TestThread.IsBackground = true;
513                         try {
514                                 TestThread.Start();
515                         } finally {
516 #if MONO_FEATURE_THREAD_ABORT
517                                 TestThread.Abort();
518 #else
519                                 TestThread.Interrupt ();
520 #endif
521                         }
522                         
523                         if (TestThread.IsAlive) {
524                                 try {
525                                         Assert.IsTrue (TestThread.IsBackground, "#52 Is Background Changed to Start ");
526                                 } catch (ThreadStateException) {
527                                         // Ignore if thread died meantime
528                                 }
529                         }
530                 }
531
532                 [Test]
533                 public void TestName()
534                 {
535                         if (is_win32 && is_mono)
536                                 Assert.Fail ("This test fails on mono on Win32. Our runtime should be fixed.");
537
538                         C2Test test1 = new C2Test();
539                         Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
540                         try {
541                                 TestThread.Start();
542                                 TestUtil.WaitForAlive (TestThread, "wait10");
543                                 string name = TestThread.Name;
544                                 Assert.IsNull (name, "#61 Name set when mustn't be set: ");
545                                 string newname = "Testing....";
546                                 TestThread.Name = newname;
547                                 Assert.AreEqual (newname, TestThread.Name, "#62 Name not set when must be set: ");
548                         } finally {
549 #if MONO_FEATURE_THREAD_ABORT
550                                 TestThread.Abort();
551 #else
552                                 TestThread.Interrupt ();
553 #endif
554                         }
555                 }
556
557                 [Test]
558                 public void Name ()
559                 {
560                         Thread t = new Thread (new ThreadStart (Name));
561                         Assert.IsNull (t.Name, "Name-1");
562                         t.Name = null;
563                         Assert.IsNull (t.Name, "Name-2");
564                 }
565
566                 [Test]
567                 [ExpectedException (typeof (InvalidOperationException))]
568                 public void Rename ()
569                 {
570                         Thread t = new Thread (new ThreadStart (Rename));
571                         t.Name = "a";
572                         t.Name = "b";
573                 }
574
575                 bool rename_finished;
576                 bool rename_failed;
577
578                 [Test]
579                 public void RenameTpThread ()
580                 {
581                         object monitor = new object ();
582                         ThreadPool.QueueUserWorkItem (new WaitCallback (Rename_callback), monitor);
583                         lock (monitor) {
584                                 if (!rename_finished)
585                                         Monitor.Wait (monitor);
586                         }
587                         Assert.IsTrue (!rename_failed);
588                 }
589
590                 void Rename_callback (object o) {
591                         Thread.CurrentThread.Name = "a";
592                         try {
593                                 Thread.CurrentThread.Name = "b";
594                                 //Console.WriteLine ("Thread name is: {0}", Thread.CurrentThread.Name);
595                         } catch (Exception e) {
596                                 //Console.Error.WriteLine (e);
597                                 rename_failed = true;
598                         }
599                         object monitor = o;
600                         lock (monitor) {
601                                 rename_finished = true;
602                                 Monitor.Pulse (monitor);
603                         }
604                 }
605
606                 [Test]
607                 public void TestNestedThreads1()
608                 {
609                         C3Test test1 = new C3Test();
610                         Thread TestThread = new Thread(new ThreadStart(test1.TestMethod1));
611                         try {
612                                 TestThread.Start();
613                                 TestUtil.WaitForAlive (TestThread, "wait11");
614                         } finally {
615 #if MONO_FEATURE_THREAD_ABORT
616                                 TestThread.Abort();
617 #else
618                                 TestThread.Interrupt ();
619 #endif
620                         }
621                 }
622
623                 [Test]
624                 public void TestNestedThreads2()
625                 {
626                         C4Test test1 = new C4Test();
627                         Thread TestThread = new Thread(new ThreadStart(test1.TestMethod1));
628                         try {
629                                 TestThread.Start();
630                         } finally {
631 #if MONO_FEATURE_THREAD_ABORT
632                                 TestThread.Abort();
633 #else
634                                 TestThread.Interrupt ();
635 #endif
636                         }
637                 }
638
639                 [Test]
640                 public void TestJoin1()
641                 {
642                         C1Test test1 = new C1Test();
643                         C1Test test2 = new C1Test();
644                         Thread thread1 = new Thread(new ThreadStart(test1.TestMethod));
645                         Thread thread2 = new Thread(new ThreadStart(test1.TestMethod2));
646                         try {
647                                 thread1.Start();
648                                 thread2.Start();
649                                 thread2.Join();
650                         } finally {
651 #if MONO_FEATURE_THREAD_ABORT
652                                 thread1.Abort();
653                                 thread2.Abort();
654 #else
655                                 thread1.Interrupt ();
656                                 thread2.Interrupt ();
657 #endif
658                         }
659                 }
660
661                 [Test]
662                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
663                 public void Join_Int32_Negative ()
664                 {
665                         // -1 is Timeout.Infinite
666                         Thread.CurrentThread.Join (-2);
667                 }
668
669                 [Test]
670                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
671                 public void Join_TimeSpan_Negative ()
672                 {
673                         Thread.CurrentThread.Join (Negative);
674                 }
675
676                 [Test]
677                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
678                 public void Join_TimeSpan_TooLarge ()
679                 {
680                         Thread.CurrentThread.Join (TooLarge);
681                 }
682
683                 [Test]
684                 public void Join_TimeSpan_SmallNegative ()
685                 {
686                         Thread.CurrentThread.Join (SmallNegative);
687                 }
688
689                 [Test]
690                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
691                 public void Sleep_Int32_Negative ()
692                 {
693                         // -1 is Timeout.Infinite
694                         Thread.Sleep (-2);
695                 }
696
697                 [Test]
698                 public void Sleep_TimeSpan_SmallNegative ()
699                 {
700                         Thread.Sleep (SmallNegative);
701                 }
702
703                 [Test]
704                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
705                 public void Sleep_TimeSpan_Negative ()
706                 {
707                         Thread.Sleep (Negative);
708                 }
709
710                 [Test]
711                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
712                 public void Sleep_TimeSpan_TooLarge ()
713                 {
714                         Thread.Sleep (TooLarge);
715                 }
716
717                 [Test]
718                 public void SpinWait ()
719                 {
720                         // no exception for negative numbers
721                         Thread.SpinWait (Int32.MinValue);
722                         Thread.SpinWait (0);
723                 }
724
725                 [Test]
726                 public void TestThreadState ()
727                 {
728                         if (is_win32 && is_mono)
729                                 Assert.Fail ("This test fails on mono on Win32. Our runtime should be fixed.");
730
731                         //TODO: Test The rest of the possible transitions
732                         C2Test test1 = new C2Test();
733                         Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
734                         Assert.AreEqual (ThreadState.Unstarted, TestThread.ThreadState, "#101 Wrong Thread State");
735                         try {
736                                 TestThread.Start();
737                                 //while(!TestThread.IsAlive); //In the MS Documentation this is not necessary
738                                                                                           //but in the MS SDK it is
739                                 Assert.IsTrue (TestThread.ThreadState == ThreadState.Running || (TestThread.ThreadState & ThreadState.Unstarted) != 0,
740                                         "#102 Wrong Thread State: " + TestThread.ThreadState.ToString ());
741                         } finally {
742 #if MONO_FEATURE_THREAD_ABORT
743                                 TestThread.Abort();
744 #else
745                                 TestThread.Interrupt ();
746 #endif
747                         }
748                         
749                         TestUtil.WaitForNotAlive (TestThread, "wait12");
750                         // Docs say state will be Stopped, but Aborted happens sometimes (?)
751                         Assert.IsTrue ((ThreadState.Stopped & TestThread.ThreadState) != 0 || (ThreadState.Aborted & TestThread.ThreadState) != 0,
752                                 "#103 Wrong Thread State: " + TestThread.ThreadState.ToString ());
753                 }
754
755                 [Test]
756                 [Ignore ("see comment below.")]
757                 public void CurrentPrincipal_PrincipalPolicy_NoPrincipal () 
758                 {
759                         // note: switching from PrincipalPolicy won't work inside the same thread
760                         // because as soon as a Principal object is created the Policy doesn't matter anymore
761                         Thread t = new Thread (new ThreadStart (ThreadedPrincipalTest.NoPrincipal));
762                         try {
763                                 t.Start ();
764                                 t.Join ();
765                         } catch {
766 #if MONO_FEATURE_THREAD_ABORT
767                                 t.Abort ();
768 #else
769                                 t.Interrupt ();
770 #endif
771                         }
772                 }
773
774                 [Test]
775                 [Ignore ("see comment below.")]
776                 public void CurrentPrincipal_PrincipalPolicy_UnauthenticatedPrincipal () 
777                 {
778                         // note: switching from PrincipalPolicy won't work inside the same thread
779                         // because as soon as a Principal object is created the Policy doesn't matter anymore
780                         Thread t = new Thread (new ThreadStart (ThreadedPrincipalTest.UnauthenticatedPrincipal));
781                         try {
782                                 t.Start ();
783                                 t.Join ();
784                         } catch {
785 #if MONO_FEATURE_THREAD_ABORT
786                                 t.Abort ();
787 #else
788                                 t.Interrupt ();
789 #endif
790                         }
791                 }
792
793                 [Test]
794                 public void CurrentPrincipal_PrincipalPolicy_WindowsPrincipal () 
795                 {
796                         // note: switching from PrincipalPolicy won't work inside the same thread
797                         // because as soon as a Principal object is created the Policy doesn't matter anymore
798                         Thread t = new Thread (new ThreadStart (ThreadedPrincipalTest.WindowsPrincipal));
799                         try {
800                                 t.Start ();
801                                 t.Join ();
802                         } catch {
803 #if MONO_FEATURE_THREAD_ABORT
804                                 t.Abort ();
805 #else
806                                 t.Interrupt ();
807 #endif
808                         }
809                 }
810                 
811                 [Test]
812                 public void IPrincipal_CopyOnNewThread () 
813                 {
814                         Thread.CurrentPrincipal = new GenericPrincipal (new GenericIdentity ("bad"), null);
815                         Thread t = new Thread (new ThreadStart (ThreadedPrincipalTest.CopyOnNewThread));
816                         try {
817                                 Thread.CurrentPrincipal = new GenericPrincipal (new GenericIdentity ("good"), null);
818                                 t.Start ();
819                                 t.Join ();
820                         } catch {
821 #if MONO_FEATURE_THREAD_ABORT
822                                 t.Abort ();
823 #else
824                                 t.Interrupt ();
825 #endif
826                         }
827                 }
828
829                 int counter = 0;
830
831 #if MONO_FEATURE_THREAD_SUSPEND_RESUME
832                 [Test]
833                 public void TestSuspend ()
834                 {
835                         Thread t = new Thread (new ThreadStart (DoCount));
836                         t.IsBackground = true;
837                         t.Start ();
838                         
839                         CheckIsRunning ("t1", t);
840                         
841                         t.Suspend ();
842                         WaitSuspended ("t2", t);
843                         
844                         CheckIsNotRunning ("t3", t);
845                         
846                         t.Resume ();
847                         WaitResumed ("t4", t);
848                         
849                         CheckIsRunning ("t5", t);
850                         
851                         t.Abort ();
852                         TestUtil.WaitForNotAlive (t, "wait13");
853                         CheckIsNotRunning ("t6", t);
854                 }
855 #endif
856                 
857 #if MONO_FEATURE_THREAD_SUSPEND_RESUME && MONO_FEATURE_THREAD_ABORT
858                 [Test]
859                 [Category("NotDotNet")] // On MS, ThreadStateException is thrown on Abort: "Thread is suspended; attempting to abort"
860                 public void TestSuspendAbort ()
861                 {
862                         if (is_win32 && is_mono)
863                                 Assert.Fail ("This test fails on Win32. The test should be fixed.");
864
865                         Thread t = new Thread (new ThreadStart (DoCount));
866                         t.IsBackground = true;
867                         t.Start ();
868                         
869                         CheckIsRunning ("t1", t);
870                         
871                         t.Suspend ();
872                         WaitSuspended ("t2", t);
873                         
874                         CheckIsNotRunning ("t3", t);
875                         
876                         t.Abort ();
877                         
878                         int n=0;
879                         while (t.IsAlive && n < 200) {
880                                 Thread.Sleep (10);
881                                 n++;
882                         }
883
884                         Assert.IsTrue (n < 200, "Timeout while waiting for abort");
885                         
886                         CheckIsNotRunning ("t6", t);
887                 }
888 #endif
889
890                 [Test]
891                 public void Test_Interrupt ()
892                 {
893                         ManualResetEvent mre = new ManualResetEvent (false);
894                         bool interruptedExceptionThrown = false;
895
896                         ThreadPool.QueueUserWorkItem (Test_Interrupt_Worker, Thread.CurrentThread);
897
898                         try {
899                                 try {
900                                         mre.WaitOne (3000);
901                                 } finally {
902                                         try {
903                                                 mre.WaitOne (0);
904                                         } catch (ThreadInterruptedException) {
905                                                 Assert.Fail ("ThreadInterruptedException thrown twice");
906                                         }
907                                 }
908                         } catch (ThreadInterruptedException) {
909                                 interruptedExceptionThrown = true;
910                         }
911
912                         Assert.IsTrue (interruptedExceptionThrown, "ThreadInterruptedException expected.");
913                 }
914
915                 [Test]
916                 [ExpectedException (typeof (ArgumentNullException))]
917                 public void TestQueueUserWorkItemNullCallback ()
918                 {
919                         ThreadPool.QueueUserWorkItem (null, null);
920                 }
921
922                 private void Test_Interrupt_Worker (object o)
923                 {
924                         Thread t = o as Thread;
925                         Thread.Sleep (100);
926                         t.Interrupt ();
927                 }
928                 
929                 [Test]
930                 [Category ("NotDotNet")] // it crashes nunit.
931                 public void Test_InterruptCurrentThread ()
932                 {
933                         ManualResetEvent mre = new ManualResetEvent (false);
934                         bool interruptedExceptionThrown = false;
935
936                         Thread.CurrentThread.Interrupt ();
937                         try {
938                                 mre.WaitOne (0);
939                                 Assert.Fail ();
940                         } catch (ThreadInterruptedException) {
941                         }
942                 }
943
944                 [Test]
945                 public void GetNamedDataSlotTest ()
946                 {
947                         Assert.IsNotNull (Thread.GetNamedDataSlot ("te#st"), "#1");
948                         Assert.AreSame (Thread.GetNamedDataSlot ("te#st"), Thread.GetNamedDataSlot ("te#st"), "#2");
949                 }
950
951                 class DomainClass : MarshalByRefObject {
952                         Thread m_thread;
953                         bool success;
954
955                         public bool Run () {
956                                 m_thread = new Thread(ThreadProc);
957                                 m_thread.Start(Thread.CurrentThread);
958                                 m_thread.Join();
959                                 return success;
960                         }
961
962                         public void ThreadProc (object arg) {
963                                 success = m_thread == Thread.CurrentThread;
964                         }
965                 }
966
967                 [Test]
968                 public void CurrentThread_Domains ()
969                 {
970                         AppDomain ad = AppDomain.CreateDomain ("foo");
971                         ad.Load (typeof (DomainClass).Assembly.GetName ());
972                         var o = (DomainClass)ad.CreateInstanceAndUnwrap (typeof (DomainClass).Assembly.FullName, typeof (DomainClass).FullName);
973                         Assert.IsTrue (o.Run ());
974                         AppDomain.Unload (ad);
975                 }
976
977                 void CheckIsRunning (string s, Thread t)
978                 {
979                         int c = counter;
980                         Thread.Sleep (100);
981                         Assert.IsTrue (counter > c, s);
982                 }
983                 
984                 void CheckIsNotRunning (string s, Thread t)
985                 {
986                         int c = counter;
987                         Thread.Sleep (100);
988                         Assert.AreEqual (counter, c, s);
989                 }
990                 
991                 void WaitSuspended (string s, Thread t)
992                 {
993                         int n=0;
994                         ThreadState state = t.ThreadState;
995                         while ((state & ThreadState.Suspended) == 0) {
996                                 Assert.IsTrue ((state & ThreadState.SuspendRequested) != 0, s + ": expected SuspendRequested state");
997                                 Thread.Sleep (10);
998                                 n++;
999                                 Assert.IsTrue (n < 100, s + ": failed to suspend");
1000                                 state = t.ThreadState;
1001                         }
1002                         Assert.IsTrue ((state & ThreadState.SuspendRequested) == 0, s + ": SuspendRequested state not expected");
1003                 }
1004                 
1005                 void WaitResumed (string s, Thread t)
1006                 {
1007                         int n=0;
1008                         while ((t.ThreadState & ThreadState.Suspended) != 0) {
1009                                 Thread.Sleep (10);
1010                                 n++;
1011                                 Assert.IsTrue (n < 100, s + ": failed to resume");
1012                         }
1013                 }
1014                 
1015                 public void DoCount ()
1016                 {
1017                         while (true) {
1018                                 counter++;
1019                                 Thread.Sleep (1);
1020                         }
1021                 }
1022         }
1023
1024         [TestFixture]
1025         public class ThreadStateTest {
1026                 void Start ()
1027                 {
1028                 }
1029
1030                 [Test] // bug #81720
1031                 public void IsBackGround ()
1032                 {
1033                         Thread t1 = new Thread (new ThreadStart (Start));
1034                         Assert.AreEqual (ThreadState.Unstarted, t1.ThreadState, "#A1");
1035                         Assert.IsFalse (t1.IsBackground, "#A2");
1036                         t1.Start ();
1037                         t1.Join ();
1038                         Assert.AreEqual (ThreadState.Stopped, t1.ThreadState, "#A3");
1039
1040                         try {
1041                                 bool isBackGround = t1.IsBackground;
1042                                 Assert.Fail ("#A4: " + isBackGround.ToString ());
1043                         } catch (ThreadStateException ex) {
1044                                 Assert.AreEqual (typeof (ThreadStateException), ex.GetType (), "#A5");
1045                                 Assert.IsNull (ex.InnerException, "#A6");
1046                                 Assert.IsNotNull (ex.Message, "#A7");
1047                         }
1048
1049                         Thread t2 = new Thread (new ThreadStart (Start));
1050                         Assert.AreEqual (ThreadState.Unstarted, t2.ThreadState, "#B1");
1051                         t2.IsBackground = true;
1052                         Assert.AreEqual (ThreadState.Unstarted | ThreadState.Background, t2.ThreadState, "#B2");
1053                         Assert.IsTrue (t2.IsBackground, "#B3");
1054                         t2.Start ();
1055                         t2.Join ();
1056                         Assert.AreEqual (ThreadState.Stopped, t2.ThreadState, "#B4");
1057
1058                         try {
1059                                 bool isBackGround = t2.IsBackground;
1060                                 Assert.Fail ("#B5: " + isBackGround.ToString ());
1061                         } catch (ThreadStateException ex) {
1062                                 Assert.AreEqual (typeof (ThreadStateException), ex.GetType (), "#B6");
1063                                 Assert.IsNull (ex.InnerException, "#B7");
1064                                 Assert.IsNotNull (ex.Message, "#B8");
1065                         }
1066                 }
1067         }
1068
1069         [TestFixture]
1070         [Serializable]
1071         public class ThreadTest_ManagedThreadId
1072         {
1073                 AppDomain ad1;
1074                 AppDomain ad2;
1075                 MBRO mbro = new MBRO ();
1076
1077                 class MBRO : MarshalByRefObject {
1078                         public int id_a1;
1079                         public int id_b1;
1080                         public int id_b2;
1081                         public string ad_a1;
1082                         public string ad_b1;
1083                         public string ad_b2;
1084                         public string message;
1085                 }
1086 #if !MOBILE
1087                 [Test]
1088                 public void ManagedThreadId_AppDomains ()
1089                 {
1090                         AppDomain currentDomain = AppDomain.CurrentDomain;
1091                         ad1 = AppDomain.CreateDomain ("AppDomain 1", currentDomain.Evidence, currentDomain.SetupInformation);
1092                         ad2 = AppDomain.CreateDomain ("AppDomain 2", currentDomain.Evidence, currentDomain.SetupInformation);
1093
1094                         Thread a = new Thread (ThreadA);
1095                         Thread b = new Thread (ThreadB);
1096                         // execute on AppDomain 1 thread A
1097                         // execute on AppDomain 2 thread B
1098                         // execute on AppDomain 1 thread B - must have same ManagedThreadId as Ad 2 on thread B
1099                         a.Start ();
1100                         a.Join ();
1101                         b.Start ();
1102                         b.Join ();
1103
1104                         AppDomain.Unload (ad1);
1105                         AppDomain.Unload (ad2);
1106
1107                         if (mbro.message != null)
1108                                 Assert.Fail (mbro.message);
1109
1110                         // 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);
1111
1112                         Assert.AreEqual ("AppDomain 1", mbro.ad_a1, "Name #1");
1113                         Assert.AreEqual ("AppDomain 1", mbro.ad_b1, "Name #2");
1114                         Assert.AreEqual ("AppDomain 2", mbro.ad_b2, "Name #3");
1115
1116                         Assert.AreNotEqual (mbro.id_a1, mbro.id_b1, "Id #1");
1117                         Assert.AreNotEqual (mbro.id_a1, mbro.id_b2, "Id #2");
1118                         Assert.AreEqual (mbro.id_b1, mbro.id_b2, "Id #3");
1119
1120                         Assert.AreNotEqual (mbro.id_a1, Thread.CurrentThread.ManagedThreadId, "Id #4");
1121                         Assert.AreNotEqual (mbro.id_b1, Thread.CurrentThread.ManagedThreadId, "Id #5");
1122                         Assert.AreNotEqual (mbro.id_b2, Thread.CurrentThread.ManagedThreadId, "Id #6");
1123                         Assert.AreNotEqual (mbro.ad_a1, AppDomain.CurrentDomain.FriendlyName, "Name #4");
1124                         Assert.AreNotEqual (mbro.ad_b1, AppDomain.CurrentDomain.FriendlyName, "Name #5");
1125                         Assert.AreNotEqual (mbro.ad_b2, AppDomain.CurrentDomain.FriendlyName, "Name #6");
1126                 }
1127 #endif
1128                 void A1 ()
1129                 {
1130                         mbro.id_a1 = Thread.CurrentThread.ManagedThreadId;
1131                         mbro.ad_a1 = AppDomain.CurrentDomain.FriendlyName;
1132                 }
1133                 
1134                 void B2 ()
1135                 {
1136                         mbro.id_b2 = Thread.CurrentThread.ManagedThreadId;
1137                         mbro.ad_b2 = AppDomain.CurrentDomain.FriendlyName;
1138                 }
1139
1140                 void B1 ()
1141                 {
1142                         mbro.id_b1 = Thread.CurrentThread.ManagedThreadId;
1143                         mbro.ad_b1 = AppDomain.CurrentDomain.FriendlyName;
1144                 }
1145
1146                 void ThreadA (object obj)
1147                 {
1148                         // Console.WriteLine ("ThreadA");
1149                         try {
1150                                 ad1.DoCallBack (A1);
1151                         } catch (Exception ex) {
1152                                 mbro.message = string.Format ("ThreadA exception: {0}", ex);
1153                         }
1154                         // Console.WriteLine ("ThreadA Done");
1155                 }
1156
1157                 void ThreadB (object obj)
1158                 {
1159                         // Console.WriteLine ("ThreadB");
1160                         try {
1161                                 ad2.DoCallBack (B2);
1162                                 ad1.DoCallBack (B1);
1163                         } catch (Exception ex) {
1164                                 mbro.message = string.Format ("ThreadB exception: {0}", ex);
1165                         }
1166                         // Console.WriteLine ("ThreadB Done");
1167                 }
1168         }
1169
1170         [TestFixture]
1171         public class ThreadApartmentTest
1172         {
1173                 void Start ()
1174                 {
1175                 }
1176
1177                 [Test] // bug #81658
1178                 public void ApartmentState_StoppedThread ()
1179                 {
1180                         Thread t1 = new Thread (new ThreadStart (Start));
1181                         t1.Start ();
1182                         t1.Join ();
1183                         try {
1184                                 ApartmentState state = t1.ApartmentState;
1185                                 Assert.Fail ("#A1: " + state.ToString ());
1186                         } catch (ThreadStateException ex) {
1187                                 Assert.AreEqual (typeof (ThreadStateException), ex.GetType (), "#A2");
1188                                 Assert.IsNull (ex.InnerException, "#A3");
1189                                 Assert.IsNotNull (ex.Message, "#A4");
1190                         }
1191
1192                         Thread t2 = new Thread (new ThreadStart (Start));
1193                         t2.IsBackground = true;
1194                         t2.Start ();
1195                         t2.Join ();
1196                         try {
1197                                 ApartmentState state = t2.ApartmentState;
1198                                 Assert.Fail ("#B1: " + state.ToString ());
1199                         } catch (ThreadStateException ex) {
1200                                 Assert.AreEqual (typeof (ThreadStateException), ex.GetType (), "#B2");
1201                                 Assert.IsNull (ex.InnerException, "#B3");
1202                                 Assert.IsNotNull (ex.Message, "#B4");
1203                         }
1204                 }
1205
1206                 [Test]
1207                 public void ApartmentState_BackGround ()
1208                 {
1209                         Thread t1 = new Thread (new ThreadStart (Start));
1210                         t1.IsBackground = true;
1211                         Assert.AreEqual (ApartmentState.Unknown, t1.ApartmentState, "#1");
1212                         t1.ApartmentState = ApartmentState.STA;
1213                         Assert.AreEqual (ApartmentState.STA, t1.ApartmentState, "#2");
1214                 }
1215
1216                 [Test]
1217                 public void TestApartmentState ()
1218                 {
1219                         Thread t1 = new Thread (new ThreadStart (Start));
1220                         Thread t2 = new Thread (new ThreadStart (Start));
1221                         Thread t3 = new Thread (new ThreadStart (Start));
1222
1223                         Assert.AreEqual (ApartmentState.Unknown, t1.ApartmentState, "Thread1 Default");
1224                         Assert.AreEqual (ApartmentState.Unknown, t2.ApartmentState, "Thread2 Default");
1225                         Assert.AreEqual (ApartmentState.Unknown, t3.ApartmentState, "Thread3 Default");
1226
1227                         t1.ApartmentState = ApartmentState.STA;
1228                         Assert.AreEqual (ApartmentState.STA, t1.ApartmentState, "Thread1 Set Once");
1229                         t1.ApartmentState = ApartmentState.MTA;
1230                         Assert.AreEqual (ApartmentState.STA, t1.ApartmentState, "Thread1 Set Twice");
1231
1232                         t2.ApartmentState = ApartmentState.MTA;
1233                         Assert.AreEqual (ApartmentState.MTA, t2.ApartmentState, "Thread2 Set Once");
1234                         t2.ApartmentState = ApartmentState.STA;
1235                         Assert.AreEqual (ApartmentState.MTA, t2.ApartmentState, "Thread2 Set Twice");
1236
1237                         bool exception_occured = false;
1238                         try {
1239                                 t3.ApartmentState = ApartmentState.Unknown;
1240                         }
1241                         catch (Exception) {
1242                                 exception_occured = true;
1243                         }
1244                         Assert.AreEqual (ApartmentState.Unknown, t3.ApartmentState, "Thread3 Set Invalid");
1245                         Assert.IsFalse (exception_occured, "Thread3 Set Invalid Exception Occured");
1246
1247                         t1.Start ();
1248                         exception_occured = false;
1249                         try {
1250                                 t1.ApartmentState = ApartmentState.STA;
1251                         }
1252                         catch (Exception) {
1253                                 exception_occured = true;
1254                         }
1255                         Assert.IsTrue (exception_occured, "Thread1 Started Invalid Exception Occured");
1256                 }
1257
1258                 [Test]
1259                 public void TestSetApartmentStateSameState ()
1260                 {
1261                         Thread t1 = new Thread (new ThreadStart (Start));
1262                         t1.SetApartmentState (ApartmentState.STA);
1263                         Assert.AreEqual (ApartmentState.STA, t1.ApartmentState, "Thread1 Set Once");
1264
1265                         t1.SetApartmentState (ApartmentState.STA);
1266                         Assert.AreEqual (ApartmentState.STA, t1.ApartmentState, "Thread1 Set twice");
1267                 }
1268
1269                 [Test]
1270                 [ExpectedException(typeof(InvalidOperationException))]
1271                 public void TestSetApartmentStateDiffState ()
1272                 {
1273                         Thread t1 = new Thread (new ThreadStart (Start));
1274                         t1.SetApartmentState (ApartmentState.STA);
1275                         Assert.AreEqual (ApartmentState.STA, t1.ApartmentState, "Thread1 Set Once");
1276
1277                         t1.SetApartmentState (ApartmentState.MTA);
1278                 }
1279
1280                 [Test]
1281                 public void TestTrySetApartmentState ()
1282                 {
1283                         Thread t1 = new Thread (new ThreadStart (Start));
1284                         t1.SetApartmentState (ApartmentState.STA);
1285                         Assert.AreEqual (ApartmentState.STA, t1.ApartmentState, "#1");
1286
1287                         bool result = t1.TrySetApartmentState (ApartmentState.MTA);
1288                         Assert.IsFalse (result, "#2");
1289
1290                         result = t1.TrySetApartmentState (ApartmentState.STA);
1291                         Assert.IsTrue (result, "#3");
1292                 }
1293
1294                 [Test]
1295                 public void TestTrySetApartmentStateRunning ()
1296                 {
1297                         Thread t1 = new Thread (new ThreadStart (Start));
1298                         t1.SetApartmentState (ApartmentState.STA);
1299                         Assert.AreEqual (ApartmentState.STA, t1.ApartmentState, "#1");
1300
1301                         t1.Start ();
1302
1303                         try {
1304                                 t1.TrySetApartmentState (ApartmentState.STA);
1305                                 Assert.Fail ("#2");
1306                         } catch (ThreadStateException) {
1307                         }
1308
1309                         t1.Join ();
1310                 }
1311
1312                 [Test]
1313                 public void Volatile () {
1314                         double v3 = 55667;
1315                         Thread.VolatileWrite (ref v3, double.MaxValue);
1316                         Assert.AreEqual (v3, double.MaxValue);
1317
1318                         float v4 = 1;
1319                         Thread.VolatileWrite (ref v4, float.MaxValue);
1320                         Assert.AreEqual (v4, float.MaxValue);
1321                 }
1322
1323                 [Test]
1324                 public void Culture ()
1325                 {
1326                         Assert.IsNotNull (Thread.CurrentThread.CurrentCulture, "CurrentCulture");
1327                         Assert.IsNotNull (Thread.CurrentThread.CurrentUICulture, "CurrentUICulture");
1328                 }
1329
1330                 [Test]
1331                 public void ThreadStartSimple ()
1332                 {
1333                         int i = 0;
1334                         Thread t = new Thread (delegate () {
1335                                 // ensure the NSAutoreleasePool works
1336                                 i++;
1337                         });
1338                         t.Start ();
1339                         t.Join ();
1340                         Assert.AreEqual (1, i, "ThreadStart");
1341                 }
1342
1343                 [Test]
1344                 public void ParametrizedThreadStart ()
1345                 {
1346                         int i = 0;
1347                         object arg = null;
1348                         Thread t = new Thread (delegate (object obj) {
1349                                 // ensure the NSAutoreleasePool works
1350                                 i++;
1351                                 arg = obj;
1352                         });
1353                         t.Start (this);
1354                         t.Join ();
1355
1356                         Assert.AreEqual (1, i, "ParametrizedThreadStart");
1357                         Assert.AreEqual (this, arg, "obj");     
1358                 }               
1359
1360                 [Test]
1361                 public void SetNameTpThread () {
1362                         ThreadPool.QueueUserWorkItem(new WaitCallback(ThreadProc));
1363                 }
1364
1365                 static void ThreadProc(Object stateInfo) {
1366                         Thread.CurrentThread.Name = "My Worker";
1367                 }
1368
1369                 [Test]
1370                 public void GetStackTraces () {
1371                         var m = typeof (Thread).GetMethod ("Mono_GetStackTraces", BindingFlags.NonPublic|BindingFlags.Static);
1372                         if (m != null) {
1373                                 var res = (Dictionary<Thread,SD.StackTrace>)typeof (Thread).GetMethod ("Mono_GetStackTraces", BindingFlags.NonPublic|BindingFlags.Static).Invoke (null, null);
1374                                 foreach (var t in res.Keys) {
1375                                         var st = res [t].ToString ();
1376                                 }
1377                         }
1378                 }
1379         }
1380
1381         public class TestUtil
1382         {
1383                 public static void WaitForNotAlive (Thread t, string s)
1384                 {
1385                         WhileAlive (t, true, s);
1386                 }
1387                 
1388                 public static void WaitForAlive (Thread t, string s)
1389                 {
1390                         WhileAlive (t, false, s);
1391                 }
1392                 
1393                 public static bool WaitForAliveOrStop (Thread t, string s)
1394                 {
1395                         return WhileAliveOrStop (t, false, s);
1396                 }
1397                 
1398                 public static void WhileAlive (Thread t, bool alive, string s)
1399                 {
1400                         DateTime ti = DateTime.Now;
1401                         while (t.IsAlive == alive) {
1402                                 if ((DateTime.Now - ti).TotalSeconds > 10) {
1403                                         if (alive) Assert.Fail ("Timeout while waiting for not alive state. " + s);
1404                                         else Assert.Fail ("Timeout while waiting for alive state. " + s);
1405                                 }
1406                         }
1407                 }
1408
1409                 public static bool WhileAliveOrStop (Thread t, bool alive, string s)
1410                 {
1411                         DateTime ti = DateTime.Now;
1412                         while (t.IsAlive == alive) {
1413                                 if (t.ThreadState == ThreadState.Stopped)
1414                                         return false;
1415
1416                                 if ((DateTime.Now - ti).TotalSeconds > 10) {
1417                                         if (alive) Assert.Fail ("Timeout while waiting for not alive state. " + s);
1418                                         else Assert.Fail ("Timeout while waiting for alive state. " + s);
1419                                 }
1420                         }
1421
1422                         return true;
1423                 }
1424         }
1425 }