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("NotDotNet")]
211 public void TestStart()
214 C1Test test1 = new C1Test();
215 Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
222 Fail ("#12 Unexpected Exception Thrown: " + e.ToString ());
225 AssertEquals("#13 Thread Not started: ", 10,test1.cnt);
228 bool errorThrown = false;
229 C2Test test1 = new C2Test();
230 Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
237 catch(ThreadStateException)
241 Assert ("#14 no ThreadStateException trown", errorThrown);
244 C2Test test1 = new C2Test();
245 Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
248 bool started = (TestThread.ThreadState == ThreadState.Running);
249 AssertEquals("#15 Thread Is not in the correct state: ", started , test1.run);
254 public void TestApartment()
256 C2Test test1 = new C2Test();
257 Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
258 ApartmentState before = TestThread.ApartmentState;
260 TestUtil.WaitForAlive (TestThread, "wait5");
261 ApartmentState after = TestThread.ApartmentState;
263 AssertEquals("#21 Apartment State Changed when not needed",before,after);
266 [Category("NotDotNet")]
267 public void TestApartmentState()
269 C2Test test1 = new C2Test();
270 Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
271 ApartmentState before = TestThread.ApartmentState;
273 TestUtil.WaitForAlive (TestThread, "wait6");
274 ApartmentState after = TestThread.ApartmentState;
276 AssertEquals("#31 Apartment State Changed when not needed: ",before,after);
279 public void TestPriority1()
281 C2Test test1 = new C2Test();
282 Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
284 TestThread.Priority=ThreadPriority.BelowNormal;
285 ThreadPriority after = TestThread.Priority;
287 TestUtil.WaitForAlive (TestThread, "wait7");
288 ThreadPriority before = TestThread.Priority;
289 AssertEquals("#41 Unexpected Priority Change: ",before,after);
297 public void AbortUnstarted ()
299 C2Test test1 = new C2Test();
300 Thread th = new Thread (new ThreadStart (test1.TestMethod));
305 [Category("NotWorking")] // this is a MonoTODO -> no support for Priority
306 public void TestPriority2()
308 C2Test test1 = new C2Test();
309 Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
311 AssertEquals("#42 Incorrect Priority in New thread: ",ThreadPriority.Normal, TestThread.Priority);
313 TestUtil.WaitForAliveOrStop (TestThread, "wait8");
314 AssertEquals("#43 Incorrect Priority in Started thread: ",ThreadPriority.Normal, TestThread.Priority);
319 AssertEquals("#44 Incorrect Priority in Aborted thread: ",ThreadPriority.Normal, TestThread.Priority);
322 [Category("NotWorking")] // this is a MonoTODO -> no support for Priority
323 public void TestPriority3()
325 C2Test test1 = new C2Test();
326 Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
329 TestThread.Priority = ThreadPriority.Lowest;
330 AssertEquals("#45A Incorrect Priority:",ThreadPriority.Lowest,TestThread.Priority);
331 TestThread.Priority = ThreadPriority.BelowNormal;
332 AssertEquals("#45B Incorrect Priority:",ThreadPriority.BelowNormal,TestThread.Priority);
333 TestThread.Priority = ThreadPriority.Normal;
334 AssertEquals("#45C Incorrect Priority:",ThreadPriority.Normal,TestThread.Priority);
335 TestThread.Priority = ThreadPriority.AboveNormal;
336 AssertEquals("#45D Incorrect Priority:",ThreadPriority.AboveNormal,TestThread.Priority);
337 TestThread.Priority = ThreadPriority.Highest;
338 AssertEquals("#45E Incorrect Priority:",ThreadPriority.Highest,TestThread.Priority);
346 public void TestIsBackground1()
348 C2Test test1 = new C2Test();
349 Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
352 TestUtil.WaitForAlive (TestThread, "wait9");
353 bool state = TestThread.IsBackground;
354 Assert("#51 IsBackground not set at the default state: ",!(state));
361 public void TestIsBackground2()
363 C2Test test1 = new C2Test();
364 Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
365 TestThread.IsBackground = true;
372 Assert("#52 Is Background Changed ot Start ",TestThread.IsBackground);
376 public void TestName()
378 C2Test test1 = new C2Test();
379 Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
382 TestUtil.WaitForAlive (TestThread, "wait10");
383 string name = TestThread.Name;
384 AssertEquals("#61 Name set when mustn't be set: ", name, (string)null);
385 string newname = "Testing....";
386 TestThread.Name = newname;
387 AssertEquals("#62 Name not set when must be set: ",TestThread.Name,newname);
394 [Category("NotDotNet")]
395 public void TestNestedThreads1()
397 C3Test test1 = new C3Test();
398 Thread TestThread = new Thread(new ThreadStart(test1.TestMethod1));
401 TestUtil.WaitForAlive (TestThread, "wait11");
404 Fail("#71 Unexpected Exception" + e.Message);
411 public void TestNestedThreads2()
413 C4Test test1 = new C4Test();
414 Thread TestThread = new Thread(new ThreadStart(test1.TestMethod1));
419 Fail("#81 Unexpected Exception" + e.ToString());
427 public void TestJoin1()
429 C1Test test1 = new C1Test();
430 C1Test test2 = new C1Test();
431 Thread thread1 = new Thread(new ThreadStart(test1.TestMethod));
432 Thread thread2 = new Thread(new ThreadStart(test1.TestMethod2));
441 Fail("#91 Unexpected Exception " + e.ToString());
450 public void TestThreadState()
452 //TODO: Test The rest of the possible transitions
453 C2Test test1 = new C2Test();
454 Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
455 AssertEquals("#101 Wrong Thread State",ThreadState.Unstarted,TestThread.ThreadState);
458 //while(!TestThread.IsAlive); //In the MS Documentation this is not necessary
459 //but in the MS SDK it is
460 Assert("#102 Wrong Thread State: " + TestThread.ThreadState.ToString(), TestThread.ThreadState == ThreadState.Running || (TestThread.ThreadState & ThreadState.Unstarted) != 0);
466 TestUtil.WaitForNotAlive (TestThread, "wait12");
467 // Docs say state will be Stopped, but Aborted happens sometimes (?)
468 Assert("#103 Wrong Thread State: " + TestThread.ThreadState.ToString(), (ThreadState.Stopped & TestThread.ThreadState) != 0
469 || (ThreadState.Aborted & TestThread.ThreadState) != 0);
473 public void CurrentPrincipal_PrincipalPolicy_NoPrincipal ()
475 // note: switching from PrincipalPolicy won't work inside the same thread
476 // because as soon as a Principal object is created the Policy doesn't matter anymore
477 Thread t = new Thread (new ThreadStart (ThreadedPrincipalTest.NoPrincipal));
488 public void CurrentPrincipal_PrincipalPolicy_UnauthenticatedPrincipal ()
490 // note: switching from PrincipalPolicy won't work inside the same thread
491 // because as soon as a Principal object is created the Policy doesn't matter anymore
492 Thread t = new Thread (new ThreadStart (ThreadedPrincipalTest.UnauthenticatedPrincipal));
503 public void CurrentPrincipal_PrincipalPolicy_WindowsPrincipal ()
505 // note: switching from PrincipalPolicy won't work inside the same thread
506 // because as soon as a Principal object is created the Policy doesn't matter anymore
507 Thread t = new Thread (new ThreadStart (ThreadedPrincipalTest.WindowsPrincipal));
519 [Category("NotDotNet")]
520 public void TestSuspend ()
522 Thread t = new Thread (new ThreadStart (DoCount));
523 t.IsBackground = true;
526 CheckIsRunning ("t1", t);
529 WaitSuspended ("t2", t);
531 CheckIsNotRunning ("t3", t);
534 WaitResumed ("t4", t);
536 CheckIsRunning ("t5", t);
539 TestUtil.WaitForNotAlive (t, "wait13");
540 CheckIsNotRunning ("t6", t);
543 [Category("NotDotNet")]
544 [Category("NotWorking")]
545 public void TestSuspendAbort ()
547 Thread t = new Thread (new ThreadStart (DoCount));
548 t.IsBackground = true;
551 CheckIsRunning ("t1", t);
554 WaitSuspended ("t2", t);
556 CheckIsNotRunning ("t3", t);
561 while (t.IsAlive && n < 200) {
566 Assert ("Timeout while waiting for abort", n < 200);
568 CheckIsNotRunning ("t6", t);
571 void CheckIsRunning (string s, Thread t)
575 Assert (s, counter > c);
578 void CheckIsNotRunning (string s, Thread t)
582 Assert (s, counter == c);
585 void WaitSuspended (string s, Thread t)
588 ThreadState state = t.ThreadState;
589 while ((state & ThreadState.Suspended) == 0) {
590 Assert (s + ": expected SuspendRequested state", (state & ThreadState.SuspendRequested) != 0);
593 Assert (s + ": failed to suspend", n < 100);
594 state = t.ThreadState;
596 Assert (s + ": SuspendRequested state not expected", (state & ThreadState.SuspendRequested) == 0);
599 void WaitResumed (string s, Thread t)
602 while ((t.ThreadState & ThreadState.Suspended) != 0) {
605 Assert (s + ": failed to resume", n < 100);
609 public void DoCount ()
618 public class TestUtil
620 public static void WaitForNotAlive (Thread t, string s)
622 WhileAlive (t, true, s);
625 public static void WaitForAlive (Thread t, string s)
627 WhileAlive (t, false, s);
630 public static bool WaitForAliveOrStop (Thread t, string s)
632 return WhileAliveOrStop (t, false, s);
635 public static void WhileAlive (Thread t, bool alive, string s)
637 DateTime ti = DateTime.Now;
638 while (t.IsAlive == alive) {
639 if ((DateTime.Now - ti).TotalSeconds > 10) {
640 if (alive) Assertion.Fail ("Timeout while waiting for not alive state. " + s);
641 else Assertion.Fail ("Timeout while waiting for alive state. " + s);
646 public static bool WhileAliveOrStop (Thread t, bool alive, string s)
648 DateTime ti = DateTime.Now;
649 while (t.IsAlive == alive) {
650 if (t.ThreadState == ThreadState.Stopped)
653 if ((DateTime.Now - ti).TotalSeconds > 10) {
654 if (alive) Assertion.Fail ("Timeout while waiting for not alive state. " + s);
655 else Assertion.Fail ("Timeout while waiting for alive state. " + s);