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
77 [Category ("NotWorking")]
78 public class ThreadTest : Assertion {
80 //Some Classes to test as threads
84 public Thread thread1;
90 thread1 = (Thread)null;
92 endm1 = endm2 = false;
95 public void TestMethod()
103 public void TestMethod2()
105 if (!(thread1==(Thread)null) )
116 public bool run = false;
123 public void TestMethod()
138 public C1Test sub_class;
139 public Thread sub_thread;
143 sub_class = new C1Test();
144 sub_thread = new Thread(new ThreadStart(sub_class.TestMethod));
147 public void TestMethod1()
157 public C1Test class1;
158 public C1Test class2;
159 public Thread thread1;
160 public Thread thread2;
168 class1 = new C1Test();
169 class2 = new C1Test();
170 thread1 = new Thread(new ThreadStart(class1.TestMethod));
171 thread2 = new Thread(new ThreadStart(class2.TestMethod));
174 public void TestMethod1()
177 TestUtil.WaitForAlive (thread1, "wait1");
180 TestUtil.WaitForAlive (thread2, "wait2");
183 TestUtil.WaitForNotAlive (thread1, "wait3");
186 TestUtil.WaitForNotAlive (thread2, "wait4");
190 public void TestMethod2()
197 public void TestCtor1()
199 C1Test test1 = new C1Test();
202 Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
206 Fail ("#01 Unexpected Exception Thrown: " + e.ToString ());
210 [Category("NotWorking")]
211 [Category("NotDotNet")]
212 public void TestStart()
215 C1Test test1 = new C1Test();
216 Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
223 Fail ("#12 Unexpected Exception Thrown: " + e.ToString ());
226 AssertEquals("#13 Thread Not started: ", 10,test1.cnt);
229 bool errorThrown = false;
230 C2Test test1 = new C2Test();
231 Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
238 catch(ThreadStateException)
242 Assert ("#14 no ThreadStateException trown", errorThrown);
245 C2Test test1 = new C2Test();
246 Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
249 bool started = (TestThread.ThreadState == ThreadState.Running);
250 AssertEquals("#15 Thread Is not in the correct state: ", started , test1.run);
255 public void TestApartment()
257 C2Test test1 = new C2Test();
258 Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
259 ApartmentState before = TestThread.ApartmentState;
261 TestUtil.WaitForAlive (TestThread, "wait5");
262 ApartmentState after = TestThread.ApartmentState;
264 AssertEquals("#21 Apartment State Changed when not needed",before,after);
267 [Category("NotDotNet")]
268 public void TestApartmentState()
270 C2Test test1 = new C2Test();
271 Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
272 ApartmentState before = TestThread.ApartmentState;
274 TestUtil.WaitForAlive (TestThread, "wait6");
275 ApartmentState after = TestThread.ApartmentState;
277 AssertEquals("#31 Apartment State Changed when not needed: ",before,after);
280 public void TestPriority1()
282 C2Test test1 = new C2Test();
283 Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
285 TestThread.Priority=ThreadPriority.BelowNormal;
286 ThreadPriority after = TestThread.Priority;
288 TestUtil.WaitForAlive (TestThread, "wait7");
289 ThreadPriority before = TestThread.Priority;
290 AssertEquals("#41 Unexpected Priority Change: ",before,after);
298 public void AbortUnstarted ()
300 C2Test test1 = new C2Test();
301 Thread th = new Thread (new ThreadStart (test1.TestMethod));
306 [Category("NotWorking")] // this is a MonoTODO -> no support for Priority
307 public void TestPriority2()
309 C2Test test1 = new C2Test();
310 Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
312 AssertEquals("#42 Incorrect Priority in New thread: ",ThreadPriority.Normal, TestThread.Priority);
314 TestUtil.WaitForAliveOrStop (TestThread, "wait8");
315 AssertEquals("#43 Incorrect Priority in Started thread: ",ThreadPriority.Normal, TestThread.Priority);
320 AssertEquals("#44 Incorrect Priority in Aborted thread: ",ThreadPriority.Normal, TestThread.Priority);
323 [Category("NotWorking")] // this is a MonoTODO -> no support for Priority
324 public void TestPriority3()
326 C2Test test1 = new C2Test();
327 Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
330 TestThread.Priority = ThreadPriority.Lowest;
331 AssertEquals("#45A Incorrect Priority:",ThreadPriority.Lowest,TestThread.Priority);
332 TestThread.Priority = ThreadPriority.BelowNormal;
333 AssertEquals("#45B Incorrect Priority:",ThreadPriority.BelowNormal,TestThread.Priority);
334 TestThread.Priority = ThreadPriority.Normal;
335 AssertEquals("#45C Incorrect Priority:",ThreadPriority.Normal,TestThread.Priority);
336 TestThread.Priority = ThreadPriority.AboveNormal;
337 AssertEquals("#45D Incorrect Priority:",ThreadPriority.AboveNormal,TestThread.Priority);
338 TestThread.Priority = ThreadPriority.Highest;
339 AssertEquals("#45E Incorrect Priority:",ThreadPriority.Highest,TestThread.Priority);
347 public void TestIsBackground1()
349 C2Test test1 = new C2Test();
350 Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
353 TestUtil.WaitForAlive (TestThread, "wait9");
354 bool state = TestThread.IsBackground;
355 Assert("#51 IsBackground not set at the default state: ",!(state));
362 public void TestIsBackground2()
364 C2Test test1 = new C2Test();
365 Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
366 TestThread.IsBackground = true;
373 Assert("#52 Is Background Changed ot Start ",TestThread.IsBackground);
377 public void TestName()
379 C2Test test1 = new C2Test();
380 Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
383 TestUtil.WaitForAlive (TestThread, "wait10");
384 string name = TestThread.Name;
385 AssertEquals("#61 Name set when mustn't be set: ", name, (string)null);
386 string newname = "Testing....";
387 TestThread.Name = newname;
388 AssertEquals("#62 Name not set when must be set: ",TestThread.Name,newname);
395 [Category("NotDotNet")]
396 public void TestNestedThreads1()
398 C3Test test1 = new C3Test();
399 Thread TestThread = new Thread(new ThreadStart(test1.TestMethod1));
402 TestUtil.WaitForAlive (TestThread, "wait11");
405 Fail("#71 Unexpected Exception" + e.Message);
412 public void TestNestedThreads2()
414 C4Test test1 = new C4Test();
415 Thread TestThread = new Thread(new ThreadStart(test1.TestMethod1));
420 Fail("#81 Unexpected Exception" + e.ToString());
427 [Category ("NotWorking")]
428 public void TestJoin1()
430 C1Test test1 = new C1Test();
431 C1Test test2 = new C1Test();
432 Thread thread1 = new Thread(new ThreadStart(test1.TestMethod));
433 Thread thread2 = new Thread(new ThreadStart(test1.TestMethod2));
442 Fail("#91 Unexpected Exception " + e.ToString());
451 public void TestThreadState()
453 //TODO: Test The rest of the possible transitions
454 C2Test test1 = new C2Test();
455 Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
456 AssertEquals("#101 Wrong Thread State",ThreadState.Unstarted,TestThread.ThreadState);
459 //while(!TestThread.IsAlive); //In the MS Documentation this is not necessary
460 //but in the MS SDK it is
461 Assert("#102 Wrong Thread State: " + TestThread.ThreadState.ToString(), TestThread.ThreadState == ThreadState.Running || (TestThread.ThreadState & ThreadState.Unstarted) != 0);
467 TestUtil.WaitForNotAlive (TestThread, "wait12");
468 // Docs say state will be Stopped, but Aborted happens sometimes (?)
469 Assert("#103 Wrong Thread State: " + TestThread.ThreadState.ToString(), (ThreadState.Stopped & TestThread.ThreadState) != 0
470 || (ThreadState.Aborted & TestThread.ThreadState) != 0);
474 [Category ("NotWorking")]
475 public void CurrentPrincipal_PrincipalPolicy_NoPrincipal ()
477 // note: switching from PrincipalPolicy won't work inside the same thread
478 // because as soon as a Principal object is created the Policy doesn't matter anymore
479 Thread t = new Thread (new ThreadStart (ThreadedPrincipalTest.NoPrincipal));
490 [Category ("NotWorking")]
491 public void CurrentPrincipal_PrincipalPolicy_UnauthenticatedPrincipal ()
493 // note: switching from PrincipalPolicy won't work inside the same thread
494 // because as soon as a Principal object is created the Policy doesn't matter anymore
495 Thread t = new Thread (new ThreadStart (ThreadedPrincipalTest.UnauthenticatedPrincipal));
506 [Category ("NotWorking")]
507 public void CurrentPrincipal_PrincipalPolicy_WindowsPrincipal ()
509 // note: switching from PrincipalPolicy won't work inside the same thread
510 // because as soon as a Principal object is created the Policy doesn't matter anymore
511 Thread t = new Thread (new ThreadStart (ThreadedPrincipalTest.WindowsPrincipal));
523 [Category("NotDotNet")]
524 public void TestSuspend ()
526 Thread t = new Thread (new ThreadStart (DoCount));
527 t.IsBackground = true;
530 CheckIsRunning ("t1", t);
533 WaitSuspended ("t2", t);
535 CheckIsNotRunning ("t3", t);
538 WaitResumed ("t4", t);
540 CheckIsRunning ("t5", t);
543 TestUtil.WaitForNotAlive (t, "wait13");
544 CheckIsNotRunning ("t6", t);
547 [Category("NotDotNet")]
548 [Category("NotWorking")]
549 public void TestSuspendAbort ()
551 Thread t = new Thread (new ThreadStart (DoCount));
552 t.IsBackground = true;
555 CheckIsRunning ("t1", t);
558 WaitSuspended ("t2", t);
560 CheckIsNotRunning ("t3", t);
565 while (t.IsAlive && n < 200) {
570 Assert ("Timeout while waiting for abort", n < 200);
572 CheckIsNotRunning ("t6", t);
575 void CheckIsRunning (string s, Thread t)
579 Assert (s, counter > c);
582 void CheckIsNotRunning (string s, Thread t)
586 Assert (s, counter == c);
589 void WaitSuspended (string s, Thread t)
592 ThreadState state = t.ThreadState;
593 while ((state & ThreadState.Suspended) == 0) {
594 Assert (s + ": expected SuspendRequested state", (state & ThreadState.SuspendRequested) != 0);
597 Assert (s + ": failed to suspend", n < 100);
598 state = t.ThreadState;
600 Assert (s + ": SuspendRequested state not expected", (state & ThreadState.SuspendRequested) == 0);
603 void WaitResumed (string s, Thread t)
606 while ((t.ThreadState & ThreadState.Suspended) != 0) {
609 Assert (s + ": failed to resume", n < 100);
613 public void DoCount ()
622 public class TestUtil
624 public static void WaitForNotAlive (Thread t, string s)
626 WhileAlive (t, true, s);
629 public static void WaitForAlive (Thread t, string s)
631 WhileAlive (t, false, s);
634 public static bool WaitForAliveOrStop (Thread t, string s)
636 return WhileAliveOrStop (t, false, s);
639 public static void WhileAlive (Thread t, bool alive, string s)
641 DateTime ti = DateTime.Now;
642 while (t.IsAlive == alive) {
643 if ((DateTime.Now - ti).TotalSeconds > 10) {
644 if (alive) Assertion.Fail ("Timeout while waiting for not alive state. " + s);
645 else Assertion.Fail ("Timeout while waiting for alive state. " + s);
650 public static bool WhileAliveOrStop (Thread t, bool alive, string s)
652 DateTime ti = DateTime.Now;
653 while (t.IsAlive == alive) {
654 if (t.ThreadState == ThreadState.Stopped)
657 if ((DateTime.Now - ti).TotalSeconds > 10) {
658 if (alive) Assertion.Fail ("Timeout while waiting for not alive state. " + s);
659 else Assertion.Fail ("Timeout while waiting for alive state. " + s);