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)
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);
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
81 public static void CopyOnNewThread ()
83 Assert.IsNotNull (Thread.CurrentPrincipal, "#1");
84 Assert.AreEqual ("good", Thread.CurrentPrincipal.Identity.Name, "#2");
89 [Category("MobileNotWorking")] // Abort #10240
90 public class ThreadTest
92 TimeSpan Infinite = new TimeSpan (-10000); // -10000 ticks == -1 ms
93 TimeSpan SmallNegative = new TimeSpan (-2); // between 0 and -1.0 (infinite) ms
94 TimeSpan Negative = new TimeSpan (-20000); // really negative
95 TimeSpan MaxValue = TimeSpan.FromMilliseconds ((long) Int32.MaxValue);
96 TimeSpan TooLarge = TimeSpan.FromMilliseconds ((long) Int32.MaxValue + 1);
103 switch (Environment.OSVersion.Platform) {
104 case PlatformID.Win32NT:
105 case PlatformID.Win32S:
106 case PlatformID.Win32Windows:
107 case PlatformID.WinCE:
112 // check a class in mscorlib to determine if we're running on Mono
113 if (Type.GetType ("System.MonoType", false) != null)
117 //Some Classes to test as threads
121 public Thread thread1;
127 thread1 = (Thread)null;
129 endm1 = endm2 = false;
132 public void TestMethod()
140 public void TestMethod2()
142 if (!(thread1==(Thread)null) )
153 public bool run = false;
160 public void TestMethod()
175 public C1Test sub_class;
176 public Thread sub_thread;
180 sub_class = new C1Test();
181 sub_thread = new Thread(new ThreadStart(sub_class.TestMethod));
184 public void TestMethod1()
194 public C1Test class1;
195 public C1Test class2;
196 public Thread thread1;
197 public Thread thread2;
205 class1 = new C1Test();
206 class2 = new C1Test();
207 thread1 = new Thread(new ThreadStart(class1.TestMethod));
208 thread2 = new Thread(new ThreadStart(class2.TestMethod));
211 public void TestMethod1()
214 TestUtil.WaitForAlive (thread1, "wait1");
217 TestUtil.WaitForAlive (thread2, "wait2");
220 TestUtil.WaitForNotAlive (thread1, "wait3");
223 TestUtil.WaitForNotAlive (thread2, "wait4");
227 public void TestMethod2()
235 public void TestCtor1()
237 C1Test test1 = new C1Test();
238 Thread t = new Thread (new ThreadStart (test1.TestMethod));
240 Assert.IsTrue (t.CurrentCulture.IsReadOnly, "CurrentCulture.IsReadOnly");
241 Assert.IsFalse (t.IsAlive, "IsAlive");
242 Assert.IsFalse (t.IsBackground, "IsBackground");
243 Assert.IsNull (t.Name, "Name");
244 Assert.AreEqual (ThreadState.Unstarted, t.ThreadState, "ThreadState");
248 [Category ("NotWorking")] // we're not sharing (read-only) CultureInfo
249 public void CultureInfo_Shared_Across_Threads ()
251 Thread t = new Thread (TestCtor1);
252 Assert.AreSame (t.CurrentCulture, t.CurrentUICulture, "Culture");
254 Assert.AreSame (t.CurrentCulture, CultureInfo.CurrentCulture, "CultureInfo.CurrentCulture");
255 Assert.AreSame (t.CurrentUICulture, CultureInfo.CurrentUICulture, "CultureInfo.CurrentUICulture");
257 Assert.AreSame (t.CurrentCulture, Thread.CurrentThread.CurrentCulture, "Thread.CurrentThread.CurrentCulture");
258 Assert.AreSame (t.CurrentUICulture, Thread.CurrentThread.CurrentUICulture, "Thread.CurrentThread.CurrentUICulture");
261 [Test] // bug #325566
262 public void GetHashCodeTest ()
264 C1Test test1 = new C1Test ();
265 Thread tA = new Thread (new ThreadStart (test1.TestMethod));
266 int hA1 = tA.GetHashCode ();
268 Assert.IsTrue (hA1 > 0, "#A1");
271 int hA2 = tA.GetHashCode ();
272 Assert.AreEqual (hA1, hA2, "#A2");
274 int hA3 = tA.GetHashCode ();
275 Assert.AreEqual (hA1, hA3, "#A3");
277 Assert.AreEqual (hA1, tA.ManagedThreadId, "#A4");
280 test1 = new C1Test ();
281 Thread tB = new Thread (new ThreadStart (test1.TestMethod));
282 int hB1 = tB.GetHashCode ();
284 Assert.IsTrue (hB1 > 0, "#B1");
287 int hB2 = tB.GetHashCode ();
288 Assert.AreEqual (hB1, hB2, "#B2");
290 int hB3 = tB.GetHashCode ();
291 Assert.AreEqual (hB1, hB3, "#B3");
293 Assert.AreEqual (hB1, tB.ManagedThreadId, "#B4");
295 Assert.IsFalse (hA2 == hB2, "#B5");
300 public void ManagedThreadId ()
302 C1Test test1 = new C1Test ();
303 Thread t1 = new Thread (new ThreadStart (test1.TestMethod));
304 int mtA1 = t1.ManagedThreadId;
306 int mtA2 = t1.ManagedThreadId;
308 int mtA3 = t1.ManagedThreadId;
309 Assert.AreEqual (mtA1, mtA2, "#A1");
310 Assert.AreEqual (mtA2, mtA3, "#A2");
312 test1 = new C1Test ();
313 Thread t2 = new Thread (new ThreadStart (test1.TestMethod));
314 int mtB1 = t2.ManagedThreadId;
316 int mtB2 = t2.ManagedThreadId;
318 int mtB3 = t2.ManagedThreadId;
319 Assert.AreEqual (mtB1, mtB2, "#B1");
320 Assert.AreEqual (mtB2, mtB3, "#B2");
321 Assert.IsFalse (mtB1 == mtA1, "#B3");
326 [Category ("NotDotNet")] // it hangs.
327 public void TestStart()
329 if (is_win32 && is_mono)
330 Assert.Fail ("This test fails on Win32. The test should be fixed.");
332 C1Test test1 = new C1Test();
333 Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
336 Assert.AreEqual (10, test1.cnt, "#1");
339 C2Test test1 = new C2Test();
340 Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
346 } catch (ThreadStateException) {
350 C2Test test1 = new C2Test();
351 Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
355 bool started = (TestThread.ThreadState == ThreadState.Running);
356 Assert.AreEqual (started, test1.run, "#15 Thread Is not in the correct state: ");
362 public void TestApartmentState ()
364 if (is_win32 && is_mono)
365 Assert.Fail ("This test fails on mono on win32. Our runtime should be fixed.");
367 C2Test test1 = new C2Test();
368 Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
369 Assert.AreEqual (ApartmentState.Unknown, TestThread.ApartmentState, "#1");
371 TestUtil.WaitForAlive (TestThread, "wait5");
373 Assert.AreEqual (ApartmentState.MTA, TestThread.ApartmentState, "#2");
375 Assert.AreEqual (ApartmentState.Unknown, TestThread.ApartmentState, "#3");
381 public void TestPriority1()
383 if (is_win32 && is_mono)
384 Assert.Fail ("This test fails on mono on Win32. Our runtime should be fixed.");
386 C2Test test1 = new C2Test();
387 Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
389 TestThread.Priority=ThreadPriority.BelowNormal;
390 ThreadPriority after = TestThread.Priority;
392 TestUtil.WaitForAlive (TestThread, "wait7");
393 ThreadPriority before = TestThread.Priority;
394 Assert.AreEqual (before, after, "#41 Unexpected Priority Change: ");
401 [Category ("NotDotNet")] // on MS, Thread is still in AbortRequested state when Start is invoked
402 public void AbortUnstarted ()
404 C2Test test1 = new C2Test();
405 Thread th = new Thread (new ThreadStart (test1.TestMethod));
411 [Category ("NotDotNet")] // on MS, ThreadState is immediately Stopped after Abort
412 [Category ("NotWorking")] // this is a MonoTODO -> no support for Priority
413 public void TestPriority2()
415 C2Test test1 = new C2Test();
416 Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
418 Assert.AreEqual (ThreadPriority.Normal, TestThread.Priority, "#42 Incorrect Priority in New thread: ");
420 TestUtil.WaitForAliveOrStop (TestThread, "wait8");
421 Assert.AreEqual (ThreadPriority.Normal, TestThread.Priority, "#43 Incorrect Priority in Started thread: ");
425 Assert.AreEqual (ThreadPriority.Normal, TestThread.Priority, "#44 Incorrect Priority in Aborted thread: ");
429 [Category ("NotWorking")] // this is a MonoTODO -> no support for Priority
430 public void TestPriority3()
432 C2Test test1 = new C2Test();
433 Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
436 TestThread.Priority = ThreadPriority.Lowest;
437 Assert.AreEqual (ThreadPriority.Lowest, TestThread.Priority, "#45A Incorrect Priority:");
438 TestThread.Priority = ThreadPriority.BelowNormal;
439 Assert.AreEqual (ThreadPriority.BelowNormal, TestThread.Priority, "#45B Incorrect Priority:");
440 TestThread.Priority = ThreadPriority.Normal;
441 Assert.AreEqual (ThreadPriority.Normal, TestThread.Priority, "#45C Incorrect Priority:");
442 TestThread.Priority = ThreadPriority.AboveNormal;
443 Assert.AreEqual (ThreadPriority.AboveNormal, TestThread.Priority, "#45D Incorrect Priority:");
444 TestThread.Priority = ThreadPriority.Highest;
445 Assert.AreEqual (ThreadPriority.Highest, TestThread.Priority, "#45E Incorrect Priority:");
453 public void TestUndivisibleByPageSizeMaxStackSize ()
455 const int undivisible_stacksize = 1048573;
457 var thread = new Thread (new ThreadStart (delegate {}), undivisible_stacksize);
463 public void TestIsBackground1 ()
465 if (is_win32 && is_mono)
466 Assert.Fail ("This test fails on mono on Win32. Our runtime should be fixed.");
468 C2Test test1 = new C2Test();
469 Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
472 TestUtil.WaitForAlive (TestThread, "wait9");
473 bool state = TestThread.IsBackground;
474 Assert.IsFalse (state, "#51 IsBackground not set at the default state: ");
481 public void TestIsBackground2 ()
483 C2Test test1 = new C2Test();
484 Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
485 TestThread.IsBackground = true;
492 if (TestThread.IsAlive) {
494 Assert.IsTrue (TestThread.IsBackground, "#52 Is Background Changed to Start ");
495 } catch (ThreadStateException) {
496 // Ignore if thread died meantime
502 public void TestName()
504 if (is_win32 && is_mono)
505 Assert.Fail ("This test fails on mono on Win32. Our runtime should be fixed.");
507 C2Test test1 = new C2Test();
508 Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
511 TestUtil.WaitForAlive (TestThread, "wait10");
512 string name = TestThread.Name;
513 Assert.IsNull (name, "#61 Name set when mustn't be set: ");
514 string newname = "Testing....";
515 TestThread.Name = newname;
516 Assert.AreEqual (newname, TestThread.Name, "#62 Name not set when must be set: ");
525 Thread t = new Thread (new ThreadStart (Name));
526 Assert.IsNull (t.Name, "Name-1");
528 Assert.IsNull (t.Name, "Name-2");
532 [ExpectedException (typeof (InvalidOperationException))]
533 public void ReName ()
535 Thread t = new Thread (new ThreadStart (ReName));
541 public void TestNestedThreads1()
543 C3Test test1 = new C3Test();
544 Thread TestThread = new Thread(new ThreadStart(test1.TestMethod1));
547 TestUtil.WaitForAlive (TestThread, "wait11");
554 public void TestNestedThreads2()
556 C4Test test1 = new C4Test();
557 Thread TestThread = new Thread(new ThreadStart(test1.TestMethod1));
566 public void TestJoin1()
568 C1Test test1 = new C1Test();
569 C1Test test2 = new C1Test();
570 Thread thread1 = new Thread(new ThreadStart(test1.TestMethod));
571 Thread thread2 = new Thread(new ThreadStart(test1.TestMethod2));
583 [ExpectedException (typeof (ArgumentOutOfRangeException))]
584 public void Join_Int32_Negative ()
586 // -1 is Timeout.Infinite
587 Thread.CurrentThread.Join (-2);
591 [ExpectedException (typeof (ArgumentOutOfRangeException))]
592 public void Join_TimeSpan_Negative ()
594 Thread.CurrentThread.Join (Negative);
598 [ExpectedException (typeof (ArgumentOutOfRangeException))]
599 public void Join_TimeSpan_TooLarge ()
601 Thread.CurrentThread.Join (TooLarge);
605 public void Join_TimeSpan_SmallNegative ()
607 Thread.CurrentThread.Join (SmallNegative);
611 [ExpectedException (typeof (ArgumentOutOfRangeException))]
612 public void Sleep_Int32_Negative ()
614 // -1 is Timeout.Infinite
619 public void Sleep_TimeSpan_SmallNegative ()
621 Thread.Sleep (SmallNegative);
625 [ExpectedException (typeof (ArgumentOutOfRangeException))]
626 public void Sleep_TimeSpan_Negative ()
628 Thread.Sleep (Negative);
632 [ExpectedException (typeof (ArgumentOutOfRangeException))]
633 public void Sleep_TimeSpan_TooLarge ()
635 Thread.Sleep (TooLarge);
639 public void SpinWait ()
641 // no exception for negative numbers
642 Thread.SpinWait (Int32.MinValue);
647 public void TestThreadState ()
649 if (is_win32 && is_mono)
650 Assert.Fail ("This test fails on mono on Win32. Our runtime should be fixed.");
652 //TODO: Test The rest of the possible transitions
653 C2Test test1 = new C2Test();
654 Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
655 Assert.AreEqual (ThreadState.Unstarted, TestThread.ThreadState, "#101 Wrong Thread State");
658 //while(!TestThread.IsAlive); //In the MS Documentation this is not necessary
659 //but in the MS SDK it is
660 Assert.IsTrue (TestThread.ThreadState == ThreadState.Running || (TestThread.ThreadState & ThreadState.Unstarted) != 0,
661 "#102 Wrong Thread State: " + TestThread.ThreadState.ToString ());
666 TestUtil.WaitForNotAlive (TestThread, "wait12");
667 // Docs say state will be Stopped, but Aborted happens sometimes (?)
668 Assert.IsTrue ((ThreadState.Stopped & TestThread.ThreadState) != 0 || (ThreadState.Aborted & TestThread.ThreadState) != 0,
669 "#103 Wrong Thread State: " + TestThread.ThreadState.ToString ());
673 [Ignore ("see comment below.")]
674 public void CurrentPrincipal_PrincipalPolicy_NoPrincipal ()
676 // note: switching from PrincipalPolicy won't work inside the same thread
677 // because as soon as a Principal object is created the Policy doesn't matter anymore
678 Thread t = new Thread (new ThreadStart (ThreadedPrincipalTest.NoPrincipal));
687 #if !TARGET_JVM // AppDomain.SetPrincipalPolicy not supported for TARGET_JVM
689 [Ignore ("see comment below.")]
690 public void CurrentPrincipal_PrincipalPolicy_UnauthenticatedPrincipal ()
692 // note: switching from PrincipalPolicy won't work inside the same thread
693 // because as soon as a Principal object is created the Policy doesn't matter anymore
694 Thread t = new Thread (new ThreadStart (ThreadedPrincipalTest.UnauthenticatedPrincipal));
704 public void CurrentPrincipal_PrincipalPolicy_WindowsPrincipal ()
706 // note: switching from PrincipalPolicy won't work inside the same thread
707 // because as soon as a Principal object is created the Policy doesn't matter anymore
708 Thread t = new Thread (new ThreadStart (ThreadedPrincipalTest.WindowsPrincipal));
719 public void IPrincipal_CopyOnNewThread ()
721 Thread.CurrentPrincipal = new GenericPrincipal (new GenericIdentity ("bad"), null);
722 Thread t = new Thread (new ThreadStart (ThreadedPrincipalTest.CopyOnNewThread));
724 Thread.CurrentPrincipal = new GenericPrincipal (new GenericIdentity ("good"), null);
735 public void TestSuspend ()
737 Thread t = new Thread (new ThreadStart (DoCount));
738 t.IsBackground = true;
741 CheckIsRunning ("t1", t);
744 WaitSuspended ("t2", t);
746 CheckIsNotRunning ("t3", t);
749 WaitResumed ("t4", t);
751 CheckIsRunning ("t5", t);
754 TestUtil.WaitForNotAlive (t, "wait13");
755 CheckIsNotRunning ("t6", t);
759 [Category("NotDotNet")] // On MS, ThreadStateException is thrown on Abort: "Thread is suspended; attempting to abort"
760 public void TestSuspendAbort ()
762 if (is_win32 && is_mono)
763 Assert.Fail ("This test fails on Win32. The test should be fixed.");
765 Thread t = new Thread (new ThreadStart (DoCount));
766 t.IsBackground = true;
769 CheckIsRunning ("t1", t);
772 WaitSuspended ("t2", t);
774 CheckIsNotRunning ("t3", t);
779 while (t.IsAlive && n < 200) {
784 Assert.IsTrue (n < 200, "Timeout while waiting for abort");
786 CheckIsNotRunning ("t6", t);
790 public void Test_Interrupt ()
792 bool interruptedExceptionThrown = false;
793 ThreadPool.QueueUserWorkItem (Test_Interrupt_Worker, Thread.CurrentThread);
801 } catch (ThreadInterruptedException) {
802 Assert.Fail ("ThreadInterruptedException thrown twice");
805 } catch (ThreadInterruptedException) {
806 interruptedExceptionThrown = true;
809 Assert.IsTrue (interruptedExceptionThrown, "ThreadInterruptedException expected.");
813 [ExpectedException (typeof (ArgumentNullException))]
814 public void TestQueueUserWorkItemNullCallback ()
816 ThreadPool.QueueUserWorkItem (null, null);
819 private void Test_Interrupt_Worker (object o)
821 Thread t = o as Thread;
827 [Category ("NotDotNet")] // it crashes nunit.
828 public void Test_InterruptCurrentThread ()
830 bool interruptedExceptionThrown = false;
832 Thread.CurrentThread.Interrupt ();
836 } catch (ThreadInterruptedException) {
841 public void GetNamedDataSlotTest ()
843 Assert.IsNotNull (Thread.GetNamedDataSlot ("te#st"), "#1");
844 Assert.AreSame (Thread.GetNamedDataSlot ("te#st"), Thread.GetNamedDataSlot ("te#st"), "#2");
847 void CheckIsRunning (string s, Thread t)
851 Assert.IsTrue (counter > c, s);
854 void CheckIsNotRunning (string s, Thread t)
858 Assert.AreEqual (counter, c, s);
861 void WaitSuspended (string s, Thread t)
864 ThreadState state = t.ThreadState;
865 while ((state & ThreadState.Suspended) == 0) {
866 Assert.IsTrue ((state & ThreadState.SuspendRequested) != 0, s + ": expected SuspendRequested state");
869 Assert.IsTrue (n < 100, s + ": failed to suspend");
870 state = t.ThreadState;
872 Assert.IsTrue ((state & ThreadState.SuspendRequested) == 0, s + ": SuspendRequested state not expected");
875 void WaitResumed (string s, Thread t)
878 while ((t.ThreadState & ThreadState.Suspended) != 0) {
881 Assert.IsTrue (n < 100, s + ": failed to resume");
885 public void DoCount ()
895 public class ThreadStateTest {
901 public void IsBackGround ()
903 Thread t1 = new Thread (new ThreadStart (Start));
904 Assert.AreEqual (ThreadState.Unstarted, t1.ThreadState, "#A1");
905 Assert.IsFalse (t1.IsBackground, "#A2");
908 Assert.AreEqual (ThreadState.Stopped, t1.ThreadState, "#A3");
911 bool isBackGround = t1.IsBackground;
912 Assert.Fail ("#A4: " + isBackGround.ToString ());
913 } catch (ThreadStateException ex) {
914 Assert.AreEqual (typeof (ThreadStateException), ex.GetType (), "#A5");
915 Assert.IsNull (ex.InnerException, "#A6");
916 Assert.IsNotNull (ex.Message, "#A7");
919 Thread t2 = new Thread (new ThreadStart (Start));
920 Assert.AreEqual (ThreadState.Unstarted, t2.ThreadState, "#B1");
921 t2.IsBackground = true;
922 Assert.AreEqual (ThreadState.Unstarted | ThreadState.Background, t2.ThreadState, "#B2");
923 Assert.IsTrue (t2.IsBackground, "#B3");
926 Assert.AreEqual (ThreadState.Stopped, t2.ThreadState, "#B4");
929 bool isBackGround = t2.IsBackground;
930 Assert.Fail ("#B5: " + isBackGround.ToString ());
931 } catch (ThreadStateException ex) {
932 Assert.AreEqual (typeof (ThreadStateException), ex.GetType (), "#B6");
933 Assert.IsNull (ex.InnerException, "#B7");
934 Assert.IsNotNull (ex.Message, "#B8");
941 public class ThreadTest_ManagedThreadId
945 MBRO mbro = new MBRO ();
947 class MBRO : MarshalByRefObject {
954 public string message;
958 public void ManagedThreadId_AppDomains ()
960 AppDomain currentDomain = AppDomain.CurrentDomain;
961 ad1 = AppDomain.CreateDomain ("AppDomain 1", currentDomain.Evidence, currentDomain.SetupInformation);
962 ad2 = AppDomain.CreateDomain ("AppDomain 2", currentDomain.Evidence, currentDomain.SetupInformation);
964 Thread a = new Thread (ThreadA);
965 Thread b = new Thread (ThreadB);
966 // execute on AppDomain 1 thread A
967 // execute on AppDomain 2 thread B
968 // execute on AppDomain 1 thread B - must have same ManagedThreadId as Ad 2 on thread B
974 AppDomain.Unload (ad1);
975 AppDomain.Unload (ad2);
977 if (mbro.message != null)
978 Assert.Fail (mbro.message);
980 // Console.WriteLine ("Done id_a1: {0} id_b1: {1} id_b2: {2} ad_a1: {3} ad_b1: {4} ad_b2: {5}", mbro.id_a1, mbro.id_b1, mbro.id_b2, mbro.ad_a1, mbro.ad_b1, mbro.ad_b2);
982 Assert.AreEqual ("AppDomain 1", mbro.ad_a1, "Name #1");
983 Assert.AreEqual ("AppDomain 1", mbro.ad_b1, "Name #2");
984 Assert.AreEqual ("AppDomain 2", mbro.ad_b2, "Name #3");
986 Assert.AreNotEqual (mbro.id_a1, mbro.id_b1, "Id #1");
987 Assert.AreNotEqual (mbro.id_a1, mbro.id_b2, "Id #2");
988 Assert.AreEqual (mbro.id_b1, mbro.id_b2, "Id #3");
990 Assert.AreNotEqual (mbro.id_a1, Thread.CurrentThread.ManagedThreadId, "Id #4");
991 Assert.AreNotEqual (mbro.id_b1, Thread.CurrentThread.ManagedThreadId, "Id #5");
992 Assert.AreNotEqual (mbro.id_b2, Thread.CurrentThread.ManagedThreadId, "Id #6");
993 Assert.AreNotEqual (mbro.ad_a1, AppDomain.CurrentDomain.FriendlyName, "Name #4");
994 Assert.AreNotEqual (mbro.ad_b1, AppDomain.CurrentDomain.FriendlyName, "Name #5");
995 Assert.AreNotEqual (mbro.ad_b2, AppDomain.CurrentDomain.FriendlyName, "Name #6");
1000 mbro.id_a1 = Thread.CurrentThread.ManagedThreadId;
1001 mbro.ad_a1 = AppDomain.CurrentDomain.FriendlyName;
1006 mbro.id_b2 = Thread.CurrentThread.ManagedThreadId;
1007 mbro.ad_b2 = AppDomain.CurrentDomain.FriendlyName;
1012 mbro.id_b1 = Thread.CurrentThread.ManagedThreadId;
1013 mbro.ad_b1 = AppDomain.CurrentDomain.FriendlyName;
1016 void ThreadA (object obj)
1018 // Console.WriteLine ("ThreadA");
1020 ad1.DoCallBack (A1);
1021 } catch (Exception ex) {
1022 mbro.message = string.Format ("ThreadA exception: {0}", ex);
1024 // Console.WriteLine ("ThreadA Done");
1027 void ThreadB (object obj)
1029 // Console.WriteLine ("ThreadB");
1031 ad2.DoCallBack (B2);
1032 ad1.DoCallBack (B1);
1033 } catch (Exception ex) {
1034 mbro.message = string.Format ("ThreadB exception: {0}", ex);
1036 // Console.WriteLine ("ThreadB Done");
1041 public class ThreadApartmentTest
1047 [Test] // bug #81658
1048 public void ApartmentState_StoppedThread ()
1050 Thread t1 = new Thread (new ThreadStart (Start));
1054 ApartmentState state = t1.ApartmentState;
1055 Assert.Fail ("#A1: " + state.ToString ());
1056 } catch (ThreadStateException ex) {
1057 Assert.AreEqual (typeof (ThreadStateException), ex.GetType (), "#A2");
1058 Assert.IsNull (ex.InnerException, "#A3");
1059 Assert.IsNotNull (ex.Message, "#A4");
1062 Thread t2 = new Thread (new ThreadStart (Start));
1063 t2.IsBackground = true;
1067 ApartmentState state = t2.ApartmentState;
1068 Assert.Fail ("#B1: " + state.ToString ());
1069 } catch (ThreadStateException ex) {
1070 Assert.AreEqual (typeof (ThreadStateException), ex.GetType (), "#B2");
1071 Assert.IsNull (ex.InnerException, "#B3");
1072 Assert.IsNotNull (ex.Message, "#B4");
1077 public void ApartmentState_BackGround ()
1079 Thread t1 = new Thread (new ThreadStart (Start));
1080 t1.IsBackground = true;
1081 Assert.AreEqual (ApartmentState.Unknown, t1.ApartmentState, "#1");
1082 t1.ApartmentState = ApartmentState.STA;
1083 Assert.AreEqual (ApartmentState.STA, t1.ApartmentState, "#2");
1087 public void TestApartmentState ()
1089 Thread t1 = new Thread (new ThreadStart (Start));
1090 Thread t2 = new Thread (new ThreadStart (Start));
1091 Thread t3 = new Thread (new ThreadStart (Start));
1093 Assert.AreEqual (ApartmentState.Unknown, t1.ApartmentState, "Thread1 Default");
1094 Assert.AreEqual (ApartmentState.Unknown, t2.ApartmentState, "Thread2 Default");
1095 Assert.AreEqual (ApartmentState.Unknown, t3.ApartmentState, "Thread3 Default");
1097 t1.ApartmentState = ApartmentState.STA;
1098 Assert.AreEqual (ApartmentState.STA, t1.ApartmentState, "Thread1 Set Once");
1099 t1.ApartmentState = ApartmentState.MTA;
1100 Assert.AreEqual (ApartmentState.STA, t1.ApartmentState, "Thread1 Set Twice");
1102 t2.ApartmentState = ApartmentState.MTA;
1103 Assert.AreEqual (ApartmentState.MTA, t2.ApartmentState, "Thread2 Set Once");
1104 t2.ApartmentState = ApartmentState.STA;
1105 Assert.AreEqual (ApartmentState.MTA, t2.ApartmentState, "Thread2 Set Twice");
1107 bool exception_occured = false;
1109 t3.ApartmentState = ApartmentState.Unknown;
1112 exception_occured = true;
1114 Assert.AreEqual (ApartmentState.Unknown, t3.ApartmentState, "Thread3 Set Invalid");
1116 Assert.IsFalse (exception_occured, "Thread3 Set Invalid Exception Occured");
1118 Assert.IsTrue (exception_occured, "Thread3 Set Invalid Exception Occured");
1122 exception_occured = false;
1124 t1.ApartmentState = ApartmentState.STA;
1127 exception_occured = true;
1129 Assert.IsTrue (exception_occured, "Thread1 Started Invalid Exception Occured");
1133 public void TestSetApartmentStateSameState ()
1135 Thread t1 = new Thread (new ThreadStart (Start));
1136 t1.SetApartmentState (ApartmentState.STA);
1137 Assert.AreEqual (ApartmentState.STA, t1.ApartmentState, "Thread1 Set Once");
1139 t1.SetApartmentState (ApartmentState.STA);
1140 Assert.AreEqual (ApartmentState.STA, t1.ApartmentState, "Thread1 Set twice");
1144 [ExpectedException(typeof(InvalidOperationException))]
1145 public void TestSetApartmentStateDiffState ()
1147 Thread t1 = new Thread (new ThreadStart (Start));
1148 t1.SetApartmentState (ApartmentState.STA);
1149 Assert.AreEqual (ApartmentState.STA, t1.ApartmentState, "Thread1 Set Once");
1151 t1.SetApartmentState (ApartmentState.MTA);
1155 public void TestTrySetApartmentState ()
1157 Thread t1 = new Thread (new ThreadStart (Start));
1158 t1.SetApartmentState (ApartmentState.STA);
1159 Assert.AreEqual (ApartmentState.STA, t1.ApartmentState, "#1");
1161 bool result = t1.TrySetApartmentState (ApartmentState.MTA);
1162 Assert.IsFalse (result, "#2");
1164 result = t1.TrySetApartmentState (ApartmentState.STA);
1165 Assert.IsTrue (result, "#3");
1169 public void TestTrySetApartmentStateRunning ()
1171 Thread t1 = new Thread (new ThreadStart (Start));
1172 t1.SetApartmentState (ApartmentState.STA);
1173 Assert.AreEqual (ApartmentState.STA, t1.ApartmentState, "#1");
1178 t1.TrySetApartmentState (ApartmentState.STA);
1180 } catch (ThreadStateException) {
1187 public void Volatile () {
1189 Thread.VolatileWrite (ref v3, double.MaxValue);
1190 Assert.AreEqual (v3, double.MaxValue);
1193 Thread.VolatileWrite (ref v4, float.MaxValue);
1194 Assert.AreEqual (v4, float.MaxValue);
1198 public void Culture ()
1200 Assert.IsNotNull (Thread.CurrentThread.CurrentCulture, "CurrentCulture");
1201 Assert.IsNotNull (Thread.CurrentThread.CurrentUICulture, "CurrentUICulture");
1205 public void ThreadStartSimple ()
1208 Thread t = new Thread (delegate () {
1209 // ensure the NSAutoreleasePool works
1214 Assert.AreEqual (1, i, "ThreadStart");
1218 public void ParametrizedThreadStart ()
1222 Thread t = new Thread (delegate (object obj) {
1223 // ensure the NSAutoreleasePool works
1230 Assert.AreEqual (1, i, "ParametrizedThreadStart");
1231 Assert.AreEqual (this, arg, "obj");
1235 public void SetNameTpThread () {
1236 ThreadPool.QueueUserWorkItem(new WaitCallback(ThreadProc));
1239 static void ThreadProc(Object stateInfo) {
1240 Thread.CurrentThread.Name = "My Worker";
1244 public class TestUtil
1246 public static void WaitForNotAlive (Thread t, string s)
1248 WhileAlive (t, true, s);
1251 public static void WaitForAlive (Thread t, string s)
1253 WhileAlive (t, false, s);
1256 public static bool WaitForAliveOrStop (Thread t, string s)
1258 return WhileAliveOrStop (t, false, s);
1261 public static void WhileAlive (Thread t, bool alive, string s)
1263 DateTime ti = DateTime.Now;
1264 while (t.IsAlive == alive) {
1265 if ((DateTime.Now - ti).TotalSeconds > 10) {
1266 if (alive) Assert.Fail ("Timeout while waiting for not alive state. " + s);
1267 else Assert.Fail ("Timeout while waiting for alive state. " + s);
1272 public static bool WhileAliveOrStop (Thread t, bool alive, string s)
1274 DateTime ti = DateTime.Now;
1275 while (t.IsAlive == alive) {
1276 if (t.ThreadState == ThreadState.Stopped)
1279 if ((DateTime.Now - ti).TotalSeconds > 10) {
1280 if (alive) Assert.Fail ("Timeout while waiting for not alive state. " + s);
1281 else Assert.Fail ("Timeout while waiting for alive state. " + s);