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()
152 public C1Test class1;
153 public C1Test class2;
154 public Thread thread1;
155 public Thread thread2;
163 class1 = new C1Test();
164 class2 = new C1Test();
165 thread1 = new Thread(new ThreadStart(class1.TestMethod));
166 thread2 = new Thread(new ThreadStart(class2.TestMethod));
169 public void TestMethod1()
172 while (!thread1.IsAlive);
175 while (!thread2.IsAlive);
178 while (thread1.IsAlive);
181 while (thread2.IsAlive);
185 public void TestMethod2()
192 public void TestCtor1()
194 C1Test test1 = new C1Test();
197 Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
199 catch (Exception e)
\r
201 Fail ("#01 Unexpected Exception Thrown: " + e.ToString ());
205 public void TestStart()
208 C1Test test1 = new C1Test();
209 Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
214 catch (Exception e)
\r
216 Fail ("#12 Unexpected Exception Thrown: " + e.ToString ());
219 AssertEquals("#13 Thread Not started: ", 10,test1.cnt);
222 bool errorThrown = false;
223 C2Test test1 = new C2Test();
224 Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
231 catch(ThreadStateException)
\r
235 Assert ("#14 no ThreadStateException trown", errorThrown);
238 C2Test test1 = new C2Test();
239 Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
242 bool started = (TestThread.ThreadState == ThreadState.Running);
243 AssertEquals("#15 Thread Is not in the correct state: ", started , test1.run);
248 public void TestApartment()
250 C2Test test1 = new C2Test();
251 Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
252 ApartmentState before = TestThread.ApartmentState;
254 while(!TestThread.IsAlive);
255 ApartmentState after = TestThread.ApartmentState;
257 AssertEquals("#21 Apartment State Changed when not needed",before,after);
260 public void TestApartmentState()
262 C2Test test1 = new C2Test();
263 Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
264 ApartmentState before = TestThread.ApartmentState;
266 while(!TestThread.IsAlive);
267 ApartmentState after = TestThread.ApartmentState;
269 AssertEquals("#31 Apartment State Changed when not needed: ",before,after);
272 public void TestPriority1()
274 C2Test test1 = new C2Test();
275 Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
276 TestThread.Priority=ThreadPriority.BelowNormal;
277 ThreadPriority after = TestThread.Priority;
279 while(!TestThread.IsAlive);
280 ThreadPriority before = TestThread.Priority;
282 AssertEquals("#41 Unexpected Priority Change: ",before,after);
285 public void TestPriority2()
287 C2Test test1 = new C2Test();
288 Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
289 AssertEquals("#42 Incorrect Priority in New thread: ",ThreadPriority.Normal, TestThread.Priority);
291 while(!TestThread.IsAlive);
292 AssertEquals("#43 Incorrect Priority in Started thread: ",ThreadPriority.Normal, TestThread.Priority);
294 AssertEquals("#44 Incorrect Priority in Aborted thread: ",ThreadPriority.Normal, TestThread.Priority);
297 public void TestPriority3()
300 C2Test test1 = new C2Test();
301 Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
303 TestThread.Priority = ThreadPriority.Lowest;
304 AssertEquals("#45A Incorrect Priority:",ThreadPriority.Lowest,TestThread.Priority);
305 TestThread.Priority = ThreadPriority.BelowNormal;
306 AssertEquals("#45B Incorrect Priority:",ThreadPriority.BelowNormal,TestThread.Priority);
307 TestThread.Priority = ThreadPriority.Normal;
308 AssertEquals("#45C Incorrect Priority:",ThreadPriority.Normal,TestThread.Priority);
309 TestThread.Priority = ThreadPriority.AboveNormal;
310 AssertEquals("#45D Incorrect Priority:",ThreadPriority.AboveNormal,TestThread.Priority);
311 TestThread.Priority = ThreadPriority.Highest;
312 AssertEquals("#45E Incorrect Priority:",ThreadPriority.Highest,TestThread.Priority);
317 public void TestIsBackground1()
319 C2Test test1 = new C2Test();
320 Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
322 while(!TestThread.IsAlive);
323 bool state = TestThread.IsBackground;
325 Assert("#51 IsBackground not set at the default state: ",!(state));
328 public void TestIsBackground2()
330 C2Test test1 = new C2Test();
331 Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
332 TestThread.IsBackground = true;
335 Assert("#52 Is Background Changed ot Start ",TestThread.IsBackground);
339 public void TestName()
341 C2Test test1 = new C2Test();
342 Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
344 while(!TestThread.IsAlive);
345 string name = TestThread.Name;
346 AssertEquals("#61 Name set when mustn't be set: ", name, (string)null);
347 string newname = "Testing....";
348 TestThread.Name = newname;
349 AssertEquals("#62 Name not set when must be set: ",TestThread.Name,newname);
353 public void TestNestedThreads1()
355 C3Test test1 = new C3Test();
356 Thread TestThread = new Thread(new ThreadStart(test1.TestMethod1));
360 while(!TestThread.IsAlive);
365 Fail("#71 Unexpected Exception" + e.Message);
369 public void TestNestedThreads2()
371 C4Test test1 = new C4Test();
372 Thread TestThread = new Thread(new ThreadStart(test1.TestMethod1));
380 Fail("#81 Unexpected Exception" + e.ToString());
384 public void TestJoin1()
386 C1Test test1 = new C1Test();
387 C1Test test2 = new C1Test();
388 Thread thread1 = new Thread(new ThreadStart(test1.TestMethod));
389 Thread thread2 = new Thread(new ThreadStart(test1.TestMethod2));
398 Fail("#91 Unexpected Exception " + e.ToString());
407 public void TestThreadState()
409 //TODO: Test The rest of the possible transitions
410 C2Test test1 = new C2Test();
411 Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
412 AssertEquals("#101 Wrong Thread State",ThreadState.Unstarted,TestThread.ThreadState);
414 //while(!TestThread.IsAlive); //In the MS Documentation this is not necessary
415 //but in the MS SDK it is
416 Assert("#102 Wrong Thread State: " + TestThread.ThreadState.ToString(), TestThread.ThreadState == ThreadState.Running || (TestThread.ThreadState & ThreadState.Unstarted) != 0);
418 while(TestThread.IsAlive);
419 // Docs say state will be Stopped, but Aborted happens sometimes (?)
420 Assert("#103 Wrong Thread State: " + TestThread.ThreadState.ToString(), (ThreadState.Stopped & TestThread.ThreadState) != 0
421 || (ThreadState.Aborted & TestThread.ThreadState) != 0);
425 public void CurrentPrincipal_PrincipalPolicy_NoPrincipal ()
427 // note: switching from PrincipalPolicy won't work inside the same thread
428 // because as soon as a Principal object is created the Policy doesn't matter anymore
429 Thread t = new Thread (new ThreadStart (ThreadedPrincipalTest.NoPrincipal));
435 public void CurrentPrincipal_PrincipalPolicy_UnauthenticatedPrincipal ()
437 // note: switching from PrincipalPolicy won't work inside the same thread
438 // because as soon as a Principal object is created the Policy doesn't matter anymore
439 Thread t = new Thread (new ThreadStart (ThreadedPrincipalTest.UnauthenticatedPrincipal));
445 public void CurrentPrincipal_PrincipalPolicy_WindowsPrincipal ()
447 // note: switching from PrincipalPolicy won't work inside the same thread
448 // because as soon as a Principal object is created the Policy doesn't matter anymore
449 Thread t = new Thread (new ThreadStart (ThreadedPrincipalTest.WindowsPrincipal));
456 public void TestSuspend ()
458 Thread t = new Thread (new ThreadStart (DoCount));
459 t.IsBackground = true;
462 CheckIsRunning ("t1", t);
465 WaitSuspended ("t2", t);
467 CheckIsNotRunning ("t3", t);
470 WaitResumed ("t4", t);
472 CheckIsRunning ("t5", t);
476 CheckIsNotRunning ("t6", t);
479 public void TestSuspendAbort ()
481 Thread t = new Thread (new ThreadStart (DoCount));
482 t.IsBackground = true;
485 CheckIsRunning ("t1", t);
488 WaitSuspended ("t2", t);
490 CheckIsNotRunning ("t3", t);
495 while (t.IsAlive && n < 200) {
500 Assert ("Timeout while waiting for abort", n < 200);
502 CheckIsNotRunning ("t6", t);
505 void CheckIsRunning (string s, Thread t)
509 Assert (s, counter > c);
512 void CheckIsNotRunning (string s, Thread t)
516 Assert (s, counter == c);
519 void WaitSuspended (string s, Thread t)
522 ThreadState state = t.ThreadState;
523 while ((state & ThreadState.Suspended) == 0) {
524 Assert (s + ": expected SuspendRequested state", (state & ThreadState.SuspendRequested) != 0);
527 Assert (s + ": failed to suspend", n < 100);
528 state = t.ThreadState;
530 Assert (s + ": SuspendRequested state not expected", (state & ThreadState.SuspendRequested) == 0);
533 void WaitResumed (string s, Thread t)
536 while ((t.ThreadState & ThreadState.Suspended) != 0) {
539 Assert (s + ": failed to resume", n < 100);
543 public void DoCount ()