2010-04-06 Jb Evain <jbevain@novell.com>
[mcs.git] / class / corlib / Test / System.Threading / ThreadTest.cs
blobd63aad8e714e4dc178b9f77b99474d58f0bcaa32
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 System;
13 using System.Globalization;
14 using System.Security.Principal;
15 using System.Threading;
17 using NUnit.Framework;
19 namespace MonoTests.System.Threading
21 // These tests seem to hang the 2.0 framework. So they are disabled for now
22 // Don't reenable them until you can run a few thousand times on an SMP box.
23 [Category ("NotWorking")]
24 public class ThreadedPrincipalTest
26 public static void NoPrincipal ()
28 #if !TARGET_JVM // AppDomain.SetPrincipalPolicy not supported for TARGET_JVM
29 AppDomain.CurrentDomain.SetPrincipalPolicy (PrincipalPolicy.NoPrincipal);
30 #endif
31 IPrincipal p = Thread.CurrentPrincipal;
32 Assert.IsNull (p, "#1");
34 Thread.CurrentPrincipal = new GenericPrincipal (new GenericIdentity ("mono"), null);
35 Assert.IsNotNull (Thread.CurrentPrincipal, "#2");
37 Thread.CurrentPrincipal = null;
38 Assert.IsNull (Thread.CurrentPrincipal, "#3");
39 // in this case we can return to null
42 #if !TARGET_JVM // AppDomain.SetPrincipalPolicy not supported for TARGET_JVM
43 public static void UnauthenticatedPrincipal ()
45 AppDomain.CurrentDomain.SetPrincipalPolicy (PrincipalPolicy.UnauthenticatedPrincipal);
46 IPrincipal p = Thread.CurrentPrincipal;
47 Assert.IsNotNull (p, "#1");
48 Assert.IsTrue ((p is GenericPrincipal), "#2");
49 Assert.AreEqual (String.Empty, p.Identity.Name, "#3");
50 Assert.AreEqual (String.Empty, p.Identity.AuthenticationType, "#4");
51 Assert.IsFalse (p.Identity.IsAuthenticated, "#5");
53 Thread.CurrentPrincipal = new GenericPrincipal (new GenericIdentity ("mono"), null);
54 Assert.IsNotNull (Thread.CurrentPrincipal, "#6");
56 Thread.CurrentPrincipal = null;
57 Assert.IsNotNull (Thread.CurrentPrincipal, "#7");
58 // in this case we can't return to null
61 public static void WindowsPrincipal ()
63 AppDomain.CurrentDomain.SetPrincipalPolicy (PrincipalPolicy.WindowsPrincipal);
64 IPrincipal p = Thread.CurrentPrincipal;
65 Assert.IsNotNull (p, "#1");
66 Assert.IsTrue ((p is WindowsPrincipal), "#2");
67 Assert.IsNotNull (p.Identity.Name, "#3");
68 Assert.IsNotNull (p.Identity.AuthenticationType, "#4");
69 Assert.IsTrue (p.Identity.IsAuthenticated, "#5");
71 // note: we can switch from a WindowsPrincipal to a GenericPrincipal
72 Thread.CurrentPrincipal = new GenericPrincipal (new GenericIdentity ("mono"), null);
73 Assert.IsNotNull (Thread.CurrentPrincipal, "#6");
75 Thread.CurrentPrincipal = null;
76 Assert.IsNotNull (Thread.CurrentPrincipal, "#7");
77 // in this case we can't return to null
79 #endif // TARGET_JVM
81 public static void CopyOnNewThread ()
83 Assert.IsNotNull (Thread.CurrentPrincipal, "#1");
84 Assert.AreEqual ("good", Thread.CurrentPrincipal.Identity.Name, "#2");
88 [TestFixture]
89 public class ThreadTest
91 TimeSpan Infinite = new TimeSpan (-10000); // -10000 ticks == -1 ms
92 TimeSpan SmallNegative = new TimeSpan (-2); // between 0 and -1.0 (infinite) ms
93 TimeSpan Negative = new TimeSpan (-20000); // really negative
94 TimeSpan MaxValue = TimeSpan.FromMilliseconds ((long) Int32.MaxValue);
95 TimeSpan TooLarge = TimeSpan.FromMilliseconds ((long) Int32.MaxValue + 1);
97 static bool is_win32;
98 static bool is_mono;
100 static ThreadTest ()
102 switch (Environment.OSVersion.Platform) {
103 case PlatformID.Win32NT:
104 case PlatformID.Win32S:
105 case PlatformID.Win32Windows:
106 case PlatformID.WinCE:
107 is_win32 = true;
108 break;
111 // check a class in mscorlib to determine if we're running on Mono
112 if (Type.GetType ("System.MonoType", false) != null)
113 is_mono = true;
116 //Some Classes to test as threads
117 private class C1Test
119 public int cnt;
120 public Thread thread1;
121 public bool endm1;
122 public bool endm2;
124 public C1Test()
126 thread1 = (Thread)null;
127 this.cnt = 0;
128 endm1 = endm2 = false;
131 public void TestMethod()
133 while (cnt < 10)
135 cnt++;
137 endm1 = true;
139 public void TestMethod2()
141 if (!(thread1==(Thread)null) )
143 thread1.Join();
145 endm2 = true;
149 private class C2Test
151 public int cnt;
152 public bool run = false;
154 public C2Test()
156 this.cnt = 0;
159 public void TestMethod()
161 run = true;
162 while (true)
164 if (cnt < 1000)
165 cnt++;
166 else
167 cnt = 0;
172 private class C3Test
174 public C1Test sub_class;
175 public Thread sub_thread;
177 public C3Test()
179 sub_class = new C1Test();
180 sub_thread = new Thread(new ThreadStart(sub_class.TestMethod));
183 public void TestMethod1()
185 sub_thread.Start();
186 Thread.Sleep (100);
187 sub_thread.Abort();
191 private class C4Test
193 public C1Test class1;
194 public C1Test class2;
195 public Thread thread1;
196 public Thread thread2;
197 public bool T1ON ;
198 public bool T2ON ;
200 public C4Test()
202 T1ON = false;
203 T2ON = false;
204 class1 = new C1Test();
205 class2 = new C1Test();
206 thread1 = new Thread(new ThreadStart(class1.TestMethod));
207 thread2 = new Thread(new ThreadStart(class2.TestMethod));
210 public void TestMethod1()
212 thread1.Start();
213 TestUtil.WaitForAlive (thread1, "wait1");
214 T1ON = true;
215 thread2.Start();
216 TestUtil.WaitForAlive (thread2, "wait2");
217 T2ON = true;
218 thread1.Abort();
219 TestUtil.WaitForNotAlive (thread1, "wait3");
220 T1ON = false;
221 thread2.Abort();
222 TestUtil.WaitForNotAlive (thread2, "wait4");
223 T2ON = false;
226 public void TestMethod2()
228 thread1.Start();
229 thread1.Join();
233 [Test]
234 public void TestCtor1()
236 C1Test test1 = new C1Test();
237 Thread t = new Thread (new ThreadStart (test1.TestMethod));
239 Assert.IsTrue (t.CurrentCulture.IsReadOnly, "CurrentCulture.IsReadOnly");
240 Assert.IsFalse (t.IsAlive, "IsAlive");
241 Assert.IsFalse (t.IsBackground, "IsBackground");
242 Assert.IsNull (t.Name, "Name");
243 Assert.AreEqual (ThreadState.Unstarted, t.ThreadState, "ThreadState");
246 [Test]
247 [Category ("NotWorking")] // we're not sharing (read-only) CultureInfo
248 public void CultureInfo_Shared_Across_Threads ()
250 Thread t = new Thread (TestCtor1);
251 Assert.AreSame (t.CurrentCulture, t.CurrentUICulture, "Culture");
253 Assert.AreSame (t.CurrentCulture, CultureInfo.CurrentCulture, "CultureInfo.CurrentCulture");
254 Assert.AreSame (t.CurrentUICulture, CultureInfo.CurrentUICulture, "CultureInfo.CurrentUICulture");
256 Assert.AreSame (t.CurrentCulture, Thread.CurrentThread.CurrentCulture, "Thread.CurrentThread.CurrentCulture");
257 Assert.AreSame (t.CurrentUICulture, Thread.CurrentThread.CurrentUICulture, "Thread.CurrentThread.CurrentUICulture");
260 [Test] // bug #325566
261 public void GetHashCodeTest ()
263 C1Test test1 = new C1Test ();
264 Thread tA = new Thread (new ThreadStart (test1.TestMethod));
265 int hA1 = tA.GetHashCode ();
266 #if NET_2_0
267 Assert.IsTrue (hA1 > 0, "#A1");
268 #endif
269 tA.Start ();
270 int hA2 = tA.GetHashCode ();
271 Assert.AreEqual (hA1, hA2, "#A2");
272 tA.Join ();
273 int hA3 = tA.GetHashCode ();
274 Assert.AreEqual (hA1, hA3, "#A3");
275 #if NET_2_0
276 Assert.AreEqual (hA1, tA.ManagedThreadId, "#A4");
277 #endif
279 test1 = new C1Test ();
280 Thread tB = new Thread (new ThreadStart (test1.TestMethod));
281 int hB1 = tB.GetHashCode ();
282 #if NET_2_0
283 Assert.IsTrue (hB1 > 0, "#B1");
284 #endif
285 tB.Start ();
286 int hB2 = tB.GetHashCode ();
287 Assert.AreEqual (hB1, hB2, "#B2");
288 tB.Join ();
289 int hB3 = tB.GetHashCode ();
290 Assert.AreEqual (hB1, hB3, "#B3");
291 #if NET_2_0
292 Assert.AreEqual (hB1, tB.ManagedThreadId, "#B4");
293 #endif
294 Assert.IsFalse (hA2 == hB2, "#B5");
297 #if NET_2_0
298 [Test] // bug #82700
299 public void ManagedThreadId ()
301 C1Test test1 = new C1Test ();
302 Thread t1 = new Thread (new ThreadStart (test1.TestMethod));
303 int mtA1 = t1.ManagedThreadId;
304 t1.Start ();
305 int mtA2 = t1.ManagedThreadId;
306 t1.Join ();
307 int mtA3 = t1.ManagedThreadId;
308 Assert.AreEqual (mtA1, mtA2, "#A1");
309 Assert.AreEqual (mtA2, mtA3, "#A2");
311 test1 = new C1Test ();
312 Thread t2 = new Thread (new ThreadStart (test1.TestMethod));
313 int mtB1 = t2.ManagedThreadId;
314 t2.Start ();
315 int mtB2 = t2.ManagedThreadId;
316 t2.Join ();
317 int mtB3 = t2.ManagedThreadId;
318 Assert.AreEqual (mtB1, mtB2, "#B1");
319 Assert.AreEqual (mtB2, mtB3, "#B2");
320 Assert.IsFalse (mtB1 == mtA1, "#B3");
322 #endif
324 [Test]
325 [Category ("NotDotNet")] // it hangs.
326 public void TestStart()
328 if (is_win32 && is_mono)
329 Assert.Fail ("This test fails on Win32. The test should be fixed.");
331 C1Test test1 = new C1Test();
332 Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
333 TestThread.Start();
334 TestThread.Join();
335 Assert.AreEqual (10, test1.cnt, "#1");
338 C2Test test1 = new C2Test();
339 Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
340 TestThread.Start();
341 TestThread.Abort();
342 try {
343 TestThread.Start();
344 Assert.Fail ("#2");
345 } catch (ThreadStateException) {
349 C2Test test1 = new C2Test();
350 Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
351 TestThread.Start();
352 while (!test1.run) {
354 bool started = (TestThread.ThreadState == ThreadState.Running);
355 Assert.AreEqual (started, test1.run, "#15 Thread Is not in the correct state: ");
356 TestThread.Abort();
360 [Test]
361 public void TestApartmentState ()
363 if (is_win32 && is_mono)
364 Assert.Fail ("This test fails on mono on win32. Our runtime should be fixed.");
366 C2Test test1 = new C2Test();
367 Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
368 Assert.AreEqual (ApartmentState.Unknown, TestThread.ApartmentState, "#1");
369 TestThread.Start();
370 TestUtil.WaitForAlive (TestThread, "wait5");
371 #if NET_2_0
372 Assert.AreEqual (ApartmentState.MTA, TestThread.ApartmentState, "#2");
373 #else
374 Assert.AreEqual (ApartmentState.Unknown, TestThread.ApartmentState, "#3");
375 #endif
376 TestThread.Abort();
379 [Test]
380 public void TestPriority1()
382 if (is_win32 && is_mono)
383 Assert.Fail ("This test fails on mono on Win32. Our runtime should be fixed.");
385 C2Test test1 = new C2Test();
386 Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
387 try {
388 TestThread.Priority=ThreadPriority.BelowNormal;
389 ThreadPriority after = TestThread.Priority;
390 TestThread.Start();
391 TestUtil.WaitForAlive (TestThread, "wait7");
392 ThreadPriority before = TestThread.Priority;
393 Assert.AreEqual (before, after, "#41 Unexpected Priority Change: ");
394 } finally {
395 TestThread.Abort();
399 [Test]
400 [Category ("NotDotNet")] // on MS, Thread is still in AbortRequested state when Start is invoked
401 public void AbortUnstarted ()
403 C2Test test1 = new C2Test();
404 Thread th = new Thread (new ThreadStart (test1.TestMethod));
405 th.Abort ();
406 th.Start ();
409 [Test]
410 [Category ("NotDotNet")] // on MS, ThreadState is immediately Stopped after Abort
411 [Category ("NotWorking")] // this is a MonoTODO -> no support for Priority
412 public void TestPriority2()
414 C2Test test1 = new C2Test();
415 Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
416 try {
417 Assert.AreEqual (ThreadPriority.Normal, TestThread.Priority, "#42 Incorrect Priority in New thread: ");
418 TestThread.Start();
419 TestUtil.WaitForAliveOrStop (TestThread, "wait8");
420 Assert.AreEqual (ThreadPriority.Normal, TestThread.Priority, "#43 Incorrect Priority in Started thread: ");
421 } finally {
422 TestThread.Abort();
424 Assert.AreEqual (ThreadPriority.Normal, TestThread.Priority, "#44 Incorrect Priority in Aborted thread: ");
427 [Test]
428 [Category ("NotWorking")] // this is a MonoTODO -> no support for Priority
429 public void TestPriority3()
431 C2Test test1 = new C2Test();
432 Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
433 try {
434 TestThread.Start();
435 TestThread.Priority = ThreadPriority.Lowest;
436 Assert.AreEqual (ThreadPriority.Lowest, TestThread.Priority, "#45A Incorrect Priority:");
437 TestThread.Priority = ThreadPriority.BelowNormal;
438 Assert.AreEqual (ThreadPriority.BelowNormal, TestThread.Priority, "#45B Incorrect Priority:");
439 TestThread.Priority = ThreadPriority.Normal;
440 Assert.AreEqual (ThreadPriority.Normal, TestThread.Priority, "#45C Incorrect Priority:");
441 TestThread.Priority = ThreadPriority.AboveNormal;
442 Assert.AreEqual (ThreadPriority.AboveNormal, TestThread.Priority, "#45D Incorrect Priority:");
443 TestThread.Priority = ThreadPriority.Highest;
444 Assert.AreEqual (ThreadPriority.Highest, TestThread.Priority, "#45E Incorrect Priority:");
446 finally {
447 TestThread.Abort();
451 [Test]
452 public void TestIsBackground1 ()
454 if (is_win32 && is_mono)
455 Assert.Fail ("This test fails on mono on Win32. Our runtime should be fixed.");
457 C2Test test1 = new C2Test();
458 Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
459 try {
460 TestThread.Start();
461 TestUtil.WaitForAlive (TestThread, "wait9");
462 bool state = TestThread.IsBackground;
463 Assert.IsFalse (state, "#51 IsBackground not set at the default state: ");
464 } finally {
465 TestThread.Abort();
469 [Test]
470 [Category ("NotDotNet")] // on MS, ThreadState is immediately Stopped after Abort
471 public void TestIsBackground2 ()
473 C2Test test1 = new C2Test();
474 Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
475 TestThread.IsBackground = true;
476 try {
477 TestThread.Start();
478 } finally {
479 TestThread.Abort();
481 Assert.IsTrue (TestThread.IsBackground, "#52 Is Background Changed to Start ");
484 [Test]
485 public void TestName()
487 if (is_win32 && is_mono)
488 Assert.Fail ("This test fails on mono on Win32. Our runtime should be fixed.");
490 C2Test test1 = new C2Test();
491 Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
492 try {
493 TestThread.Start();
494 TestUtil.WaitForAlive (TestThread, "wait10");
495 string name = TestThread.Name;
496 Assert.IsNull (name, "#61 Name set when mustn't be set: ");
497 string newname = "Testing....";
498 TestThread.Name = newname;
499 Assert.AreEqual (newname, TestThread.Name, "#62 Name not set when must be set: ");
500 } finally {
501 TestThread.Abort();
505 [Test]
506 public void Name ()
508 Thread t = new Thread (new ThreadStart (Name));
509 Assert.IsNull (t.Name, "Name-1");
510 t.Name = null;
511 Assert.IsNull (t.Name, "Name-2");
514 [Test]
515 [ExpectedException (typeof (InvalidOperationException))]
516 public void ReName ()
518 Thread t = new Thread (new ThreadStart (ReName));
519 t.Name = "a";
520 t.Name = "b";
523 [Test]
524 public void TestNestedThreads1()
526 C3Test test1 = new C3Test();
527 Thread TestThread = new Thread(new ThreadStart(test1.TestMethod1));
528 try {
529 TestThread.Start();
530 TestUtil.WaitForAlive (TestThread, "wait11");
531 } finally {
532 TestThread.Abort();
536 [Test]
537 public void TestNestedThreads2()
539 C4Test test1 = new C4Test();
540 Thread TestThread = new Thread(new ThreadStart(test1.TestMethod1));
541 try {
542 TestThread.Start();
543 } finally {
544 TestThread.Abort();
548 [Test]
549 public void TestJoin1()
551 C1Test test1 = new C1Test();
552 C1Test test2 = new C1Test();
553 Thread thread1 = new Thread(new ThreadStart(test1.TestMethod));
554 Thread thread2 = new Thread(new ThreadStart(test1.TestMethod2));
555 try {
556 thread1.Start();
557 thread2.Start();
558 thread2.Join();
559 } finally {
560 thread1.Abort();
561 thread2.Abort();
565 [Test]
566 [ExpectedException (typeof (ArgumentOutOfRangeException))]
567 public void Join_Int32_Negative ()
569 // -1 is Timeout.Infinite
570 Thread.CurrentThread.Join (-2);
573 [Test]
574 [ExpectedException (typeof (ArgumentOutOfRangeException))]
575 public void Join_TimeSpan_Negative ()
577 Thread.CurrentThread.Join (Negative);
580 [Test]
581 [ExpectedException (typeof (ArgumentOutOfRangeException))]
582 public void Join_TimeSpan_TooLarge ()
584 Thread.CurrentThread.Join (TooLarge);
587 [Test]
588 public void Join_TimeSpan_SmallNegative ()
590 Thread.CurrentThread.Join (SmallNegative);
593 [Test]
594 [ExpectedException (typeof (ArgumentOutOfRangeException))]
595 public void Sleep_Int32_Negative ()
597 // -1 is Timeout.Infinite
598 Thread.Sleep (-2);
601 [Test]
602 public void Sleep_TimeSpan_SmallNegative ()
604 Thread.Sleep (SmallNegative);
607 [Test]
608 [ExpectedException (typeof (ArgumentOutOfRangeException))]
609 public void Sleep_TimeSpan_Negative ()
611 Thread.Sleep (Negative);
614 [Test]
615 [ExpectedException (typeof (ArgumentOutOfRangeException))]
616 public void Sleep_TimeSpan_TooLarge ()
618 Thread.Sleep (TooLarge);
621 [Test]
622 public void SpinWait ()
624 // no exception for negative numbers
625 Thread.SpinWait (Int32.MinValue);
626 Thread.SpinWait (0);
629 [Test]
630 public void TestThreadState ()
632 if (is_win32 && is_mono)
633 Assert.Fail ("This test fails on mono on Win32. Our runtime should be fixed.");
635 //TODO: Test The rest of the possible transitions
636 C2Test test1 = new C2Test();
637 Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
638 Assert.AreEqual (ThreadState.Unstarted, TestThread.ThreadState, "#101 Wrong Thread State");
639 try {
640 TestThread.Start();
641 //while(!TestThread.IsAlive); //In the MS Documentation this is not necessary
642 //but in the MS SDK it is
643 Assert.IsTrue (TestThread.ThreadState == ThreadState.Running || (TestThread.ThreadState & ThreadState.Unstarted) != 0,
644 "#102 Wrong Thread State: " + TestThread.ThreadState.ToString ());
645 } finally {
646 TestThread.Abort();
649 TestUtil.WaitForNotAlive (TestThread, "wait12");
650 // Docs say state will be Stopped, but Aborted happens sometimes (?)
651 Assert.IsTrue ((ThreadState.Stopped & TestThread.ThreadState) != 0 || (ThreadState.Aborted & TestThread.ThreadState) != 0,
652 "#103 Wrong Thread State: " + TestThread.ThreadState.ToString ());
655 [Test]
656 [Ignore ("see comment below.")]
657 public void CurrentPrincipal_PrincipalPolicy_NoPrincipal ()
659 // note: switching from PrincipalPolicy won't work inside the same thread
660 // because as soon as a Principal object is created the Policy doesn't matter anymore
661 Thread t = new Thread (new ThreadStart (ThreadedPrincipalTest.NoPrincipal));
662 try {
663 t.Start ();
664 t.Join ();
665 } catch {
666 t.Abort ();
670 #if !TARGET_JVM // AppDomain.SetPrincipalPolicy not supported for TARGET_JVM
671 [Test]
672 [Ignore ("see comment below.")]
673 public void CurrentPrincipal_PrincipalPolicy_UnauthenticatedPrincipal ()
675 // note: switching from PrincipalPolicy won't work inside the same thread
676 // because as soon as a Principal object is created the Policy doesn't matter anymore
677 Thread t = new Thread (new ThreadStart (ThreadedPrincipalTest.UnauthenticatedPrincipal));
678 try {
679 t.Start ();
680 t.Join ();
681 } catch {
682 t.Abort ();
686 [Test]
687 public void CurrentPrincipal_PrincipalPolicy_WindowsPrincipal ()
689 // note: switching from PrincipalPolicy won't work inside the same thread
690 // because as soon as a Principal object is created the Policy doesn't matter anymore
691 Thread t = new Thread (new ThreadStart (ThreadedPrincipalTest.WindowsPrincipal));
692 try {
693 t.Start ();
694 t.Join ();
695 } catch {
696 t.Abort ();
699 #endif // TARGET_JVM
701 [Test]
702 public void IPrincipal_CopyOnNewThread ()
704 Thread.CurrentPrincipal = new GenericPrincipal (new GenericIdentity ("bad"), null);
705 Thread t = new Thread (new ThreadStart (ThreadedPrincipalTest.CopyOnNewThread));
706 try {
707 Thread.CurrentPrincipal = new GenericPrincipal (new GenericIdentity ("good"), null);
708 t.Start ();
709 t.Join ();
710 } catch {
711 t.Abort ();
715 int counter = 0;
717 [Test]
718 public void TestSuspend ()
720 Thread t = new Thread (new ThreadStart (DoCount));
721 t.IsBackground = true;
722 t.Start ();
724 CheckIsRunning ("t1", t);
726 t.Suspend ();
727 WaitSuspended ("t2", t);
729 CheckIsNotRunning ("t3", t);
731 t.Resume ();
732 WaitResumed ("t4", t);
734 CheckIsRunning ("t5", t);
736 t.Abort ();
737 TestUtil.WaitForNotAlive (t, "wait13");
738 CheckIsNotRunning ("t6", t);
741 [Test]
742 [Category("NotDotNet")] // On MS, ThreadStateException is thrown on Abort: "Thread is suspended; attempting to abort"
743 public void TestSuspendAbort ()
745 if (is_win32 && is_mono)
746 Assert.Fail ("This test fails on Win32. The test should be fixed.");
748 Thread t = new Thread (new ThreadStart (DoCount));
749 t.IsBackground = true;
750 t.Start ();
752 CheckIsRunning ("t1", t);
754 t.Suspend ();
755 WaitSuspended ("t2", t);
757 CheckIsNotRunning ("t3", t);
759 t.Abort ();
761 int n=0;
762 while (t.IsAlive && n < 200) {
763 Thread.Sleep (10);
764 n++;
767 Assert.IsTrue (n < 200, "Timeout while waiting for abort");
769 CheckIsNotRunning ("t6", t);
772 [Test]
773 public void Test_Interrupt ()
775 bool interruptedExceptionThrown = false;
776 ThreadPool.QueueUserWorkItem (Test_Interrupt_Worker, Thread.CurrentThread);
778 try {
779 try {
780 Thread.Sleep (3000);
781 } finally {
782 try {
783 Thread.Sleep (0);
784 } catch (ThreadInterruptedException) {
785 Assert.Fail ("ThreadInterruptedException thrown twice");
788 } catch (ThreadInterruptedException) {
789 interruptedExceptionThrown = true;
792 Assert.IsTrue (interruptedExceptionThrown, "ThreadInterruptedException expected.");
795 [Test]
796 [ExpectedException (typeof (ArgumentNullException))]
797 public void TestQueueUserWorkItemNullCallback ()
799 ThreadPool.QueueUserWorkItem (null, null);
802 private void Test_Interrupt_Worker (object o)
804 Thread t = o as Thread;
805 Thread.Sleep (100);
806 t.Interrupt ();
809 [Test]
810 public void Test_InterruptCurrentThread ()
812 bool interruptedExceptionThrown = false;
814 try {
815 try {
816 Thread.CurrentThread.Interrupt ();
817 } finally {
818 try {
819 Thread.Sleep (0);
820 } catch (ThreadInterruptedException) {
821 Assert.Fail ("ThreadInterruptedException should not be thrown.");
824 } catch (ThreadInterruptedException) {
825 interruptedExceptionThrown = true;
828 Assert.IsFalse (interruptedExceptionThrown, "ThreadInterruptedException should not be thrown.");
831 void CheckIsRunning (string s, Thread t)
833 int c = counter;
834 Thread.Sleep (100);
835 Assert.IsTrue (counter > c, s);
838 void CheckIsNotRunning (string s, Thread t)
840 int c = counter;
841 Thread.Sleep (100);
842 Assert.AreEqual (counter, c, s);
845 void WaitSuspended (string s, Thread t)
847 int n=0;
848 ThreadState state = t.ThreadState;
849 while ((state & ThreadState.Suspended) == 0) {
850 Assert.IsTrue ((state & ThreadState.SuspendRequested) != 0, s + ": expected SuspendRequested state");
851 Thread.Sleep (10);
852 n++;
853 Assert.IsTrue (n < 100, s + ": failed to suspend");
854 state = t.ThreadState;
856 Assert.IsTrue ((state & ThreadState.SuspendRequested) == 0, s + ": SuspendRequested state not expected");
859 void WaitResumed (string s, Thread t)
861 int n=0;
862 while ((t.ThreadState & ThreadState.Suspended) != 0) {
863 Thread.Sleep (10);
864 n++;
865 Assert.IsTrue (n < 100, s + ": failed to resume");
869 public void DoCount ()
871 while (true) {
872 counter++;
873 Thread.Sleep (1);
878 [TestFixture]
879 public class ThreadStateTest {
880 void Start ()
884 [Test] // bug #81720
885 public void IsBackGround ()
887 Thread t1 = new Thread (new ThreadStart (Start));
888 Assert.AreEqual (ThreadState.Unstarted, t1.ThreadState, "#A1");
889 Assert.IsFalse (t1.IsBackground, "#A2");
890 t1.Start ();
891 t1.Join ();
892 Assert.AreEqual (ThreadState.Stopped, t1.ThreadState, "#A3");
894 try {
895 bool isBackGround = t1.IsBackground;
896 Assert.Fail ("#A4: " + isBackGround.ToString ());
897 } catch (ThreadStateException ex) {
898 Assert.AreEqual (typeof (ThreadStateException), ex.GetType (), "#A5");
899 Assert.IsNull (ex.InnerException, "#A6");
900 Assert.IsNotNull (ex.Message, "#A7");
903 Thread t2 = new Thread (new ThreadStart (Start));
904 Assert.AreEqual (ThreadState.Unstarted, t2.ThreadState, "#B1");
905 t2.IsBackground = true;
906 Assert.AreEqual (ThreadState.Unstarted | ThreadState.Background, t2.ThreadState, "#B2");
907 Assert.IsTrue (t2.IsBackground, "#B3");
908 t2.Start ();
909 t2.Join ();
910 Assert.AreEqual (ThreadState.Stopped, t2.ThreadState, "#B4");
912 try {
913 bool isBackGround = t2.IsBackground;
914 Assert.Fail ("#B5: " + isBackGround.ToString ());
915 } catch (ThreadStateException ex) {
916 Assert.AreEqual (typeof (ThreadStateException), ex.GetType (), "#B6");
917 Assert.IsNull (ex.InnerException, "#B7");
918 Assert.IsNotNull (ex.Message, "#B8");
923 [TestFixture]
924 public class ThreadApartmentTest
926 void Start ()
930 [Test] // bug #81658
931 public void ApartmentState_StoppedThread ()
933 Thread t1 = new Thread (new ThreadStart (Start));
934 t1.Start ();
935 t1.Join ();
936 try {
937 ApartmentState state = t1.ApartmentState;
938 Assert.Fail ("#A1: " + state.ToString ());
939 } catch (ThreadStateException ex) {
940 Assert.AreEqual (typeof (ThreadStateException), ex.GetType (), "#A2");
941 Assert.IsNull (ex.InnerException, "#A3");
942 Assert.IsNotNull (ex.Message, "#A4");
945 Thread t2 = new Thread (new ThreadStart (Start));
946 t2.IsBackground = true;
947 t2.Start ();
948 t2.Join ();
949 try {
950 ApartmentState state = t2.ApartmentState;
951 Assert.Fail ("#B1: " + state.ToString ());
952 } catch (ThreadStateException ex) {
953 Assert.AreEqual (typeof (ThreadStateException), ex.GetType (), "#B2");
954 Assert.IsNull (ex.InnerException, "#B3");
955 Assert.IsNotNull (ex.Message, "#B4");
959 [Test]
960 public void ApartmentState_BackGround ()
962 Thread t1 = new Thread (new ThreadStart (Start));
963 t1.IsBackground = true;
964 Assert.AreEqual (ApartmentState.Unknown, t1.ApartmentState, "#1");
965 t1.ApartmentState = ApartmentState.STA;
966 Assert.AreEqual (ApartmentState.STA, t1.ApartmentState, "#2");
969 [Test]
970 public void TestApartmentState ()
972 Thread t1 = new Thread (new ThreadStart (Start));
973 Thread t2 = new Thread (new ThreadStart (Start));
974 Thread t3 = new Thread (new ThreadStart (Start));
976 Assert.AreEqual (ApartmentState.Unknown, t1.ApartmentState, "Thread1 Default");
977 Assert.AreEqual (ApartmentState.Unknown, t2.ApartmentState, "Thread2 Default");
978 Assert.AreEqual (ApartmentState.Unknown, t3.ApartmentState, "Thread3 Default");
980 t1.ApartmentState = ApartmentState.STA;
981 Assert.AreEqual (ApartmentState.STA, t1.ApartmentState, "Thread1 Set Once");
982 t1.ApartmentState = ApartmentState.MTA;
983 Assert.AreEqual (ApartmentState.STA, t1.ApartmentState, "Thread1 Set Twice");
985 t2.ApartmentState = ApartmentState.MTA;
986 Assert.AreEqual (ApartmentState.MTA, t2.ApartmentState, "Thread2 Set Once");
987 t2.ApartmentState = ApartmentState.STA;
988 Assert.AreEqual (ApartmentState.MTA, t2.ApartmentState, "Thread2 Set Twice");
990 bool exception_occured = false;
991 try {
992 t3.ApartmentState = ApartmentState.Unknown;
994 catch (Exception) {
995 exception_occured = true;
997 Assert.AreEqual (ApartmentState.Unknown, t3.ApartmentState, "Thread3 Set Invalid");
998 #if NET_2_0
999 Assert.IsFalse (exception_occured, "Thread3 Set Invalid Exception Occured");
1000 #else
1001 Assert.IsTrue (exception_occured, "Thread3 Set Invalid Exception Occured");
1002 #endif
1004 t1.Start ();
1005 exception_occured = false;
1006 try {
1007 t1.ApartmentState = ApartmentState.STA;
1009 catch (Exception) {
1010 exception_occured = true;
1012 Assert.IsTrue (exception_occured, "Thread1 Started Invalid Exception Occured");
1016 public class TestUtil
1018 public static void WaitForNotAlive (Thread t, string s)
1020 WhileAlive (t, true, s);
1023 public static void WaitForAlive (Thread t, string s)
1025 WhileAlive (t, false, s);
1028 public static bool WaitForAliveOrStop (Thread t, string s)
1030 return WhileAliveOrStop (t, false, s);
1033 public static void WhileAlive (Thread t, bool alive, string s)
1035 DateTime ti = DateTime.Now;
1036 while (t.IsAlive == alive) {
1037 if ((DateTime.Now - ti).TotalSeconds > 10) {
1038 if (alive) Assert.Fail ("Timeout while waiting for not alive state. " + s);
1039 else Assert.Fail ("Timeout while waiting for alive state. " + s);
1044 public static bool WhileAliveOrStop (Thread t, bool alive, string s)
1046 DateTime ti = DateTime.Now;
1047 while (t.IsAlive == alive) {
1048 if (t.ThreadState == ThreadState.Stopped)
1049 return false;
1051 if ((DateTime.Now - ti).TotalSeconds > 10) {
1052 if (alive) Assert.Fail ("Timeout while waiting for not alive state. " + s);
1053 else Assert.Fail ("Timeout while waiting for alive state. " + s);
1057 return true;