**** Merged from MCS ****
[mono-project.git] / mcs / class / corlib / Test / System.Threading / ThreadTest.cs
blob26cc596dfc67d7a5bf39038bdaae6078b7bea3fd
1 // ThreadTest.cs - NUnit Test Cases for the System.Threading.Thread class
2 //
3 // Authors
4 // Eduardo Garcia Cebollero (kiwnix@yahoo.es)
5 // Sebastien Pouliot <sebastien@ximian.com>
6 //
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;
13 using System;
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
73 [TestFixture]
74 public class ThreadTest : Assertion {
76 //Some Classes to test as threads
77 private class C1Test
79 public int cnt;
80 public Thread thread1;
81 public bool endm1;
82 public bool endm2;
84 public C1Test()
86 thread1 = (Thread)null;
87 this.cnt = 0;
88 endm1 = endm2 = false;
91 public void TestMethod()
93 while (cnt < 10)
95 cnt++;
97 endm1 = true;
99 public void TestMethod2()
101 if (!(thread1==(Thread)null) )
103 thread1.Join();
105 endm2 = true;
109 private class C2Test
111 public int cnt;
112 public bool run = false;
114 public C2Test()
116 this.cnt = 0;
119 public void TestMethod()
121 run = true;
122 while (true)
124 if (cnt < 1000)
125 cnt++;
126 else
127 cnt = 0;
132 private class C3Test
134 public C1Test sub_class;
135 public Thread sub_thread;
137 public C3Test()
139 sub_class = new C1Test();
140 sub_thread = new Thread(new ThreadStart(sub_class.TestMethod));
143 public void TestMethod1()
145 sub_thread.Start();
146 Thread.Sleep (100);
147 sub_thread.Abort();
151 private class C4Test
153 public C1Test class1;
154 public C1Test class2;
155 public Thread thread1;
156 public Thread thread2;
157 public bool T1ON ;
158 public bool T2ON ;
160 public C4Test()
162 T1ON = false;
163 T2ON = false;
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()
172 thread1.Start();
173 TestUtil.WaitForAlive (thread1, "wait1");
174 T1ON = true;
175 thread2.Start();
176 TestUtil.WaitForAlive (thread2, "wait2");
177 T2ON = true;
178 thread1.Abort();
179 TestUtil.WaitForNotAlive (thread1, "wait3");
180 T1ON = false;
181 thread2.Abort();
182 TestUtil.WaitForNotAlive (thread2, "wait4");
183 T2ON = false;
186 public void TestMethod2()
188 thread1.Start();
189 thread1.Join();
193 public void TestCtor1()
195 C1Test test1 = new C1Test();
198 Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
200 catch (Exception e)
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));
214 TestThread.Start();
216 catch (Exception e)
218 Fail ("#12 Unexpected Exception Thrown: " + e.ToString ());
220 TestThread.Join();
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));
227 TestThread.Start();
228 TestThread.Abort();
231 TestThread.Start();
233 catch(ThreadStateException)
235 errorThrown = true;
237 Assert ("#14 no ThreadStateException trown", errorThrown);
240 C2Test test1 = new C2Test();
241 Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
242 TestThread.Start();
243 while(!test1.run);
244 bool started = (TestThread.ThreadState == ThreadState.Running);
245 AssertEquals("#15 Thread Is not in the correct state: ", started , test1.run);
246 TestThread.Abort();
250 public void TestApartment()
252 C2Test test1 = new C2Test();
253 Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
254 ApartmentState before = TestThread.ApartmentState;
255 TestThread.Start();
256 TestUtil.WaitForAlive (TestThread, "wait5");
257 ApartmentState after = TestThread.ApartmentState;
258 TestThread.Abort();
259 AssertEquals("#21 Apartment State Changed when not needed",before,after);
262 public void TestApartmentState()
264 C2Test test1 = new C2Test();
265 Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
266 ApartmentState before = TestThread.ApartmentState;
267 TestThread.Start();
268 TestUtil.WaitForAlive (TestThread, "wait6");
269 ApartmentState after = TestThread.ApartmentState;
270 TestThread.Abort();
271 AssertEquals("#31 Apartment State Changed when not needed: ",before,after);
274 public void TestPriority1()
276 C2Test test1 = new C2Test();
277 Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
278 try {
279 TestThread.Priority=ThreadPriority.BelowNormal;
280 ThreadPriority after = TestThread.Priority;
281 TestThread.Start();
282 TestUtil.WaitForAlive (TestThread, "wait7");
283 ThreadPriority before = TestThread.Priority;
284 AssertEquals("#41 Unexpected Priority Change: ",before,after);
286 finally {
287 TestThread.Abort();
291 public void TestPriority2()
293 C2Test test1 = new C2Test();
294 Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
295 try {
296 AssertEquals("#42 Incorrect Priority in New thread: ",ThreadPriority.Normal, TestThread.Priority);
297 TestThread.Start();
298 TestUtil.WaitForAlive (TestThread, "wait8");
299 AssertEquals("#43 Incorrect Priority in Started thread: ",ThreadPriority.Normal, TestThread.Priority);
301 finally {
302 TestThread.Abort();
304 AssertEquals("#44 Incorrect Priority in Aborted thread: ",ThreadPriority.Normal, TestThread.Priority);
307 public void TestPriority3()
309 C2Test test1 = new C2Test();
310 Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
311 try {
312 TestThread.Start();
313 TestThread.Priority = ThreadPriority.Lowest;
314 AssertEquals("#45A Incorrect Priority:",ThreadPriority.Lowest,TestThread.Priority);
315 TestThread.Priority = ThreadPriority.BelowNormal;
316 AssertEquals("#45B Incorrect Priority:",ThreadPriority.BelowNormal,TestThread.Priority);
317 TestThread.Priority = ThreadPriority.Normal;
318 AssertEquals("#45C Incorrect Priority:",ThreadPriority.Normal,TestThread.Priority);
319 TestThread.Priority = ThreadPriority.AboveNormal;
320 AssertEquals("#45D Incorrect Priority:",ThreadPriority.AboveNormal,TestThread.Priority);
321 TestThread.Priority = ThreadPriority.Highest;
322 AssertEquals("#45E Incorrect Priority:",ThreadPriority.Highest,TestThread.Priority);
324 finally {
325 TestThread.Abort();
330 public void TestIsBackground1()
332 C2Test test1 = new C2Test();
333 Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
334 try {
335 TestThread.Start();
336 TestUtil.WaitForAlive (TestThread, "wait9");
337 bool state = TestThread.IsBackground;
338 Assert("#51 IsBackground not set at the default state: ",!(state));
340 finally {
341 TestThread.Abort();
345 public void TestIsBackground2()
347 C2Test test1 = new C2Test();
348 Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
349 TestThread.IsBackground = true;
350 try {
351 TestThread.Start();
353 finally {
354 TestThread.Abort();
356 Assert("#52 Is Background Changed ot Start ",TestThread.IsBackground);
360 public void TestName()
362 C2Test test1 = new C2Test();
363 Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
364 try {
365 TestThread.Start();
366 TestUtil.WaitForAlive (TestThread, "wait10");
367 string name = TestThread.Name;
368 AssertEquals("#61 Name set when mustn't be set: ", name, (string)null);
369 string newname = "Testing....";
370 TestThread.Name = newname;
371 AssertEquals("#62 Name not set when must be set: ",TestThread.Name,newname);
373 finally {
374 TestThread.Abort();
378 [Category("NotDotNet")]
379 public void TestNestedThreads1()
381 C3Test test1 = new C3Test();
382 Thread TestThread = new Thread(new ThreadStart(test1.TestMethod1));
383 try {
384 TestThread.Start();
385 TestUtil.WaitForAlive (TestThread, "wait11");
387 catch(Exception e) {
388 Fail("#71 Unexpected Exception" + e.Message);
390 finally {
391 TestThread.Abort();
395 public void TestNestedThreads2()
397 C4Test test1 = new C4Test();
398 Thread TestThread = new Thread(new ThreadStart(test1.TestMethod1));
399 try {
400 TestThread.Start();
402 catch(Exception e) {
403 Fail("#81 Unexpected Exception" + e.ToString());
405 finally {
406 TestThread.Abort();
410 public void TestJoin1()
412 C1Test test1 = new C1Test();
413 C1Test test2 = new C1Test();
414 Thread thread1 = new Thread(new ThreadStart(test1.TestMethod));
415 Thread thread2 = new Thread(new ThreadStart(test1.TestMethod2));
418 thread1.Start();
419 thread2.Start();
420 thread2.Join();
422 catch(Exception e)
424 Fail("#91 Unexpected Exception " + e.ToString());
426 finally
428 thread1.Abort();
429 thread2.Abort();
433 public void TestThreadState()
435 //TODO: Test The rest of the possible transitions
436 C2Test test1 = new C2Test();
437 Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
438 AssertEquals("#101 Wrong Thread State",ThreadState.Unstarted,TestThread.ThreadState);
439 try {
440 TestThread.Start();
441 //while(!TestThread.IsAlive); //In the MS Documentation this is not necessary
442 //but in the MS SDK it is
443 Assert("#102 Wrong Thread State: " + TestThread.ThreadState.ToString(), TestThread.ThreadState == ThreadState.Running || (TestThread.ThreadState & ThreadState.Unstarted) != 0);
445 finally {
446 TestThread.Abort();
449 TestUtil.WaitForNotAlive (TestThread, "wait12");
450 // Docs say state will be Stopped, but Aborted happens sometimes (?)
451 Assert("#103 Wrong Thread State: " + TestThread.ThreadState.ToString(), (ThreadState.Stopped & TestThread.ThreadState) != 0
452 || (ThreadState.Aborted & TestThread.ThreadState) != 0);
455 [Test]
456 public void CurrentPrincipal_PrincipalPolicy_NoPrincipal ()
458 // note: switching from PrincipalPolicy won't work inside the same thread
459 // because as soon as a Principal object is created the Policy doesn't matter anymore
460 Thread t = new Thread (new ThreadStart (ThreadedPrincipalTest.NoPrincipal));
461 try {
462 t.Start ();
463 t.Join ();
465 catch {
466 t.Abort ();
470 [Test]
471 public void CurrentPrincipal_PrincipalPolicy_UnauthenticatedPrincipal ()
473 // note: switching from PrincipalPolicy won't work inside the same thread
474 // because as soon as a Principal object is created the Policy doesn't matter anymore
475 Thread t = new Thread (new ThreadStart (ThreadedPrincipalTest.UnauthenticatedPrincipal));
476 try {
477 t.Start ();
478 t.Join ();
480 catch {
481 t.Abort ();
485 [Test]
486 public void CurrentPrincipal_PrincipalPolicy_WindowsPrincipal ()
488 // note: switching from PrincipalPolicy won't work inside the same thread
489 // because as soon as a Principal object is created the Policy doesn't matter anymore
490 Thread t = new Thread (new ThreadStart (ThreadedPrincipalTest.WindowsPrincipal));
491 try {
492 t.Start ();
493 t.Join ();
495 catch {
496 t.Abort ();
500 int counter = 0;
502 public void TestSuspend ()
504 Thread t = new Thread (new ThreadStart (DoCount));
505 t.IsBackground = true;
506 t.Start ();
508 CheckIsRunning ("t1", t);
510 t.Suspend ();
511 WaitSuspended ("t2", t);
513 CheckIsNotRunning ("t3", t);
515 t.Resume ();
516 WaitResumed ("t4", t);
518 CheckIsRunning ("t5", t);
520 t.Abort ();
521 TestUtil.WaitForNotAlive (t, "wait13");
522 CheckIsNotRunning ("t6", t);
525 public void TestSuspendAbort ()
527 Thread t = new Thread (new ThreadStart (DoCount));
528 t.IsBackground = true;
529 t.Start ();
531 CheckIsRunning ("t1", t);
533 t.Suspend ();
534 WaitSuspended ("t2", t);
536 CheckIsNotRunning ("t3", t);
538 t.Abort ();
540 int n=0;
541 while (t.IsAlive && n < 200) {
542 Thread.Sleep (10);
543 n++;
546 Assert ("Timeout while waiting for abort", n < 200);
548 CheckIsNotRunning ("t6", t);
551 void CheckIsRunning (string s, Thread t)
553 int c = counter;
554 Thread.Sleep (100);
555 Assert (s, counter > c);
558 void CheckIsNotRunning (string s, Thread t)
560 int c = counter;
561 Thread.Sleep (100);
562 Assert (s, counter == c);
565 void WaitSuspended (string s, Thread t)
567 int n=0;
568 ThreadState state = t.ThreadState;
569 while ((state & ThreadState.Suspended) == 0) {
570 Assert (s + ": expected SuspendRequested state", (state & ThreadState.SuspendRequested) != 0);
571 Thread.Sleep (10);
572 n++;
573 Assert (s + ": failed to suspend", n < 100);
574 state = t.ThreadState;
576 Assert (s + ": SuspendRequested state not expected", (state & ThreadState.SuspendRequested) == 0);
579 void WaitResumed (string s, Thread t)
581 int n=0;
582 while ((t.ThreadState & ThreadState.Suspended) != 0) {
583 Thread.Sleep (10);
584 n++;
585 Assert (s + ": failed to resume", n < 100);
589 public void DoCount ()
591 while (true) {
592 counter++;
593 Thread.Sleep (1);
598 public class TestUtil
600 public static void WaitForNotAlive (Thread t, string s)
602 WhileAlive (t, true, s);
605 public static void WaitForAlive (Thread t, string s)
607 WhileAlive (t, false, s);
610 public static void WhileAlive (Thread t, bool alive, string s)
612 DateTime ti = DateTime.Now;
613 while (t.IsAlive == alive) {
614 if ((DateTime.Now - ti).TotalSeconds > 10) {
615 if (alive) Assertion.Fail ("Timeout while waiting for not alive state. " + s);
616 else Assertion.Fail ("Timeout while waiting for alive state. " + s);