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 public void TestApartmentState()
264 C2Test test1
= new C2Test();
265 Thread TestThread
= new Thread(new ThreadStart(test1
.TestMethod
));
266 ApartmentState before
= TestThread
.ApartmentState
;
268 TestUtil
.WaitForAlive (TestThread
, "wait6");
269 ApartmentState after
= TestThread
.ApartmentState
;
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
));
279 TestThread
.Priority
=ThreadPriority
.BelowNormal
;
280 ThreadPriority after
= TestThread
.Priority
;
282 TestUtil
.WaitForAlive (TestThread
, "wait7");
283 ThreadPriority before
= TestThread
.Priority
;
284 AssertEquals("#41 Unexpected Priority Change: ",before
,after
);
291 public void TestPriority2()
293 C2Test test1
= new C2Test();
294 Thread TestThread
= new Thread(new ThreadStart(test1
.TestMethod
));
296 AssertEquals("#42 Incorrect Priority in New thread: ",ThreadPriority
.Normal
, TestThread
.Priority
);
298 TestUtil
.WaitForAlive (TestThread
, "wait8");
299 AssertEquals("#43 Incorrect Priority in Started thread: ",ThreadPriority
.Normal
, TestThread
.Priority
);
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
));
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
);
330 public void TestIsBackground1()
332 C2Test test1
= new C2Test();
333 Thread TestThread
= new Thread(new ThreadStart(test1
.TestMethod
));
336 TestUtil
.WaitForAlive (TestThread
, "wait9");
337 bool state
= TestThread
.IsBackground
;
338 Assert("#51 IsBackground not set at the default state: ",!(state
));
345 public void TestIsBackground2()
347 C2Test test1
= new C2Test();
348 Thread TestThread
= new Thread(new ThreadStart(test1
.TestMethod
));
349 TestThread
.IsBackground
= true;
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
));
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
);
378 [Category("NotDotNet")]
379 public void TestNestedThreads1()
381 C3Test test1
= new C3Test();
382 Thread TestThread
= new Thread(new ThreadStart(test1
.TestMethod1
));
385 TestUtil
.WaitForAlive (TestThread
, "wait11");
388 Fail("#71 Unexpected Exception" + e
.Message
);
395 public void TestNestedThreads2()
397 C4Test test1
= new C4Test();
398 Thread TestThread
= new Thread(new ThreadStart(test1
.TestMethod1
));
403 Fail("#81 Unexpected Exception" + e
.ToString());
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
));
424 Fail("#91 Unexpected Exception " + e
.ToString());
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
);
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);
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);
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
));
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
));
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
));
502 public void TestSuspend ()
504 Thread t
= new Thread (new ThreadStart (DoCount
));
505 t
.IsBackground
= true;
508 CheckIsRunning ("t1", t
);
511 WaitSuspended ("t2", t
);
513 CheckIsNotRunning ("t3", t
);
516 WaitResumed ("t4", t
);
518 CheckIsRunning ("t5", t
);
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;
531 CheckIsRunning ("t1", t
);
534 WaitSuspended ("t2", t
);
536 CheckIsNotRunning ("t3", t
);
541 while (t
.IsAlive
&& n
< 200) {
546 Assert ("Timeout while waiting for abort", n
< 200);
548 CheckIsNotRunning ("t6", t
);
551 void CheckIsRunning (string s
, Thread t
)
555 Assert (s
, counter
> c
);
558 void CheckIsNotRunning (string s
, Thread t
)
562 Assert (s
, counter
== c
);
565 void WaitSuspended (string s
, Thread t
)
568 ThreadState state
= t
.ThreadState
;
569 while ((state
& ThreadState
.Suspended
) == 0) {
570 Assert (s
+ ": expected SuspendRequested state", (state
& ThreadState
.SuspendRequested
) != 0);
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
)
582 while ((t
.ThreadState
& ThreadState
.Suspended
) != 0) {
585 Assert (s
+ ": failed to resume", n
< 100);
589 public void DoCount ()
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
);