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) {
840 void CheckIsRunning (string s, Thread t)
844 Assert.IsTrue (counter > c, s);
847 void CheckIsNotRunning (string s, Thread t)
851 Assert.AreEqual (counter, c, s);
854 void WaitSuspended (string s, Thread t)
857 ThreadState state = t.ThreadState;
858 while ((state & ThreadState.Suspended) == 0) {
859 Assert.IsTrue ((state & ThreadState.SuspendRequested) != 0, s + ": expected SuspendRequested state");
862 Assert.IsTrue (n < 100, s + ": failed to suspend");
863 state = t.ThreadState;
865 Assert.IsTrue ((state & ThreadState.SuspendRequested) == 0, s + ": SuspendRequested state not expected");
868 void WaitResumed (string s, Thread t)
871 while ((t.ThreadState & ThreadState.Suspended) != 0) {
874 Assert.IsTrue (n < 100, s + ": failed to resume");
878 public void DoCount ()
888 public class ThreadStateTest {
894 public void IsBackGround ()
896 Thread t1 = new Thread (new ThreadStart (Start));
897 Assert.AreEqual (ThreadState.Unstarted, t1.ThreadState, "#A1");
898 Assert.IsFalse (t1.IsBackground, "#A2");
901 Assert.AreEqual (ThreadState.Stopped, t1.ThreadState, "#A3");
904 bool isBackGround = t1.IsBackground;
905 Assert.Fail ("#A4: " + isBackGround.ToString ());
906 } catch (ThreadStateException ex) {
907 Assert.AreEqual (typeof (ThreadStateException), ex.GetType (), "#A5");
908 Assert.IsNull (ex.InnerException, "#A6");
909 Assert.IsNotNull (ex.Message, "#A7");
912 Thread t2 = new Thread (new ThreadStart (Start));
913 Assert.AreEqual (ThreadState.Unstarted, t2.ThreadState, "#B1");
914 t2.IsBackground = true;
915 Assert.AreEqual (ThreadState.Unstarted | ThreadState.Background, t2.ThreadState, "#B2");
916 Assert.IsTrue (t2.IsBackground, "#B3");
919 Assert.AreEqual (ThreadState.Stopped, t2.ThreadState, "#B4");
922 bool isBackGround = t2.IsBackground;
923 Assert.Fail ("#B5: " + isBackGround.ToString ());
924 } catch (ThreadStateException ex) {
925 Assert.AreEqual (typeof (ThreadStateException), ex.GetType (), "#B6");
926 Assert.IsNull (ex.InnerException, "#B7");
927 Assert.IsNotNull (ex.Message, "#B8");
934 public class ThreadTest_ManagedThreadId
938 MBRO mbro = new MBRO ();
940 class MBRO : MarshalByRefObject {
947 public string message;
951 public void ManagedThreadId_AppDomains ()
953 AppDomain currentDomain = AppDomain.CurrentDomain;
954 ad1 = AppDomain.CreateDomain ("AppDomain 1", currentDomain.Evidence, currentDomain.SetupInformation);
955 ad2 = AppDomain.CreateDomain ("AppDomain 2", currentDomain.Evidence, currentDomain.SetupInformation);
957 Thread a = new Thread (ThreadA);
958 Thread b = new Thread (ThreadB);
959 // execute on AppDomain 1 thread A
960 // execute on AppDomain 2 thread B
961 // execute on AppDomain 1 thread B - must have same ManagedThreadId as Ad 2 on thread B
967 AppDomain.Unload (ad1);
968 AppDomain.Unload (ad2);
970 if (mbro.message != null)
971 Assert.Fail (mbro.message);
973 // 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);
975 Assert.AreEqual ("AppDomain 1", mbro.ad_a1, "Name #1");
976 Assert.AreEqual ("AppDomain 1", mbro.ad_b1, "Name #2");
977 Assert.AreEqual ("AppDomain 2", mbro.ad_b2, "Name #3");
979 Assert.AreNotEqual (mbro.id_a1, mbro.id_b1, "Id #1");
980 Assert.AreNotEqual (mbro.id_a1, mbro.id_b2, "Id #2");
981 Assert.AreEqual (mbro.id_b1, mbro.id_b2, "Id #3");
983 Assert.AreNotEqual (mbro.id_a1, Thread.CurrentThread.ManagedThreadId, "Id #4");
984 Assert.AreNotEqual (mbro.id_b1, Thread.CurrentThread.ManagedThreadId, "Id #5");
985 Assert.AreNotEqual (mbro.id_b2, Thread.CurrentThread.ManagedThreadId, "Id #6");
986 Assert.AreNotEqual (mbro.ad_a1, AppDomain.CurrentDomain.FriendlyName, "Name #4");
987 Assert.AreNotEqual (mbro.ad_b1, AppDomain.CurrentDomain.FriendlyName, "Name #5");
988 Assert.AreNotEqual (mbro.ad_b2, AppDomain.CurrentDomain.FriendlyName, "Name #6");
993 mbro.id_a1 = Thread.CurrentThread.ManagedThreadId;
994 mbro.ad_a1 = AppDomain.CurrentDomain.FriendlyName;
999 mbro.id_b2 = Thread.CurrentThread.ManagedThreadId;
1000 mbro.ad_b2 = AppDomain.CurrentDomain.FriendlyName;
1005 mbro.id_b1 = Thread.CurrentThread.ManagedThreadId;
1006 mbro.ad_b1 = AppDomain.CurrentDomain.FriendlyName;
1009 void ThreadA (object obj)
1011 // Console.WriteLine ("ThreadA");
1013 ad1.DoCallBack (A1);
1014 } catch (Exception ex) {
1015 mbro.message = string.Format ("ThreadA exception: {0}", ex);
1017 // Console.WriteLine ("ThreadA Done");
1020 void ThreadB (object obj)
1022 // Console.WriteLine ("ThreadB");
1024 ad2.DoCallBack (B2);
1025 ad1.DoCallBack (B1);
1026 } catch (Exception ex) {
1027 mbro.message = string.Format ("ThreadB exception: {0}", ex);
1029 // Console.WriteLine ("ThreadB Done");
1034 public class ThreadApartmentTest
1040 [Test] // bug #81658
1041 public void ApartmentState_StoppedThread ()
1043 Thread t1 = new Thread (new ThreadStart (Start));
1047 ApartmentState state = t1.ApartmentState;
1048 Assert.Fail ("#A1: " + state.ToString ());
1049 } catch (ThreadStateException ex) {
1050 Assert.AreEqual (typeof (ThreadStateException), ex.GetType (), "#A2");
1051 Assert.IsNull (ex.InnerException, "#A3");
1052 Assert.IsNotNull (ex.Message, "#A4");
1055 Thread t2 = new Thread (new ThreadStart (Start));
1056 t2.IsBackground = true;
1060 ApartmentState state = t2.ApartmentState;
1061 Assert.Fail ("#B1: " + state.ToString ());
1062 } catch (ThreadStateException ex) {
1063 Assert.AreEqual (typeof (ThreadStateException), ex.GetType (), "#B2");
1064 Assert.IsNull (ex.InnerException, "#B3");
1065 Assert.IsNotNull (ex.Message, "#B4");
1070 public void ApartmentState_BackGround ()
1072 Thread t1 = new Thread (new ThreadStart (Start));
1073 t1.IsBackground = true;
1074 Assert.AreEqual (ApartmentState.Unknown, t1.ApartmentState, "#1");
1075 t1.ApartmentState = ApartmentState.STA;
1076 Assert.AreEqual (ApartmentState.STA, t1.ApartmentState, "#2");
1080 public void TestApartmentState ()
1082 Thread t1 = new Thread (new ThreadStart (Start));
1083 Thread t2 = new Thread (new ThreadStart (Start));
1084 Thread t3 = new Thread (new ThreadStart (Start));
1086 Assert.AreEqual (ApartmentState.Unknown, t1.ApartmentState, "Thread1 Default");
1087 Assert.AreEqual (ApartmentState.Unknown, t2.ApartmentState, "Thread2 Default");
1088 Assert.AreEqual (ApartmentState.Unknown, t3.ApartmentState, "Thread3 Default");
1090 t1.ApartmentState = ApartmentState.STA;
1091 Assert.AreEqual (ApartmentState.STA, t1.ApartmentState, "Thread1 Set Once");
1092 t1.ApartmentState = ApartmentState.MTA;
1093 Assert.AreEqual (ApartmentState.STA, t1.ApartmentState, "Thread1 Set Twice");
1095 t2.ApartmentState = ApartmentState.MTA;
1096 Assert.AreEqual (ApartmentState.MTA, t2.ApartmentState, "Thread2 Set Once");
1097 t2.ApartmentState = ApartmentState.STA;
1098 Assert.AreEqual (ApartmentState.MTA, t2.ApartmentState, "Thread2 Set Twice");
1100 bool exception_occured = false;
1102 t3.ApartmentState = ApartmentState.Unknown;
1105 exception_occured = true;
1107 Assert.AreEqual (ApartmentState.Unknown, t3.ApartmentState, "Thread3 Set Invalid");
1109 Assert.IsFalse (exception_occured, "Thread3 Set Invalid Exception Occured");
1111 Assert.IsTrue (exception_occured, "Thread3 Set Invalid Exception Occured");
1115 exception_occured = false;
1117 t1.ApartmentState = ApartmentState.STA;
1120 exception_occured = true;
1122 Assert.IsTrue (exception_occured, "Thread1 Started Invalid Exception Occured");
1126 public void TestSetApartmentStateSameState ()
1128 Thread t1 = new Thread (new ThreadStart (Start));
1129 t1.SetApartmentState (ApartmentState.STA);
1130 Assert.AreEqual (ApartmentState.STA, t1.ApartmentState, "Thread1 Set Once");
1132 t1.SetApartmentState (ApartmentState.STA);
1133 Assert.AreEqual (ApartmentState.STA, t1.ApartmentState, "Thread1 Set twice");
1137 [ExpectedException(typeof(InvalidOperationException))]
1138 public void TestSetApartmentStateDiffState ()
1140 Thread t1 = new Thread (new ThreadStart (Start));
1141 t1.SetApartmentState (ApartmentState.STA);
1142 Assert.AreEqual (ApartmentState.STA, t1.ApartmentState, "Thread1 Set Once");
1144 t1.SetApartmentState (ApartmentState.MTA);
1148 public void TestTrySetApartmentState ()
1150 Thread t1 = new Thread (new ThreadStart (Start));
1151 t1.SetApartmentState (ApartmentState.STA);
1152 Assert.AreEqual (ApartmentState.STA, t1.ApartmentState, "#1");
1154 bool result = t1.TrySetApartmentState (ApartmentState.MTA);
1155 Assert.IsFalse (result, "#2");
1157 result = t1.TrySetApartmentState (ApartmentState.STA);
1158 Assert.IsTrue (result, "#3");
1162 public void TestTrySetApartmentStateRunning ()
1164 Thread t1 = new Thread (new ThreadStart (Start));
1165 t1.SetApartmentState (ApartmentState.STA);
1166 Assert.AreEqual (ApartmentState.STA, t1.ApartmentState, "#1");
1171 t1.TrySetApartmentState (ApartmentState.STA);
1173 } catch (ThreadStateException) {
1180 public void Volatile () {
1182 Thread.VolatileWrite (ref v3, double.MaxValue);
1183 Assert.AreEqual (v3, double.MaxValue);
1186 Thread.VolatileWrite (ref v4, float.MaxValue);
1187 Assert.AreEqual (v4, float.MaxValue);
1191 public void Culture ()
1193 Assert.IsNotNull (Thread.CurrentThread.CurrentCulture, "CurrentCulture");
1194 Assert.IsNotNull (Thread.CurrentThread.CurrentUICulture, "CurrentUICulture");
1198 public void ThreadStartSimple ()
1201 Thread t = new Thread (delegate () {
1202 // ensure the NSAutoreleasePool works
1207 Assert.AreEqual (1, i, "ThreadStart");
1211 public void ParametrizedThreadStart ()
1215 Thread t = new Thread (delegate (object obj) {
1216 // ensure the NSAutoreleasePool works
1223 Assert.AreEqual (1, i, "ParametrizedThreadStart");
1224 Assert.AreEqual (this, arg, "obj");
1228 public void SetNameTpThread () {
1229 ThreadPool.QueueUserWorkItem(new WaitCallback(ThreadProc));
1232 static void ThreadProc(Object stateInfo) {
1233 Thread.CurrentThread.Name = "My Worker";
1237 public class TestUtil
1239 public static void WaitForNotAlive (Thread t, string s)
1241 WhileAlive (t, true, s);
1244 public static void WaitForAlive (Thread t, string s)
1246 WhileAlive (t, false, s);
1249 public static bool WaitForAliveOrStop (Thread t, string s)
1251 return WhileAliveOrStop (t, false, s);
1254 public static void WhileAlive (Thread t, bool alive, string s)
1256 DateTime ti = DateTime.Now;
1257 while (t.IsAlive == alive) {
1258 if ((DateTime.Now - ti).TotalSeconds > 10) {
1259 if (alive) Assert.Fail ("Timeout while waiting for not alive state. " + s);
1260 else Assert.Fail ("Timeout while waiting for alive state. " + s);
1265 public static bool WhileAliveOrStop (Thread t, bool alive, string s)
1267 DateTime ti = DateTime.Now;
1268 while (t.IsAlive == alive) {
1269 if (t.ThreadState == ThreadState.Stopped)
1272 if ((DateTime.Now - ti).TotalSeconds > 10) {
1273 if (alive) Assert.Fail ("Timeout while waiting for not alive state. " + s);
1274 else Assert.Fail ("Timeout while waiting for alive state. " + s);