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 // These tests seem to hang the 2.0 framework. So they are disabled for now
20 namespace MonoTests.System.Threading {
22 [Category ("NotWorking")]
23 public class ThreadedPrincipalTest : Assertion {
25 public static void NoPrincipal ()
27 AppDomain.CurrentDomain.SetPrincipalPolicy (PrincipalPolicy.NoPrincipal);
28 IPrincipal p = Thread.CurrentPrincipal;
29 AssertNull ("Thread.CurrentPrincipal-1", p);
31 Thread.CurrentPrincipal = new GenericPrincipal (new GenericIdentity ("mono"), null);
32 AssertNotNull ("Thread.CurrentPrincipal-2", Thread.CurrentPrincipal);
34 Thread.CurrentPrincipal = null;
35 AssertNull ("Thread.CurrentPrincipal-3", Thread.CurrentPrincipal);
36 // in this case we can return to null
39 public static void UnauthenticatedPrincipal ()
41 AppDomain.CurrentDomain.SetPrincipalPolicy (PrincipalPolicy.UnauthenticatedPrincipal);
42 IPrincipal p = Thread.CurrentPrincipal;
43 AssertNotNull ("Thread.CurrentPrincipal", p);
44 Assert ("Type", (p is GenericPrincipal));
45 AssertEquals ("Name", String.Empty, p.Identity.Name);
46 AssertEquals ("AuthenticationType", String.Empty, p.Identity.AuthenticationType);
47 Assert ("IsAuthenticated", !p.Identity.IsAuthenticated);
49 Thread.CurrentPrincipal = new GenericPrincipal (new GenericIdentity ("mono"), null);
50 AssertNotNull ("Thread.CurrentPrincipal-2", Thread.CurrentPrincipal);
52 Thread.CurrentPrincipal = null;
53 AssertNotNull ("Thread.CurrentPrincipal-3", Thread.CurrentPrincipal);
54 // in this case we can't return to null
57 public static void WindowsPrincipal ()
59 AppDomain.CurrentDomain.SetPrincipalPolicy (PrincipalPolicy.WindowsPrincipal);
60 IPrincipal p = Thread.CurrentPrincipal;
61 AssertNotNull ("Thread.CurrentPrincipal", p);
62 Assert ("Type", (p is WindowsPrincipal));
63 AssertNotNull ("Name", p.Identity.Name);
64 AssertNotNull ("AuthenticationType", p.Identity.AuthenticationType);
65 Assert ("IsAuthenticated", p.Identity.IsAuthenticated);
67 // note: we can switch from a WindowsPrincipal to a GenericPrincipal
68 Thread.CurrentPrincipal = new GenericPrincipal (new GenericIdentity ("mono"), null);
69 AssertNotNull ("Thread.CurrentPrincipal-2", Thread.CurrentPrincipal);
71 Thread.CurrentPrincipal = null;
72 AssertNotNull ("Thread.CurrentPrincipal-3", Thread.CurrentPrincipal);
73 // in this case we can't return to null
78 [Category ("NotWorking")]
79 public class ThreadTest : Assertion {
81 //Some Classes to test as threads
85 public Thread thread1;
91 thread1 = (Thread)null;
93 endm1 = endm2 = false;
96 public void TestMethod()
104 public void TestMethod2()
106 if (!(thread1==(Thread)null) )
117 public bool run = false;
124 public void TestMethod()
139 public C1Test sub_class;
140 public Thread sub_thread;
144 sub_class = new C1Test();
145 sub_thread = new Thread(new ThreadStart(sub_class.TestMethod));
148 public void TestMethod1()
158 public C1Test class1;
159 public C1Test class2;
160 public Thread thread1;
161 public Thread thread2;
169 class1 = new C1Test();
170 class2 = new C1Test();
171 thread1 = new Thread(new ThreadStart(class1.TestMethod));
172 thread2 = new Thread(new ThreadStart(class2.TestMethod));
175 public void TestMethod1()
178 TestUtil.WaitForAlive (thread1, "wait1");
181 TestUtil.WaitForAlive (thread2, "wait2");
184 TestUtil.WaitForNotAlive (thread1, "wait3");
187 TestUtil.WaitForNotAlive (thread2, "wait4");
191 public void TestMethod2()
198 public void TestCtor1()
200 C1Test test1 = new C1Test();
203 Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
207 Fail ("#01 Unexpected Exception Thrown: " + e.ToString ());
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);
297 [Category("NotWorking")] // this is a MonoTODO
298 public void TestPriority2()
300 C2Test test1 = new C2Test();
301 Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
303 AssertEquals("#42 Incorrect Priority in New thread: ",ThreadPriority.Normal, TestThread.Priority);
305 TestUtil.WaitForAlive (TestThread, "wait8");
306 AssertEquals("#43 Incorrect Priority in Started thread: ",ThreadPriority.Normal, TestThread.Priority);
311 AssertEquals("#44 Incorrect Priority in Aborted thread: ",ThreadPriority.Normal, TestThread.Priority);
314 [Category("NotWorking")] // this is a MonoTODO
315 public void TestPriority3()
317 C2Test test1 = new C2Test();
318 Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
321 TestThread.Priority = ThreadPriority.Lowest;
322 AssertEquals("#45A Incorrect Priority:",ThreadPriority.Lowest,TestThread.Priority);
323 TestThread.Priority = ThreadPriority.BelowNormal;
324 AssertEquals("#45B Incorrect Priority:",ThreadPriority.BelowNormal,TestThread.Priority);
325 TestThread.Priority = ThreadPriority.Normal;
326 AssertEquals("#45C Incorrect Priority:",ThreadPriority.Normal,TestThread.Priority);
327 TestThread.Priority = ThreadPriority.AboveNormal;
328 AssertEquals("#45D Incorrect Priority:",ThreadPriority.AboveNormal,TestThread.Priority);
329 TestThread.Priority = ThreadPriority.Highest;
330 AssertEquals("#45E Incorrect Priority:",ThreadPriority.Highest,TestThread.Priority);
338 public void TestIsBackground1()
340 C2Test test1 = new C2Test();
341 Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
344 TestUtil.WaitForAlive (TestThread, "wait9");
345 bool state = TestThread.IsBackground;
346 Assert("#51 IsBackground not set at the default state: ",!(state));
353 public void TestIsBackground2()
355 C2Test test1 = new C2Test();
356 Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
357 TestThread.IsBackground = true;
364 Assert("#52 Is Background Changed ot Start ",TestThread.IsBackground);
368 public void TestName()
370 C2Test test1 = new C2Test();
371 Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
374 TestUtil.WaitForAlive (TestThread, "wait10");
375 string name = TestThread.Name;
376 AssertEquals("#61 Name set when mustn't be set: ", name, (string)null);
377 string newname = "Testing....";
378 TestThread.Name = newname;
379 AssertEquals("#62 Name not set when must be set: ",TestThread.Name,newname);
386 [Category("NotDotNet")]
387 public void TestNestedThreads1()
389 C3Test test1 = new C3Test();
390 Thread TestThread = new Thread(new ThreadStart(test1.TestMethod1));
393 TestUtil.WaitForAlive (TestThread, "wait11");
396 Fail("#71 Unexpected Exception" + e.Message);
403 public void TestNestedThreads2()
405 C4Test test1 = new C4Test();
406 Thread TestThread = new Thread(new ThreadStart(test1.TestMethod1));
411 Fail("#81 Unexpected Exception" + e.ToString());
419 public void TestJoin1()
421 C1Test test1 = new C1Test();
422 C1Test test2 = new C1Test();
423 Thread thread1 = new Thread(new ThreadStart(test1.TestMethod));
424 Thread thread2 = new Thread(new ThreadStart(test1.TestMethod2));
433 Fail("#91 Unexpected Exception " + e.ToString());
442 public void TestThreadState()
444 //TODO: Test The rest of the possible transitions
445 C2Test test1 = new C2Test();
446 Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
447 AssertEquals("#101 Wrong Thread State",ThreadState.Unstarted,TestThread.ThreadState);
450 //while(!TestThread.IsAlive); //In the MS Documentation this is not necessary
451 //but in the MS SDK it is
452 Assert("#102 Wrong Thread State: " + TestThread.ThreadState.ToString(), TestThread.ThreadState == ThreadState.Running || (TestThread.ThreadState & ThreadState.Unstarted) != 0);
458 TestUtil.WaitForNotAlive (TestThread, "wait12");
459 // Docs say state will be Stopped, but Aborted happens sometimes (?)
460 Assert("#103 Wrong Thread State: " + TestThread.ThreadState.ToString(), (ThreadState.Stopped & TestThread.ThreadState) != 0
461 || (ThreadState.Aborted & TestThread.ThreadState) != 0);
465 public void CurrentPrincipal_PrincipalPolicy_NoPrincipal ()
467 // note: switching from PrincipalPolicy won't work inside the same thread
468 // because as soon as a Principal object is created the Policy doesn't matter anymore
469 Thread t = new Thread (new ThreadStart (ThreadedPrincipalTest.NoPrincipal));
480 public void CurrentPrincipal_PrincipalPolicy_UnauthenticatedPrincipal ()
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.UnauthenticatedPrincipal));
495 public void CurrentPrincipal_PrincipalPolicy_WindowsPrincipal ()
497 // note: switching from PrincipalPolicy won't work inside the same thread
498 // because as soon as a Principal object is created the Policy doesn't matter anymore
499 Thread t = new Thread (new ThreadStart (ThreadedPrincipalTest.WindowsPrincipal));
511 [Category("NotDotNet")]
512 public void TestSuspend ()
514 Thread t = new Thread (new ThreadStart (DoCount));
515 t.IsBackground = true;
518 CheckIsRunning ("t1", t);
521 WaitSuspended ("t2", t);
523 CheckIsNotRunning ("t3", t);
526 WaitResumed ("t4", t);
528 CheckIsRunning ("t5", t);
531 TestUtil.WaitForNotAlive (t, "wait13");
532 CheckIsNotRunning ("t6", t);
535 [Category("NotDotNet")]
536 [Category("NotWorking")]
537 public void TestSuspendAbort ()
539 Thread t = new Thread (new ThreadStart (DoCount));
540 t.IsBackground = true;
543 CheckIsRunning ("t1", t);
546 WaitSuspended ("t2", t);
548 CheckIsNotRunning ("t3", t);
553 while (t.IsAlive && n < 200) {
558 Assert ("Timeout while waiting for abort", n < 200);
560 CheckIsNotRunning ("t6", t);
563 void CheckIsRunning (string s, Thread t)
567 Assert (s, counter > c);
570 void CheckIsNotRunning (string s, Thread t)
574 Assert (s, counter == c);
577 void WaitSuspended (string s, Thread t)
580 ThreadState state = t.ThreadState;
581 while ((state & ThreadState.Suspended) == 0) {
582 Assert (s + ": expected SuspendRequested state", (state & ThreadState.SuspendRequested) != 0);
585 Assert (s + ": failed to suspend", n < 100);
586 state = t.ThreadState;
588 Assert (s + ": SuspendRequested state not expected", (state & ThreadState.SuspendRequested) == 0);
591 void WaitResumed (string s, Thread t)
594 while ((t.ThreadState & ThreadState.Suspended) != 0) {
597 Assert (s + ": failed to resume", n < 100);
601 public void DoCount ()
610 public class TestUtil
612 public static void WaitForNotAlive (Thread t, string s)
614 WhileAlive (t, true, s);
617 public static void WaitForAlive (Thread t, string s)
619 WhileAlive (t, false, s);
622 public static void WhileAlive (Thread t, bool alive, string s)
624 DateTime ti = DateTime.Now;
625 while (t.IsAlive == alive) {
626 if ((DateTime.Now - ti).TotalSeconds > 10) {
627 if (alive) Assertion.Fail ("Timeout while waiting for not alive state. " + s);
628 else Assertion.Fail ("Timeout while waiting for alive state. " + s);