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