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 test1.thread1.Start();
373 test1.thread1.Abort();
374 while(test1.thread1.IsAlive);
375 Thread TestThread = new Thread(new ThreadStart(test1.TestMethod1));
383 Fail("#81 Unexpected Exception" + e.ToString());
387 public void TestJoin1()
389 C1Test test1 = new C1Test();
390 C1Test test2 = new C1Test();
391 Thread thread1 = new Thread(new ThreadStart(test1.TestMethod));
392 Thread thread2 = new Thread(new ThreadStart(test1.TestMethod2));
401 Fail("#91 Unexpected Exception " + e.ToString());
410 public void TestThreadState()
412 //TODO: Test The rest of the possible transitions
413 C2Test test1 = new C2Test();
414 Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
415 AssertEquals("#101 Wrong Thread State",ThreadState.Unstarted,TestThread.ThreadState);
417 //while(!TestThread.IsAlive); //In the MS Documentation this is not necessary
418 //but in the MS SDK it is
419 AssertEquals("#102 Wrong Thread State", ThreadState.Running | ThreadState.Unstarted ,TestThread.ThreadState);
421 while(TestThread.IsAlive);
422 // Docs say state will be Stopped, but Aborted happens sometimes (?)
423 Assert("#103 Wrong Thread State", ThreadState.Stopped == TestThread.ThreadState
424 || ThreadState.Aborted == TestThread.ThreadState);
428 public void CurrentPrincipal_PrincipalPolicy_NoPrincipal ()
430 // note: switching from PrincipalPolicy won't work inside the same thread
431 // because as soon as a Principal object is created the Policy doesn't matter anymore
432 Thread t = new Thread (new ThreadStart (ThreadedPrincipalTest.NoPrincipal));
438 public void CurrentPrincipal_PrincipalPolicy_UnauthenticatedPrincipal ()
440 // note: switching from PrincipalPolicy won't work inside the same thread
441 // because as soon as a Principal object is created the Policy doesn't matter anymore
442 Thread t = new Thread (new ThreadStart (ThreadedPrincipalTest.UnauthenticatedPrincipal));
448 public void CurrentPrincipal_PrincipalPolicy_WindowsPrincipal ()
450 // note: switching from PrincipalPolicy won't work inside the same thread
451 // because as soon as a Principal object is created the Policy doesn't matter anymore
452 Thread t = new Thread (new ThreadStart (ThreadedPrincipalTest.WindowsPrincipal));