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 public class ThreadedPrincipalTest : Assertion {
21 public static void NoPrincipal ()
23 AppDomain.CurrentDomain.SetPrincipalPolicy (PrincipalPolicy.NoPrincipal);
24 IPrincipal p = Thread.CurrentPrincipal;
25 AssertNull ("Thread.CurrentPrincipal-1", p);
27 Thread.CurrentPrincipal = new GenericPrincipal (new GenericIdentity ("mono"), null);
28 AssertNotNull ("Thread.CurrentPrincipal-2", Thread.CurrentPrincipal);
30 Thread.CurrentPrincipal = null;
31 AssertNull ("Thread.CurrentPrincipal-3", Thread.CurrentPrincipal);
32 // in this case we can return to null
35 public static void UnauthenticatedPrincipal ()
37 AppDomain.CurrentDomain.SetPrincipalPolicy (PrincipalPolicy.UnauthenticatedPrincipal);
38 IPrincipal p = Thread.CurrentPrincipal;
39 AssertNotNull ("Thread.CurrentPrincipal", p);
40 Assert ("Type", (p is GenericPrincipal));
41 AssertEquals ("Name", String.Empty, p.Identity.Name);
42 AssertEquals ("AuthenticationType", String.Empty, p.Identity.AuthenticationType);
43 Assert ("IsAuthenticated", !p.Identity.IsAuthenticated);
45 Thread.CurrentPrincipal = new GenericPrincipal (new GenericIdentity ("mono"), null);
46 AssertNotNull ("Thread.CurrentPrincipal-2", Thread.CurrentPrincipal);
48 Thread.CurrentPrincipal = null;
49 AssertNotNull ("Thread.CurrentPrincipal-3", Thread.CurrentPrincipal);
50 // in this case we can't return to null
53 public static void WindowsPrincipal ()
55 AppDomain.CurrentDomain.SetPrincipalPolicy (PrincipalPolicy.WindowsPrincipal);
56 IPrincipal p = Thread.CurrentPrincipal;
57 AssertNotNull ("Thread.CurrentPrincipal", p);
58 Assert ("Type", (p is WindowsPrincipal));
59 AssertNotNull ("Name", p.Identity.Name);
60 AssertNotNull ("AuthenticationType", p.Identity.AuthenticationType);
61 Assert ("IsAuthenticated", p.Identity.IsAuthenticated);
63 // note: we can switch from a WindowsPrincipal to a GenericPrincipal
64 Thread.CurrentPrincipal = new GenericPrincipal (new GenericIdentity ("mono"), null);
65 AssertNotNull ("Thread.CurrentPrincipal-2", Thread.CurrentPrincipal);
67 Thread.CurrentPrincipal = null;
68 AssertNotNull ("Thread.CurrentPrincipal-3", Thread.CurrentPrincipal);
69 // in this case we can't return to null
74 public class ThreadTest : Assertion {
76 //Some Classes to test as threads
80 public Thread thread1;
86 thread1 = (Thread)null;
88 endm1 = endm2 = false;
91 public void TestMethod()
99 public void TestMethod2()
101 if (!(thread1==(Thread)null) )
112 public bool run = false;
119 public void TestMethod()
134 public C1Test sub_class;
135 public Thread sub_thread;
139 sub_class = new C1Test();
140 sub_thread = new Thread(new ThreadStart(sub_class.TestMethod));
143 public void TestMethod1()
153 public C1Test class1;
154 public C1Test class2;
155 public Thread thread1;
156 public Thread thread2;
164 class1 = new C1Test();
165 class2 = new C1Test();
166 thread1 = new Thread(new ThreadStart(class1.TestMethod));
167 thread2 = new Thread(new ThreadStart(class2.TestMethod));
170 public void TestMethod1()
173 TestUtil.WaitForAlive (thread1, "wait1");
176 TestUtil.WaitForAlive (thread2, "wait2");
179 TestUtil.WaitForNotAlive (thread1, "wait3");
182 TestUtil.WaitForNotAlive (thread2, "wait4");
186 public void TestMethod2()
193 public void TestCtor1()
195 C1Test test1 = new C1Test();
198 Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
202 Fail ("#01 Unexpected Exception Thrown: " + e.ToString ());
206 [Category("NotDotNet")]
207 public void TestStart()
210 C1Test test1 = new C1Test();
211 Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
218 Fail ("#12 Unexpected Exception Thrown: " + e.ToString ());
221 AssertEquals("#13 Thread Not started: ", 10,test1.cnt);
224 bool errorThrown = false;
225 C2Test test1 = new C2Test();
226 Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
233 catch(ThreadStateException)
237 Assert ("#14 no ThreadStateException trown", errorThrown);
240 C2Test test1 = new C2Test();
241 Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
244 bool started = (TestThread.ThreadState == ThreadState.Running);
245 AssertEquals("#15 Thread Is not in the correct state: ", started , test1.run);
250 public void TestApartment()
252 C2Test test1 = new C2Test();
253 Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
254 ApartmentState before = TestThread.ApartmentState;
256 TestUtil.WaitForAlive (TestThread, "wait5");
257 ApartmentState after = TestThread.ApartmentState;
259 AssertEquals("#21 Apartment State Changed when not needed",before,after);
262 [Category("NotDotNet")]
263 public void TestApartmentState()
265 C2Test test1 = new C2Test();
266 Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
267 ApartmentState before = TestThread.ApartmentState;
269 TestUtil.WaitForAlive (TestThread, "wait6");
270 ApartmentState after = TestThread.ApartmentState;
272 AssertEquals("#31 Apartment State Changed when not needed: ",before,after);
275 public void TestPriority1()
277 C2Test test1 = new C2Test();
278 Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
280 TestThread.Priority=ThreadPriority.BelowNormal;
281 ThreadPriority after = TestThread.Priority;
283 TestUtil.WaitForAlive (TestThread, "wait7");
284 ThreadPriority before = TestThread.Priority;
285 AssertEquals("#41 Unexpected Priority Change: ",before,after);
292 [Category("NotWorking")] // this is a MonoTODO
293 public void TestPriority2()
295 C2Test test1 = new C2Test();
296 Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
298 AssertEquals("#42 Incorrect Priority in New thread: ",ThreadPriority.Normal, TestThread.Priority);
300 TestUtil.WaitForAlive (TestThread, "wait8");
301 AssertEquals("#43 Incorrect Priority in Started thread: ",ThreadPriority.Normal, TestThread.Priority);
306 AssertEquals("#44 Incorrect Priority in Aborted thread: ",ThreadPriority.Normal, TestThread.Priority);
309 [Category("NotWorking")] // this is a MonoTODO
310 public void TestPriority3()
312 C2Test test1 = new C2Test();
313 Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
316 TestThread.Priority = ThreadPriority.Lowest;
317 AssertEquals("#45A Incorrect Priority:",ThreadPriority.Lowest,TestThread.Priority);
318 TestThread.Priority = ThreadPriority.BelowNormal;
319 AssertEquals("#45B Incorrect Priority:",ThreadPriority.BelowNormal,TestThread.Priority);
320 TestThread.Priority = ThreadPriority.Normal;
321 AssertEquals("#45C Incorrect Priority:",ThreadPriority.Normal,TestThread.Priority);
322 TestThread.Priority = ThreadPriority.AboveNormal;
323 AssertEquals("#45D Incorrect Priority:",ThreadPriority.AboveNormal,TestThread.Priority);
324 TestThread.Priority = ThreadPriority.Highest;
325 AssertEquals("#45E Incorrect Priority:",ThreadPriority.Highest,TestThread.Priority);
333 public void TestIsBackground1()
335 C2Test test1 = new C2Test();
336 Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
339 TestUtil.WaitForAlive (TestThread, "wait9");
340 bool state = TestThread.IsBackground;
341 Assert("#51 IsBackground not set at the default state: ",!(state));
348 public void TestIsBackground2()
350 C2Test test1 = new C2Test();
351 Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
352 TestThread.IsBackground = true;
359 Assert("#52 Is Background Changed ot Start ",TestThread.IsBackground);
363 public void TestName()
365 C2Test test1 = new C2Test();
366 Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
369 TestUtil.WaitForAlive (TestThread, "wait10");
370 string name = TestThread.Name;
371 AssertEquals("#61 Name set when mustn't be set: ", name, (string)null);
372 string newname = "Testing....";
373 TestThread.Name = newname;
374 AssertEquals("#62 Name not set when must be set: ",TestThread.Name,newname);
381 [Category("NotDotNet")]
382 public void TestNestedThreads1()
384 C3Test test1 = new C3Test();
385 Thread TestThread = new Thread(new ThreadStart(test1.TestMethod1));
388 TestUtil.WaitForAlive (TestThread, "wait11");
391 Fail("#71 Unexpected Exception" + e.Message);
398 public void TestNestedThreads2()
400 C4Test test1 = new C4Test();
401 Thread TestThread = new Thread(new ThreadStart(test1.TestMethod1));
406 Fail("#81 Unexpected Exception" + e.ToString());
413 public void TestJoin1()
415 C1Test test1 = new C1Test();
416 C1Test test2 = new C1Test();
417 Thread thread1 = new Thread(new ThreadStart(test1.TestMethod));
418 Thread thread2 = new Thread(new ThreadStart(test1.TestMethod2));
427 Fail("#91 Unexpected Exception " + e.ToString());
436 public void TestThreadState()
438 //TODO: Test The rest of the possible transitions
439 C2Test test1 = new C2Test();
440 Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
441 AssertEquals("#101 Wrong Thread State",ThreadState.Unstarted,TestThread.ThreadState);
444 //while(!TestThread.IsAlive); //In the MS Documentation this is not necessary
445 //but in the MS SDK it is
446 Assert("#102 Wrong Thread State: " + TestThread.ThreadState.ToString(), TestThread.ThreadState == ThreadState.Running || (TestThread.ThreadState & ThreadState.Unstarted) != 0);
452 TestUtil.WaitForNotAlive (TestThread, "wait12");
453 // Docs say state will be Stopped, but Aborted happens sometimes (?)
454 Assert("#103 Wrong Thread State: " + TestThread.ThreadState.ToString(), (ThreadState.Stopped & TestThread.ThreadState) != 0
455 || (ThreadState.Aborted & TestThread.ThreadState) != 0);
459 public void CurrentPrincipal_PrincipalPolicy_NoPrincipal ()
461 // note: switching from PrincipalPolicy won't work inside the same thread
462 // because as soon as a Principal object is created the Policy doesn't matter anymore
463 Thread t = new Thread (new ThreadStart (ThreadedPrincipalTest.NoPrincipal));
474 public void CurrentPrincipal_PrincipalPolicy_UnauthenticatedPrincipal ()
476 // note: switching from PrincipalPolicy won't work inside the same thread
477 // because as soon as a Principal object is created the Policy doesn't matter anymore
478 Thread t = new Thread (new ThreadStart (ThreadedPrincipalTest.UnauthenticatedPrincipal));
489 public void CurrentPrincipal_PrincipalPolicy_WindowsPrincipal ()
491 // note: switching from PrincipalPolicy won't work inside the same thread
492 // because as soon as a Principal object is created the Policy doesn't matter anymore
493 Thread t = new Thread (new ThreadStart (ThreadedPrincipalTest.WindowsPrincipal));
505 [Category("NotDotNet")]
506 public void TestSuspend ()
508 Thread t = new Thread (new ThreadStart (DoCount));
509 t.IsBackground = true;
512 CheckIsRunning ("t1", t);
515 WaitSuspended ("t2", t);
517 CheckIsNotRunning ("t3", t);
520 WaitResumed ("t4", t);
522 CheckIsRunning ("t5", t);
525 TestUtil.WaitForNotAlive (t, "wait13");
526 CheckIsNotRunning ("t6", t);
529 [Category("NotDotNet")]
530 public void TestSuspendAbort ()
532 Thread t = new Thread (new ThreadStart (DoCount));
533 t.IsBackground = true;
536 CheckIsRunning ("t1", t);
539 WaitSuspended ("t2", t);
541 CheckIsNotRunning ("t3", t);
546 while (t.IsAlive && n < 200) {
551 Assert ("Timeout while waiting for abort", n < 200);
553 CheckIsNotRunning ("t6", t);
556 void CheckIsRunning (string s, Thread t)
560 Assert (s, counter > c);
563 void CheckIsNotRunning (string s, Thread t)
567 Assert (s, counter == c);
570 void WaitSuspended (string s, Thread t)
573 ThreadState state = t.ThreadState;
574 while ((state & ThreadState.Suspended) == 0) {
575 Assert (s + ": expected SuspendRequested state", (state & ThreadState.SuspendRequested) != 0);
578 Assert (s + ": failed to suspend", n < 100);
579 state = t.ThreadState;
581 Assert (s + ": SuspendRequested state not expected", (state & ThreadState.SuspendRequested) == 0);
584 void WaitResumed (string s, Thread t)
587 while ((t.ThreadState & ThreadState.Suspended) != 0) {
590 Assert (s + ": failed to resume", n < 100);
594 public void DoCount ()
603 public class TestUtil
605 public static void WaitForNotAlive (Thread t, string s)
607 WhileAlive (t, true, s);
610 public static void WaitForAlive (Thread t, string s)
612 WhileAlive (t, false, s);
615 public static void WhileAlive (Thread t, bool alive, string s)
617 DateTime ti = DateTime.Now;
618 while (t.IsAlive == alive) {
619 if ((DateTime.Now - ti).TotalSeconds > 10) {
620 if (alive) Assertion.Fail ("Timeout while waiting for not alive state. " + s);
621 else Assertion.Fail ("Timeout while waiting for alive state. " + s);