Merge pull request #2179 from BogdanovKirill/enummodulesfix
[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                 void CheckIsRunning (string s, Thread t)
865                 {
866                         int c = counter;
867                         Thread.Sleep (100);
868                         Assert.IsTrue (counter > c, s);
869                 }
870                 
871                 void CheckIsNotRunning (string s, Thread t)
872                 {
873                         int c = counter;
874                         Thread.Sleep (100);
875                         Assert.AreEqual (counter, c, s);
876                 }
877                 
878                 void WaitSuspended (string s, Thread t)
879                 {
880                         int n=0;
881                         ThreadState state = t.ThreadState;
882                         while ((state & ThreadState.Suspended) == 0) {
883                                 Assert.IsTrue ((state & ThreadState.SuspendRequested) != 0, s + ": expected SuspendRequested state");
884                                 Thread.Sleep (10);
885                                 n++;
886                                 Assert.IsTrue (n < 100, s + ": failed to suspend");
887                                 state = t.ThreadState;
888                         }
889                         Assert.IsTrue ((state & ThreadState.SuspendRequested) == 0, s + ": SuspendRequested state not expected");
890                 }
891                 
892                 void WaitResumed (string s, Thread t)
893                 {
894                         int n=0;
895                         while ((t.ThreadState & ThreadState.Suspended) != 0) {
896                                 Thread.Sleep (10);
897                                 n++;
898                                 Assert.IsTrue (n < 100, s + ": failed to resume");
899                         }
900                 }
901                 
902                 public void DoCount ()
903                 {
904                         while (true) {
905                                 counter++;
906                                 Thread.Sleep (1);
907                         }
908                 }
909         }
910
911         [TestFixture]
912         public class ThreadStateTest {
913                 void Start ()
914                 {
915                 }
916
917                 [Test] // bug #81720
918                 public void IsBackGround ()
919                 {
920                         Thread t1 = new Thread (new ThreadStart (Start));
921                         Assert.AreEqual (ThreadState.Unstarted, t1.ThreadState, "#A1");
922                         Assert.IsFalse (t1.IsBackground, "#A2");
923                         t1.Start ();
924                         t1.Join ();
925                         Assert.AreEqual (ThreadState.Stopped, t1.ThreadState, "#A3");
926
927                         try {
928                                 bool isBackGround = t1.IsBackground;
929                                 Assert.Fail ("#A4: " + isBackGround.ToString ());
930                         } catch (ThreadStateException ex) {
931                                 Assert.AreEqual (typeof (ThreadStateException), ex.GetType (), "#A5");
932                                 Assert.IsNull (ex.InnerException, "#A6");
933                                 Assert.IsNotNull (ex.Message, "#A7");
934                         }
935
936                         Thread t2 = new Thread (new ThreadStart (Start));
937                         Assert.AreEqual (ThreadState.Unstarted, t2.ThreadState, "#B1");
938                         t2.IsBackground = true;
939                         Assert.AreEqual (ThreadState.Unstarted | ThreadState.Background, t2.ThreadState, "#B2");
940                         Assert.IsTrue (t2.IsBackground, "#B3");
941                         t2.Start ();
942                         t2.Join ();
943                         Assert.AreEqual (ThreadState.Stopped, t2.ThreadState, "#B4");
944
945                         try {
946                                 bool isBackGround = t2.IsBackground;
947                                 Assert.Fail ("#B5: " + isBackGround.ToString ());
948                         } catch (ThreadStateException ex) {
949                                 Assert.AreEqual (typeof (ThreadStateException), ex.GetType (), "#B6");
950                                 Assert.IsNull (ex.InnerException, "#B7");
951                                 Assert.IsNotNull (ex.Message, "#B8");
952                         }
953                 }
954         }
955
956         [TestFixture]
957         [Serializable]
958         public class ThreadTest_ManagedThreadId
959         {
960                 AppDomain ad1;
961                 AppDomain ad2;
962                 MBRO mbro = new MBRO ();
963
964                 class MBRO : MarshalByRefObject {
965                         public int id_a1;
966                         public int id_b1;
967                         public int id_b2;
968                         public string ad_a1;
969                         public string ad_b1;
970                         public string ad_b2;
971                         public string message;
972                 }
973 #if !MOBILE
974                 [Test]
975                 public void ManagedThreadId_AppDomains ()
976                 {
977                         AppDomain currentDomain = AppDomain.CurrentDomain;
978                         ad1 = AppDomain.CreateDomain ("AppDomain 1", currentDomain.Evidence, currentDomain.SetupInformation);
979                         ad2 = AppDomain.CreateDomain ("AppDomain 2", currentDomain.Evidence, currentDomain.SetupInformation);
980
981                         Thread a = new Thread (ThreadA);
982                         Thread b = new Thread (ThreadB);
983                         // execute on AppDomain 1 thread A
984                         // execute on AppDomain 2 thread B
985                         // execute on AppDomain 1 thread B - must have same ManagedThreadId as Ad 2 on thread B
986                         a.Start ();
987                         a.Join ();
988                         b.Start ();
989                         b.Join ();
990
991                         AppDomain.Unload (ad1);
992                         AppDomain.Unload (ad2);
993
994                         if (mbro.message != null)
995                                 Assert.Fail (mbro.message);
996
997                         // 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);
998
999                         Assert.AreEqual ("AppDomain 1", mbro.ad_a1, "Name #1");
1000                         Assert.AreEqual ("AppDomain 1", mbro.ad_b1, "Name #2");
1001                         Assert.AreEqual ("AppDomain 2", mbro.ad_b2, "Name #3");
1002
1003                         Assert.AreNotEqual (mbro.id_a1, mbro.id_b1, "Id #1");
1004                         Assert.AreNotEqual (mbro.id_a1, mbro.id_b2, "Id #2");
1005                         Assert.AreEqual (mbro.id_b1, mbro.id_b2, "Id #3");
1006
1007                         Assert.AreNotEqual (mbro.id_a1, Thread.CurrentThread.ManagedThreadId, "Id #4");
1008                         Assert.AreNotEqual (mbro.id_b1, Thread.CurrentThread.ManagedThreadId, "Id #5");
1009                         Assert.AreNotEqual (mbro.id_b2, Thread.CurrentThread.ManagedThreadId, "Id #6");
1010                         Assert.AreNotEqual (mbro.ad_a1, AppDomain.CurrentDomain.FriendlyName, "Name #4");
1011                         Assert.AreNotEqual (mbro.ad_b1, AppDomain.CurrentDomain.FriendlyName, "Name #5");
1012                         Assert.AreNotEqual (mbro.ad_b2, AppDomain.CurrentDomain.FriendlyName, "Name #6");
1013                 }
1014 #endif
1015                 void A1 ()
1016                 {
1017                         mbro.id_a1 = Thread.CurrentThread.ManagedThreadId;
1018                         mbro.ad_a1 = AppDomain.CurrentDomain.FriendlyName;
1019                 }
1020                 
1021                 void B2 ()
1022                 {
1023                         mbro.id_b2 = Thread.CurrentThread.ManagedThreadId;
1024                         mbro.ad_b2 = AppDomain.CurrentDomain.FriendlyName;
1025                 }
1026
1027                 void B1 ()
1028                 {
1029                         mbro.id_b1 = Thread.CurrentThread.ManagedThreadId;
1030                         mbro.ad_b1 = AppDomain.CurrentDomain.FriendlyName;
1031                 }
1032
1033                 void ThreadA (object obj)
1034                 {
1035                         // Console.WriteLine ("ThreadA");
1036                         try {
1037                                 ad1.DoCallBack (A1);
1038                         } catch (Exception ex) {
1039                                 mbro.message = string.Format ("ThreadA exception: {0}", ex);
1040                         }
1041                         // Console.WriteLine ("ThreadA Done");
1042                 }
1043
1044                 void ThreadB (object obj)
1045                 {
1046                         // Console.WriteLine ("ThreadB");
1047                         try {
1048                                 ad2.DoCallBack (B2);
1049                                 ad1.DoCallBack (B1);
1050                         } catch (Exception ex) {
1051                                 mbro.message = string.Format ("ThreadB exception: {0}", ex);
1052                         }
1053                         // Console.WriteLine ("ThreadB Done");
1054                 }
1055         }
1056
1057         [TestFixture]
1058         public class ThreadApartmentTest
1059         {
1060                 void Start ()
1061                 {
1062                 }
1063
1064                 [Test] // bug #81658
1065                 public void ApartmentState_StoppedThread ()
1066                 {
1067                         Thread t1 = new Thread (new ThreadStart (Start));
1068                         t1.Start ();
1069                         t1.Join ();
1070                         try {
1071                                 ApartmentState state = t1.ApartmentState;
1072                                 Assert.Fail ("#A1: " + state.ToString ());
1073                         } catch (ThreadStateException ex) {
1074                                 Assert.AreEqual (typeof (ThreadStateException), ex.GetType (), "#A2");
1075                                 Assert.IsNull (ex.InnerException, "#A3");
1076                                 Assert.IsNotNull (ex.Message, "#A4");
1077                         }
1078
1079                         Thread t2 = new Thread (new ThreadStart (Start));
1080                         t2.IsBackground = true;
1081                         t2.Start ();
1082                         t2.Join ();
1083                         try {
1084                                 ApartmentState state = t2.ApartmentState;
1085                                 Assert.Fail ("#B1: " + state.ToString ());
1086                         } catch (ThreadStateException ex) {
1087                                 Assert.AreEqual (typeof (ThreadStateException), ex.GetType (), "#B2");
1088                                 Assert.IsNull (ex.InnerException, "#B3");
1089                                 Assert.IsNotNull (ex.Message, "#B4");
1090                         }
1091                 }
1092
1093                 [Test]
1094                 public void ApartmentState_BackGround ()
1095                 {
1096                         Thread t1 = new Thread (new ThreadStart (Start));
1097                         t1.IsBackground = true;
1098                         Assert.AreEqual (ApartmentState.Unknown, t1.ApartmentState, "#1");
1099                         t1.ApartmentState = ApartmentState.STA;
1100                         Assert.AreEqual (ApartmentState.STA, t1.ApartmentState, "#2");
1101                 }
1102
1103                 [Test]
1104                 public void TestApartmentState ()
1105                 {
1106                         Thread t1 = new Thread (new ThreadStart (Start));
1107                         Thread t2 = new Thread (new ThreadStart (Start));
1108                         Thread t3 = new Thread (new ThreadStart (Start));
1109
1110                         Assert.AreEqual (ApartmentState.Unknown, t1.ApartmentState, "Thread1 Default");
1111                         Assert.AreEqual (ApartmentState.Unknown, t2.ApartmentState, "Thread2 Default");
1112                         Assert.AreEqual (ApartmentState.Unknown, t3.ApartmentState, "Thread3 Default");
1113
1114                         t1.ApartmentState = ApartmentState.STA;
1115                         Assert.AreEqual (ApartmentState.STA, t1.ApartmentState, "Thread1 Set Once");
1116                         t1.ApartmentState = ApartmentState.MTA;
1117                         Assert.AreEqual (ApartmentState.STA, t1.ApartmentState, "Thread1 Set Twice");
1118
1119                         t2.ApartmentState = ApartmentState.MTA;
1120                         Assert.AreEqual (ApartmentState.MTA, t2.ApartmentState, "Thread2 Set Once");
1121                         t2.ApartmentState = ApartmentState.STA;
1122                         Assert.AreEqual (ApartmentState.MTA, t2.ApartmentState, "Thread2 Set Twice");
1123
1124                         bool exception_occured = false;
1125                         try {
1126                                 t3.ApartmentState = ApartmentState.Unknown;
1127                         }
1128                         catch (Exception) {
1129                                 exception_occured = true;
1130                         }
1131                         Assert.AreEqual (ApartmentState.Unknown, t3.ApartmentState, "Thread3 Set Invalid");
1132                         Assert.IsFalse (exception_occured, "Thread3 Set Invalid Exception Occured");
1133
1134                         t1.Start ();
1135                         exception_occured = false;
1136                         try {
1137                                 t1.ApartmentState = ApartmentState.STA;
1138                         }
1139                         catch (Exception) {
1140                                 exception_occured = true;
1141                         }
1142                         Assert.IsTrue (exception_occured, "Thread1 Started Invalid Exception Occured");
1143                 }
1144
1145                 [Test]
1146                 public void TestSetApartmentStateSameState ()
1147                 {
1148                         Thread t1 = new Thread (new ThreadStart (Start));
1149                         t1.SetApartmentState (ApartmentState.STA);
1150                         Assert.AreEqual (ApartmentState.STA, t1.ApartmentState, "Thread1 Set Once");
1151
1152                         t1.SetApartmentState (ApartmentState.STA);
1153                         Assert.AreEqual (ApartmentState.STA, t1.ApartmentState, "Thread1 Set twice");
1154                 }
1155
1156                 [Test]
1157                 [ExpectedException(typeof(InvalidOperationException))]
1158                 public void TestSetApartmentStateDiffState ()
1159                 {
1160                         Thread t1 = new Thread (new ThreadStart (Start));
1161                         t1.SetApartmentState (ApartmentState.STA);
1162                         Assert.AreEqual (ApartmentState.STA, t1.ApartmentState, "Thread1 Set Once");
1163
1164                         t1.SetApartmentState (ApartmentState.MTA);
1165                 }
1166
1167                 [Test]
1168                 public void TestTrySetApartmentState ()
1169                 {
1170                         Thread t1 = new Thread (new ThreadStart (Start));
1171                         t1.SetApartmentState (ApartmentState.STA);
1172                         Assert.AreEqual (ApartmentState.STA, t1.ApartmentState, "#1");
1173
1174                         bool result = t1.TrySetApartmentState (ApartmentState.MTA);
1175                         Assert.IsFalse (result, "#2");
1176
1177                         result = t1.TrySetApartmentState (ApartmentState.STA);
1178                         Assert.IsTrue (result, "#3");
1179                 }
1180
1181                 [Test]
1182                 public void TestTrySetApartmentStateRunning ()
1183                 {
1184                         Thread t1 = new Thread (new ThreadStart (Start));
1185                         t1.SetApartmentState (ApartmentState.STA);
1186                         Assert.AreEqual (ApartmentState.STA, t1.ApartmentState, "#1");
1187
1188                         t1.Start ();
1189
1190                         try {
1191                                 t1.TrySetApartmentState (ApartmentState.STA);
1192                                 Assert.Fail ("#2");
1193                         } catch (ThreadStateException) {
1194                         }
1195
1196                         t1.Join ();
1197                 }
1198
1199                 [Test]
1200                 public void Volatile () {
1201                         double v3 = 55667;
1202                         Thread.VolatileWrite (ref v3, double.MaxValue);
1203                         Assert.AreEqual (v3, double.MaxValue);
1204
1205                         float v4 = 1;
1206                         Thread.VolatileWrite (ref v4, float.MaxValue);
1207                         Assert.AreEqual (v4, float.MaxValue);
1208                 }
1209
1210                 [Test]
1211                 public void Culture ()
1212                 {
1213                         Assert.IsNotNull (Thread.CurrentThread.CurrentCulture, "CurrentCulture");
1214                         Assert.IsNotNull (Thread.CurrentThread.CurrentUICulture, "CurrentUICulture");
1215                 }
1216
1217                 [Test]
1218                 public void ThreadStartSimple ()
1219                 {
1220                         int i = 0;
1221                         Thread t = new Thread (delegate () {
1222                                 // ensure the NSAutoreleasePool works
1223                                 i++;
1224                         });
1225                         t.Start ();
1226                         t.Join ();
1227                         Assert.AreEqual (1, i, "ThreadStart");
1228                 }
1229
1230                 [Test]
1231                 public void ParametrizedThreadStart ()
1232                 {
1233                         int i = 0;
1234                         object arg = null;
1235                         Thread t = new Thread (delegate (object obj) {
1236                                 // ensure the NSAutoreleasePool works
1237                                 i++;
1238                                 arg = obj;
1239                         });
1240                         t.Start (this);
1241                         t.Join ();
1242
1243                         Assert.AreEqual (1, i, "ParametrizedThreadStart");
1244                         Assert.AreEqual (this, arg, "obj");     
1245                 }               
1246
1247                 [Test]
1248                 public void SetNameTpThread () {
1249                         ThreadPool.QueueUserWorkItem(new WaitCallback(ThreadProc));
1250                 }
1251
1252                 static void ThreadProc(Object stateInfo) {
1253                         Thread.CurrentThread.Name = "My Worker";
1254                 }
1255
1256                 [Test]
1257                 public void GetStackTraces () {
1258                         var m = typeof (Thread).GetMethod ("Mono_GetStackTraces", BindingFlags.NonPublic|BindingFlags.Static);
1259                         if (m != null) {
1260                                 var res = (Dictionary<Thread,SD.StackTrace>)typeof (Thread).GetMethod ("Mono_GetStackTraces", BindingFlags.NonPublic|BindingFlags.Static).Invoke (null, null);
1261                                 foreach (var t in res.Keys) {
1262                                         var st = res [t].ToString ();
1263                                 }
1264                         }
1265                 }
1266         }
1267
1268         public class TestUtil
1269         {
1270                 public static void WaitForNotAlive (Thread t, string s)
1271                 {
1272                         WhileAlive (t, true, s);
1273                 }
1274                 
1275                 public static void WaitForAlive (Thread t, string s)
1276                 {
1277                         WhileAlive (t, false, s);
1278                 }
1279                 
1280                 public static bool WaitForAliveOrStop (Thread t, string s)
1281                 {
1282                         return WhileAliveOrStop (t, false, s);
1283                 }
1284                 
1285                 public static void WhileAlive (Thread t, bool alive, string s)
1286                 {
1287                         DateTime ti = DateTime.Now;
1288                         while (t.IsAlive == alive) {
1289                                 if ((DateTime.Now - ti).TotalSeconds > 10) {
1290                                         if (alive) Assert.Fail ("Timeout while waiting for not alive state. " + s);
1291                                         else Assert.Fail ("Timeout while waiting for alive state. " + s);
1292                                 }
1293                         }
1294                 }
1295
1296                 public static bool WhileAliveOrStop (Thread t, bool alive, string s)
1297                 {
1298                         DateTime ti = DateTime.Now;
1299                         while (t.IsAlive == alive) {
1300                                 if (t.ThreadState == ThreadState.Stopped)
1301                                         return false;
1302
1303                                 if ((DateTime.Now - ti).TotalSeconds > 10) {
1304                                         if (alive) Assert.Fail ("Timeout while waiting for not alive state. " + s);
1305                                         else Assert.Fail ("Timeout while waiting for alive state. " + s);
1306                                 }
1307                         }
1308
1309                         return true;
1310                 }
1311         }
1312 }