1 // ThreadTest.cs - NUnit Test Cases for the System.Threading.Thread class
4 // Eduardo Garcia Cebollero (kiwnix@yahoo.es)
5 // Sebastien Pouliot <sebastien@ximian.com>
7 // (C) Eduardo Garcia Cebollero.
8 // (C) Ximian, Inc. http://www.ximian.com
9 // (C) 2004 Novell (http://www.novell.com)
13 using System.Security.Principal;
14 using System.Threading;
16 using NUnit.Framework;
18 namespace MonoTests.System.Threading
20 // These tests seem to hang the 2.0 framework. So they are disabled for now
21 // Don't reenable them until you can run a few thousand times on an SMP box.
22 [Category ("NotWorking")]
23 public class ThreadedPrincipalTest
25 public static void NoPrincipal ()
27 #if !TARGET_JVM // AppDomain.SetPrincipalPolicy not supported for TARGET_JVM
28 AppDomain.CurrentDomain.SetPrincipalPolicy (PrincipalPolicy.NoPrincipal);
30 IPrincipal p = Thread.CurrentPrincipal;
31 Assert.IsNull (p, "#1");
33 Thread.CurrentPrincipal = new GenericPrincipal (new GenericIdentity ("mono"), null);
34 Assert.IsNotNull (Thread.CurrentPrincipal, "#2");
36 Thread.CurrentPrincipal = null;
37 Assert.IsNull (Thread.CurrentPrincipal, "#3");
38 // in this case we can return to null
41 #if !TARGET_JVM // AppDomain.SetPrincipalPolicy not supported for TARGET_JVM
42 public static void UnauthenticatedPrincipal ()
44 AppDomain.CurrentDomain.SetPrincipalPolicy (PrincipalPolicy.UnauthenticatedPrincipal);
45 IPrincipal p = Thread.CurrentPrincipal;
46 Assert.IsNotNull (p, "#1");
47 Assert.IsTrue ((p is GenericPrincipal), "#2");
48 Assert.AreEqual (String.Empty, p.Identity.Name, "#3");
49 Assert.AreEqual (String.Empty, p.Identity.AuthenticationType, "#4");
50 Assert.IsFalse (p.Identity.IsAuthenticated, "#5");
52 Thread.CurrentPrincipal = new GenericPrincipal (new GenericIdentity ("mono"), null);
53 Assert.IsNotNull (Thread.CurrentPrincipal, "#6");
55 Thread.CurrentPrincipal = null;
56 Assert.IsNotNull (Thread.CurrentPrincipal, "#7");
57 // in this case we can't return to null
60 public static void WindowsPrincipal ()
62 AppDomain.CurrentDomain.SetPrincipalPolicy (PrincipalPolicy.WindowsPrincipal);
63 IPrincipal p = Thread.CurrentPrincipal;
64 Assert.IsNotNull (p, "#1");
65 Assert.IsTrue ((p is WindowsPrincipal), "#2");
66 Assert.IsNotNull (p.Identity.Name, "#3");
67 Assert.IsNotNull (p.Identity.AuthenticationType, "#4");
68 Assert.IsTrue (p.Identity.IsAuthenticated, "#5");
70 // note: we can switch from a WindowsPrincipal to a GenericPrincipal
71 Thread.CurrentPrincipal = new GenericPrincipal (new GenericIdentity ("mono"), null);
72 Assert.IsNotNull (Thread.CurrentPrincipal, "#6");
74 Thread.CurrentPrincipal = null;
75 Assert.IsNotNull (Thread.CurrentPrincipal, "#7");
76 // in this case we can't return to null
80 public static void CopyOnNewThread ()
82 Assert.IsNotNull (Thread.CurrentPrincipal, "#1");
83 Assert.AreEqual ("good", Thread.CurrentPrincipal.Identity.Name, "#2");
88 [Category ("NotWorking")]
89 public class ThreadTest
91 //Some Classes to test as threads
95 public Thread thread1;
101 thread1 = (Thread)null;
103 endm1 = endm2 = false;
106 public void TestMethod()
114 public void TestMethod2()
116 if (!(thread1==(Thread)null) )
127 public bool run = false;
134 public void TestMethod()
149 public C1Test sub_class;
150 public Thread sub_thread;
154 sub_class = new C1Test();
155 sub_thread = new Thread(new ThreadStart(sub_class.TestMethod));
158 public void TestMethod1()
168 public C1Test class1;
169 public C1Test class2;
170 public Thread thread1;
171 public Thread thread2;
179 class1 = new C1Test();
180 class2 = new C1Test();
181 thread1 = new Thread(new ThreadStart(class1.TestMethod));
182 thread2 = new Thread(new ThreadStart(class2.TestMethod));
185 public void TestMethod1()
188 TestUtil.WaitForAlive (thread1, "wait1");
191 TestUtil.WaitForAlive (thread2, "wait2");
194 TestUtil.WaitForNotAlive (thread1, "wait3");
197 TestUtil.WaitForNotAlive (thread2, "wait4");
201 public void TestMethod2()
208 public void TestCtor1()
210 C1Test test1 = new C1Test();
213 Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
217 Assert.Fail ("#01 Unexpected Exception Thrown: " + e.ToString ());
221 [Category("NotDotNet")]
222 public void TestStart()
225 C1Test test1 = new C1Test();
226 Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
233 Assert.Fail ("#12 Unexpected Exception Thrown: " + e.ToString ());
236 Assert.AreEqual (10, test1.cnt, "#13 Thread Not started");
239 bool errorThrown = false;
240 C2Test test1 = new C2Test();
241 Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
248 catch(ThreadStateException)
252 Assert.IsTrue (errorThrown, "#14 no ThreadStateException trown");
255 C2Test test1 = new C2Test();
256 Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
259 bool started = (TestThread.ThreadState == ThreadState.Running);
260 Assert.AreEqual (started, test1.run, "#15 Thread Is not in the correct state: ");
265 public void TestApartment()
267 C2Test test1 = new C2Test();
268 Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
269 ApartmentState before = TestThread.ApartmentState;
271 TestUtil.WaitForAlive (TestThread, "wait5");
272 ApartmentState after = TestThread.ApartmentState;
274 Assert.AreEqual (before, after, "#21 Apartment State Changed when not needed");
277 [Category("NotDotNet")]
278 public void TestApartmentState()
280 C2Test test1 = new C2Test();
281 Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
282 ApartmentState before = TestThread.ApartmentState;
284 TestUtil.WaitForAlive (TestThread, "wait6");
285 ApartmentState after = TestThread.ApartmentState;
287 Assert.AreEqual (before, after, "#31 Apartment State Changed when not needed: ");
290 public void TestPriority1()
292 C2Test test1 = new C2Test();
293 Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
295 TestThread.Priority=ThreadPriority.BelowNormal;
296 ThreadPriority after = TestThread.Priority;
298 TestUtil.WaitForAlive (TestThread, "wait7");
299 ThreadPriority before = TestThread.Priority;
300 Assert.AreEqual (before, after, "#41 Unexpected Priority Change: ");
308 public void AbortUnstarted ()
310 C2Test test1 = new C2Test();
311 Thread th = new Thread (new ThreadStart (test1.TestMethod));
316 [Category("NotWorking")] // this is a MonoTODO -> no support for Priority
317 public void TestPriority2()
319 C2Test test1 = new C2Test();
320 Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
322 Assert.AreEqual (ThreadPriority.Normal, TestThread.Priority, "#42 Incorrect Priority in New thread: ");
324 TestUtil.WaitForAliveOrStop (TestThread, "wait8");
325 Assert.AreEqual (ThreadPriority.Normal, TestThread.Priority, "#43 Incorrect Priority in Started thread: ");
330 Assert.AreEqual (ThreadPriority.Normal, TestThread.Priority, "#44 Incorrect Priority in Aborted thread: ");
333 [Category("NotWorking")] // this is a MonoTODO -> no support for Priority
334 public void TestPriority3()
336 C2Test test1 = new C2Test();
337 Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
340 TestThread.Priority = ThreadPriority.Lowest;
341 Assert.AreEqual (ThreadPriority.Lowest, TestThread.Priority, "#45A Incorrect Priority:");
342 TestThread.Priority = ThreadPriority.BelowNormal;
343 Assert.AreEqual (ThreadPriority.BelowNormal, TestThread.Priority, "#45B Incorrect Priority:");
344 TestThread.Priority = ThreadPriority.Normal;
345 Assert.AreEqual (ThreadPriority.Normal, TestThread.Priority, "#45C Incorrect Priority:");
346 TestThread.Priority = ThreadPriority.AboveNormal;
347 Assert.AreEqual (ThreadPriority.AboveNormal, TestThread.Priority, "#45D Incorrect Priority:");
348 TestThread.Priority = ThreadPriority.Highest;
349 Assert.AreEqual (ThreadPriority.Highest, TestThread.Priority, "#45E Incorrect Priority:");
356 public void TestIsBackground1()
358 C2Test test1 = new C2Test();
359 Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
362 TestUtil.WaitForAlive (TestThread, "wait9");
363 bool state = TestThread.IsBackground;
364 Assert.IsFalse (state, "#51 IsBackground not set at the default state: ");
371 public void TestIsBackground2()
373 C2Test test1 = new C2Test();
374 Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
375 TestThread.IsBackground = true;
382 Assert.IsTrue (TestThread.IsBackground, "#52 Is Background Changed ot Start ");
386 public void TestName()
388 C2Test test1 = new C2Test();
389 Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
392 TestUtil.WaitForAlive (TestThread, "wait10");
393 string name = TestThread.Name;
394 Assert.IsNull (name, "#61 Name set when mustn't be set: ");
395 string newname = "Testing....";
396 TestThread.Name = newname;
397 Assert.AreEqual (newname, TestThread.Name, "#62 Name not set when must be set: ");
404 [Category("NotDotNet")]
405 public void TestNestedThreads1()
407 C3Test test1 = new C3Test();
408 Thread TestThread = new Thread(new ThreadStart(test1.TestMethod1));
411 TestUtil.WaitForAlive (TestThread, "wait11");
414 Assert.Fail ("#71 Unexpected Exception" + e.Message);
421 public void TestNestedThreads2()
423 C4Test test1 = new C4Test();
424 Thread TestThread = new Thread(new ThreadStart(test1.TestMethod1));
429 Assert.Fail ("#81 Unexpected Exception" + e.ToString());
437 public void TestJoin1()
439 C1Test test1 = new C1Test();
440 C1Test test2 = new C1Test();
441 Thread thread1 = new Thread(new ThreadStart(test1.TestMethod));
442 Thread thread2 = new Thread(new ThreadStart(test1.TestMethod2));
451 Assert.Fail ("#91 Unexpected Exception " + e.ToString());
460 public void TestThreadState()
462 //TODO: Test The rest of the possible transitions
463 C2Test test1 = new C2Test();
464 Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
465 Assert.AreEqual (ThreadState.Unstarted, TestThread.ThreadState, "#101 Wrong Thread State");
468 //while(!TestThread.IsAlive); //In the MS Documentation this is not necessary
469 //but in the MS SDK it is
470 Assert.IsTrue (TestThread.ThreadState == ThreadState.Running || (TestThread.ThreadState & ThreadState.Unstarted) != 0,
471 "#102 Wrong Thread State: " + TestThread.ThreadState.ToString ());
477 TestUtil.WaitForNotAlive (TestThread, "wait12");
478 // Docs say state will be Stopped, but Aborted happens sometimes (?)
479 Assert.IsTrue ((ThreadState.Stopped & TestThread.ThreadState) != 0 || (ThreadState.Aborted & TestThread.ThreadState) != 0,
480 "#103 Wrong Thread State: " + TestThread.ThreadState.ToString ());
484 [Ignore ("see comment below.")]
485 public void CurrentPrincipal_PrincipalPolicy_NoPrincipal ()
487 // note: switching from PrincipalPolicy won't work inside the same thread
488 // because as soon as a Principal object is created the Policy doesn't matter anymore
489 Thread t = new Thread (new ThreadStart (ThreadedPrincipalTest.NoPrincipal));
499 #if !TARGET_JVM // AppDomain.SetPrincipalPolicy not supported for TARGET_JVM
501 [Ignore ("see comment below.")]
502 public void CurrentPrincipal_PrincipalPolicy_UnauthenticatedPrincipal ()
504 // note: switching from PrincipalPolicy won't work inside the same thread
505 // because as soon as a Principal object is created the Policy doesn't matter anymore
506 Thread t = new Thread (new ThreadStart (ThreadedPrincipalTest.UnauthenticatedPrincipal));
517 public void CurrentPrincipal_PrincipalPolicy_WindowsPrincipal ()
519 // note: switching from PrincipalPolicy won't work inside the same thread
520 // because as soon as a Principal object is created the Policy doesn't matter anymore
521 Thread t = new Thread (new ThreadStart (ThreadedPrincipalTest.WindowsPrincipal));
533 public void IPrincipal_CopyOnNewThread ()
535 Thread.CurrentPrincipal = new GenericPrincipal (new GenericIdentity ("bad"), null);
536 Thread t = new Thread (new ThreadStart (ThreadedPrincipalTest.CopyOnNewThread));
538 Thread.CurrentPrincipal = new GenericPrincipal (new GenericIdentity ("good"), null);
549 [Category("NotDotNet")]
550 public void TestSuspend ()
552 Thread t = new Thread (new ThreadStart (DoCount));
553 t.IsBackground = true;
556 CheckIsRunning ("t1", t);
559 WaitSuspended ("t2", t);
561 CheckIsNotRunning ("t3", t);
564 WaitResumed ("t4", t);
566 CheckIsRunning ("t5", t);
569 TestUtil.WaitForNotAlive (t, "wait13");
570 CheckIsNotRunning ("t6", t);
573 [Category("NotDotNet")]
574 [Category("NotWorking")]
575 public void TestSuspendAbort ()
577 Thread t = new Thread (new ThreadStart (DoCount));
578 t.IsBackground = true;
581 CheckIsRunning ("t1", t);
584 WaitSuspended ("t2", t);
586 CheckIsNotRunning ("t3", t);
591 while (t.IsAlive && n < 200) {
596 Assert.IsTrue (n < 200, "Timeout while waiting for abort");
598 CheckIsNotRunning ("t6", t);
601 void CheckIsRunning (string s, Thread t)
605 Assert.IsTrue (counter > c, s);
608 void CheckIsNotRunning (string s, Thread t)
612 Assert.AreEqual (counter, c, s);
615 void WaitSuspended (string s, Thread t)
618 ThreadState state = t.ThreadState;
619 while ((state & ThreadState.Suspended) == 0) {
620 Assert.IsTrue ((state & ThreadState.SuspendRequested) != 0, s + ": expected SuspendRequested state");
623 Assert.IsTrue (n < 100, s + ": failed to suspend");
624 state = t.ThreadState;
626 Assert.IsTrue ((state & ThreadState.SuspendRequested) == 0, s + ": SuspendRequested state not expected");
629 void WaitResumed (string s, Thread t)
632 while ((t.ThreadState & ThreadState.Suspended) != 0) {
635 Assert.IsTrue (n < 100, s + ": failed to resume");
639 public void DoCount ()
649 public class ThreadStateTest {
655 public void IsBackGround ()
657 Thread t1 = new Thread (new ThreadStart (Start));
658 Assert.AreEqual (ThreadState.Unstarted, t1.ThreadState, "#A1");
659 Assert.IsFalse (t1.IsBackground, "#A2");
662 Assert.AreEqual (ThreadState.Stopped, t1.ThreadState, "#A3");
664 // uncomment when bug #81658 is fixed
667 bool isBackGround = t1.IsBackground;
668 Assert.Fail ("#A4: " + isBackGround.ToString ());
669 } catch (ThreadStateException ex) {
670 Assert.AreEqual (typeof (ThreadStateException), ex.GetType (), "#A5");
671 Assert.IsNull (ex.InnerException, "#A6");
672 Assert.IsNotNull (ex.Message, "#A7");
676 Thread t2 = new Thread (new ThreadStart (Start));
677 Assert.AreEqual (ThreadState.Unstarted, t2.ThreadState, "#B1");
678 t2.IsBackground = true;
679 Assert.AreEqual (ThreadState.Unstarted | ThreadState.Background, t2.ThreadState, "#B2");
680 Assert.IsTrue (t2.IsBackground, "#B3");
683 Assert.AreEqual (ThreadState.Stopped, t2.ThreadState, "#B4");
684 // uncomment when bug #81658 is fixed
687 bool isBackGround = t2.IsBackground;
688 Assert.Fail ("#B5: " + isBackGround.ToString ());
689 } catch (ThreadStateException ex) {
690 Assert.AreEqual (typeof (ThreadStateException), ex.GetType (), "#B6");
691 Assert.IsNull (ex.InnerException, "#B7");
692 Assert.IsNotNull (ex.Message, "#B8");
699 public class ThreadApartmentTest
706 [Category ("NotWorking")]
707 public void ApartmentState_StoppedThread ()
709 Thread t1 = new Thread (new ThreadStart (Start));
713 ApartmentState state = t1.ApartmentState;
714 Assert.Fail ("#A1: " + state.ToString ());
715 } catch (ThreadStateException ex) {
716 Assert.AreEqual (typeof (ThreadStateException), ex.GetType (), "#A2");
717 Assert.IsNull (ex.InnerException, "#A3");
718 Assert.IsNotNull (ex.Message, "#A4");
721 Thread t2 = new Thread (new ThreadStart (Start));
722 t2.IsBackground = true;
726 ApartmentState state = t2.ApartmentState;
727 Assert.Fail ("#B1: " + state.ToString ());
728 } catch (ThreadStateException ex) {
729 Assert.AreEqual (typeof (ThreadStateException), ex.GetType (), "#B2");
730 Assert.IsNull (ex.InnerException, "#B3");
731 Assert.IsNotNull (ex.Message, "#B4");
736 public void ApartmentState_BackGround ()
738 Thread t1 = new Thread (new ThreadStart (Start));
739 t1.IsBackground = true;
740 Assert.AreEqual (ApartmentState.Unknown, t1.ApartmentState, "#1");
741 t1.ApartmentState = ApartmentState.STA;
742 Assert.AreEqual (ApartmentState.STA, t1.ApartmentState, "#2");
746 public void TestApartmentState ()
748 Thread t1 = new Thread (new ThreadStart (Start));
749 Thread t2 = new Thread (new ThreadStart (Start));
750 Thread t3 = new Thread (new ThreadStart (Start));
752 Assert.AreEqual (ApartmentState.Unknown, t1.ApartmentState, "Thread1 Default");
753 Assert.AreEqual (ApartmentState.Unknown, t2.ApartmentState, "Thread2 Default");
754 Assert.AreEqual (ApartmentState.Unknown, t3.ApartmentState, "Thread3 Default");
756 t1.ApartmentState = ApartmentState.STA;
757 Assert.AreEqual (ApartmentState.STA, t1.ApartmentState, "Thread1 Set Once");
758 t1.ApartmentState = ApartmentState.MTA;
759 Assert.AreEqual (ApartmentState.STA, t1.ApartmentState, "Thread1 Set Twice");
761 t2.ApartmentState = ApartmentState.MTA;
762 Assert.AreEqual (ApartmentState.MTA, t2.ApartmentState, "Thread2 Set Once");
763 t2.ApartmentState = ApartmentState.STA;
764 Assert.AreEqual (ApartmentState.MTA, t2.ApartmentState, "Thread2 Set Twice");
766 bool exception_occured = false;
768 t3.ApartmentState = ApartmentState.Unknown;
771 exception_occured = true;
773 Assert.AreEqual (ApartmentState.Unknown, t3.ApartmentState, "Thread3 Set Invalid");
775 Assert.IsFalse (exception_occured, "Thread3 Set Invalid Exception Occured");
777 Assert.IsTrue (exception_occured, "Thread3 Set Invalid Exception Occured");
781 exception_occured = false;
783 t1.ApartmentState = ApartmentState.STA;
786 exception_occured = true;
788 Assert.IsTrue (exception_occured, "Thread1 Started Invalid Exception Occured");
792 public class TestUtil
794 public static void WaitForNotAlive (Thread t, string s)
796 WhileAlive (t, true, s);
799 public static void WaitForAlive (Thread t, string s)
801 WhileAlive (t, false, s);
804 public static bool WaitForAliveOrStop (Thread t, string s)
806 return WhileAliveOrStop (t, false, s);
809 public static void WhileAlive (Thread t, bool alive, string s)
811 DateTime ti = DateTime.Now;
812 while (t.IsAlive == alive) {
813 if ((DateTime.Now - ti).TotalSeconds > 10) {
814 if (alive) Assert.Fail ("Timeout while waiting for not alive state. " + s);
815 else Assert.Fail ("Timeout while waiting for alive state. " + s);
820 public static bool WhileAliveOrStop (Thread t, bool alive, string s)
822 DateTime ti = DateTime.Now;
823 while (t.IsAlive == alive) {
824 if (t.ThreadState == ThreadState.Stopped)
827 if ((DateTime.Now - ti).TotalSeconds > 10) {
828 if (alive) Assert.Fail ("Timeout while waiting for not alive state. " + s);
829 else Assert.Fail ("Timeout while waiting for alive state. " + s);