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