Merge pull request #439 from mono-soc-2012/garyb/iconfix
[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 #if !TARGET_JVM // AppDomain.SetPrincipalPolicy not supported for TARGET_JVM
29                         AppDomain.CurrentDomain.SetPrincipalPolicy (PrincipalPolicy.NoPrincipal);
30 #endif
31                         IPrincipal p = Thread.CurrentPrincipal;
32                         Assert.IsNull (p, "#1");
33
34                         Thread.CurrentPrincipal = new GenericPrincipal (new GenericIdentity ("mono"), null);
35                         Assert.IsNotNull (Thread.CurrentPrincipal, "#2");
36
37                         Thread.CurrentPrincipal = null;
38                         Assert.IsNull (Thread.CurrentPrincipal, "#3");
39                         // in this case we can return to null
40                 }
41
42 #if !TARGET_JVM // AppDomain.SetPrincipalPolicy not supported for TARGET_JVM
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 #endif // TARGET_JVM
80
81                 public static void CopyOnNewThread ()
82                 {
83                         Assert.IsNotNull (Thread.CurrentPrincipal, "#1");
84                         Assert.AreEqual ("good", Thread.CurrentPrincipal.Identity.Name, "#2");
85                 }
86         }
87
88         [TestFixture]
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 #if NET_2_0
267                         Assert.IsTrue (hA1 > 0, "#A1");
268 #endif
269                         tA.Start ();
270                         int hA2 = tA.GetHashCode ();
271                         Assert.AreEqual (hA1, hA2, "#A2");
272                         tA.Join ();
273                         int hA3 = tA.GetHashCode ();
274                         Assert.AreEqual (hA1, hA3, "#A3");
275 #if NET_2_0
276                         Assert.AreEqual (hA1, tA.ManagedThreadId, "#A4");
277 #endif
278
279                         test1 = new C1Test ();
280                         Thread tB = new Thread (new ThreadStart (test1.TestMethod));
281                         int hB1 = tB.GetHashCode ();
282 #if NET_2_0
283                         Assert.IsTrue (hB1 > 0, "#B1");
284 #endif
285                         tB.Start ();
286                         int hB2 = tB.GetHashCode ();
287                         Assert.AreEqual (hB1, hB2, "#B2");
288                         tB.Join ();
289                         int hB3 = tB.GetHashCode ();
290                         Assert.AreEqual (hB1, hB3, "#B3");
291 #if NET_2_0
292                         Assert.AreEqual (hB1, tB.ManagedThreadId, "#B4");
293 #endif
294                         Assert.IsFalse (hA2 == hB2, "#B5");
295                 }
296
297 #if NET_2_0
298                 [Test] // bug #82700
299                 public void ManagedThreadId ()
300                 {
301                         C1Test test1 = new C1Test ();
302                         Thread t1 = new Thread (new ThreadStart (test1.TestMethod));
303                         int mtA1 = t1.ManagedThreadId;
304                         t1.Start ();
305                         int mtA2 = t1.ManagedThreadId;
306                         t1.Join ();
307                         int mtA3 = t1.ManagedThreadId;
308                         Assert.AreEqual (mtA1, mtA2, "#A1");
309                         Assert.AreEqual (mtA2, mtA3, "#A2");
310
311                         test1 = new C1Test ();
312                         Thread t2 = new Thread (new ThreadStart (test1.TestMethod));
313                         int mtB1 = t2.ManagedThreadId;
314                         t2.Start ();
315                         int mtB2 = t2.ManagedThreadId;
316                         t2.Join ();
317                         int mtB3 = t2.ManagedThreadId;
318                         Assert.AreEqual (mtB1, mtB2, "#B1");
319                         Assert.AreEqual (mtB2, mtB3, "#B2");
320                         Assert.IsFalse (mtB1 == mtA1, "#B3");
321                 }
322 #endif
323
324                 [Test]
325                 [Category ("NotDotNet")] // it hangs.
326                 public void TestStart()
327                 {
328                         if (is_win32 && is_mono)
329                                 Assert.Fail ("This test fails on Win32. The test should be fixed.");
330                 {
331                         C1Test test1 = new C1Test();
332                         Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
333                         TestThread.Start();
334                         TestThread.Join();
335                         Assert.AreEqual (10, test1.cnt, "#1");
336                 }
337                 {
338                         C2Test test1 = new C2Test();
339                         Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
340                         TestThread.Start();
341                         TestThread.Abort();
342                         try {
343                                 TestThread.Start();
344                                 Assert.Fail ("#2");
345                         } catch (ThreadStateException) {
346                         }
347                 }
348                 {
349                         C2Test test1 = new C2Test();
350                         Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
351                         TestThread.Start();
352                         while (!test1.run) {
353                         }
354                         bool started = (TestThread.ThreadState == ThreadState.Running);
355                         Assert.AreEqual (started, test1.run, "#15 Thread Is not in the correct state: ");
356                         TestThread.Abort();
357                 }
358                 }
359
360                 [Test]
361                 public void TestApartmentState ()
362                 {
363                         if (is_win32 && is_mono)
364                                 Assert.Fail ("This test fails on mono on win32. Our runtime should be fixed.");
365
366                         C2Test test1 = new C2Test();
367                         Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
368                         Assert.AreEqual (ApartmentState.Unknown, TestThread.ApartmentState, "#1");
369                         TestThread.Start();
370                         TestUtil.WaitForAlive (TestThread, "wait5");
371 #if NET_2_0
372                         Assert.AreEqual (ApartmentState.MTA, TestThread.ApartmentState, "#2");
373 #else
374                         Assert.AreEqual (ApartmentState.Unknown, TestThread.ApartmentState, "#3");
375 #endif
376                         TestThread.Abort();
377                 }
378
379                 [Test]
380                 public void TestPriority1()
381                 {
382                         if (is_win32 && is_mono)
383                                 Assert.Fail ("This test fails on mono on Win32. Our runtime should be fixed.");
384
385                         C2Test test1 = new C2Test();
386                         Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
387                         try {
388                                 TestThread.Priority=ThreadPriority.BelowNormal;
389                                 ThreadPriority after = TestThread.Priority;
390                                 TestThread.Start();
391                                 TestUtil.WaitForAlive (TestThread, "wait7");
392                                 ThreadPriority before = TestThread.Priority;
393                                 Assert.AreEqual (before, after, "#41 Unexpected Priority Change: ");
394                         } finally {
395                                 TestThread.Abort();
396                         }
397                 }
398
399                 [Test]
400                 [Category ("NotDotNet")] // on MS, Thread is still in AbortRequested state when Start is invoked
401                 public void AbortUnstarted ()
402                 {
403                         C2Test test1 = new C2Test();
404                         Thread th = new Thread (new ThreadStart (test1.TestMethod));
405                         th.Abort ();
406                         th.Start ();
407                 }
408
409                 [Test]
410                 [Category ("NotDotNet")] // on MS, ThreadState is immediately Stopped after Abort
411                 [Category ("NotWorking")] // this is a MonoTODO -> no support for Priority
412                 public void TestPriority2()
413                 {
414                         C2Test test1 = new C2Test();
415                         Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
416                         try {
417                                 Assert.AreEqual (ThreadPriority.Normal, TestThread.Priority, "#42 Incorrect Priority in New thread: ");
418                                 TestThread.Start();
419                                 TestUtil.WaitForAliveOrStop (TestThread, "wait8");
420                                 Assert.AreEqual (ThreadPriority.Normal, TestThread.Priority, "#43 Incorrect Priority in Started thread: ");
421                         } finally {
422                                 TestThread.Abort();
423                         }
424                         Assert.AreEqual (ThreadPriority.Normal, TestThread.Priority, "#44 Incorrect Priority in Aborted thread: ");
425                 }
426
427                 [Test]
428                 [Category ("NotWorking")] // this is a MonoTODO -> no support for Priority
429                 public void TestPriority3()
430                 {
431                         C2Test test1 = new C2Test();
432                         Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
433                         try {
434                                 TestThread.Start();
435                                 TestThread.Priority = ThreadPriority.Lowest;
436                                 Assert.AreEqual (ThreadPriority.Lowest, TestThread.Priority, "#45A Incorrect Priority:");
437                                 TestThread.Priority = ThreadPriority.BelowNormal;
438                                 Assert.AreEqual (ThreadPriority.BelowNormal, TestThread.Priority, "#45B Incorrect Priority:");
439                                 TestThread.Priority = ThreadPriority.Normal;
440                                 Assert.AreEqual (ThreadPriority.Normal, TestThread.Priority, "#45C Incorrect Priority:");
441                                 TestThread.Priority = ThreadPriority.AboveNormal;
442                                 Assert.AreEqual (ThreadPriority.AboveNormal, TestThread.Priority, "#45D Incorrect Priority:");
443                                 TestThread.Priority = ThreadPriority.Highest;
444                                 Assert.AreEqual (ThreadPriority.Highest, TestThread.Priority, "#45E Incorrect Priority:");
445                         }
446                         finally {
447                                 TestThread.Abort();
448                         }
449                 }
450
451                 [Test]
452                 public void TestUndivisibleByPageSizeMaxStackSize ()
453                 {
454                         const int undivisible_stacksize = 1048573;
455
456                         var thread = new Thread (new ThreadStart (delegate {}), undivisible_stacksize);
457                         thread.Start ();
458                         thread.Join ();
459                 }
460
461                 [Test]
462                 public void TestIsBackground1 ()
463                 {
464                         if (is_win32 && is_mono)
465                                 Assert.Fail ("This test fails on mono on Win32. Our runtime should be fixed.");
466
467                         C2Test test1 = new C2Test();
468                         Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
469                         try {
470                                 TestThread.Start();
471                                 TestUtil.WaitForAlive (TestThread, "wait9");
472                                 bool state = TestThread.IsBackground;
473                                 Assert.IsFalse (state, "#51 IsBackground not set at the default state: ");
474                         } finally {
475                                 TestThread.Abort();
476                         }
477                 }
478
479                 [Test]
480                 public void TestIsBackground2 ()
481                 {
482                         C2Test test1 = new C2Test();
483                         Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
484                         TestThread.IsBackground = true;
485                         try {
486                                 TestThread.Start();
487                         } finally {
488                                 TestThread.Abort();
489                         }
490                         
491                         if (TestThread.IsAlive) {
492                                 try {
493                                         Assert.IsTrue (TestThread.IsBackground, "#52 Is Background Changed to Start ");
494                                 } catch (ThreadStateException) {
495                                         // Ignore if thread died meantime
496                                 }
497                         }
498                 }
499
500                 [Test]
501                 public void TestName()
502                 {
503                         if (is_win32 && is_mono)
504                                 Assert.Fail ("This test fails on mono on Win32. Our runtime should be fixed.");
505
506                         C2Test test1 = new C2Test();
507                         Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
508                         try {
509                                 TestThread.Start();
510                                 TestUtil.WaitForAlive (TestThread, "wait10");
511                                 string name = TestThread.Name;
512                                 Assert.IsNull (name, "#61 Name set when mustn't be set: ");
513                                 string newname = "Testing....";
514                                 TestThread.Name = newname;
515                                 Assert.AreEqual (newname, TestThread.Name, "#62 Name not set when must be set: ");
516                         } finally {
517                                 TestThread.Abort();
518                         }
519                 }
520
521                 [Test]
522                 public void Name ()
523                 {
524                         Thread t = new Thread (new ThreadStart (Name));
525                         Assert.IsNull (t.Name, "Name-1");
526                         t.Name = null;
527                         Assert.IsNull (t.Name, "Name-2");
528                 }
529
530                 [Test]
531                 [ExpectedException (typeof (InvalidOperationException))]
532                 public void ReName ()
533                 {
534                         Thread t = new Thread (new ThreadStart (ReName));
535                         t.Name = "a";
536                         t.Name = "b";
537                 }
538
539                 [Test]
540                 public void TestNestedThreads1()
541                 {
542                         C3Test test1 = new C3Test();
543                         Thread TestThread = new Thread(new ThreadStart(test1.TestMethod1));
544                         try {
545                                 TestThread.Start();
546                                 TestUtil.WaitForAlive (TestThread, "wait11");
547                         } finally {
548                                 TestThread.Abort();
549                         }
550                 }
551
552                 [Test]
553                 public void TestNestedThreads2()
554                 {
555                         C4Test test1 = new C4Test();
556                         Thread TestThread = new Thread(new ThreadStart(test1.TestMethod1));
557                         try {
558                                 TestThread.Start();
559                         } finally {
560                                 TestThread.Abort();
561                         }
562                 }
563
564                 [Test]
565                 public void TestJoin1()
566                 {
567                         C1Test test1 = new C1Test();
568                         C1Test test2 = new C1Test();
569                         Thread thread1 = new Thread(new ThreadStart(test1.TestMethod));
570                         Thread thread2 = new Thread(new ThreadStart(test1.TestMethod2));
571                         try {
572                                 thread1.Start();
573                                 thread2.Start();
574                                 thread2.Join();
575                         } finally {
576                                 thread1.Abort();
577                                 thread2.Abort();
578                         }
579                 }
580
581                 [Test]
582                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
583                 public void Join_Int32_Negative ()
584                 {
585                         // -1 is Timeout.Infinite
586                         Thread.CurrentThread.Join (-2);
587                 }
588
589                 [Test]
590                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
591                 public void Join_TimeSpan_Negative ()
592                 {
593                         Thread.CurrentThread.Join (Negative);
594                 }
595
596                 [Test]
597                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
598                 public void Join_TimeSpan_TooLarge ()
599                 {
600                         Thread.CurrentThread.Join (TooLarge);
601                 }
602
603                 [Test]
604                 public void Join_TimeSpan_SmallNegative ()
605                 {
606                         Thread.CurrentThread.Join (SmallNegative);
607                 }
608
609                 [Test]
610                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
611                 public void Sleep_Int32_Negative ()
612                 {
613                         // -1 is Timeout.Infinite
614                         Thread.Sleep (-2);
615                 }
616
617                 [Test]
618                 public void Sleep_TimeSpan_SmallNegative ()
619                 {
620                         Thread.Sleep (SmallNegative);
621                 }
622
623                 [Test]
624                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
625                 public void Sleep_TimeSpan_Negative ()
626                 {
627                         Thread.Sleep (Negative);
628                 }
629
630                 [Test]
631                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
632                 public void Sleep_TimeSpan_TooLarge ()
633                 {
634                         Thread.Sleep (TooLarge);
635                 }
636
637                 [Test]
638                 public void SpinWait ()
639                 {
640                         // no exception for negative numbers
641                         Thread.SpinWait (Int32.MinValue);
642                         Thread.SpinWait (0);
643                 }
644
645                 [Test]
646                 public void TestThreadState ()
647                 {
648                         if (is_win32 && is_mono)
649                                 Assert.Fail ("This test fails on mono on Win32. Our runtime should be fixed.");
650
651                         //TODO: Test The rest of the possible transitions
652                         C2Test test1 = new C2Test();
653                         Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
654                         Assert.AreEqual (ThreadState.Unstarted, TestThread.ThreadState, "#101 Wrong Thread State");
655                         try {
656                                 TestThread.Start();
657                                 //while(!TestThread.IsAlive); //In the MS Documentation this is not necessary
658                                                                                           //but in the MS SDK it is
659                                 Assert.IsTrue (TestThread.ThreadState == ThreadState.Running || (TestThread.ThreadState & ThreadState.Unstarted) != 0,
660                                         "#102 Wrong Thread State: " + TestThread.ThreadState.ToString ());
661                         } finally {
662                                 TestThread.Abort();
663                         }
664                         
665                         TestUtil.WaitForNotAlive (TestThread, "wait12");
666                         // Docs say state will be Stopped, but Aborted happens sometimes (?)
667                         Assert.IsTrue ((ThreadState.Stopped & TestThread.ThreadState) != 0 || (ThreadState.Aborted & TestThread.ThreadState) != 0,
668                                 "#103 Wrong Thread State: " + TestThread.ThreadState.ToString ());
669                 }
670
671                 [Test]
672                 [Ignore ("see comment below.")]
673                 public void CurrentPrincipal_PrincipalPolicy_NoPrincipal () 
674                 {
675                         // note: switching from PrincipalPolicy won't work inside the same thread
676                         // because as soon as a Principal object is created the Policy doesn't matter anymore
677                         Thread t = new Thread (new ThreadStart (ThreadedPrincipalTest.NoPrincipal));
678                         try {
679                                 t.Start ();
680                                 t.Join ();
681                         } catch {
682                                 t.Abort ();
683                         }
684                 }
685
686 #if !TARGET_JVM // AppDomain.SetPrincipalPolicy not supported for TARGET_JVM
687                 [Test]
688                 [Ignore ("see comment below.")]
689                 public void CurrentPrincipal_PrincipalPolicy_UnauthenticatedPrincipal () 
690                 {
691                         // note: switching from PrincipalPolicy won't work inside the same thread
692                         // because as soon as a Principal object is created the Policy doesn't matter anymore
693                         Thread t = new Thread (new ThreadStart (ThreadedPrincipalTest.UnauthenticatedPrincipal));
694                         try {
695                                 t.Start ();
696                                 t.Join ();
697                         } catch {
698                                 t.Abort ();
699                         }
700                 }
701
702                 [Test]
703                 public void CurrentPrincipal_PrincipalPolicy_WindowsPrincipal () 
704                 {
705                         // note: switching from PrincipalPolicy won't work inside the same thread
706                         // because as soon as a Principal object is created the Policy doesn't matter anymore
707                         Thread t = new Thread (new ThreadStart (ThreadedPrincipalTest.WindowsPrincipal));
708                         try {
709                                 t.Start ();
710                                 t.Join ();
711                         } catch {
712                                 t.Abort ();
713                         }
714                 }
715 #endif // TARGET_JVM
716                 
717                 [Test]
718                 public void IPrincipal_CopyOnNewThread () 
719                 {
720                         Thread.CurrentPrincipal = new GenericPrincipal (new GenericIdentity ("bad"), null);
721                         Thread t = new Thread (new ThreadStart (ThreadedPrincipalTest.CopyOnNewThread));
722                         try {
723                                 Thread.CurrentPrincipal = new GenericPrincipal (new GenericIdentity ("good"), null);
724                                 t.Start ();
725                                 t.Join ();
726                         } catch {
727                                 t.Abort ();
728                         }
729                 }
730
731                 int counter = 0;
732
733                 [Test]
734                 public void TestSuspend ()
735                 {
736                         Thread t = new Thread (new ThreadStart (DoCount));
737                         t.IsBackground = true;
738                         t.Start ();
739                         
740                         CheckIsRunning ("t1", t);
741                         
742                         t.Suspend ();
743                         WaitSuspended ("t2", t);
744                         
745                         CheckIsNotRunning ("t3", t);
746                         
747                         t.Resume ();
748                         WaitResumed ("t4", t);
749                         
750                         CheckIsRunning ("t5", t);
751                         
752                         t.Abort ();
753                         TestUtil.WaitForNotAlive (t, "wait13");
754                         CheckIsNotRunning ("t6", t);
755                 }
756
757                 [Test]
758                 [Category("NotDotNet")] // On MS, ThreadStateException is thrown on Abort: "Thread is suspended; attempting to abort"
759                 public void TestSuspendAbort ()
760                 {
761                         if (is_win32 && is_mono)
762                                 Assert.Fail ("This test fails on Win32. The test should be fixed.");
763
764                         Thread t = new Thread (new ThreadStart (DoCount));
765                         t.IsBackground = true;
766                         t.Start ();
767                         
768                         CheckIsRunning ("t1", t);
769                         
770                         t.Suspend ();
771                         WaitSuspended ("t2", t);
772                         
773                         CheckIsNotRunning ("t3", t);
774                         
775                         t.Abort ();
776                         
777                         int n=0;
778                         while (t.IsAlive && n < 200) {
779                                 Thread.Sleep (10);
780                                 n++;
781                         }
782
783                         Assert.IsTrue (n < 200, "Timeout while waiting for abort");
784                         
785                         CheckIsNotRunning ("t6", t);
786                 }
787
788                 [Test]
789                 public void Test_Interrupt ()
790                 {
791                         bool interruptedExceptionThrown = false;
792                         ThreadPool.QueueUserWorkItem (Test_Interrupt_Worker, Thread.CurrentThread);
793
794                         try {
795                                 try {
796                                         Thread.Sleep (3000);
797                                 } finally {
798                                         try {
799                                                 Thread.Sleep (0);
800                                         } catch (ThreadInterruptedException) {
801                                                 Assert.Fail ("ThreadInterruptedException thrown twice");
802                                         }
803                                 }
804                         } catch (ThreadInterruptedException) {
805                                 interruptedExceptionThrown = true;
806                         }
807
808                         Assert.IsTrue (interruptedExceptionThrown, "ThreadInterruptedException expected.");
809                 }
810
811                 [Test]
812                 [ExpectedException (typeof (ArgumentNullException))]
813                 public void TestQueueUserWorkItemNullCallback ()
814                 {
815                         ThreadPool.QueueUserWorkItem (null, null);
816                 }
817
818                 private void Test_Interrupt_Worker (object o)
819                 {
820                         Thread t = o as Thread;
821                         Thread.Sleep (100);
822                         t.Interrupt ();
823                 }
824                 
825                 [Test]
826                 [Category ("NotDotNet")] // it crashes nunit.
827                 public void Test_InterruptCurrentThread ()
828                 {
829                         bool interruptedExceptionThrown = false;
830
831                         Thread.CurrentThread.Interrupt ();
832                         try {
833                                 Thread.Sleep (0);
834                                 Assert.Fail ();
835                         } catch (ThreadInterruptedException) {
836                         }
837                 }
838
839                 void CheckIsRunning (string s, Thread t)
840                 {
841                         int c = counter;
842                         Thread.Sleep (100);
843                         Assert.IsTrue (counter > c, s);
844                 }
845                 
846                 void CheckIsNotRunning (string s, Thread t)
847                 {
848                         int c = counter;
849                         Thread.Sleep (100);
850                         Assert.AreEqual (counter, c, s);
851                 }
852                 
853                 void WaitSuspended (string s, Thread t)
854                 {
855                         int n=0;
856                         ThreadState state = t.ThreadState;
857                         while ((state & ThreadState.Suspended) == 0) {
858                                 Assert.IsTrue ((state & ThreadState.SuspendRequested) != 0, s + ": expected SuspendRequested state");
859                                 Thread.Sleep (10);
860                                 n++;
861                                 Assert.IsTrue (n < 100, s + ": failed to suspend");
862                                 state = t.ThreadState;
863                         }
864                         Assert.IsTrue ((state & ThreadState.SuspendRequested) == 0, s + ": SuspendRequested state not expected");
865                 }
866                 
867                 void WaitResumed (string s, Thread t)
868                 {
869                         int n=0;
870                         while ((t.ThreadState & ThreadState.Suspended) != 0) {
871                                 Thread.Sleep (10);
872                                 n++;
873                                 Assert.IsTrue (n < 100, s + ": failed to resume");
874                         }
875                 }
876                 
877                 public void DoCount ()
878                 {
879                         while (true) {
880                                 counter++;
881                                 Thread.Sleep (1);
882                         }
883                 }
884         }
885
886         [TestFixture]
887         public class ThreadStateTest {
888                 void Start ()
889                 {
890                 }
891
892                 [Test] // bug #81720
893                 public void IsBackGround ()
894                 {
895                         Thread t1 = new Thread (new ThreadStart (Start));
896                         Assert.AreEqual (ThreadState.Unstarted, t1.ThreadState, "#A1");
897                         Assert.IsFalse (t1.IsBackground, "#A2");
898                         t1.Start ();
899                         t1.Join ();
900                         Assert.AreEqual (ThreadState.Stopped, t1.ThreadState, "#A3");
901
902                         try {
903                                 bool isBackGround = t1.IsBackground;
904                                 Assert.Fail ("#A4: " + isBackGround.ToString ());
905                         } catch (ThreadStateException ex) {
906                                 Assert.AreEqual (typeof (ThreadStateException), ex.GetType (), "#A5");
907                                 Assert.IsNull (ex.InnerException, "#A6");
908                                 Assert.IsNotNull (ex.Message, "#A7");
909                         }
910
911                         Thread t2 = new Thread (new ThreadStart (Start));
912                         Assert.AreEqual (ThreadState.Unstarted, t2.ThreadState, "#B1");
913                         t2.IsBackground = true;
914                         Assert.AreEqual (ThreadState.Unstarted | ThreadState.Background, t2.ThreadState, "#B2");
915                         Assert.IsTrue (t2.IsBackground, "#B3");
916                         t2.Start ();
917                         t2.Join ();
918                         Assert.AreEqual (ThreadState.Stopped, t2.ThreadState, "#B4");
919
920                         try {
921                                 bool isBackGround = t2.IsBackground;
922                                 Assert.Fail ("#B5: " + isBackGround.ToString ());
923                         } catch (ThreadStateException ex) {
924                                 Assert.AreEqual (typeof (ThreadStateException), ex.GetType (), "#B6");
925                                 Assert.IsNull (ex.InnerException, "#B7");
926                                 Assert.IsNotNull (ex.Message, "#B8");
927                         }
928                 }
929         }
930
931         [TestFixture]
932         [Serializable]
933         public class ThreadTest_ManagedThreadId
934         {
935                 AppDomain ad1;
936                 AppDomain ad2;
937                 MBRO mbro = new MBRO ();
938
939                 class MBRO : MarshalByRefObject {
940                         public int id_a1;
941                         public int id_b1;
942                         public int id_b2;
943                         public string ad_a1;
944                         public string ad_b1;
945                         public string ad_b2;
946                         public string message;
947                 }
948
949                 [Test]
950                 public void ManagedThreadId_AppDomains ()
951                 {
952                         AppDomain currentDomain = AppDomain.CurrentDomain;
953                         ad1 = AppDomain.CreateDomain ("AppDomain 1", currentDomain.Evidence, currentDomain.SetupInformation);
954                         ad2 = AppDomain.CreateDomain ("AppDomain 2", currentDomain.Evidence, currentDomain.SetupInformation);
955
956                         Thread a = new Thread (ThreadA);
957                         Thread b = new Thread (ThreadB);
958                         // execute on AppDomain 1 thread A
959                         // execute on AppDomain 2 thread B
960                         // execute on AppDomain 1 thread B - must have same ManagedThreadId as Ad 2 on thread B
961                         a.Start ();
962                         a.Join ();
963                         b.Start ();
964                         b.Join ();
965
966                         AppDomain.Unload (ad1);
967                         AppDomain.Unload (ad2);
968
969                         if (mbro.message != null)
970                                 Assert.Fail (mbro.message);
971
972                         // 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);
973
974                         Assert.AreEqual ("AppDomain 1", mbro.ad_a1, "Name #1");
975                         Assert.AreEqual ("AppDomain 1", mbro.ad_b1, "Name #2");
976                         Assert.AreEqual ("AppDomain 2", mbro.ad_b2, "Name #3");
977
978                         Assert.AreNotEqual (mbro.id_a1, mbro.id_b1, "Id #1");
979                         Assert.AreNotEqual (mbro.id_a1, mbro.id_b2, "Id #2");
980                         Assert.AreEqual (mbro.id_b1, mbro.id_b2, "Id #3");
981
982                         Assert.AreNotEqual (mbro.id_a1, Thread.CurrentThread.ManagedThreadId, "Id #4");
983                         Assert.AreNotEqual (mbro.id_b1, Thread.CurrentThread.ManagedThreadId, "Id #5");
984                         Assert.AreNotEqual (mbro.id_b2, Thread.CurrentThread.ManagedThreadId, "Id #6");
985                         Assert.AreNotEqual (mbro.ad_a1, AppDomain.CurrentDomain.FriendlyName, "Name #4");
986                         Assert.AreNotEqual (mbro.ad_b1, AppDomain.CurrentDomain.FriendlyName, "Name #5");
987                         Assert.AreNotEqual (mbro.ad_b2, AppDomain.CurrentDomain.FriendlyName, "Name #6");
988                 }
989
990                 void A1 ()
991                 {
992                         mbro.id_a1 = Thread.CurrentThread.ManagedThreadId;
993                         mbro.ad_a1 = AppDomain.CurrentDomain.FriendlyName;
994                 }
995                 
996                 void B2 ()
997                 {
998                         mbro.id_b2 = Thread.CurrentThread.ManagedThreadId;
999                         mbro.ad_b2 = AppDomain.CurrentDomain.FriendlyName;
1000                 }
1001
1002                 void B1 ()
1003                 {
1004                         mbro.id_b1 = Thread.CurrentThread.ManagedThreadId;
1005                         mbro.ad_b1 = AppDomain.CurrentDomain.FriendlyName;
1006                 }
1007
1008                 void ThreadA (object obj)
1009                 {
1010                         // Console.WriteLine ("ThreadA");
1011                         try {
1012                                 ad1.DoCallBack (A1);
1013                         } catch (Exception ex) {
1014                                 mbro.message = string.Format ("ThreadA exception: {0}", ex);
1015                         }
1016                         // Console.WriteLine ("ThreadA Done");
1017                 }
1018
1019                 void ThreadB (object obj)
1020                 {
1021                         // Console.WriteLine ("ThreadB");
1022                         try {
1023                                 ad2.DoCallBack (B2);
1024                                 ad1.DoCallBack (B1);
1025                         } catch (Exception ex) {
1026                                 mbro.message = string.Format ("ThreadB exception: {0}", ex);
1027                         }
1028                         // Console.WriteLine ("ThreadB Done");
1029                 }
1030         }
1031
1032         [TestFixture]
1033         public class ThreadApartmentTest
1034         {
1035                 void Start ()
1036                 {
1037                 }
1038
1039                 [Test] // bug #81658
1040                 public void ApartmentState_StoppedThread ()
1041                 {
1042                         Thread t1 = new Thread (new ThreadStart (Start));
1043                         t1.Start ();
1044                         t1.Join ();
1045                         try {
1046                                 ApartmentState state = t1.ApartmentState;
1047                                 Assert.Fail ("#A1: " + state.ToString ());
1048                         } catch (ThreadStateException ex) {
1049                                 Assert.AreEqual (typeof (ThreadStateException), ex.GetType (), "#A2");
1050                                 Assert.IsNull (ex.InnerException, "#A3");
1051                                 Assert.IsNotNull (ex.Message, "#A4");
1052                         }
1053
1054                         Thread t2 = new Thread (new ThreadStart (Start));
1055                         t2.IsBackground = true;
1056                         t2.Start ();
1057                         t2.Join ();
1058                         try {
1059                                 ApartmentState state = t2.ApartmentState;
1060                                 Assert.Fail ("#B1: " + state.ToString ());
1061                         } catch (ThreadStateException ex) {
1062                                 Assert.AreEqual (typeof (ThreadStateException), ex.GetType (), "#B2");
1063                                 Assert.IsNull (ex.InnerException, "#B3");
1064                                 Assert.IsNotNull (ex.Message, "#B4");
1065                         }
1066                 }
1067
1068                 [Test]
1069                 public void ApartmentState_BackGround ()
1070                 {
1071                         Thread t1 = new Thread (new ThreadStart (Start));
1072                         t1.IsBackground = true;
1073                         Assert.AreEqual (ApartmentState.Unknown, t1.ApartmentState, "#1");
1074                         t1.ApartmentState = ApartmentState.STA;
1075                         Assert.AreEqual (ApartmentState.STA, t1.ApartmentState, "#2");
1076                 }
1077
1078                 [Test]
1079                 public void TestApartmentState ()
1080                 {
1081                         Thread t1 = new Thread (new ThreadStart (Start));
1082                         Thread t2 = new Thread (new ThreadStart (Start));
1083                         Thread t3 = new Thread (new ThreadStart (Start));
1084
1085                         Assert.AreEqual (ApartmentState.Unknown, t1.ApartmentState, "Thread1 Default");
1086                         Assert.AreEqual (ApartmentState.Unknown, t2.ApartmentState, "Thread2 Default");
1087                         Assert.AreEqual (ApartmentState.Unknown, t3.ApartmentState, "Thread3 Default");
1088
1089                         t1.ApartmentState = ApartmentState.STA;
1090                         Assert.AreEqual (ApartmentState.STA, t1.ApartmentState, "Thread1 Set Once");
1091                         t1.ApartmentState = ApartmentState.MTA;
1092                         Assert.AreEqual (ApartmentState.STA, t1.ApartmentState, "Thread1 Set Twice");
1093
1094                         t2.ApartmentState = ApartmentState.MTA;
1095                         Assert.AreEqual (ApartmentState.MTA, t2.ApartmentState, "Thread2 Set Once");
1096                         t2.ApartmentState = ApartmentState.STA;
1097                         Assert.AreEqual (ApartmentState.MTA, t2.ApartmentState, "Thread2 Set Twice");
1098
1099                         bool exception_occured = false;
1100                         try {
1101                                 t3.ApartmentState = ApartmentState.Unknown;
1102                         }
1103                         catch (Exception) {
1104                                 exception_occured = true;
1105                         }
1106                         Assert.AreEqual (ApartmentState.Unknown, t3.ApartmentState, "Thread3 Set Invalid");
1107 #if NET_2_0
1108                         Assert.IsFalse (exception_occured, "Thread3 Set Invalid Exception Occured");
1109 #else
1110                         Assert.IsTrue (exception_occured, "Thread3 Set Invalid Exception Occured");
1111 #endif
1112
1113                         t1.Start ();
1114                         exception_occured = false;
1115                         try {
1116                                 t1.ApartmentState = ApartmentState.STA;
1117                         }
1118                         catch (Exception) {
1119                                 exception_occured = true;
1120                         }
1121                         Assert.IsTrue (exception_occured, "Thread1 Started Invalid Exception Occured");
1122                 }
1123
1124                 [Test]
1125                 public void Volatile () {
1126                         double v3 = 55667;
1127                         Thread.VolatileWrite (ref v3, double.MaxValue);
1128                         Assert.AreEqual (v3, double.MaxValue);
1129
1130                         float v4 = 1;
1131                         Thread.VolatileWrite (ref v4, float.MaxValue);
1132                         Assert.AreEqual (v4, float.MaxValue);
1133                 }
1134
1135                 [Test]
1136                 public void SetNameTpThread () {
1137                         ThreadPool.QueueUserWorkItem(new WaitCallback(ThreadProc));
1138                 }
1139
1140                 static void ThreadProc(Object stateInfo) {
1141                         Thread.CurrentThread.Name = "My Worker";
1142                 }
1143         }
1144
1145         public class TestUtil
1146         {
1147                 public static void WaitForNotAlive (Thread t, string s)
1148                 {
1149                         WhileAlive (t, true, s);
1150                 }
1151                 
1152                 public static void WaitForAlive (Thread t, string s)
1153                 {
1154                         WhileAlive (t, false, s);
1155                 }
1156                 
1157                 public static bool WaitForAliveOrStop (Thread t, string s)
1158                 {
1159                         return WhileAliveOrStop (t, false, s);
1160                 }
1161                 
1162                 public static void WhileAlive (Thread t, bool alive, string s)
1163                 {
1164                         DateTime ti = DateTime.Now;
1165                         while (t.IsAlive == alive) {
1166                                 if ((DateTime.Now - ti).TotalSeconds > 10) {
1167                                         if (alive) Assert.Fail ("Timeout while waiting for not alive state. " + s);
1168                                         else Assert.Fail ("Timeout while waiting for alive state. " + s);
1169                                 }
1170                         }
1171                 }
1172
1173                 public static bool WhileAliveOrStop (Thread t, bool alive, string s)
1174                 {
1175                         DateTime ti = DateTime.Now;
1176                         while (t.IsAlive == alive) {
1177                                 if (t.ThreadState == ThreadState.Stopped)
1178                                         return false;
1179
1180                                 if ((DateTime.Now - ti).TotalSeconds > 10) {
1181                                         if (alive) Assert.Fail ("Timeout while waiting for not alive state. " + s);
1182                                         else Assert.Fail ("Timeout while waiting for alive state. " + s);
1183                                 }
1184                         }
1185
1186                         return true;
1187                 }
1188         }
1189 }