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 #if !TARGET_JVM // AppDomain.SetPrincipalPolicy not supported for TARGET_JVM
27 AppDomain.CurrentDomain.SetPrincipalPolicy (PrincipalPolicy.NoPrincipal);
29 IPrincipal p = Thread.CurrentPrincipal;
30 AssertNull ("Thread.CurrentPrincipal-1", p);
32 Thread.CurrentPrincipal = new GenericPrincipal (new GenericIdentity ("mono"), null);
33 AssertNotNull ("Thread.CurrentPrincipal-2", Thread.CurrentPrincipal);
35 Thread.CurrentPrincipal = null;
36 AssertNull ("Thread.CurrentPrincipal-3", Thread.CurrentPrincipal);
37 // in this case we can return to null
40 #if !TARGET_JVM // AppDomain.SetPrincipalPolicy not supported for TARGET_JVM
41 public static void UnauthenticatedPrincipal ()
43 AppDomain.CurrentDomain.SetPrincipalPolicy (PrincipalPolicy.UnauthenticatedPrincipal);
44 IPrincipal p = Thread.CurrentPrincipal;
45 AssertNotNull ("Thread.CurrentPrincipal", p);
46 Assert ("Type", (p is GenericPrincipal));
47 AssertEquals ("Name", String.Empty, p.Identity.Name);
48 AssertEquals ("AuthenticationType", String.Empty, p.Identity.AuthenticationType);
49 Assert ("IsAuthenticated", !p.Identity.IsAuthenticated);
51 Thread.CurrentPrincipal = new GenericPrincipal (new GenericIdentity ("mono"), null);
52 AssertNotNull ("Thread.CurrentPrincipal-2", Thread.CurrentPrincipal);
54 Thread.CurrentPrincipal = null;
55 AssertNotNull ("Thread.CurrentPrincipal-3", Thread.CurrentPrincipal);
56 // in this case we can't return to null
59 public static void WindowsPrincipal ()
61 AppDomain.CurrentDomain.SetPrincipalPolicy (PrincipalPolicy.WindowsPrincipal);
62 IPrincipal p = Thread.CurrentPrincipal;
63 AssertNotNull ("Thread.CurrentPrincipal", p);
64 Assert ("Type", (p is WindowsPrincipal));
65 AssertNotNull ("Name", p.Identity.Name);
66 AssertNotNull ("AuthenticationType", p.Identity.AuthenticationType);
67 Assert ("IsAuthenticated", p.Identity.IsAuthenticated);
69 // note: we can switch from a WindowsPrincipal to a GenericPrincipal
70 Thread.CurrentPrincipal = new GenericPrincipal (new GenericIdentity ("mono"), null);
71 AssertNotNull ("Thread.CurrentPrincipal-2", Thread.CurrentPrincipal);
73 Thread.CurrentPrincipal = null;
74 AssertNotNull ("Thread.CurrentPrincipal-3", Thread.CurrentPrincipal);
75 // in this case we can't return to null
79 public static void CopyOnNewThread ()
81 AssertNotNull ("Thread.CurrentPrincipal", Thread.CurrentPrincipal);
82 AssertEquals ("Identity", "good", Thread.CurrentPrincipal.Identity.Name);
87 [Category ("NotWorking")]
88 public class ThreadTest : Assertion {
90 //Some Classes to test as threads
94 public Thread thread1;
100 thread1 = (Thread)null;
102 endm1 = endm2 = false;
105 public void TestMethod()
113 public void TestMethod2()
115 if (!(thread1==(Thread)null) )
126 public bool run = false;
133 public void TestMethod()
148 public C1Test sub_class;
149 public Thread sub_thread;
153 sub_class = new C1Test();
154 sub_thread = new Thread(new ThreadStart(sub_class.TestMethod));
157 public void TestMethod1()
167 public C1Test class1;
168 public C1Test class2;
169 public Thread thread1;
170 public Thread thread2;
178 class1 = new C1Test();
179 class2 = new C1Test();
180 thread1 = new Thread(new ThreadStart(class1.TestMethod));
181 thread2 = new Thread(new ThreadStart(class2.TestMethod));
184 public void TestMethod1()
187 TestUtil.WaitForAlive (thread1, "wait1");
190 TestUtil.WaitForAlive (thread2, "wait2");
193 TestUtil.WaitForNotAlive (thread1, "wait3");
196 TestUtil.WaitForNotAlive (thread2, "wait4");
200 public void TestMethod2()
207 public void TestCtor1()
209 C1Test test1 = new C1Test();
212 Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
216 Fail ("#01 Unexpected Exception Thrown: " + e.ToString ());
220 [Category("NotDotNet")]
221 public void TestStart()
224 C1Test test1 = new C1Test();
225 Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
232 Fail ("#12 Unexpected Exception Thrown: " + e.ToString ());
235 AssertEquals("#13 Thread Not started: ", 10,test1.cnt);
238 bool errorThrown = false;
239 C2Test test1 = new C2Test();
240 Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
247 catch(ThreadStateException)
251 Assert ("#14 no ThreadStateException trown", errorThrown);
254 C2Test test1 = new C2Test();
255 Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
258 bool started = (TestThread.ThreadState == ThreadState.Running);
259 AssertEquals("#15 Thread Is not in the correct state: ", started , test1.run);
264 public void TestApartment()
266 C2Test test1 = new C2Test();
267 Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
268 ApartmentState before = TestThread.ApartmentState;
270 TestUtil.WaitForAlive (TestThread, "wait5");
271 ApartmentState after = TestThread.ApartmentState;
273 AssertEquals("#21 Apartment State Changed when not needed",before,after);
276 [Category("NotDotNet")]
277 public void TestApartmentState()
279 C2Test test1 = new C2Test();
280 Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
281 ApartmentState before = TestThread.ApartmentState;
283 TestUtil.WaitForAlive (TestThread, "wait6");
284 ApartmentState after = TestThread.ApartmentState;
286 AssertEquals("#31 Apartment State Changed when not needed: ",before,after);
289 public void TestPriority1()
291 C2Test test1 = new C2Test();
292 Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
294 TestThread.Priority=ThreadPriority.BelowNormal;
295 ThreadPriority after = TestThread.Priority;
297 TestUtil.WaitForAlive (TestThread, "wait7");
298 ThreadPriority before = TestThread.Priority;
299 AssertEquals("#41 Unexpected Priority Change: ",before,after);
307 public void AbortUnstarted ()
309 C2Test test1 = new C2Test();
310 Thread th = new Thread (new ThreadStart (test1.TestMethod));
315 [Category("NotWorking")] // this is a MonoTODO -> no support for Priority
316 public void TestPriority2()
318 C2Test test1 = new C2Test();
319 Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
321 AssertEquals("#42 Incorrect Priority in New thread: ",ThreadPriority.Normal, TestThread.Priority);
323 TestUtil.WaitForAliveOrStop (TestThread, "wait8");
324 AssertEquals("#43 Incorrect Priority in Started thread: ",ThreadPriority.Normal, TestThread.Priority);
329 AssertEquals("#44 Incorrect Priority in Aborted thread: ",ThreadPriority.Normal, TestThread.Priority);
332 [Category("NotWorking")] // this is a MonoTODO -> no support for Priority
333 public void TestPriority3()
335 C2Test test1 = new C2Test();
336 Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
339 TestThread.Priority = ThreadPriority.Lowest;
340 AssertEquals("#45A Incorrect Priority:",ThreadPriority.Lowest,TestThread.Priority);
341 TestThread.Priority = ThreadPriority.BelowNormal;
342 AssertEquals("#45B Incorrect Priority:",ThreadPriority.BelowNormal,TestThread.Priority);
343 TestThread.Priority = ThreadPriority.Normal;
344 AssertEquals("#45C Incorrect Priority:",ThreadPriority.Normal,TestThread.Priority);
345 TestThread.Priority = ThreadPriority.AboveNormal;
346 AssertEquals("#45D Incorrect Priority:",ThreadPriority.AboveNormal,TestThread.Priority);
347 TestThread.Priority = ThreadPriority.Highest;
348 AssertEquals("#45E Incorrect Priority:",ThreadPriority.Highest,TestThread.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("#51 IsBackground not set at the default state: ",!(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("#52 Is Background Changed ot Start ",TestThread.IsBackground);
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 AssertEquals("#61 Name set when mustn't be set: ", name, (string)null);
395 string newname = "Testing....";
396 TestThread.Name = newname;
397 AssertEquals("#62 Name not set when must be set: ",TestThread.Name,newname);
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 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 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 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 AssertEquals("#101 Wrong Thread State",ThreadState.Unstarted,TestThread.ThreadState);
468 //while(!TestThread.IsAlive); //In the MS Documentation this is not necessary
469 //but in the MS SDK it is
470 Assert("#102 Wrong Thread State: " + TestThread.ThreadState.ToString(), TestThread.ThreadState == ThreadState.Running || (TestThread.ThreadState & ThreadState.Unstarted) != 0);
476 TestUtil.WaitForNotAlive (TestThread, "wait12");
477 // Docs say state will be Stopped, but Aborted happens sometimes (?)
478 Assert("#103 Wrong Thread State: " + TestThread.ThreadState.ToString(), (ThreadState.Stopped & TestThread.ThreadState) != 0
479 || (ThreadState.Aborted & TestThread.ThreadState) != 0);
483 [Ignore ("see comment below.")]
484 public void CurrentPrincipal_PrincipalPolicy_NoPrincipal ()
486 // note: switching from PrincipalPolicy won't work inside the same thread
487 // because as soon as a Principal object is created the Policy doesn't matter anymore
488 Thread t = new Thread (new ThreadStart (ThreadedPrincipalTest.NoPrincipal));
498 #if !TARGET_JVM // AppDomain.SetPrincipalPolicy not supported for TARGET_JVM
500 [Ignore ("see comment below.")]
501 public void CurrentPrincipal_PrincipalPolicy_UnauthenticatedPrincipal ()
503 // note: switching from PrincipalPolicy won't work inside the same thread
504 // because as soon as a Principal object is created the Policy doesn't matter anymore
505 Thread t = new Thread (new ThreadStart (ThreadedPrincipalTest.UnauthenticatedPrincipal));
516 public void CurrentPrincipal_PrincipalPolicy_WindowsPrincipal ()
518 // note: switching from PrincipalPolicy won't work inside the same thread
519 // because as soon as a Principal object is created the Policy doesn't matter anymore
520 Thread t = new Thread (new ThreadStart (ThreadedPrincipalTest.WindowsPrincipal));
532 public void IPrincipal_CopyOnNewThread ()
534 Thread.CurrentPrincipal = new GenericPrincipal (new GenericIdentity ("bad"), null);
535 Thread t = new Thread (new ThreadStart (ThreadedPrincipalTest.CopyOnNewThread));
537 Thread.CurrentPrincipal = new GenericPrincipal (new GenericIdentity ("good"), null);
548 [Category("NotDotNet")]
549 public void TestSuspend ()
551 Thread t = new Thread (new ThreadStart (DoCount));
552 t.IsBackground = true;
555 CheckIsRunning ("t1", t);
558 WaitSuspended ("t2", t);
560 CheckIsNotRunning ("t3", t);
563 WaitResumed ("t4", t);
565 CheckIsRunning ("t5", t);
568 TestUtil.WaitForNotAlive (t, "wait13");
569 CheckIsNotRunning ("t6", t);
572 [Category("NotDotNet")]
573 [Category("NotWorking")]
574 public void TestSuspendAbort ()
576 Thread t = new Thread (new ThreadStart (DoCount));
577 t.IsBackground = true;
580 CheckIsRunning ("t1", t);
583 WaitSuspended ("t2", t);
585 CheckIsNotRunning ("t3", t);
590 while (t.IsAlive && n < 200) {
595 Assert ("Timeout while waiting for abort", n < 200);
597 CheckIsNotRunning ("t6", t);
600 void CheckIsRunning (string s, Thread t)
604 Assert (s, counter > c);
607 void CheckIsNotRunning (string s, Thread t)
611 Assert (s, counter == c);
614 void WaitSuspended (string s, Thread t)
617 ThreadState state = t.ThreadState;
618 while ((state & ThreadState.Suspended) == 0) {
619 Assert (s + ": expected SuspendRequested state", (state & ThreadState.SuspendRequested) != 0);
622 Assert (s + ": failed to suspend", n < 100);
623 state = t.ThreadState;
625 Assert (s + ": SuspendRequested state not expected", (state & ThreadState.SuspendRequested) == 0);
628 void WaitResumed (string s, Thread t)
631 while ((t.ThreadState & ThreadState.Suspended) != 0) {
634 Assert (s + ": failed to resume", n < 100);
638 public void DoCount ()
647 public class TestUtil
649 public static void WaitForNotAlive (Thread t, string s)
651 WhileAlive (t, true, s);
654 public static void WaitForAlive (Thread t, string s)
656 WhileAlive (t, false, s);
659 public static bool WaitForAliveOrStop (Thread t, string s)
661 return WhileAliveOrStop (t, false, s);
664 public static void WhileAlive (Thread t, bool alive, string s)
666 DateTime ti = DateTime.Now;
667 while (t.IsAlive == alive) {
668 if ((DateTime.Now - ti).TotalSeconds > 10) {
669 if (alive) Assertion.Fail ("Timeout while waiting for not alive state. " + s);
670 else Assertion.Fail ("Timeout while waiting for alive state. " + s);
675 public static bool WhileAliveOrStop (Thread t, bool alive, string s)
677 DateTime ti = DateTime.Now;
678 while (t.IsAlive == alive) {
679 if (t.ThreadState == ThreadState.Stopped)
682 if ((DateTime.Now - ti).TotalSeconds > 10) {
683 if (alive) Assertion.Fail ("Timeout while waiting for not alive state. " + s);
684 else Assertion.Fail ("Timeout while waiting for alive state. " + s);