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