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
;
16 using System
.Reflection
;
17 using System
.Collections
.Generic
;
18 using SD
= System
.Diagnostics
;
20 using NUnit
.Framework
;
22 namespace MonoTests
.System
.Threading
24 // These tests seem to hang the 2.0 framework. So they are disabled for now
25 // Don't reenable them until you can run a few thousand times on an SMP box.
26 [Category ("NotWorking")]
27 public class ThreadedPrincipalTest
29 public static void NoPrincipal ()
31 AppDomain
.CurrentDomain
.SetPrincipalPolicy (PrincipalPolicy
.NoPrincipal
);
32 IPrincipal p
= Thread
.CurrentPrincipal
;
33 Assert
.IsNull (p
, "#1");
35 Thread
.CurrentPrincipal
= new GenericPrincipal (new GenericIdentity ("mono"), null);
36 Assert
.IsNotNull (Thread
.CurrentPrincipal
, "#2");
38 Thread
.CurrentPrincipal
= null;
39 Assert
.IsNull (Thread
.CurrentPrincipal
, "#3");
40 // in this case we can return to null
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
80 public static void CopyOnNewThread ()
82 Assert
.IsNotNull (Thread
.CurrentPrincipal
, "#1");
83 Assert
.AreEqual ("good", Thread
.CurrentPrincipal
.Identity
.Name
, "#2");
88 [Category("MobileNotWorking")] // Abort #10240
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);
102 switch (Environment
.OSVersion
.Platform
) {
103 case PlatformID
.Win32NT
:
104 case PlatformID
.Win32S
:
105 case PlatformID
.Win32Windows
:
106 case PlatformID
.WinCE
:
111 // check a class in mscorlib to determine if we're running on Mono
112 if (Type
.GetType ("System.MonoType", false) != null)
116 //Some Classes to test as threads
120 public Thread thread1
;
126 thread1
= (Thread
)null;
128 endm1
= endm2
= false;
131 public void TestMethod()
139 public void TestMethod2()
141 if (!(thread1
==(Thread
)null) )
152 public bool run
= false;
159 public void TestMethod()
174 public C1Test sub_class
;
175 public Thread sub_thread
;
179 sub_class
= new C1Test();
180 sub_thread
= new Thread(new ThreadStart(sub_class
.TestMethod
));
183 public void TestMethod1()
187 #if MONO_FEATURE_THREAD_ABORT
190 sub_thread
.Interrupt ();
197 public C1Test class1
;
198 public C1Test class2
;
199 public Thread thread1
;
200 public Thread thread2
;
208 class1
= new C1Test();
209 class2
= new C1Test();
210 thread1
= new Thread(new ThreadStart(class1
.TestMethod
));
211 thread2
= new Thread(new ThreadStart(class2
.TestMethod
));
214 public void TestMethod1()
217 TestUtil
.WaitForAlive (thread1
, "wait1");
220 TestUtil
.WaitForAlive (thread2
, "wait2");
222 #if MONO_FEATURE_THREAD_ABORT
225 thread1
.Interrupt ();
227 TestUtil
.WaitForNotAlive (thread1
, "wait3");
229 #if MONO_FEATURE_THREAD_ABORT
232 thread2
.Interrupt ();
234 TestUtil
.WaitForNotAlive (thread2
, "wait4");
238 public void TestMethod2()
246 public void TestCtor1()
248 C1Test test1
= new C1Test();
249 Thread t
= new Thread (new ThreadStart (test1
.TestMethod
));
251 Assert
.IsTrue (t
.CurrentCulture
.IsReadOnly
, "CurrentCulture.IsReadOnly");
252 Assert
.IsFalse (t
.IsAlive
, "IsAlive");
253 Assert
.IsFalse (t
.IsBackground
, "IsBackground");
254 Assert
.IsNull (t
.Name
, "Name");
255 Assert
.AreEqual (ThreadState
.Unstarted
, t
.ThreadState
, "ThreadState");
259 [Category ("NotWorking")] // we're not sharing (read-only) CultureInfo
260 public void CultureInfo_Shared_Across_Threads ()
262 Thread t
= new Thread (TestCtor1
);
263 Assert
.AreSame (t
.CurrentCulture
, t
.CurrentUICulture
, "Culture");
265 Assert
.AreSame (t
.CurrentCulture
, CultureInfo
.CurrentCulture
, "CultureInfo.CurrentCulture");
266 Assert
.AreSame (t
.CurrentUICulture
, CultureInfo
.CurrentUICulture
, "CultureInfo.CurrentUICulture");
268 Assert
.AreSame (t
.CurrentCulture
, Thread
.CurrentThread
.CurrentCulture
, "Thread.CurrentThread.CurrentCulture");
269 Assert
.AreSame (t
.CurrentUICulture
, Thread
.CurrentThread
.CurrentUICulture
, "Thread.CurrentThread.CurrentUICulture");
272 [Test
] // bug #325566
273 public void GetHashCodeTest ()
275 C1Test test1
= new C1Test ();
276 Thread tA
= new Thread (new ThreadStart (test1
.TestMethod
));
277 int hA1
= tA
.GetHashCode ();
278 Assert
.IsTrue (hA1
> 0, "#A1");
280 int hA2
= tA
.GetHashCode ();
281 Assert
.AreEqual (hA1
, hA2
, "#A2");
283 int hA3
= tA
.GetHashCode ();
284 Assert
.AreEqual (hA1
, hA3
, "#A3");
285 Assert
.AreEqual (hA1
, tA
.ManagedThreadId
, "#A4");
287 test1
= new C1Test ();
288 Thread tB
= new Thread (new ThreadStart (test1
.TestMethod
));
289 int hB1
= tB
.GetHashCode ();
290 Assert
.IsTrue (hB1
> 0, "#B1");
292 int hB2
= tB
.GetHashCode ();
293 Assert
.AreEqual (hB1
, hB2
, "#B2");
295 int hB3
= tB
.GetHashCode ();
296 Assert
.AreEqual (hB1
, hB3
, "#B3");
297 Assert
.AreEqual (hB1
, tB
.ManagedThreadId
, "#B4");
298 Assert
.IsFalse (hA2
== hB2
, "#B5");
302 public void ManagedThreadId ()
304 C1Test test1
= new C1Test ();
305 Thread t1
= new Thread (new ThreadStart (test1
.TestMethod
));
306 int mtA1
= t1
.ManagedThreadId
;
308 int mtA2
= t1
.ManagedThreadId
;
310 int mtA3
= t1
.ManagedThreadId
;
311 Assert
.AreEqual (mtA1
, mtA2
, "#A1");
312 Assert
.AreEqual (mtA2
, mtA3
, "#A2");
314 test1
= new C1Test ();
315 Thread t2
= new Thread (new ThreadStart (test1
.TestMethod
));
316 int mtB1
= t2
.ManagedThreadId
;
318 int mtB2
= t2
.ManagedThreadId
;
320 int mtB3
= t2
.ManagedThreadId
;
321 Assert
.AreEqual (mtB1
, mtB2
, "#B1");
322 Assert
.AreEqual (mtB2
, mtB3
, "#B2");
323 Assert
.IsFalse (mtB1
== mtA1
, "#B3");
327 [Category ("NotDotNet")] // it hangs.
328 public void TestStart()
330 if (is_win32
&& is_mono
)
331 Assert
.Fail ("This test fails on Win32. The test should be fixed.");
333 C1Test test1
= new C1Test();
334 Thread TestThread
= new Thread(new ThreadStart(test1
.TestMethod
));
337 Assert
.AreEqual (10, test1
.cnt
, "#1");
340 C2Test test1
= new C2Test();
341 Thread TestThread
= new Thread(new ThreadStart(test1
.TestMethod
));
343 #if MONO_FEATURE_THREAD_ABORT
346 TestThread
.Interrupt ();
351 } catch (ThreadStateException
) {
355 C2Test test1
= new C2Test();
356 Thread TestThread
= new Thread(new ThreadStart(test1
.TestMethod
));
360 bool started
= (TestThread
.ThreadState
== ThreadState
.Running
);
361 Assert
.AreEqual (started
, test1
.run
, "#15 Thread Is not in the correct state: ");
362 #if MONO_FEATURE_THREAD_ABORT
365 TestThread
.Interrupt ();
371 public void TestApartmentState ()
373 if (is_win32
&& is_mono
)
374 Assert
.Fail ("This test fails on mono on win32. Our runtime should be fixed.");
376 C2Test test1
= new C2Test();
377 Thread TestThread
= new Thread(new ThreadStart(test1
.TestMethod
));
378 Assert
.AreEqual (ApartmentState
.Unknown
, TestThread
.ApartmentState
, "#1");
380 TestUtil
.WaitForAlive (TestThread
, "wait5");
381 Assert
.AreEqual (ApartmentState
.MTA
, TestThread
.ApartmentState
, "#2");
382 #if MONO_FEATURE_THREAD_ABORT
385 TestThread
.Interrupt ();
390 public void TestPriority1()
392 if (is_win32
&& is_mono
)
393 Assert
.Fail ("This test fails on mono on Win32. Our runtime should be fixed.");
395 C2Test test1
= new C2Test();
396 Thread TestThread
= new Thread(new ThreadStart(test1
.TestMethod
));
398 TestThread
.Priority
=ThreadPriority
.BelowNormal
;
399 ThreadPriority after
= TestThread
.Priority
;
401 TestUtil
.WaitForAlive (TestThread
, "wait7");
402 ThreadPriority before
= TestThread
.Priority
;
403 Assert
.AreEqual (before
, after
, "#41 Unexpected Priority Change: ");
405 #if MONO_FEATURE_THREAD_ABORT
408 TestThread
.Interrupt ();
413 #if MONO_FEATURE_THREAD_ABORT
415 [Category ("NotDotNet")] // on MS, Thread is still in AbortRequested state when Start is invoked
416 public void AbortUnstarted ()
418 C2Test test1
= new C2Test();
419 Thread th
= new Thread (new ThreadStart (test1
.TestMethod
));
426 [Category ("NotDotNet")] // on MS, ThreadState is immediately Stopped after Abort
427 [Category ("NotWorking")] // this is a MonoTODO -> no support for Priority
428 public void TestPriority2()
430 C2Test test1
= new C2Test();
431 Thread TestThread
= new Thread(new ThreadStart(test1
.TestMethod
));
433 Assert
.AreEqual (ThreadPriority
.Normal
, TestThread
.Priority
, "#42 Incorrect Priority in New thread: ");
435 TestUtil
.WaitForAliveOrStop (TestThread
, "wait8");
436 Assert
.AreEqual (ThreadPriority
.Normal
, TestThread
.Priority
, "#43 Incorrect Priority in Started thread: ");
438 #if MONO_FEATURE_THREAD_ABORT
441 TestThread
.Interrupt ();
444 Assert
.AreEqual (ThreadPriority
.Normal
, TestThread
.Priority
, "#44 Incorrect Priority in Aborted thread: ");
448 [Category ("NotWorking")] // this is a MonoTODO -> no support for Priority
449 public void TestPriority3()
451 C2Test test1
= new C2Test();
452 Thread TestThread
= new Thread(new ThreadStart(test1
.TestMethod
));
455 TestThread
.Priority
= ThreadPriority
.Lowest
;
456 Assert
.AreEqual (ThreadPriority
.Lowest
, TestThread
.Priority
, "#45A Incorrect Priority:");
457 TestThread
.Priority
= ThreadPriority
.BelowNormal
;
458 Assert
.AreEqual (ThreadPriority
.BelowNormal
, TestThread
.Priority
, "#45B Incorrect Priority:");
459 TestThread
.Priority
= ThreadPriority
.Normal
;
460 Assert
.AreEqual (ThreadPriority
.Normal
, TestThread
.Priority
, "#45C Incorrect Priority:");
461 TestThread
.Priority
= ThreadPriority
.AboveNormal
;
462 Assert
.AreEqual (ThreadPriority
.AboveNormal
, TestThread
.Priority
, "#45D Incorrect Priority:");
463 TestThread
.Priority
= ThreadPriority
.Highest
;
464 Assert
.AreEqual (ThreadPriority
.Highest
, TestThread
.Priority
, "#45E Incorrect Priority:");
467 #if MONO_FEATURE_THREAD_ABORT
470 TestThread
.Interrupt ();
476 public void TestUndivisibleByPageSizeMaxStackSize ()
478 const int undivisible_stacksize
= 1048573;
480 var thread
= new Thread (new ThreadStart (delegate {}), undivisible_stacksize
);
486 public void TestIsBackground1 ()
488 if (is_win32
&& is_mono
)
489 Assert
.Fail ("This test fails on mono on Win32. Our runtime should be fixed.");
491 C2Test test1
= new C2Test();
492 Thread TestThread
= new Thread(new ThreadStart(test1
.TestMethod
));
495 TestUtil
.WaitForAlive (TestThread
, "wait9");
496 bool state
= TestThread
.IsBackground
;
497 Assert
.IsFalse (state
, "#51 IsBackground not set at the default state: ");
499 #if MONO_FEATURE_THREAD_ABORT
502 TestThread
.Interrupt ();
508 public void TestIsBackground2 ()
510 C2Test test1
= new C2Test();
511 Thread TestThread
= new Thread(new ThreadStart(test1
.TestMethod
));
512 TestThread
.IsBackground
= true;
516 #if MONO_FEATURE_THREAD_ABORT
519 TestThread
.Interrupt ();
523 if (TestThread
.IsAlive
) {
525 Assert
.IsTrue (TestThread
.IsBackground
, "#52 Is Background Changed to Start ");
526 } catch (ThreadStateException
) {
527 // Ignore if thread died meantime
533 public void TestName()
535 if (is_win32
&& is_mono
)
536 Assert
.Fail ("This test fails on mono on Win32. Our runtime should be fixed.");
538 C2Test test1
= new C2Test();
539 Thread TestThread
= new Thread(new ThreadStart(test1
.TestMethod
));
542 TestUtil
.WaitForAlive (TestThread
, "wait10");
543 string name
= TestThread
.Name
;
544 Assert
.IsNull (name
, "#61 Name set when mustn't be set: ");
545 string newname
= "Testing....";
546 TestThread
.Name
= newname
;
547 Assert
.AreEqual (newname
, TestThread
.Name
, "#62 Name not set when must be set: ");
549 #if MONO_FEATURE_THREAD_ABORT
552 TestThread
.Interrupt ();
560 Thread t
= new Thread (new ThreadStart (Name
));
561 Assert
.IsNull (t
.Name
, "Name-1");
563 Assert
.IsNull (t
.Name
, "Name-2");
567 [ExpectedException (typeof (InvalidOperationException
))]
568 public void Rename ()
570 Thread t
= new Thread (new ThreadStart (Rename
));
575 bool rename_finished
;
579 public void RenameTpThread ()
581 object monitor
= new object ();
582 ThreadPool
.QueueUserWorkItem (new WaitCallback (Rename_callback
), monitor
);
584 if (!rename_finished
)
585 Monitor
.Wait (monitor
);
587 Assert
.IsTrue (!rename_failed
);
590 void Rename_callback (object o
) {
591 Thread
.CurrentThread
.Name
= "a";
593 Thread
.CurrentThread
.Name
= "b";
594 //Console.WriteLine ("Thread name is: {0}", Thread.CurrentThread.Name);
595 } catch (Exception e
) {
596 //Console.Error.WriteLine (e);
597 rename_failed
= true;
601 rename_finished
= true;
602 Monitor
.Pulse (monitor
);
607 public void TestNestedThreads1()
609 C3Test test1
= new C3Test();
610 Thread TestThread
= new Thread(new ThreadStart(test1
.TestMethod1
));
613 TestUtil
.WaitForAlive (TestThread
, "wait11");
615 #if MONO_FEATURE_THREAD_ABORT
618 TestThread
.Interrupt ();
624 public void TestNestedThreads2()
626 C4Test test1
= new C4Test();
627 Thread TestThread
= new Thread(new ThreadStart(test1
.TestMethod1
));
631 #if MONO_FEATURE_THREAD_ABORT
634 TestThread
.Interrupt ();
640 public void TestJoin1()
642 C1Test test1
= new C1Test();
643 C1Test test2
= new C1Test();
644 Thread thread1
= new Thread(new ThreadStart(test1
.TestMethod
));
645 Thread thread2
= new Thread(new ThreadStart(test1
.TestMethod2
));
651 #if MONO_FEATURE_THREAD_ABORT
655 thread1
.Interrupt ();
656 thread2
.Interrupt ();
662 [ExpectedException (typeof (ArgumentOutOfRangeException
))]
663 public void Join_Int32_Negative ()
665 // -1 is Timeout.Infinite
666 Thread
.CurrentThread
.Join (-2);
670 [ExpectedException (typeof (ArgumentOutOfRangeException
))]
671 public void Join_TimeSpan_Negative ()
673 Thread
.CurrentThread
.Join (Negative
);
677 [ExpectedException (typeof (ArgumentOutOfRangeException
))]
678 public void Join_TimeSpan_TooLarge ()
680 Thread
.CurrentThread
.Join (TooLarge
);
684 public void Join_TimeSpan_SmallNegative ()
686 Thread
.CurrentThread
.Join (SmallNegative
);
690 [ExpectedException (typeof (ArgumentOutOfRangeException
))]
691 public void Sleep_Int32_Negative ()
693 // -1 is Timeout.Infinite
698 public void Sleep_TimeSpan_SmallNegative ()
700 Thread
.Sleep (SmallNegative
);
704 [ExpectedException (typeof (ArgumentOutOfRangeException
))]
705 public void Sleep_TimeSpan_Negative ()
707 Thread
.Sleep (Negative
);
711 [ExpectedException (typeof (ArgumentOutOfRangeException
))]
712 public void Sleep_TimeSpan_TooLarge ()
714 Thread
.Sleep (TooLarge
);
718 public void SpinWait ()
720 // no exception for negative numbers
721 Thread
.SpinWait (Int32
.MinValue
);
726 public void TestThreadState ()
728 if (is_win32
&& is_mono
)
729 Assert
.Fail ("This test fails on mono on Win32. Our runtime should be fixed.");
731 //TODO: Test The rest of the possible transitions
732 C2Test test1
= new C2Test();
733 Thread TestThread
= new Thread(new ThreadStart(test1
.TestMethod
));
734 Assert
.AreEqual (ThreadState
.Unstarted
, TestThread
.ThreadState
, "#101 Wrong Thread State");
737 //while(!TestThread.IsAlive); //In the MS Documentation this is not necessary
738 //but in the MS SDK it is
739 Assert
.IsTrue (TestThread
.ThreadState
== ThreadState
.Running
|| (TestThread
.ThreadState
& ThreadState
.Unstarted
) != 0,
740 "#102 Wrong Thread State: " + TestThread
.ThreadState
.ToString ());
742 #if MONO_FEATURE_THREAD_ABORT
745 TestThread
.Interrupt ();
749 TestUtil
.WaitForNotAlive (TestThread
, "wait12");
750 // Docs say state will be Stopped, but Aborted happens sometimes (?)
751 Assert
.IsTrue ((ThreadState
.Stopped
& TestThread
.ThreadState
) != 0 || (ThreadState
.Aborted
& TestThread
.ThreadState
) != 0,
752 "#103 Wrong Thread State: " + TestThread
.ThreadState
.ToString ());
756 [Ignore ("see comment below.")]
757 public void CurrentPrincipal_PrincipalPolicy_NoPrincipal ()
759 // note: switching from PrincipalPolicy won't work inside the same thread
760 // because as soon as a Principal object is created the Policy doesn't matter anymore
761 Thread t
= new Thread (new ThreadStart (ThreadedPrincipalTest
.NoPrincipal
));
766 #if MONO_FEATURE_THREAD_ABORT
775 [Ignore ("see comment below.")]
776 public void CurrentPrincipal_PrincipalPolicy_UnauthenticatedPrincipal ()
778 // note: switching from PrincipalPolicy won't work inside the same thread
779 // because as soon as a Principal object is created the Policy doesn't matter anymore
780 Thread t
= new Thread (new ThreadStart (ThreadedPrincipalTest
.UnauthenticatedPrincipal
));
785 #if MONO_FEATURE_THREAD_ABORT
794 public void CurrentPrincipal_PrincipalPolicy_WindowsPrincipal ()
796 // note: switching from PrincipalPolicy won't work inside the same thread
797 // because as soon as a Principal object is created the Policy doesn't matter anymore
798 Thread t
= new Thread (new ThreadStart (ThreadedPrincipalTest
.WindowsPrincipal
));
803 #if MONO_FEATURE_THREAD_ABORT
812 public void IPrincipal_CopyOnNewThread ()
814 Thread
.CurrentPrincipal
= new GenericPrincipal (new GenericIdentity ("bad"), null);
815 Thread t
= new Thread (new ThreadStart (ThreadedPrincipalTest
.CopyOnNewThread
));
817 Thread
.CurrentPrincipal
= new GenericPrincipal (new GenericIdentity ("good"), null);
821 #if MONO_FEATURE_THREAD_ABORT
831 #if MONO_FEATURE_THREAD_SUSPEND_RESUME
833 public void TestSuspend ()
835 Thread t
= new Thread (new ThreadStart (DoCount
));
836 t
.IsBackground
= true;
839 CheckIsRunning ("t1", t
);
842 WaitSuspended ("t2", t
);
844 CheckIsNotRunning ("t3", t
);
847 WaitResumed ("t4", t
);
849 CheckIsRunning ("t5", t
);
852 TestUtil
.WaitForNotAlive (t
, "wait13");
853 CheckIsNotRunning ("t6", t
);
857 #if MONO_FEATURE_THREAD_SUSPEND_RESUME && MONO_FEATURE_THREAD_ABORT
859 [Category("NotDotNet")] // On MS, ThreadStateException is thrown on Abort: "Thread is suspended; attempting to abort"
860 public void TestSuspendAbort ()
862 if (is_win32
&& is_mono
)
863 Assert
.Fail ("This test fails on Win32. The test should be fixed.");
865 Thread t
= new Thread (new ThreadStart (DoCount
));
866 t
.IsBackground
= true;
869 CheckIsRunning ("t1", t
);
872 WaitSuspended ("t2", t
);
874 CheckIsNotRunning ("t3", t
);
879 while (t
.IsAlive
&& n
< 200) {
884 Assert
.IsTrue (n
< 200, "Timeout while waiting for abort");
886 CheckIsNotRunning ("t6", t
);
891 public void Test_Interrupt ()
893 ManualResetEvent mre
= new ManualResetEvent (false);
894 bool interruptedExceptionThrown
= false;
896 ThreadPool
.QueueUserWorkItem (Test_Interrupt_Worker
, Thread
.CurrentThread
);
904 } catch (ThreadInterruptedException
) {
905 Assert
.Fail ("ThreadInterruptedException thrown twice");
908 } catch (ThreadInterruptedException
) {
909 interruptedExceptionThrown
= true;
912 Assert
.IsTrue (interruptedExceptionThrown
, "ThreadInterruptedException expected.");
916 [ExpectedException (typeof (ArgumentNullException
))]
917 public void TestQueueUserWorkItemNullCallback ()
919 ThreadPool
.QueueUserWorkItem (null, null);
922 private void Test_Interrupt_Worker (object o
)
924 Thread t
= o
as Thread
;
930 [Category ("NotDotNet")] // it crashes nunit.
931 public void Test_InterruptCurrentThread ()
933 ManualResetEvent mre
= new ManualResetEvent (false);
934 bool interruptedExceptionThrown
= false;
936 Thread
.CurrentThread
.Interrupt ();
940 } catch (ThreadInterruptedException
) {
945 public void GetNamedDataSlotTest ()
947 Assert
.IsNotNull (Thread
.GetNamedDataSlot ("te#st"), "#1");
948 Assert
.AreSame (Thread
.GetNamedDataSlot ("te#st"), Thread
.GetNamedDataSlot ("te#st"), "#2");
951 class DomainClass
: MarshalByRefObject
{
956 m_thread
= new Thread(ThreadProc
);
957 m_thread
.Start(Thread
.CurrentThread
);
962 public void ThreadProc (object arg
) {
963 success
= m_thread
== Thread
.CurrentThread
;
967 #if MONO_FEATURE_MULTIPLE_APPDOMAINS
969 public void CurrentThread_Domains ()
971 AppDomain ad
= AppDomain
.CreateDomain ("foo");
972 ad
.Load (typeof (DomainClass
).Assembly
.GetName ());
973 var o
= (DomainClass
)ad
.CreateInstanceAndUnwrap (typeof (DomainClass
).Assembly
.FullName
, typeof (DomainClass
).FullName
);
974 Assert
.IsTrue (o
.Run ());
975 AppDomain
.Unload (ad
);
977 #endif // MONO_FEATURE_MULTIPLE_APPDOMAINS
979 void CheckIsRunning (string s
, Thread t
)
983 Assert
.IsTrue (counter
> c
, s
);
986 void CheckIsNotRunning (string s
, Thread t
)
990 Assert
.AreEqual (counter
, c
, s
);
993 void WaitSuspended (string s
, Thread t
)
996 ThreadState state
= t
.ThreadState
;
997 while ((state
& ThreadState
.Suspended
) == 0) {
998 Assert
.IsTrue ((state
& ThreadState
.SuspendRequested
) != 0, s
+ ": expected SuspendRequested state");
1001 Assert
.IsTrue (n
< 100, s
+ ": failed to suspend");
1002 state
= t
.ThreadState
;
1004 Assert
.IsTrue ((state
& ThreadState
.SuspendRequested
) == 0, s
+ ": SuspendRequested state not expected");
1007 void WaitResumed (string s
, Thread t
)
1010 while ((t
.ThreadState
& ThreadState
.Suspended
) != 0) {
1013 Assert
.IsTrue (n
< 100, s
+ ": failed to resume");
1017 public void DoCount ()
1027 public class ThreadStateTest
{
1032 [Test
] // bug #81720
1033 public void IsBackGround ()
1035 Thread t1
= new Thread (new ThreadStart (Start
));
1036 Assert
.AreEqual (ThreadState
.Unstarted
, t1
.ThreadState
, "#A1");
1037 Assert
.IsFalse (t1
.IsBackground
, "#A2");
1040 Assert
.AreEqual (ThreadState
.Stopped
, t1
.ThreadState
, "#A3");
1043 bool isBackGround
= t1
.IsBackground
;
1044 Assert
.Fail ("#A4: " + isBackGround
.ToString ());
1045 } catch (ThreadStateException ex
) {
1046 Assert
.AreEqual (typeof (ThreadStateException
), ex
.GetType (), "#A5");
1047 Assert
.IsNull (ex
.InnerException
, "#A6");
1048 Assert
.IsNotNull (ex
.Message
, "#A7");
1051 Thread t2
= new Thread (new ThreadStart (Start
));
1052 Assert
.AreEqual (ThreadState
.Unstarted
, t2
.ThreadState
, "#B1");
1053 t2
.IsBackground
= true;
1054 Assert
.AreEqual (ThreadState
.Unstarted
| ThreadState
.Background
, t2
.ThreadState
, "#B2");
1055 Assert
.IsTrue (t2
.IsBackground
, "#B3");
1058 Assert
.AreEqual (ThreadState
.Stopped
, t2
.ThreadState
, "#B4");
1061 bool isBackGround
= t2
.IsBackground
;
1062 Assert
.Fail ("#B5: " + isBackGround
.ToString ());
1063 } catch (ThreadStateException ex
) {
1064 Assert
.AreEqual (typeof (ThreadStateException
), ex
.GetType (), "#B6");
1065 Assert
.IsNull (ex
.InnerException
, "#B7");
1066 Assert
.IsNotNull (ex
.Message
, "#B8");
1073 public class ThreadTest_ManagedThreadId
1077 MBRO mbro
= new MBRO ();
1079 class MBRO
: MarshalByRefObject
{
1083 public string ad_a1
;
1084 public string ad_b1
;
1085 public string ad_b2
;
1086 public string message
;
1090 public void ManagedThreadId_AppDomains ()
1092 AppDomain currentDomain
= AppDomain
.CurrentDomain
;
1093 ad1
= AppDomain
.CreateDomain ("AppDomain 1", currentDomain
.Evidence
, currentDomain
.SetupInformation
);
1094 ad2
= AppDomain
.CreateDomain ("AppDomain 2", currentDomain
.Evidence
, currentDomain
.SetupInformation
);
1096 Thread a
= new Thread (ThreadA
);
1097 Thread b
= new Thread (ThreadB
);
1098 // execute on AppDomain 1 thread A
1099 // execute on AppDomain 2 thread B
1100 // execute on AppDomain 1 thread B - must have same ManagedThreadId as Ad 2 on thread B
1106 AppDomain
.Unload (ad1
);
1107 AppDomain
.Unload (ad2
);
1109 if (mbro
.message
!= null)
1110 Assert
.Fail (mbro
.message
);
1112 // 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);
1114 Assert
.AreEqual ("AppDomain 1", mbro
.ad_a1
, "Name #1");
1115 Assert
.AreEqual ("AppDomain 1", mbro
.ad_b1
, "Name #2");
1116 Assert
.AreEqual ("AppDomain 2", mbro
.ad_b2
, "Name #3");
1118 Assert
.AreNotEqual (mbro
.id_a1
, mbro
.id_b1
, "Id #1");
1119 Assert
.AreNotEqual (mbro
.id_a1
, mbro
.id_b2
, "Id #2");
1120 Assert
.AreEqual (mbro
.id_b1
, mbro
.id_b2
, "Id #3");
1122 Assert
.AreNotEqual (mbro
.id_a1
, Thread
.CurrentThread
.ManagedThreadId
, "Id #4");
1123 Assert
.AreNotEqual (mbro
.id_b1
, Thread
.CurrentThread
.ManagedThreadId
, "Id #5");
1124 Assert
.AreNotEqual (mbro
.id_b2
, Thread
.CurrentThread
.ManagedThreadId
, "Id #6");
1125 Assert
.AreNotEqual (mbro
.ad_a1
, AppDomain
.CurrentDomain
.FriendlyName
, "Name #4");
1126 Assert
.AreNotEqual (mbro
.ad_b1
, AppDomain
.CurrentDomain
.FriendlyName
, "Name #5");
1127 Assert
.AreNotEqual (mbro
.ad_b2
, AppDomain
.CurrentDomain
.FriendlyName
, "Name #6");
1132 mbro
.id_a1
= Thread
.CurrentThread
.ManagedThreadId
;
1133 mbro
.ad_a1
= AppDomain
.CurrentDomain
.FriendlyName
;
1138 mbro
.id_b2
= Thread
.CurrentThread
.ManagedThreadId
;
1139 mbro
.ad_b2
= AppDomain
.CurrentDomain
.FriendlyName
;
1144 mbro
.id_b1
= Thread
.CurrentThread
.ManagedThreadId
;
1145 mbro
.ad_b1
= AppDomain
.CurrentDomain
.FriendlyName
;
1148 void ThreadA (object obj
)
1150 // Console.WriteLine ("ThreadA");
1152 ad1
.DoCallBack (A1
);
1153 } catch (Exception ex
) {
1154 mbro
.message
= string.Format ("ThreadA exception: {0}", ex
);
1156 // Console.WriteLine ("ThreadA Done");
1159 void ThreadB (object obj
)
1161 // Console.WriteLine ("ThreadB");
1163 ad2
.DoCallBack (B2
);
1164 ad1
.DoCallBack (B1
);
1165 } catch (Exception ex
) {
1166 mbro
.message
= string.Format ("ThreadB exception: {0}", ex
);
1168 // Console.WriteLine ("ThreadB Done");
1173 public class ThreadApartmentTest
1179 [Test
] // bug #81658
1180 public void ApartmentState_StoppedThread ()
1182 Thread t1
= new Thread (new ThreadStart (Start
));
1186 ApartmentState state
= t1
.ApartmentState
;
1187 Assert
.Fail ("#A1: " + state
.ToString ());
1188 } catch (ThreadStateException ex
) {
1189 Assert
.AreEqual (typeof (ThreadStateException
), ex
.GetType (), "#A2");
1190 Assert
.IsNull (ex
.InnerException
, "#A3");
1191 Assert
.IsNotNull (ex
.Message
, "#A4");
1194 Thread t2
= new Thread (new ThreadStart (Start
));
1195 t2
.IsBackground
= true;
1199 ApartmentState state
= t2
.ApartmentState
;
1200 Assert
.Fail ("#B1: " + state
.ToString ());
1201 } catch (ThreadStateException ex
) {
1202 Assert
.AreEqual (typeof (ThreadStateException
), ex
.GetType (), "#B2");
1203 Assert
.IsNull (ex
.InnerException
, "#B3");
1204 Assert
.IsNotNull (ex
.Message
, "#B4");
1209 public void ApartmentState_BackGround ()
1211 Thread t1
= new Thread (new ThreadStart (Start
));
1212 t1
.IsBackground
= true;
1213 Assert
.AreEqual (ApartmentState
.Unknown
, t1
.ApartmentState
, "#1");
1214 t1
.ApartmentState
= ApartmentState
.STA
;
1215 Assert
.AreEqual (ApartmentState
.STA
, t1
.ApartmentState
, "#2");
1219 public void TestApartmentState ()
1221 Thread t1
= new Thread (new ThreadStart (Start
));
1222 Thread t2
= new Thread (new ThreadStart (Start
));
1223 Thread t3
= new Thread (new ThreadStart (Start
));
1225 Assert
.AreEqual (ApartmentState
.Unknown
, t1
.ApartmentState
, "Thread1 Default");
1226 Assert
.AreEqual (ApartmentState
.Unknown
, t2
.ApartmentState
, "Thread2 Default");
1227 Assert
.AreEqual (ApartmentState
.Unknown
, t3
.ApartmentState
, "Thread3 Default");
1229 t1
.ApartmentState
= ApartmentState
.STA
;
1230 Assert
.AreEqual (ApartmentState
.STA
, t1
.ApartmentState
, "Thread1 Set Once");
1231 t1
.ApartmentState
= ApartmentState
.MTA
;
1232 Assert
.AreEqual (ApartmentState
.STA
, t1
.ApartmentState
, "Thread1 Set Twice");
1234 t2
.ApartmentState
= ApartmentState
.MTA
;
1235 Assert
.AreEqual (ApartmentState
.MTA
, t2
.ApartmentState
, "Thread2 Set Once");
1236 t2
.ApartmentState
= ApartmentState
.STA
;
1237 Assert
.AreEqual (ApartmentState
.MTA
, t2
.ApartmentState
, "Thread2 Set Twice");
1239 bool exception_occured
= false;
1241 t3
.ApartmentState
= ApartmentState
.Unknown
;
1244 exception_occured
= true;
1246 Assert
.AreEqual (ApartmentState
.Unknown
, t3
.ApartmentState
, "Thread3 Set Invalid");
1247 Assert
.IsFalse (exception_occured
, "Thread3 Set Invalid Exception Occured");
1250 exception_occured
= false;
1252 t1
.ApartmentState
= ApartmentState
.STA
;
1255 exception_occured
= true;
1257 Assert
.IsTrue (exception_occured
, "Thread1 Started Invalid Exception Occured");
1261 public void TestSetApartmentStateSameState ()
1263 Thread t1
= new Thread (new ThreadStart (Start
));
1264 t1
.SetApartmentState (ApartmentState
.STA
);
1265 Assert
.AreEqual (ApartmentState
.STA
, t1
.ApartmentState
, "Thread1 Set Once");
1267 t1
.SetApartmentState (ApartmentState
.STA
);
1268 Assert
.AreEqual (ApartmentState
.STA
, t1
.ApartmentState
, "Thread1 Set twice");
1272 [ExpectedException(typeof(InvalidOperationException
))]
1273 public void TestSetApartmentStateDiffState ()
1275 Thread t1
= new Thread (new ThreadStart (Start
));
1276 t1
.SetApartmentState (ApartmentState
.STA
);
1277 Assert
.AreEqual (ApartmentState
.STA
, t1
.ApartmentState
, "Thread1 Set Once");
1279 t1
.SetApartmentState (ApartmentState
.MTA
);
1283 public void TestTrySetApartmentState ()
1285 Thread t1
= new Thread (new ThreadStart (Start
));
1286 t1
.SetApartmentState (ApartmentState
.STA
);
1287 Assert
.AreEqual (ApartmentState
.STA
, t1
.ApartmentState
, "#1");
1289 bool result
= t1
.TrySetApartmentState (ApartmentState
.MTA
);
1290 Assert
.IsFalse (result
, "#2");
1292 result
= t1
.TrySetApartmentState (ApartmentState
.STA
);
1293 Assert
.IsTrue (result
, "#3");
1297 public void TestTrySetApartmentStateRunning ()
1299 Thread t1
= new Thread (new ThreadStart (Start
));
1300 t1
.SetApartmentState (ApartmentState
.STA
);
1301 Assert
.AreEqual (ApartmentState
.STA
, t1
.ApartmentState
, "#1");
1306 t1
.TrySetApartmentState (ApartmentState
.STA
);
1308 } catch (ThreadStateException
) {
1315 public void Volatile () {
1317 Thread
.VolatileWrite (ref v3
, double.MaxValue
);
1318 Assert
.AreEqual (v3
, double.MaxValue
);
1321 Thread
.VolatileWrite (ref v4
, float.MaxValue
);
1322 Assert
.AreEqual (v4
, float.MaxValue
);
1326 public void Culture ()
1328 Assert
.IsNotNull (Thread
.CurrentThread
.CurrentCulture
, "CurrentCulture");
1329 Assert
.IsNotNull (Thread
.CurrentThread
.CurrentUICulture
, "CurrentUICulture");
1333 public void ThreadStartSimple ()
1336 Thread t
= new Thread (delegate () {
1337 // ensure the NSAutoreleasePool works
1342 Assert
.AreEqual (1, i
, "ThreadStart");
1346 public void ParametrizedThreadStart ()
1350 Thread t
= new Thread (delegate (object obj
) {
1351 // ensure the NSAutoreleasePool works
1358 Assert
.AreEqual (1, i
, "ParametrizedThreadStart");
1359 Assert
.AreEqual (this, arg
, "obj");
1363 public void SetNameTpThread () {
1364 ThreadPool
.QueueUserWorkItem(new WaitCallback(ThreadProc
));
1367 static void ThreadProc(Object stateInfo
) {
1368 Thread
.CurrentThread
.Name
= "My Worker";
1372 public void GetStackTraces () {
1373 var m
= typeof (Thread
).GetMethod ("Mono_GetStackTraces", BindingFlags
.NonPublic
|BindingFlags
.Static
);
1375 var res
= (Dictionary
<Thread
,SD
.StackTrace
>)typeof (Thread
).GetMethod ("Mono_GetStackTraces", BindingFlags
.NonPublic
|BindingFlags
.Static
).Invoke (null, null);
1376 foreach (var t
in res
.Keys
) {
1377 var st
= res
[t
].ToString ();
1383 public class TestUtil
1385 public static void WaitForNotAlive (Thread t
, string s
)
1387 WhileAlive (t
, true, s
);
1390 public static void WaitForAlive (Thread t
, string s
)
1392 WhileAlive (t
, false, s
);
1395 public static bool WaitForAliveOrStop (Thread t
, string s
)
1397 return WhileAliveOrStop (t
, false, s
);
1400 public static void WhileAlive (Thread t
, bool alive
, string s
)
1402 DateTime ti
= DateTime
.Now
;
1403 while (t
.IsAlive
== alive
) {
1404 if ((DateTime
.Now
- ti
).TotalSeconds
> 10) {
1405 if (alive
) Assert
.Fail ("Timeout while waiting for not alive state. " + s
);
1406 else Assert
.Fail ("Timeout while waiting for alive state. " + s
);
1411 public static bool WhileAliveOrStop (Thread t
, bool alive
, string s
)
1413 DateTime ti
= DateTime
.Now
;
1414 while (t
.IsAlive
== alive
) {
1415 if (t
.ThreadState
== ThreadState
.Stopped
)
1418 if ((DateTime
.Now
- ti
).TotalSeconds
> 10) {
1419 if (alive
) Assert
.Fail ("Timeout while waiting for not alive state. " + s
);
1420 else Assert
.Fail ("Timeout while waiting for alive state. " + s
);