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 AppDomain.CurrentDomain.SetPrincipalPolicy (PrincipalPolicy.NoPrincipal);
29 IPrincipal p = Thread.CurrentPrincipal;
30 Assert.IsNull (p, "#1");
32 Thread.CurrentPrincipal = new GenericPrincipal (new GenericIdentity ("mono"), null);
33 Assert.IsNotNull (Thread.CurrentPrincipal, "#2");
35 Thread.CurrentPrincipal = null;
36 Assert.IsNull (Thread.CurrentPrincipal, "#3");
37 // in this case we can return to null
40 public static void UnauthenticatedPrincipal ()
42 AppDomain.CurrentDomain.SetPrincipalPolicy (PrincipalPolicy.UnauthenticatedPrincipal);
43 IPrincipal p = Thread.CurrentPrincipal;
44 Assert.IsNotNull (p, "#1");
45 Assert.IsTrue ((p is GenericPrincipal), "#2");
46 Assert.AreEqual (String.Empty, p.Identity.Name, "#3");
47 Assert.AreEqual (String.Empty, p.Identity.AuthenticationType, "#4");
48 Assert.IsFalse (p.Identity.IsAuthenticated, "#5");
50 Thread.CurrentPrincipal = new GenericPrincipal (new GenericIdentity ("mono"), null);
51 Assert.IsNotNull (Thread.CurrentPrincipal, "#6");
53 Thread.CurrentPrincipal = null;
54 Assert.IsNotNull (Thread.CurrentPrincipal, "#7");
55 // in this case we can't return to null
58 public static void WindowsPrincipal ()
60 AppDomain.CurrentDomain.SetPrincipalPolicy (PrincipalPolicy.WindowsPrincipal);
61 IPrincipal p = Thread.CurrentPrincipal;
62 Assert.IsNotNull (p, "#1");
63 Assert.IsTrue ((p is WindowsPrincipal), "#2");
64 Assert.IsNotNull (p.Identity.Name, "#3");
65 Assert.IsNotNull (p.Identity.AuthenticationType, "#4");
66 Assert.IsTrue (p.Identity.IsAuthenticated, "#5");
68 // note: we can switch from a WindowsPrincipal to a GenericPrincipal
69 Thread.CurrentPrincipal = new GenericPrincipal (new GenericIdentity ("mono"), null);
70 Assert.IsNotNull (Thread.CurrentPrincipal, "#6");
72 Thread.CurrentPrincipal = null;
73 Assert.IsNotNull (Thread.CurrentPrincipal, "#7");
74 // in this case we can't return to null
77 public static void CopyOnNewThread ()
79 Assert.IsNotNull (Thread.CurrentPrincipal, "#1");
80 Assert.AreEqual ("good", Thread.CurrentPrincipal.Identity.Name, "#2");
85 [Category("MobileNotWorking")] // Abort #10240
86 public class ThreadTest
88 TimeSpan Infinite = new TimeSpan (-10000); // -10000 ticks == -1 ms
89 TimeSpan SmallNegative = new TimeSpan (-2); // between 0 and -1.0 (infinite) ms
90 TimeSpan Negative = new TimeSpan (-20000); // really negative
91 TimeSpan MaxValue = TimeSpan.FromMilliseconds ((long) Int32.MaxValue);
92 TimeSpan TooLarge = TimeSpan.FromMilliseconds ((long) Int32.MaxValue + 1);
99 switch (Environment.OSVersion.Platform) {
100 case PlatformID.Win32NT:
101 case PlatformID.Win32S:
102 case PlatformID.Win32Windows:
103 case PlatformID.WinCE:
108 // check a class in mscorlib to determine if we're running on Mono
109 if (Type.GetType ("System.MonoType", false) != null)
113 //Some Classes to test as threads
117 public Thread thread1;
123 thread1 = (Thread)null;
125 endm1 = endm2 = false;
128 public void TestMethod()
136 public void TestMethod2()
138 if (!(thread1==(Thread)null) )
149 public bool run = false;
156 public void TestMethod()
171 public C1Test sub_class;
172 public Thread sub_thread;
176 sub_class = new C1Test();
177 sub_thread = new Thread(new ThreadStart(sub_class.TestMethod));
180 public void TestMethod1()
190 public C1Test class1;
191 public C1Test class2;
192 public Thread thread1;
193 public Thread thread2;
201 class1 = new C1Test();
202 class2 = new C1Test();
203 thread1 = new Thread(new ThreadStart(class1.TestMethod));
204 thread2 = new Thread(new ThreadStart(class2.TestMethod));
207 public void TestMethod1()
210 TestUtil.WaitForAlive (thread1, "wait1");
213 TestUtil.WaitForAlive (thread2, "wait2");
216 TestUtil.WaitForNotAlive (thread1, "wait3");
219 TestUtil.WaitForNotAlive (thread2, "wait4");
223 public void TestMethod2()
231 public void TestCtor1()
233 C1Test test1 = new C1Test();
234 Thread t = new Thread (new ThreadStart (test1.TestMethod));
236 Assert.IsTrue (t.CurrentCulture.IsReadOnly, "CurrentCulture.IsReadOnly");
237 Assert.IsFalse (t.IsAlive, "IsAlive");
238 Assert.IsFalse (t.IsBackground, "IsBackground");
239 Assert.IsNull (t.Name, "Name");
240 Assert.AreEqual (ThreadState.Unstarted, t.ThreadState, "ThreadState");
244 [Category ("NotWorking")] // we're not sharing (read-only) CultureInfo
245 public void CultureInfo_Shared_Across_Threads ()
247 Thread t = new Thread (TestCtor1);
248 Assert.AreSame (t.CurrentCulture, t.CurrentUICulture, "Culture");
250 Assert.AreSame (t.CurrentCulture, CultureInfo.CurrentCulture, "CultureInfo.CurrentCulture");
251 Assert.AreSame (t.CurrentUICulture, CultureInfo.CurrentUICulture, "CultureInfo.CurrentUICulture");
253 Assert.AreSame (t.CurrentCulture, Thread.CurrentThread.CurrentCulture, "Thread.CurrentThread.CurrentCulture");
254 Assert.AreSame (t.CurrentUICulture, Thread.CurrentThread.CurrentUICulture, "Thread.CurrentThread.CurrentUICulture");
257 [Test] // bug #325566
258 public void GetHashCodeTest ()
260 C1Test test1 = new C1Test ();
261 Thread tA = new Thread (new ThreadStart (test1.TestMethod));
262 int hA1 = tA.GetHashCode ();
264 Assert.IsTrue (hA1 > 0, "#A1");
267 int hA2 = tA.GetHashCode ();
268 Assert.AreEqual (hA1, hA2, "#A2");
270 int hA3 = tA.GetHashCode ();
271 Assert.AreEqual (hA1, hA3, "#A3");
273 Assert.AreEqual (hA1, tA.ManagedThreadId, "#A4");
276 test1 = new C1Test ();
277 Thread tB = new Thread (new ThreadStart (test1.TestMethod));
278 int hB1 = tB.GetHashCode ();
280 Assert.IsTrue (hB1 > 0, "#B1");
283 int hB2 = tB.GetHashCode ();
284 Assert.AreEqual (hB1, hB2, "#B2");
286 int hB3 = tB.GetHashCode ();
287 Assert.AreEqual (hB1, hB3, "#B3");
289 Assert.AreEqual (hB1, tB.ManagedThreadId, "#B4");
291 Assert.IsFalse (hA2 == hB2, "#B5");
296 public void ManagedThreadId ()
298 C1Test test1 = new C1Test ();
299 Thread t1 = new Thread (new ThreadStart (test1.TestMethod));
300 int mtA1 = t1.ManagedThreadId;
302 int mtA2 = t1.ManagedThreadId;
304 int mtA3 = t1.ManagedThreadId;
305 Assert.AreEqual (mtA1, mtA2, "#A1");
306 Assert.AreEqual (mtA2, mtA3, "#A2");
308 test1 = new C1Test ();
309 Thread t2 = new Thread (new ThreadStart (test1.TestMethod));
310 int mtB1 = t2.ManagedThreadId;
312 int mtB2 = t2.ManagedThreadId;
314 int mtB3 = t2.ManagedThreadId;
315 Assert.AreEqual (mtB1, mtB2, "#B1");
316 Assert.AreEqual (mtB2, mtB3, "#B2");
317 Assert.IsFalse (mtB1 == mtA1, "#B3");
322 [Category ("NotDotNet")] // it hangs.
323 public void TestStart()
325 if (is_win32 && is_mono)
326 Assert.Fail ("This test fails on Win32. The test should be fixed.");
328 C1Test test1 = new C1Test();
329 Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
332 Assert.AreEqual (10, test1.cnt, "#1");
335 C2Test test1 = new C2Test();
336 Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
342 } catch (ThreadStateException) {
346 C2Test test1 = new C2Test();
347 Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
351 bool started = (TestThread.ThreadState == ThreadState.Running);
352 Assert.AreEqual (started, test1.run, "#15 Thread Is not in the correct state: ");
358 public void TestApartmentState ()
360 if (is_win32 && is_mono)
361 Assert.Fail ("This test fails on mono on win32. Our runtime should be fixed.");
363 C2Test test1 = new C2Test();
364 Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
365 Assert.AreEqual (ApartmentState.Unknown, TestThread.ApartmentState, "#1");
367 TestUtil.WaitForAlive (TestThread, "wait5");
369 Assert.AreEqual (ApartmentState.MTA, TestThread.ApartmentState, "#2");
371 Assert.AreEqual (ApartmentState.Unknown, TestThread.ApartmentState, "#3");
377 public void TestPriority1()
379 if (is_win32 && is_mono)
380 Assert.Fail ("This test fails on mono on Win32. Our runtime should be fixed.");
382 C2Test test1 = new C2Test();
383 Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
385 TestThread.Priority=ThreadPriority.BelowNormal;
386 ThreadPriority after = TestThread.Priority;
388 TestUtil.WaitForAlive (TestThread, "wait7");
389 ThreadPriority before = TestThread.Priority;
390 Assert.AreEqual (before, after, "#41 Unexpected Priority Change: ");
397 [Category ("NotDotNet")] // on MS, Thread is still in AbortRequested state when Start is invoked
398 public void AbortUnstarted ()
400 C2Test test1 = new C2Test();
401 Thread th = new Thread (new ThreadStart (test1.TestMethod));
407 [Category ("NotDotNet")] // on MS, ThreadState is immediately Stopped after Abort
408 [Category ("NotWorking")] // this is a MonoTODO -> no support for Priority
409 public void TestPriority2()
411 C2Test test1 = new C2Test();
412 Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
414 Assert.AreEqual (ThreadPriority.Normal, TestThread.Priority, "#42 Incorrect Priority in New thread: ");
416 TestUtil.WaitForAliveOrStop (TestThread, "wait8");
417 Assert.AreEqual (ThreadPriority.Normal, TestThread.Priority, "#43 Incorrect Priority in Started thread: ");
421 Assert.AreEqual (ThreadPriority.Normal, TestThread.Priority, "#44 Incorrect Priority in Aborted thread: ");
425 [Category ("NotWorking")] // this is a MonoTODO -> no support for Priority
426 public void TestPriority3()
428 C2Test test1 = new C2Test();
429 Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
432 TestThread.Priority = ThreadPriority.Lowest;
433 Assert.AreEqual (ThreadPriority.Lowest, TestThread.Priority, "#45A Incorrect Priority:");
434 TestThread.Priority = ThreadPriority.BelowNormal;
435 Assert.AreEqual (ThreadPriority.BelowNormal, TestThread.Priority, "#45B Incorrect Priority:");
436 TestThread.Priority = ThreadPriority.Normal;
437 Assert.AreEqual (ThreadPriority.Normal, TestThread.Priority, "#45C Incorrect Priority:");
438 TestThread.Priority = ThreadPriority.AboveNormal;
439 Assert.AreEqual (ThreadPriority.AboveNormal, TestThread.Priority, "#45D Incorrect Priority:");
440 TestThread.Priority = ThreadPriority.Highest;
441 Assert.AreEqual (ThreadPriority.Highest, TestThread.Priority, "#45E Incorrect Priority:");
449 public void TestUndivisibleByPageSizeMaxStackSize ()
451 const int undivisible_stacksize = 1048573;
453 var thread = new Thread (new ThreadStart (delegate {}), undivisible_stacksize);
459 public void TestIsBackground1 ()
461 if (is_win32 && is_mono)
462 Assert.Fail ("This test fails on mono on Win32. Our runtime should be fixed.");
464 C2Test test1 = new C2Test();
465 Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
468 TestUtil.WaitForAlive (TestThread, "wait9");
469 bool state = TestThread.IsBackground;
470 Assert.IsFalse (state, "#51 IsBackground not set at the default state: ");
477 public void TestIsBackground2 ()
479 C2Test test1 = new C2Test();
480 Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
481 TestThread.IsBackground = true;
488 if (TestThread.IsAlive) {
490 Assert.IsTrue (TestThread.IsBackground, "#52 Is Background Changed to Start ");
491 } catch (ThreadStateException) {
492 // Ignore if thread died meantime
498 public void TestName()
500 if (is_win32 && is_mono)
501 Assert.Fail ("This test fails on mono on Win32. Our runtime should be fixed.");
503 C2Test test1 = new C2Test();
504 Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
507 TestUtil.WaitForAlive (TestThread, "wait10");
508 string name = TestThread.Name;
509 Assert.IsNull (name, "#61 Name set when mustn't be set: ");
510 string newname = "Testing....";
511 TestThread.Name = newname;
512 Assert.AreEqual (newname, TestThread.Name, "#62 Name not set when must be set: ");
521 Thread t = new Thread (new ThreadStart (Name));
522 Assert.IsNull (t.Name, "Name-1");
524 Assert.IsNull (t.Name, "Name-2");
528 [ExpectedException (typeof (InvalidOperationException))]
529 public void Rename ()
531 Thread t = new Thread (new ThreadStart (Rename));
536 bool rename_finished;
540 public void RenameTpThread ()
542 object monitor = new object ();
543 ThreadPool.QueueUserWorkItem (new WaitCallback (Rename_callback), monitor);
545 if (!rename_finished)
546 Monitor.Wait (monitor);
548 Assert.IsFalse (rename_failed);
551 void Rename_callback (object o) {
552 Thread.CurrentThread.Name = "a";
554 Thread.CurrentThread.Name = "b";
555 } catch (Exception) {
556 rename_failed = true;
560 rename_finished = true;
561 Monitor.Pulse (monitor);
566 public void TestNestedThreads1()
568 C3Test test1 = new C3Test();
569 Thread TestThread = new Thread(new ThreadStart(test1.TestMethod1));
572 TestUtil.WaitForAlive (TestThread, "wait11");
579 public void TestNestedThreads2()
581 C4Test test1 = new C4Test();
582 Thread TestThread = new Thread(new ThreadStart(test1.TestMethod1));
591 public void TestJoin1()
593 C1Test test1 = new C1Test();
594 C1Test test2 = new C1Test();
595 Thread thread1 = new Thread(new ThreadStart(test1.TestMethod));
596 Thread thread2 = new Thread(new ThreadStart(test1.TestMethod2));
608 [ExpectedException (typeof (ArgumentOutOfRangeException))]
609 public void Join_Int32_Negative ()
611 // -1 is Timeout.Infinite
612 Thread.CurrentThread.Join (-2);
616 [ExpectedException (typeof (ArgumentOutOfRangeException))]
617 public void Join_TimeSpan_Negative ()
619 Thread.CurrentThread.Join (Negative);
623 [ExpectedException (typeof (ArgumentOutOfRangeException))]
624 public void Join_TimeSpan_TooLarge ()
626 Thread.CurrentThread.Join (TooLarge);
630 public void Join_TimeSpan_SmallNegative ()
632 Thread.CurrentThread.Join (SmallNegative);
636 [ExpectedException (typeof (ArgumentOutOfRangeException))]
637 public void Sleep_Int32_Negative ()
639 // -1 is Timeout.Infinite
644 public void Sleep_TimeSpan_SmallNegative ()
646 Thread.Sleep (SmallNegative);
650 [ExpectedException (typeof (ArgumentOutOfRangeException))]
651 public void Sleep_TimeSpan_Negative ()
653 Thread.Sleep (Negative);
657 [ExpectedException (typeof (ArgumentOutOfRangeException))]
658 public void Sleep_TimeSpan_TooLarge ()
660 Thread.Sleep (TooLarge);
664 public void SpinWait ()
666 // no exception for negative numbers
667 Thread.SpinWait (Int32.MinValue);
672 public void TestThreadState ()
674 if (is_win32 && is_mono)
675 Assert.Fail ("This test fails on mono on Win32. Our runtime should be fixed.");
677 //TODO: Test The rest of the possible transitions
678 C2Test test1 = new C2Test();
679 Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
680 Assert.AreEqual (ThreadState.Unstarted, TestThread.ThreadState, "#101 Wrong Thread State");
683 //while(!TestThread.IsAlive); //In the MS Documentation this is not necessary
684 //but in the MS SDK it is
685 Assert.IsTrue (TestThread.ThreadState == ThreadState.Running || (TestThread.ThreadState & ThreadState.Unstarted) != 0,
686 "#102 Wrong Thread State: " + TestThread.ThreadState.ToString ());
691 TestUtil.WaitForNotAlive (TestThread, "wait12");
692 // Docs say state will be Stopped, but Aborted happens sometimes (?)
693 Assert.IsTrue ((ThreadState.Stopped & TestThread.ThreadState) != 0 || (ThreadState.Aborted & TestThread.ThreadState) != 0,
694 "#103 Wrong Thread State: " + TestThread.ThreadState.ToString ());
698 [Ignore ("see comment below.")]
699 public void CurrentPrincipal_PrincipalPolicy_NoPrincipal ()
701 // note: switching from PrincipalPolicy won't work inside the same thread
702 // because as soon as a Principal object is created the Policy doesn't matter anymore
703 Thread t = new Thread (new ThreadStart (ThreadedPrincipalTest.NoPrincipal));
713 [Ignore ("see comment below.")]
714 public void CurrentPrincipal_PrincipalPolicy_UnauthenticatedPrincipal ()
716 // note: switching from PrincipalPolicy won't work inside the same thread
717 // because as soon as a Principal object is created the Policy doesn't matter anymore
718 Thread t = new Thread (new ThreadStart (ThreadedPrincipalTest.UnauthenticatedPrincipal));
728 public void CurrentPrincipal_PrincipalPolicy_WindowsPrincipal ()
730 // note: switching from PrincipalPolicy won't work inside the same thread
731 // because as soon as a Principal object is created the Policy doesn't matter anymore
732 Thread t = new Thread (new ThreadStart (ThreadedPrincipalTest.WindowsPrincipal));
742 public void IPrincipal_CopyOnNewThread ()
744 Thread.CurrentPrincipal = new GenericPrincipal (new GenericIdentity ("bad"), null);
745 Thread t = new Thread (new ThreadStart (ThreadedPrincipalTest.CopyOnNewThread));
747 Thread.CurrentPrincipal = new GenericPrincipal (new GenericIdentity ("good"), null);
758 public void TestSuspend ()
760 Thread t = new Thread (new ThreadStart (DoCount));
761 t.IsBackground = true;
764 CheckIsRunning ("t1", t);
767 WaitSuspended ("t2", t);
769 CheckIsNotRunning ("t3", t);
772 WaitResumed ("t4", t);
774 CheckIsRunning ("t5", t);
777 TestUtil.WaitForNotAlive (t, "wait13");
778 CheckIsNotRunning ("t6", t);
782 [Category("NotDotNet")] // On MS, ThreadStateException is thrown on Abort: "Thread is suspended; attempting to abort"
783 public void TestSuspendAbort ()
785 if (is_win32 && is_mono)
786 Assert.Fail ("This test fails on Win32. The test should be fixed.");
788 Thread t = new Thread (new ThreadStart (DoCount));
789 t.IsBackground = true;
792 CheckIsRunning ("t1", t);
795 WaitSuspended ("t2", t);
797 CheckIsNotRunning ("t3", t);
802 while (t.IsAlive && n < 200) {
807 Assert.IsTrue (n < 200, "Timeout while waiting for abort");
809 CheckIsNotRunning ("t6", t);
813 public void Test_Interrupt ()
815 bool interruptedExceptionThrown = false;
816 ThreadPool.QueueUserWorkItem (Test_Interrupt_Worker, Thread.CurrentThread);
824 } catch (ThreadInterruptedException) {
825 Assert.Fail ("ThreadInterruptedException thrown twice");
828 } catch (ThreadInterruptedException) {
829 interruptedExceptionThrown = true;
832 Assert.IsTrue (interruptedExceptionThrown, "ThreadInterruptedException expected.");
836 [ExpectedException (typeof (ArgumentNullException))]
837 public void TestQueueUserWorkItemNullCallback ()
839 ThreadPool.QueueUserWorkItem (null, null);
842 private void Test_Interrupt_Worker (object o)
844 Thread t = o as Thread;
850 [Category ("NotDotNet")] // it crashes nunit.
851 public void Test_InterruptCurrentThread ()
853 bool interruptedExceptionThrown = false;
855 Thread.CurrentThread.Interrupt ();
859 } catch (ThreadInterruptedException) {
864 public void GetNamedDataSlotTest ()
866 Assert.IsNotNull (Thread.GetNamedDataSlot ("te#st"), "#1");
867 Assert.AreSame (Thread.GetNamedDataSlot ("te#st"), Thread.GetNamedDataSlot ("te#st"), "#2");
870 void CheckIsRunning (string s, Thread t)
874 Assert.IsTrue (counter > c, s);
877 void CheckIsNotRunning (string s, Thread t)
881 Assert.AreEqual (counter, c, s);
884 void WaitSuspended (string s, Thread t)
887 ThreadState state = t.ThreadState;
888 while ((state & ThreadState.Suspended) == 0) {
889 Assert.IsTrue ((state & ThreadState.SuspendRequested) != 0, s + ": expected SuspendRequested state");
892 Assert.IsTrue (n < 100, s + ": failed to suspend");
893 state = t.ThreadState;
895 Assert.IsTrue ((state & ThreadState.SuspendRequested) == 0, s + ": SuspendRequested state not expected");
898 void WaitResumed (string s, Thread t)
901 while ((t.ThreadState & ThreadState.Suspended) != 0) {
904 Assert.IsTrue (n < 100, s + ": failed to resume");
908 public void DoCount ()
918 public class ThreadStateTest {
924 public void IsBackGround ()
926 Thread t1 = new Thread (new ThreadStart (Start));
927 Assert.AreEqual (ThreadState.Unstarted, t1.ThreadState, "#A1");
928 Assert.IsFalse (t1.IsBackground, "#A2");
931 Assert.AreEqual (ThreadState.Stopped, t1.ThreadState, "#A3");
934 bool isBackGround = t1.IsBackground;
935 Assert.Fail ("#A4: " + isBackGround.ToString ());
936 } catch (ThreadStateException ex) {
937 Assert.AreEqual (typeof (ThreadStateException), ex.GetType (), "#A5");
938 Assert.IsNull (ex.InnerException, "#A6");
939 Assert.IsNotNull (ex.Message, "#A7");
942 Thread t2 = new Thread (new ThreadStart (Start));
943 Assert.AreEqual (ThreadState.Unstarted, t2.ThreadState, "#B1");
944 t2.IsBackground = true;
945 Assert.AreEqual (ThreadState.Unstarted | ThreadState.Background, t2.ThreadState, "#B2");
946 Assert.IsTrue (t2.IsBackground, "#B3");
949 Assert.AreEqual (ThreadState.Stopped, t2.ThreadState, "#B4");
952 bool isBackGround = t2.IsBackground;
953 Assert.Fail ("#B5: " + isBackGround.ToString ());
954 } catch (ThreadStateException ex) {
955 Assert.AreEqual (typeof (ThreadStateException), ex.GetType (), "#B6");
956 Assert.IsNull (ex.InnerException, "#B7");
957 Assert.IsNotNull (ex.Message, "#B8");
964 public class ThreadTest_ManagedThreadId
968 MBRO mbro = new MBRO ();
970 class MBRO : MarshalByRefObject {
977 public string message;
981 public void ManagedThreadId_AppDomains ()
983 AppDomain currentDomain = AppDomain.CurrentDomain;
984 ad1 = AppDomain.CreateDomain ("AppDomain 1", currentDomain.Evidence, currentDomain.SetupInformation);
985 ad2 = AppDomain.CreateDomain ("AppDomain 2", currentDomain.Evidence, currentDomain.SetupInformation);
987 Thread a = new Thread (ThreadA);
988 Thread b = new Thread (ThreadB);
989 // execute on AppDomain 1 thread A
990 // execute on AppDomain 2 thread B
991 // execute on AppDomain 1 thread B - must have same ManagedThreadId as Ad 2 on thread B
997 AppDomain.Unload (ad1);
998 AppDomain.Unload (ad2);
1000 if (mbro.message != null)
1001 Assert.Fail (mbro.message);
1003 // 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);
1005 Assert.AreEqual ("AppDomain 1", mbro.ad_a1, "Name #1");
1006 Assert.AreEqual ("AppDomain 1", mbro.ad_b1, "Name #2");
1007 Assert.AreEqual ("AppDomain 2", mbro.ad_b2, "Name #3");
1009 Assert.AreNotEqual (mbro.id_a1, mbro.id_b1, "Id #1");
1010 Assert.AreNotEqual (mbro.id_a1, mbro.id_b2, "Id #2");
1011 Assert.AreEqual (mbro.id_b1, mbro.id_b2, "Id #3");
1013 Assert.AreNotEqual (mbro.id_a1, Thread.CurrentThread.ManagedThreadId, "Id #4");
1014 Assert.AreNotEqual (mbro.id_b1, Thread.CurrentThread.ManagedThreadId, "Id #5");
1015 Assert.AreNotEqual (mbro.id_b2, Thread.CurrentThread.ManagedThreadId, "Id #6");
1016 Assert.AreNotEqual (mbro.ad_a1, AppDomain.CurrentDomain.FriendlyName, "Name #4");
1017 Assert.AreNotEqual (mbro.ad_b1, AppDomain.CurrentDomain.FriendlyName, "Name #5");
1018 Assert.AreNotEqual (mbro.ad_b2, AppDomain.CurrentDomain.FriendlyName, "Name #6");
1023 mbro.id_a1 = Thread.CurrentThread.ManagedThreadId;
1024 mbro.ad_a1 = AppDomain.CurrentDomain.FriendlyName;
1029 mbro.id_b2 = Thread.CurrentThread.ManagedThreadId;
1030 mbro.ad_b2 = AppDomain.CurrentDomain.FriendlyName;
1035 mbro.id_b1 = Thread.CurrentThread.ManagedThreadId;
1036 mbro.ad_b1 = AppDomain.CurrentDomain.FriendlyName;
1039 void ThreadA (object obj)
1041 // Console.WriteLine ("ThreadA");
1043 ad1.DoCallBack (A1);
1044 } catch (Exception ex) {
1045 mbro.message = string.Format ("ThreadA exception: {0}", ex);
1047 // Console.WriteLine ("ThreadA Done");
1050 void ThreadB (object obj)
1052 // Console.WriteLine ("ThreadB");
1054 ad2.DoCallBack (B2);
1055 ad1.DoCallBack (B1);
1056 } catch (Exception ex) {
1057 mbro.message = string.Format ("ThreadB exception: {0}", ex);
1059 // Console.WriteLine ("ThreadB Done");
1064 public class ThreadApartmentTest
1070 [Test] // bug #81658
1071 public void ApartmentState_StoppedThread ()
1073 Thread t1 = new Thread (new ThreadStart (Start));
1077 ApartmentState state = t1.ApartmentState;
1078 Assert.Fail ("#A1: " + state.ToString ());
1079 } catch (ThreadStateException ex) {
1080 Assert.AreEqual (typeof (ThreadStateException), ex.GetType (), "#A2");
1081 Assert.IsNull (ex.InnerException, "#A3");
1082 Assert.IsNotNull (ex.Message, "#A4");
1085 Thread t2 = new Thread (new ThreadStart (Start));
1086 t2.IsBackground = true;
1090 ApartmentState state = t2.ApartmentState;
1091 Assert.Fail ("#B1: " + state.ToString ());
1092 } catch (ThreadStateException ex) {
1093 Assert.AreEqual (typeof (ThreadStateException), ex.GetType (), "#B2");
1094 Assert.IsNull (ex.InnerException, "#B3");
1095 Assert.IsNotNull (ex.Message, "#B4");
1100 public void ApartmentState_BackGround ()
1102 Thread t1 = new Thread (new ThreadStart (Start));
1103 t1.IsBackground = true;
1104 Assert.AreEqual (ApartmentState.Unknown, t1.ApartmentState, "#1");
1105 t1.ApartmentState = ApartmentState.STA;
1106 Assert.AreEqual (ApartmentState.STA, t1.ApartmentState, "#2");
1110 public void TestApartmentState ()
1112 Thread t1 = new Thread (new ThreadStart (Start));
1113 Thread t2 = new Thread (new ThreadStart (Start));
1114 Thread t3 = new Thread (new ThreadStart (Start));
1116 Assert.AreEqual (ApartmentState.Unknown, t1.ApartmentState, "Thread1 Default");
1117 Assert.AreEqual (ApartmentState.Unknown, t2.ApartmentState, "Thread2 Default");
1118 Assert.AreEqual (ApartmentState.Unknown, t3.ApartmentState, "Thread3 Default");
1120 t1.ApartmentState = ApartmentState.STA;
1121 Assert.AreEqual (ApartmentState.STA, t1.ApartmentState, "Thread1 Set Once");
1122 t1.ApartmentState = ApartmentState.MTA;
1123 Assert.AreEqual (ApartmentState.STA, t1.ApartmentState, "Thread1 Set Twice");
1125 t2.ApartmentState = ApartmentState.MTA;
1126 Assert.AreEqual (ApartmentState.MTA, t2.ApartmentState, "Thread2 Set Once");
1127 t2.ApartmentState = ApartmentState.STA;
1128 Assert.AreEqual (ApartmentState.MTA, t2.ApartmentState, "Thread2 Set Twice");
1130 bool exception_occured = false;
1132 t3.ApartmentState = ApartmentState.Unknown;
1135 exception_occured = true;
1137 Assert.AreEqual (ApartmentState.Unknown, t3.ApartmentState, "Thread3 Set Invalid");
1139 Assert.IsFalse (exception_occured, "Thread3 Set Invalid Exception Occured");
1141 Assert.IsTrue (exception_occured, "Thread3 Set Invalid Exception Occured");
1145 exception_occured = false;
1147 t1.ApartmentState = ApartmentState.STA;
1150 exception_occured = true;
1152 Assert.IsTrue (exception_occured, "Thread1 Started Invalid Exception Occured");
1156 public void TestSetApartmentStateSameState ()
1158 Thread t1 = new Thread (new ThreadStart (Start));
1159 t1.SetApartmentState (ApartmentState.STA);
1160 Assert.AreEqual (ApartmentState.STA, t1.ApartmentState, "Thread1 Set Once");
1162 t1.SetApartmentState (ApartmentState.STA);
1163 Assert.AreEqual (ApartmentState.STA, t1.ApartmentState, "Thread1 Set twice");
1167 [ExpectedException(typeof(InvalidOperationException))]
1168 public void TestSetApartmentStateDiffState ()
1170 Thread t1 = new Thread (new ThreadStart (Start));
1171 t1.SetApartmentState (ApartmentState.STA);
1172 Assert.AreEqual (ApartmentState.STA, t1.ApartmentState, "Thread1 Set Once");
1174 t1.SetApartmentState (ApartmentState.MTA);
1178 public void TestTrySetApartmentState ()
1180 Thread t1 = new Thread (new ThreadStart (Start));
1181 t1.SetApartmentState (ApartmentState.STA);
1182 Assert.AreEqual (ApartmentState.STA, t1.ApartmentState, "#1");
1184 bool result = t1.TrySetApartmentState (ApartmentState.MTA);
1185 Assert.IsFalse (result, "#2");
1187 result = t1.TrySetApartmentState (ApartmentState.STA);
1188 Assert.IsTrue (result, "#3");
1192 public void TestTrySetApartmentStateRunning ()
1194 Thread t1 = new Thread (new ThreadStart (Start));
1195 t1.SetApartmentState (ApartmentState.STA);
1196 Assert.AreEqual (ApartmentState.STA, t1.ApartmentState, "#1");
1201 t1.TrySetApartmentState (ApartmentState.STA);
1203 } catch (ThreadStateException) {
1210 public void Volatile () {
1212 Thread.VolatileWrite (ref v3, double.MaxValue);
1213 Assert.AreEqual (v3, double.MaxValue);
1216 Thread.VolatileWrite (ref v4, float.MaxValue);
1217 Assert.AreEqual (v4, float.MaxValue);
1221 public void Culture ()
1223 Assert.IsNotNull (Thread.CurrentThread.CurrentCulture, "CurrentCulture");
1224 Assert.IsNotNull (Thread.CurrentThread.CurrentUICulture, "CurrentUICulture");
1228 public void ThreadStartSimple ()
1231 Thread t = new Thread (delegate () {
1232 // ensure the NSAutoreleasePool works
1237 Assert.AreEqual (1, i, "ThreadStart");
1241 public void ParametrizedThreadStart ()
1245 Thread t = new Thread (delegate (object obj) {
1246 // ensure the NSAutoreleasePool works
1253 Assert.AreEqual (1, i, "ParametrizedThreadStart");
1254 Assert.AreEqual (this, arg, "obj");
1257 bool set_name_failed;
1260 public void SetNameTpThread () {
1261 ThreadPool.QueueUserWorkItem(new WaitCallback(ThreadProc));
1264 static void ThreadProc(Object stateInfo) {
1265 Thread.CurrentThread.Name = "My Worker";
1269 public class TestUtil
1271 public static void WaitForNotAlive (Thread t, string s)
1273 WhileAlive (t, true, s);
1276 public static void WaitForAlive (Thread t, string s)
1278 WhileAlive (t, false, s);
1281 public static bool WaitForAliveOrStop (Thread t, string s)
1283 return WhileAliveOrStop (t, false, s);
1286 public static void WhileAlive (Thread t, bool alive, string s)
1288 DateTime ti = DateTime.Now;
1289 while (t.IsAlive == alive) {
1290 if ((DateTime.Now - ti).TotalSeconds > 10) {
1291 if (alive) Assert.Fail ("Timeout while waiting for not alive state. " + s);
1292 else Assert.Fail ("Timeout while waiting for alive state. " + s);
1297 public static bool WhileAliveOrStop (Thread t, bool alive, string s)
1299 DateTime ti = DateTime.Now;
1300 while (t.IsAlive == alive) {
1301 if (t.ThreadState == ThreadState.Stopped)
1304 if ((DateTime.Now - ti).TotalSeconds > 10) {
1305 if (alive) Assert.Fail ("Timeout while waiting for not alive state. " + s);
1306 else Assert.Fail ("Timeout while waiting for alive state. " + s);