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)
12 using NUnit.Framework;
14 using System.Security.Principal;
15 using System.Threading;
17 namespace MonoTests.System.Threading {
19 // These tests seem to hang the 2.0 framework. So they are disabled for now
20 // Don't reenable them until you can run a few thousand times on an SMP box.
21 [Category ("NotWorking")]
22 public class ThreadedPrincipalTest : Assertion {
24 public static void NoPrincipal ()
26 AppDomain.CurrentDomain.SetPrincipalPolicy (PrincipalPolicy.NoPrincipal);
27 IPrincipal p = Thread.CurrentPrincipal;
28 AssertNull ("Thread.CurrentPrincipal-1", p);
30 Thread.CurrentPrincipal = new GenericPrincipal (new GenericIdentity ("mono"), null);
31 AssertNotNull ("Thread.CurrentPrincipal-2", Thread.CurrentPrincipal);
33 Thread.CurrentPrincipal = null;
34 AssertNull ("Thread.CurrentPrincipal-3", Thread.CurrentPrincipal);
35 // in this case we can return to null
38 public static void UnauthenticatedPrincipal ()
40 AppDomain.CurrentDomain.SetPrincipalPolicy (PrincipalPolicy.UnauthenticatedPrincipal);
41 IPrincipal p = Thread.CurrentPrincipal;
42 AssertNotNull ("Thread.CurrentPrincipal", p);
43 Assert ("Type", (p is GenericPrincipal));
44 AssertEquals ("Name", String.Empty, p.Identity.Name);
45 AssertEquals ("AuthenticationType", String.Empty, p.Identity.AuthenticationType);
46 Assert ("IsAuthenticated", !p.Identity.IsAuthenticated);
48 Thread.CurrentPrincipal = new GenericPrincipal (new GenericIdentity ("mono"), null);
49 AssertNotNull ("Thread.CurrentPrincipal-2", Thread.CurrentPrincipal);
51 Thread.CurrentPrincipal = null;
52 AssertNotNull ("Thread.CurrentPrincipal-3", Thread.CurrentPrincipal);
53 // in this case we can't return to null
56 public static void WindowsPrincipal ()
58 AppDomain.CurrentDomain.SetPrincipalPolicy (PrincipalPolicy.WindowsPrincipal);
59 IPrincipal p = Thread.CurrentPrincipal;
60 AssertNotNull ("Thread.CurrentPrincipal", p);
61 Assert ("Type", (p is WindowsPrincipal));
62 AssertNotNull ("Name", p.Identity.Name);
63 AssertNotNull ("AuthenticationType", p.Identity.AuthenticationType);
64 Assert ("IsAuthenticated", p.Identity.IsAuthenticated);
66 // note: we can switch from a WindowsPrincipal to a GenericPrincipal
67 Thread.CurrentPrincipal = new GenericPrincipal (new GenericIdentity ("mono"), null);
68 AssertNotNull ("Thread.CurrentPrincipal-2", Thread.CurrentPrincipal);
70 Thread.CurrentPrincipal = null;
71 AssertNotNull ("Thread.CurrentPrincipal-3", Thread.CurrentPrincipal);
72 // in this case we can't return to null
75 public static void CopyOnNewThread ()
77 AssertNotNull ("Thread.CurrentPrincipal", Thread.CurrentPrincipal);
78 AssertEquals ("Identity", "good", Thread.CurrentPrincipal.Identity.Name);
83 [Category ("NotWorking")]
84 public class ThreadTest : Assertion {
86 //Some Classes to test as threads
90 public Thread thread1;
96 thread1 = (Thread)null;
98 endm1 = endm2 = false;
101 public void TestMethod()
109 public void TestMethod2()
111 if (!(thread1==(Thread)null) )
122 public bool run = false;
129 public void TestMethod()
144 public C1Test sub_class;
145 public Thread sub_thread;
149 sub_class = new C1Test();
150 sub_thread = new Thread(new ThreadStart(sub_class.TestMethod));
153 public void TestMethod1()
163 public C1Test class1;
164 public C1Test class2;
165 public Thread thread1;
166 public Thread thread2;
174 class1 = new C1Test();
175 class2 = new C1Test();
176 thread1 = new Thread(new ThreadStart(class1.TestMethod));
177 thread2 = new Thread(new ThreadStart(class2.TestMethod));
180 public void TestMethod1()
183 TestUtil.WaitForAlive (thread1, "wait1");
186 TestUtil.WaitForAlive (thread2, "wait2");
189 TestUtil.WaitForNotAlive (thread1, "wait3");
192 TestUtil.WaitForNotAlive (thread2, "wait4");
196 public void TestMethod2()
203 public void TestCtor1()
205 C1Test test1 = new C1Test();
208 Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
212 Fail ("#01 Unexpected Exception Thrown: " + e.ToString ());
216 [Category("NotDotNet")]
217 public void TestStart()
220 C1Test test1 = new C1Test();
221 Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
228 Fail ("#12 Unexpected Exception Thrown: " + e.ToString ());
231 AssertEquals("#13 Thread Not started: ", 10,test1.cnt);
234 bool errorThrown = false;
235 C2Test test1 = new C2Test();
236 Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
243 catch(ThreadStateException)
247 Assert ("#14 no ThreadStateException trown", errorThrown);
250 C2Test test1 = new C2Test();
251 Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
254 bool started = (TestThread.ThreadState == ThreadState.Running);
255 AssertEquals("#15 Thread Is not in the correct state: ", started , test1.run);
260 public void TestApartment()
262 C2Test test1 = new C2Test();
263 Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
264 ApartmentState before = TestThread.ApartmentState;
266 TestUtil.WaitForAlive (TestThread, "wait5");
267 ApartmentState after = TestThread.ApartmentState;
269 AssertEquals("#21 Apartment State Changed when not needed",before,after);
272 [Category("NotDotNet")]
273 public void TestApartmentState()
275 C2Test test1 = new C2Test();
276 Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
277 ApartmentState before = TestThread.ApartmentState;
279 TestUtil.WaitForAlive (TestThread, "wait6");
280 ApartmentState after = TestThread.ApartmentState;
282 AssertEquals("#31 Apartment State Changed when not needed: ",before,after);
285 public void TestPriority1()
287 C2Test test1 = new C2Test();
288 Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
290 TestThread.Priority=ThreadPriority.BelowNormal;
291 ThreadPriority after = TestThread.Priority;
293 TestUtil.WaitForAlive (TestThread, "wait7");
294 ThreadPriority before = TestThread.Priority;
295 AssertEquals("#41 Unexpected Priority Change: ",before,after);
303 public void AbortUnstarted ()
305 C2Test test1 = new C2Test();
306 Thread th = new Thread (new ThreadStart (test1.TestMethod));
311 [Category("NotWorking")] // this is a MonoTODO -> no support for Priority
312 public void TestPriority2()
314 C2Test test1 = new C2Test();
315 Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
317 AssertEquals("#42 Incorrect Priority in New thread: ",ThreadPriority.Normal, TestThread.Priority);
319 TestUtil.WaitForAliveOrStop (TestThread, "wait8");
320 AssertEquals("#43 Incorrect Priority in Started thread: ",ThreadPriority.Normal, TestThread.Priority);
325 AssertEquals("#44 Incorrect Priority in Aborted thread: ",ThreadPriority.Normal, TestThread.Priority);
328 [Category("NotWorking")] // this is a MonoTODO -> no support for Priority
329 public void TestPriority3()
331 C2Test test1 = new C2Test();
332 Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
335 TestThread.Priority = ThreadPriority.Lowest;
336 AssertEquals("#45A Incorrect Priority:",ThreadPriority.Lowest,TestThread.Priority);
337 TestThread.Priority = ThreadPriority.BelowNormal;
338 AssertEquals("#45B Incorrect Priority:",ThreadPriority.BelowNormal,TestThread.Priority);
339 TestThread.Priority = ThreadPriority.Normal;
340 AssertEquals("#45C Incorrect Priority:",ThreadPriority.Normal,TestThread.Priority);
341 TestThread.Priority = ThreadPriority.AboveNormal;
342 AssertEquals("#45D Incorrect Priority:",ThreadPriority.AboveNormal,TestThread.Priority);
343 TestThread.Priority = ThreadPriority.Highest;
344 AssertEquals("#45E Incorrect Priority:",ThreadPriority.Highest,TestThread.Priority);
352 public void TestIsBackground1()
354 C2Test test1 = new C2Test();
355 Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
358 TestUtil.WaitForAlive (TestThread, "wait9");
359 bool state = TestThread.IsBackground;
360 Assert("#51 IsBackground not set at the default state: ",!(state));
367 public void TestIsBackground2()
369 C2Test test1 = new C2Test();
370 Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
371 TestThread.IsBackground = true;
378 Assert("#52 Is Background Changed ot Start ",TestThread.IsBackground);
382 public void TestName()
384 C2Test test1 = new C2Test();
385 Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
388 TestUtil.WaitForAlive (TestThread, "wait10");
389 string name = TestThread.Name;
390 AssertEquals("#61 Name set when mustn't be set: ", name, (string)null);
391 string newname = "Testing....";
392 TestThread.Name = newname;
393 AssertEquals("#62 Name not set when must be set: ",TestThread.Name,newname);
400 [Category("NotDotNet")]
401 public void TestNestedThreads1()
403 C3Test test1 = new C3Test();
404 Thread TestThread = new Thread(new ThreadStart(test1.TestMethod1));
407 TestUtil.WaitForAlive (TestThread, "wait11");
410 Fail("#71 Unexpected Exception" + e.Message);
417 public void TestNestedThreads2()
419 C4Test test1 = new C4Test();
420 Thread TestThread = new Thread(new ThreadStart(test1.TestMethod1));
425 Fail("#81 Unexpected Exception" + e.ToString());
433 public void TestJoin1()
435 C1Test test1 = new C1Test();
436 C1Test test2 = new C1Test();
437 Thread thread1 = new Thread(new ThreadStart(test1.TestMethod));
438 Thread thread2 = new Thread(new ThreadStart(test1.TestMethod2));
447 Fail("#91 Unexpected Exception " + e.ToString());
456 public void TestThreadState()
458 //TODO: Test The rest of the possible transitions
459 C2Test test1 = new C2Test();
460 Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
461 AssertEquals("#101 Wrong Thread State",ThreadState.Unstarted,TestThread.ThreadState);
464 //while(!TestThread.IsAlive); //In the MS Documentation this is not necessary
465 //but in the MS SDK it is
466 Assert("#102 Wrong Thread State: " + TestThread.ThreadState.ToString(), TestThread.ThreadState == ThreadState.Running || (TestThread.ThreadState & ThreadState.Unstarted) != 0);
472 TestUtil.WaitForNotAlive (TestThread, "wait12");
473 // Docs say state will be Stopped, but Aborted happens sometimes (?)
474 Assert("#103 Wrong Thread State: " + TestThread.ThreadState.ToString(), (ThreadState.Stopped & TestThread.ThreadState) != 0
475 || (ThreadState.Aborted & TestThread.ThreadState) != 0);
479 [Ignore ("see comment below.")]
480 public void CurrentPrincipal_PrincipalPolicy_NoPrincipal ()
482 // note: switching from PrincipalPolicy won't work inside the same thread
483 // because as soon as a Principal object is created the Policy doesn't matter anymore
484 Thread t = new Thread (new ThreadStart (ThreadedPrincipalTest.NoPrincipal));
495 [Ignore ("see comment below.")]
496 public void CurrentPrincipal_PrincipalPolicy_UnauthenticatedPrincipal ()
498 // note: switching from PrincipalPolicy won't work inside the same thread
499 // because as soon as a Principal object is created the Policy doesn't matter anymore
500 Thread t = new Thread (new ThreadStart (ThreadedPrincipalTest.UnauthenticatedPrincipal));
511 public void CurrentPrincipal_PrincipalPolicy_WindowsPrincipal ()
513 // note: switching from PrincipalPolicy won't work inside the same thread
514 // because as soon as a Principal object is created the Policy doesn't matter anymore
515 Thread t = new Thread (new ThreadStart (ThreadedPrincipalTest.WindowsPrincipal));
526 public void IPrincipal_CopyOnNewThread ()
528 Thread.CurrentPrincipal = new GenericPrincipal (new GenericIdentity ("bad"), null);
529 Thread t = new Thread (new ThreadStart (ThreadedPrincipalTest.CopyOnNewThread));
531 Thread.CurrentPrincipal = new GenericPrincipal (new GenericIdentity ("good"), null);
542 [Category("NotDotNet")]
543 public void TestSuspend ()
545 Thread t = new Thread (new ThreadStart (DoCount));
546 t.IsBackground = true;
549 CheckIsRunning ("t1", t);
552 WaitSuspended ("t2", t);
554 CheckIsNotRunning ("t3", t);
557 WaitResumed ("t4", t);
559 CheckIsRunning ("t5", t);
562 TestUtil.WaitForNotAlive (t, "wait13");
563 CheckIsNotRunning ("t6", t);
566 [Category("NotDotNet")]
567 [Category("NotWorking")]
568 public void TestSuspendAbort ()
570 Thread t = new Thread (new ThreadStart (DoCount));
571 t.IsBackground = true;
574 CheckIsRunning ("t1", t);
577 WaitSuspended ("t2", t);
579 CheckIsNotRunning ("t3", t);
584 while (t.IsAlive && n < 200) {
589 Assert ("Timeout while waiting for abort", n < 200);
591 CheckIsNotRunning ("t6", t);
594 void CheckIsRunning (string s, Thread t)
598 Assert (s, counter > c);
601 void CheckIsNotRunning (string s, Thread t)
605 Assert (s, counter == c);
608 void WaitSuspended (string s, Thread t)
611 ThreadState state = t.ThreadState;
612 while ((state & ThreadState.Suspended) == 0) {
613 Assert (s + ": expected SuspendRequested state", (state & ThreadState.SuspendRequested) != 0);
616 Assert (s + ": failed to suspend", n < 100);
617 state = t.ThreadState;
619 Assert (s + ": SuspendRequested state not expected", (state & ThreadState.SuspendRequested) == 0);
622 void WaitResumed (string s, Thread t)
625 while ((t.ThreadState & ThreadState.Suspended) != 0) {
628 Assert (s + ": failed to resume", n < 100);
632 public void DoCount ()
641 public class TestUtil
643 public static void WaitForNotAlive (Thread t, string s)
645 WhileAlive (t, true, s);
648 public static void WaitForAlive (Thread t, string s)
650 WhileAlive (t, false, s);
653 public static bool WaitForAliveOrStop (Thread t, string s)
655 return WhileAliveOrStop (t, false, s);
658 public static void WhileAlive (Thread t, bool alive, string s)
660 DateTime ti = DateTime.Now;
661 while (t.IsAlive == alive) {
662 if ((DateTime.Now - ti).TotalSeconds > 10) {
663 if (alive) Assertion.Fail ("Timeout while waiting for not alive state. " + s);
664 else Assertion.Fail ("Timeout while waiting for alive state. " + s);
669 public static bool WhileAliveOrStop (Thread t, bool alive, string s)
671 DateTime ti = DateTime.Now;
672 while (t.IsAlive == alive) {
673 if (t.ThreadState == ThreadState.Stopped)
676 if ((DateTime.Now - ti).TotalSeconds > 10) {
677 if (alive) Assertion.Fail ("Timeout while waiting for not alive state. " + s);
678 else Assertion.Fail ("Timeout while waiting for alive state. " + s);