fix formatting issues
[mono-project.git] / mcs / class / corlib / Test / System.Threading / ThreadTest.cs
blob9fd2a5bbf68ecffa94b5badc7d43a77484397f8f
1 // ThreadTest.cs - NUnit Test Cases for the System.Threading.Thread class
2 //
3 // Authors
4 // Eduardo Garcia Cebollero (kiwnix@yahoo.es)
5 // Sebastien Pouliot <sebastien@ximian.com>
6 //
7 // (C) Eduardo Garcia Cebollero.
8 // (C) Ximian, Inc. http://www.ximian.com
9 // (C) 2004 Novell (http://www.novell.com)
12 using System;
13 using System.Globalization;
14 using System.Security.Principal;
15 using System.Threading;
16 using System.Threading.Tasks;
17 using System.Reflection;
18 using System.Collections.Generic;
19 using SD = System.Diagnostics;
21 using NUnit.Framework;
23 namespace MonoTests.System.Threading
25 // These tests seem to hang the 2.0 framework. So they are disabled for now
26 // Don't reenable them until you can run a few thousand times on an SMP box.
27 [Category ("NotWorking")]
28 public class ThreadedPrincipalTest
30 public static void NoPrincipal ()
32 AppDomain.CurrentDomain.SetPrincipalPolicy (PrincipalPolicy.NoPrincipal);
33 IPrincipal p = Thread.CurrentPrincipal;
34 Assert.IsNull (p, "#1");
36 Thread.CurrentPrincipal = new GenericPrincipal (new GenericIdentity ("mono"), null);
37 Assert.IsNotNull (Thread.CurrentPrincipal, "#2");
39 Thread.CurrentPrincipal = null;
40 Assert.IsNull (Thread.CurrentPrincipal, "#3");
41 // in this case we can return to null
44 public static void UnauthenticatedPrincipal ()
46 AppDomain.CurrentDomain.SetPrincipalPolicy (PrincipalPolicy.UnauthenticatedPrincipal);
47 IPrincipal p = Thread.CurrentPrincipal;
48 Assert.IsNotNull (p, "#1");
49 Assert.IsTrue ((p is GenericPrincipal), "#2");
50 Assert.AreEqual (String.Empty, p.Identity.Name, "#3");
51 Assert.AreEqual (String.Empty, p.Identity.AuthenticationType, "#4");
52 Assert.IsFalse (p.Identity.IsAuthenticated, "#5");
54 Thread.CurrentPrincipal = new GenericPrincipal (new GenericIdentity ("mono"), null);
55 Assert.IsNotNull (Thread.CurrentPrincipal, "#6");
57 Thread.CurrentPrincipal = null;
58 Assert.IsNotNull (Thread.CurrentPrincipal, "#7");
59 // in this case we can't return to null
62 public static void WindowsPrincipal ()
64 AppDomain.CurrentDomain.SetPrincipalPolicy (PrincipalPolicy.WindowsPrincipal);
65 IPrincipal p = Thread.CurrentPrincipal;
66 Assert.IsNotNull (p, "#1");
67 Assert.IsTrue ((p is WindowsPrincipal), "#2");
68 Assert.IsNotNull (p.Identity.Name, "#3");
69 Assert.IsNotNull (p.Identity.AuthenticationType, "#4");
70 Assert.IsTrue (p.Identity.IsAuthenticated, "#5");
72 // note: we can switch from a WindowsPrincipal to a GenericPrincipal
73 Thread.CurrentPrincipal = new GenericPrincipal (new GenericIdentity ("mono"), null);
74 Assert.IsNotNull (Thread.CurrentPrincipal, "#6");
76 Thread.CurrentPrincipal = null;
77 Assert.IsNotNull (Thread.CurrentPrincipal, "#7");
78 // 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");
88 [TestFixture]
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);
98 //Some Classes to test as threads
99 private class C1Test
101 public int cnt;
102 public Thread thread1;
103 public bool endm1;
104 public bool endm2;
106 public C1Test()
108 thread1 = (Thread)null;
109 this.cnt = 0;
110 endm1 = endm2 = false;
113 public void TestMethod()
115 while (cnt < 10)
117 cnt++;
119 endm1 = true;
121 public void TestMethod2()
123 if (!(thread1==(Thread)null) )
125 thread1.Join();
127 endm2 = true;
131 private class C2Test
133 public int cnt;
134 public bool run = false;
136 public C2Test()
138 this.cnt = 0;
141 public void TestMethod()
143 run = true;
144 while (true)
146 if (cnt < 1000)
147 cnt++;
148 else
149 cnt = 0;
154 private class C3Test
156 public C1Test sub_class;
157 public Thread sub_thread;
159 public C3Test()
161 sub_class = new C1Test();
162 sub_thread = new Thread(new ThreadStart(sub_class.TestMethod));
165 public void TestMethod1()
167 sub_thread.Start();
168 Thread.Sleep (100);
169 #if MONO_FEATURE_THREAD_ABORT
170 sub_thread.Abort();
171 #else
172 sub_thread.Interrupt ();
173 #endif
177 private class C4Test
179 public C1Test class1;
180 public C1Test class2;
181 public Thread thread1;
182 public Thread thread2;
183 public bool T1ON ;
184 public bool T2ON ;
186 public C4Test()
188 T1ON = false;
189 T2ON = false;
190 class1 = new C1Test();
191 class2 = new C1Test();
192 thread1 = new Thread(new ThreadStart(class1.TestMethod));
193 thread2 = new Thread(new ThreadStart(class2.TestMethod));
196 public void TestMethod1()
198 thread1.Start();
199 TestUtil.WaitForAlive (thread1, "wait1");
200 T1ON = true;
201 thread2.Start();
202 TestUtil.WaitForAlive (thread2, "wait2");
203 T2ON = true;
204 #if MONO_FEATURE_THREAD_ABORT
205 thread1.Abort();
206 #else
207 thread1.Interrupt ();
208 #endif
209 TestUtil.WaitForNotAlive (thread1, "wait3");
210 T1ON = false;
211 #if MONO_FEATURE_THREAD_ABORT
212 thread2.Abort();
213 #else
214 thread2.Interrupt ();
215 #endif
216 TestUtil.WaitForNotAlive (thread2, "wait4");
217 T2ON = false;
220 public void TestMethod2()
222 thread1.Start();
223 thread1.Join();
227 [Test]
228 public void TestCtor1()
230 C1Test test1 = new C1Test();
231 Thread t = new Thread (new ThreadStart (test1.TestMethod));
233 Assert.IsTrue (t.CurrentCulture.IsReadOnly, "CurrentCulture.IsReadOnly");
234 Assert.IsFalse (t.IsAlive, "IsAlive");
235 Assert.IsFalse (t.IsBackground, "IsBackground");
236 Assert.IsNull (t.Name, "Name");
237 Assert.AreEqual (ThreadState.Unstarted, t.ThreadState, "ThreadState");
240 [Test]
241 [Category ("NotWorking")] // we're not sharing (read-only) CultureInfo
242 public void CultureInfo_Shared_Across_Threads ()
244 Thread t = new Thread (TestCtor1);
245 Assert.AreSame (t.CurrentCulture, t.CurrentUICulture, "Culture");
247 Assert.AreSame (t.CurrentCulture, CultureInfo.CurrentCulture, "CultureInfo.CurrentCulture");
248 Assert.AreSame (t.CurrentUICulture, CultureInfo.CurrentUICulture, "CultureInfo.CurrentUICulture");
250 Assert.AreSame (t.CurrentCulture, Thread.CurrentThread.CurrentCulture, "Thread.CurrentThread.CurrentCulture");
251 Assert.AreSame (t.CurrentUICulture, Thread.CurrentThread.CurrentUICulture, "Thread.CurrentThread.CurrentUICulture");
254 [Test] // bug #325566
255 public void GetHashCodeTest ()
257 C1Test test1 = new C1Test ();
258 Thread tA = new Thread (new ThreadStart (test1.TestMethod));
259 int hA1 = tA.GetHashCode ();
260 Assert.IsTrue (hA1 > 0, "#A1");
261 tA.Start ();
262 int hA2 = tA.GetHashCode ();
263 Assert.AreEqual (hA1, hA2, "#A2");
264 tA.Join ();
265 int hA3 = tA.GetHashCode ();
266 Assert.AreEqual (hA1, hA3, "#A3");
267 Assert.AreEqual (hA1, tA.ManagedThreadId, "#A4");
269 test1 = new C1Test ();
270 Thread tB = new Thread (new ThreadStart (test1.TestMethod));
271 int hB1 = tB.GetHashCode ();
272 Assert.IsTrue (hB1 > 0, "#B1");
273 tB.Start ();
274 int hB2 = tB.GetHashCode ();
275 Assert.AreEqual (hB1, hB2, "#B2");
276 tB.Join ();
277 int hB3 = tB.GetHashCode ();
278 Assert.AreEqual (hB1, hB3, "#B3");
279 Assert.AreEqual (hB1, tB.ManagedThreadId, "#B4");
280 Assert.IsFalse (hA2 == hB2, "#B5");
283 [Test] // bug #82700
284 public void ManagedThreadId ()
286 C1Test test1 = new C1Test ();
287 Thread t1 = new Thread (new ThreadStart (test1.TestMethod));
288 int mtA1 = t1.ManagedThreadId;
289 t1.Start ();
290 int mtA2 = t1.ManagedThreadId;
291 t1.Join ();
292 int mtA3 = t1.ManagedThreadId;
293 Assert.AreEqual (mtA1, mtA2, "#A1");
294 Assert.AreEqual (mtA2, mtA3, "#A2");
296 test1 = new C1Test ();
297 Thread t2 = new Thread (new ThreadStart (test1.TestMethod));
298 int mtB1 = t2.ManagedThreadId;
299 t2.Start ();
300 int mtB2 = t2.ManagedThreadId;
301 t2.Join ();
302 int mtB3 = t2.ManagedThreadId;
303 Assert.AreEqual (mtB1, mtB2, "#B1");
304 Assert.AreEqual (mtB2, mtB3, "#B2");
305 Assert.IsFalse (mtB1 == mtA1, "#B3");
308 [Test]
309 [Category ("NotDotNet")] // it hangs.
310 public void TestStart()
313 C1Test test1 = new C1Test();
314 Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
315 TestThread.Start();
316 TestThread.Join();
317 Assert.AreEqual (10, test1.cnt, "#1");
320 C2Test test1 = new C2Test();
321 Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
322 TestThread.Start();
323 #if MONO_FEATURE_THREAD_ABORT
324 TestThread.Abort();
325 #else
326 TestThread.Interrupt ();
327 #endif
328 try {
329 TestThread.Start();
330 Assert.Fail ("#2");
331 } catch (ThreadStateException) {
335 C2Test test1 = new C2Test();
336 Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
337 TestThread.Start();
338 while (!test1.run) {
340 bool started = (TestThread.ThreadState == ThreadState.Running);
341 Assert.AreEqual (started, test1.run, "#15 Thread Is not in the correct state: ");
342 #if MONO_FEATURE_THREAD_ABORT
343 TestThread.Abort();
344 #else
345 TestThread.Interrupt ();
346 #endif
350 [Test]
351 public void TestApartmentState ()
353 C2Test test1 = new C2Test();
354 Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
355 Assert.AreEqual (ApartmentState.Unknown, TestThread.ApartmentState, "#1");
356 TestThread.Start();
357 TestUtil.WaitForAlive (TestThread, "wait5");
358 Assert.AreEqual (ApartmentState.MTA, TestThread.ApartmentState, "#2");
359 #if MONO_FEATURE_THREAD_ABORT
360 TestThread.Abort();
361 #else
362 TestThread.Interrupt ();
363 #endif
366 [Test]
367 [Category ("NotWorking")] // setting the priority of a Thread before it is started isn't implemented in Mono yet
368 public void TestPriority1()
370 C2Test test1 = new C2Test();
371 Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
372 try {
373 TestThread.Priority=ThreadPriority.BelowNormal;
374 ThreadPriority before = TestThread.Priority;
375 Assert.AreEqual (ThreadPriority.BelowNormal, before, "#40 Unexpected priority before thread start: ");
376 TestThread.Start();
377 TestUtil.WaitForAlive (TestThread, "wait7");
378 ThreadPriority after = TestThread.Priority;
379 Assert.AreEqual (before, after, "#41 Unexpected Priority Change: ");
380 } finally {
381 #if MONO_FEATURE_THREAD_ABORT
382 TestThread.Abort();
383 #else
384 TestThread.Interrupt ();
385 #endif
389 #if MONO_FEATURE_THREAD_ABORT
390 [Test]
391 [Category ("NotDotNet")] // on MS, Thread is still in AbortRequested state when Start is invoked
392 public void AbortUnstarted ()
394 C2Test test1 = new C2Test();
395 Thread th = new Thread (new ThreadStart (test1.TestMethod));
396 th.Abort ();
397 th.Start ();
399 #endif
401 [Test]
402 [Category ("NotDotNet")] // on MS, ThreadState is immediately Stopped after Abort
403 [Category ("NotWorking")] // this is a MonoTODO -> no support for Priority
404 public void TestPriority2()
406 C2Test test1 = new C2Test();
407 Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
408 try {
409 Assert.AreEqual (ThreadPriority.Normal, TestThread.Priority, "#42 Incorrect Priority in New thread: ");
410 TestThread.Start();
411 TestUtil.WaitForAliveOrStop (TestThread, "wait8");
412 Assert.AreEqual (ThreadPriority.Normal, TestThread.Priority, "#43 Incorrect Priority in Started thread: ");
413 } finally {
414 #if MONO_FEATURE_THREAD_ABORT
415 TestThread.Abort();
416 #else
417 TestThread.Interrupt ();
418 #endif
420 Assert.AreEqual (ThreadPriority.Normal, TestThread.Priority, "#44 Incorrect Priority in Aborted thread: ");
423 [Test]
424 [Category ("NotWorking")] // this is a MonoTODO -> no support for Priority
425 public void TestPriority3()
427 C2Test test1 = new C2Test();
428 Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
429 try {
430 TestThread.Start();
431 TestThread.Priority = ThreadPriority.Lowest;
432 Assert.AreEqual (ThreadPriority.Lowest, TestThread.Priority, "#45A Incorrect Priority:");
433 TestThread.Priority = ThreadPriority.BelowNormal;
434 Assert.AreEqual (ThreadPriority.BelowNormal, TestThread.Priority, "#45B Incorrect Priority:");
435 TestThread.Priority = ThreadPriority.Normal;
436 Assert.AreEqual (ThreadPriority.Normal, TestThread.Priority, "#45C Incorrect Priority:");
437 TestThread.Priority = ThreadPriority.AboveNormal;
438 Assert.AreEqual (ThreadPriority.AboveNormal, TestThread.Priority, "#45D Incorrect Priority:");
439 TestThread.Priority = ThreadPriority.Highest;
440 Assert.AreEqual (ThreadPriority.Highest, TestThread.Priority, "#45E Incorrect Priority:");
442 finally {
443 #if MONO_FEATURE_THREAD_ABORT
444 TestThread.Abort();
445 #else
446 TestThread.Interrupt ();
447 #endif
451 [Test]
452 public void TestUndivisibleByPageSizeMaxStackSize ()
454 const int undivisible_stacksize = 1048573;
456 var thread = new Thread (new ThreadStart (delegate {}), undivisible_stacksize);
457 thread.Start ();
458 thread.Join ();
461 [Test]
462 public void TestIsBackground1 ()
464 C2Test test1 = new C2Test();
465 Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
466 try {
467 TestThread.Start();
468 TestUtil.WaitForAlive (TestThread, "wait9");
469 bool state = TestThread.IsBackground;
470 Assert.IsFalse (state, "#51 IsBackground not set at the default state: ");
471 } finally {
472 #if MONO_FEATURE_THREAD_ABORT
473 TestThread.Abort();
474 #else
475 TestThread.Interrupt ();
476 #endif
480 [Test]
481 public void TestIsBackground2 ()
483 C2Test test1 = new C2Test();
484 Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
485 TestThread.IsBackground = true;
486 try {
487 TestThread.Start();
488 } finally {
489 #if MONO_FEATURE_THREAD_ABORT
490 TestThread.Abort();
491 #else
492 TestThread.Interrupt ();
493 #endif
496 if (TestThread.IsAlive) {
497 try {
498 Assert.IsTrue (TestThread.IsBackground, "#52 Is Background Changed to Start ");
499 } catch (ThreadStateException) {
500 // Ignore if thread died meantime
505 [Test]
506 public void TestName()
508 C2Test test1 = new C2Test();
509 Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
510 try {
511 TestThread.Start();
512 TestUtil.WaitForAlive (TestThread, "wait10");
513 string name = TestThread.Name;
514 Assert.IsNull (name, "#61 Name set when mustn't be set: ");
515 string newname = "Testing....";
516 TestThread.Name = newname;
517 Assert.AreEqual (newname, TestThread.Name, "#62 Name not set when must be set: ");
518 } finally {
519 #if MONO_FEATURE_THREAD_ABORT
520 TestThread.Abort();
521 #else
522 TestThread.Interrupt ();
523 #endif
527 [Test]
528 public void Name ()
530 Thread t = new Thread (new ThreadStart (Name));
531 Assert.IsNull (t.Name, "Name-1");
532 t.Name = null;
533 Assert.IsNull (t.Name, "Name-2");
536 [Test]
537 [ExpectedException (typeof (InvalidOperationException))]
538 public void Rename ()
540 Thread t = new Thread (new ThreadStart (Rename));
541 t.Name = "a";
542 t.Name = "b";
545 [Test]
546 public void TestNestedThreads1()
548 C3Test test1 = new C3Test();
549 Thread TestThread = new Thread(new ThreadStart(test1.TestMethod1));
550 try {
551 TestThread.Start();
552 TestUtil.WaitForAlive (TestThread, "wait11");
553 } finally {
554 #if MONO_FEATURE_THREAD_ABORT
555 TestThread.Abort();
556 #else
557 TestThread.Interrupt ();
558 #endif
562 [Test]
563 public void TestNestedThreads2()
565 C4Test test1 = new C4Test();
566 Thread TestThread = new Thread(new ThreadStart(test1.TestMethod1));
567 try {
568 TestThread.Start();
569 } finally {
570 #if MONO_FEATURE_THREAD_ABORT
571 TestThread.Abort();
572 #else
573 TestThread.Interrupt ();
574 #endif
578 [Test]
579 public void TestJoin1()
581 C1Test test1 = new C1Test();
582 C1Test test2 = new C1Test();
583 Thread thread1 = new Thread(new ThreadStart(test1.TestMethod));
584 Thread thread2 = new Thread(new ThreadStart(test1.TestMethod2));
585 try {
586 thread1.Start();
587 thread2.Start();
588 thread2.Join();
589 } finally {
590 #if MONO_FEATURE_THREAD_ABORT
591 thread1.Abort();
592 thread2.Abort();
593 #else
594 thread1.Interrupt ();
595 thread2.Interrupt ();
596 #endif
600 [Test]
601 [ExpectedException (typeof (ArgumentOutOfRangeException))]
602 public void Join_Int32_Negative ()
604 // -1 is Timeout.Infinite
605 Thread.CurrentThread.Join (-2);
608 [Test]
609 [ExpectedException (typeof (ArgumentOutOfRangeException))]
610 public void Join_TimeSpan_Negative ()
612 Thread.CurrentThread.Join (Negative);
615 [Test]
616 [ExpectedException (typeof (ArgumentOutOfRangeException))]
617 public void Join_TimeSpan_TooLarge ()
619 Thread.CurrentThread.Join (TooLarge);
622 [Test]
623 public void Join_TimeSpan_SmallNegative ()
625 Thread.CurrentThread.Join (SmallNegative);
628 [Test]
629 [ExpectedException (typeof (ArgumentOutOfRangeException))]
630 public void Sleep_Int32_Negative ()
632 // -1 is Timeout.Infinite
633 Thread.Sleep (-2);
636 [Test]
637 public void Sleep_TimeSpan_SmallNegative ()
639 Thread.Sleep (SmallNegative);
642 [Test]
643 [ExpectedException (typeof (ArgumentOutOfRangeException))]
644 public void Sleep_TimeSpan_Negative ()
646 Thread.Sleep (Negative);
649 [Test]
650 [ExpectedException (typeof (ArgumentOutOfRangeException))]
651 public void Sleep_TimeSpan_TooLarge ()
653 Thread.Sleep (TooLarge);
656 [Test]
657 public void SpinWait ()
659 // no exception for negative numbers
660 Thread.SpinWait (Int32.MinValue);
661 Thread.SpinWait (0);
664 [Test]
665 public void TestThreadState ()
667 //TODO: Test The rest of the possible transitions
668 C2Test test1 = new C2Test();
669 Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
670 Assert.AreEqual (ThreadState.Unstarted, TestThread.ThreadState, "#101 Wrong Thread State");
671 try {
672 TestThread.Start();
673 //while(!TestThread.IsAlive); //In the MS Documentation this is not necessary
674 //but in the MS SDK it is
675 Assert.IsTrue (TestThread.ThreadState == ThreadState.Running || (TestThread.ThreadState & ThreadState.Unstarted) != 0,
676 "#102 Wrong Thread State: " + TestThread.ThreadState.ToString ());
677 } finally {
678 #if MONO_FEATURE_THREAD_ABORT
679 TestThread.Abort();
680 #else
681 TestThread.Interrupt ();
682 #endif
685 TestUtil.WaitForNotAlive (TestThread, "wait12");
686 // Docs say state will be Stopped, but Aborted happens sometimes (?)
687 Assert.IsTrue ((ThreadState.Stopped & TestThread.ThreadState) != 0 || (ThreadState.Aborted & TestThread.ThreadState) != 0,
688 "#103 Wrong Thread State: " + TestThread.ThreadState.ToString ());
691 [Test]
692 [Ignore ("see comment below.")]
693 public void CurrentPrincipal_PrincipalPolicy_NoPrincipal ()
695 // note: switching from PrincipalPolicy won't work inside the same thread
696 // because as soon as a Principal object is created the Policy doesn't matter anymore
697 Thread t = new Thread (new ThreadStart (ThreadedPrincipalTest.NoPrincipal));
698 try {
699 t.Start ();
700 t.Join ();
701 } catch {
702 #if MONO_FEATURE_THREAD_ABORT
703 t.Abort ();
704 #else
705 t.Interrupt ();
706 #endif
710 [Test]
711 [Ignore ("see comment below.")]
712 public void CurrentPrincipal_PrincipalPolicy_UnauthenticatedPrincipal ()
714 // note: switching from PrincipalPolicy won't work inside the same thread
715 // because as soon as a Principal object is created the Policy doesn't matter anymore
716 Thread t = new Thread (new ThreadStart (ThreadedPrincipalTest.UnauthenticatedPrincipal));
717 try {
718 t.Start ();
719 t.Join ();
720 } catch {
721 #if MONO_FEATURE_THREAD_ABORT
722 t.Abort ();
723 #else
724 t.Interrupt ();
725 #endif
729 [Test]
730 public void CurrentPrincipal_PrincipalPolicy_WindowsPrincipal ()
732 // note: switching from PrincipalPolicy won't work inside the same thread
733 // because as soon as a Principal object is created the Policy doesn't matter anymore
734 Thread t = new Thread (new ThreadStart (ThreadedPrincipalTest.WindowsPrincipal));
735 try {
736 t.Start ();
737 t.Join ();
738 } catch {
739 #if MONO_FEATURE_THREAD_ABORT
740 t.Abort ();
741 #else
742 t.Interrupt ();
743 #endif
747 [Test]
748 public void IPrincipal_CopyOnNewThread ()
750 Thread.CurrentPrincipal = new GenericPrincipal (new GenericIdentity ("bad"), null);
751 Thread t = new Thread (new ThreadStart (ThreadedPrincipalTest.CopyOnNewThread));
752 try {
753 Thread.CurrentPrincipal = new GenericPrincipal (new GenericIdentity ("good"), null);
754 t.Start ();
755 t.Join ();
756 } catch {
757 #if MONO_FEATURE_THREAD_ABORT
758 t.Abort ();
759 #else
760 t.Interrupt ();
761 #endif
765 int counter = 0;
767 #if MONO_FEATURE_THREAD_SUSPEND_RESUME
768 [Test]
769 public void TestSuspend ()
771 Thread t = new Thread (new ThreadStart (DoCount));
772 t.IsBackground = true;
773 t.Start ();
775 CheckIsRunning ("t1", t);
777 t.Suspend ();
778 WaitSuspended ("t2", t);
780 CheckIsNotRunning ("t3", t);
782 t.Resume ();
783 WaitResumed ("t4", t);
785 CheckIsRunning ("t5", t);
787 t.Abort ();
788 TestUtil.WaitForNotAlive (t, "wait13");
789 CheckIsNotRunning ("t6", t);
791 #endif
793 #if MONO_FEATURE_THREAD_SUSPEND_RESUME && MONO_FEATURE_THREAD_ABORT
794 [Test]
795 [Category("NotDotNet")] // On MS, ThreadStateException is thrown on Abort: "Thread is suspended; attempting to abort"
796 public void TestSuspendAbort ()
798 Thread t = new Thread (new ThreadStart (DoCount));
799 t.IsBackground = true;
800 t.Start ();
802 CheckIsRunning ("t1", t);
804 t.Suspend ();
805 WaitSuspended ("t2", t);
807 CheckIsNotRunning ("t3", t);
809 t.Abort ();
811 int n=0;
812 while (t.IsAlive && n < 200) {
813 Thread.Sleep (10);
814 n++;
817 Assert.IsTrue (n < 200, "Timeout while waiting for abort");
819 CheckIsNotRunning ("t6", t);
821 #endif
823 [Test]
824 public void Test_Interrupt ()
826 ManualResetEvent mre = new ManualResetEvent (false);
827 bool interruptedExceptionThrown = false;
829 ThreadPool.QueueUserWorkItem (Test_Interrupt_Worker, Thread.CurrentThread);
831 try {
832 try {
833 mre.WaitOne (3000);
834 } finally {
835 try {
836 mre.WaitOne (0);
837 } catch (ThreadInterruptedException) {
838 Assert.Fail ("ThreadInterruptedException thrown twice");
841 } catch (ThreadInterruptedException) {
842 interruptedExceptionThrown = true;
845 Assert.IsTrue (interruptedExceptionThrown, "ThreadInterruptedException expected.");
848 [Test]
849 [ExpectedException (typeof (ArgumentNullException))]
850 public void TestQueueUserWorkItemNullCallback ()
852 ThreadPool.QueueUserWorkItem (null, null);
855 private void Test_Interrupt_Worker (object o)
857 Thread t = o as Thread;
858 Thread.Sleep (100);
859 t.Interrupt ();
862 [Test]
863 public void Test_InterruptCurrentThread ()
865 ManualResetEvent mre = new ManualResetEvent (false);
866 bool interruptedExceptionThrown = false;
868 Thread.CurrentThread.Interrupt ();
869 try {
870 mre.WaitOne (0);
871 Assert.Fail ();
872 } catch (ThreadInterruptedException) {
876 [Test]
877 public void GetNamedDataSlotTest ()
879 Assert.IsNotNull (Thread.GetNamedDataSlot ("te#st"), "#1");
880 Assert.AreSame (Thread.GetNamedDataSlot ("te#st"), Thread.GetNamedDataSlot ("te#st"), "#2");
883 class DomainClass : MarshalByRefObject {
884 Thread m_thread;
885 bool success;
887 public bool Run () {
888 m_thread = new Thread(ThreadProc);
889 m_thread.Start(Thread.CurrentThread);
890 m_thread.Join();
891 return success;
894 public void ThreadProc (object arg) {
895 success = m_thread == Thread.CurrentThread;
899 #if MONO_FEATURE_MULTIPLE_APPDOMAINS
900 [Test]
901 [Category ("NotDotNet")]
902 public void CurrentThread_Domains ()
904 AppDomain ad = AppDomain.CreateDomain ("foo");
905 ad.Load (typeof (DomainClass).Assembly.GetName ());
906 var o = (DomainClass)ad.CreateInstanceAndUnwrap (typeof (DomainClass).Assembly.FullName, typeof (DomainClass).FullName);
907 Assert.IsTrue (o.Run ());
908 AppDomain.Unload (ad);
910 #endif // MONO_FEATURE_MULTIPLE_APPDOMAINS
912 [Test]
913 public void SetNameInThreadPoolThread ()
915 Task t = Task.Run (delegate () {
916 Thread.CurrentThread.Name = "ThreadName1";
917 Assert.AreEqual (Thread.CurrentThread.Name, "ThreadName1", "#1");
919 try {
920 Thread.CurrentThread.Name = "ThreadName2";
921 Assert.Fail ("#2");
922 } catch (InvalidOperationException) {
926 t.Wait ();
929 void CheckIsRunning (string s, Thread t)
931 int c = counter;
932 Thread.Sleep (100);
933 Assert.IsTrue (counter > c, s);
936 void CheckIsNotRunning (string s, Thread t)
938 int c = counter;
939 Thread.Sleep (100);
940 Assert.AreEqual (counter, c, s);
943 void WaitSuspended (string s, Thread t)
945 int n=0;
946 ThreadState state = t.ThreadState;
947 while ((state & ThreadState.Suspended) == 0) {
948 Assert.IsTrue ((state & ThreadState.SuspendRequested) != 0, s + ": expected SuspendRequested state");
949 Thread.Sleep (10);
950 n++;
951 Assert.IsTrue (n < 100, s + ": failed to suspend");
952 state = t.ThreadState;
954 Assert.IsTrue ((state & ThreadState.SuspendRequested) == 0, s + ": SuspendRequested state not expected");
957 void WaitResumed (string s, Thread t)
959 int n=0;
960 while ((t.ThreadState & ThreadState.Suspended) != 0) {
961 Thread.Sleep (10);
962 n++;
963 Assert.IsTrue (n < 100, s + ": failed to resume");
967 public void DoCount ()
969 while (true) {
970 counter++;
971 Thread.Sleep (1);
976 [TestFixture]
977 public class ThreadStateTest {
978 void Start ()
982 [Test] // bug #81720
983 public void IsBackGround ()
985 Thread t1 = new Thread (new ThreadStart (Start));
986 Assert.AreEqual (ThreadState.Unstarted, t1.ThreadState, "#A1");
987 Assert.IsFalse (t1.IsBackground, "#A2");
988 t1.Start ();
989 t1.Join ();
990 Assert.AreEqual (ThreadState.Stopped, t1.ThreadState, "#A3");
992 try {
993 bool isBackGround = t1.IsBackground;
994 Assert.Fail ("#A4: " + isBackGround.ToString ());
995 } catch (ThreadStateException ex) {
996 Assert.AreEqual (typeof (ThreadStateException), ex.GetType (), "#A5");
997 Assert.IsNull (ex.InnerException, "#A6");
998 Assert.IsNotNull (ex.Message, "#A7");
1001 Thread t2 = new Thread (new ThreadStart (Start));
1002 Assert.AreEqual (ThreadState.Unstarted, t2.ThreadState, "#B1");
1003 t2.IsBackground = true;
1004 Assert.AreEqual (ThreadState.Unstarted | ThreadState.Background, t2.ThreadState, "#B2");
1005 Assert.IsTrue (t2.IsBackground, "#B3");
1006 t2.Start ();
1007 t2.Join ();
1008 Assert.AreEqual (ThreadState.Stopped, t2.ThreadState, "#B4");
1010 try {
1011 bool isBackGround = t2.IsBackground;
1012 Assert.Fail ("#B5: " + isBackGround.ToString ());
1013 } catch (ThreadStateException ex) {
1014 Assert.AreEqual (typeof (ThreadStateException), ex.GetType (), "#B6");
1015 Assert.IsNull (ex.InnerException, "#B7");
1016 Assert.IsNotNull (ex.Message, "#B8");
1020 [Test] // bug #60031
1021 public void StoppedThreadsThrowThreadStateException ()
1023 var t = new Thread (() => { });
1024 t.Start ();
1025 t.Join ();
1027 Assert.Throws<ThreadStateException> (() => { var isb = t.IsBackground; }, "IsBackground getter");
1028 Assert.Throws<ThreadStateException> (() => { var isb = t.ApartmentState; }, "ApartmentState getter");
1029 Assert.Throws<ThreadStateException> (() => t.ApartmentState = ApartmentState.MTA, "ApartmentState setter");
1030 Assert.Throws<ThreadStateException> (() => t.IsBackground = false, "IsBackground setter");
1031 Assert.Throws<ThreadStateException> (() => t.Start (), "Start ()");
1032 Assert.Throws<ThreadStateException> (() => t.Resume (), "Resume ()");
1033 Assert.Throws<ThreadStateException> (() => t.Suspend (), "Suspend ()");
1034 Assert.Throws<ThreadStateException> (() => t.GetApartmentState (), "GetApartmentState ()");
1035 Assert.Throws<ThreadStateException> (() => t.SetApartmentState (ApartmentState.MTA), "SetApartmentState ()");
1036 Assert.Throws<ThreadStateException> (() => t.TrySetApartmentState (ApartmentState.MTA), "TrySetApartmentState ()");
1040 [TestFixture]
1041 [Serializable]
1042 public class ThreadTest_ManagedThreadId
1044 AppDomain ad1;
1045 AppDomain ad2;
1046 MBRO mbro = new MBRO ();
1048 class MBRO : MarshalByRefObject {
1049 public int id_a1;
1050 public int id_b1;
1051 public int id_b2;
1052 public string ad_a1;
1053 public string ad_b1;
1054 public string ad_b2;
1055 public string message;
1057 #if !MOBILE
1058 [Test]
1059 public void ManagedThreadId_AppDomains ()
1061 AppDomain currentDomain = AppDomain.CurrentDomain;
1062 ad1 = AppDomain.CreateDomain ("AppDomain 1", currentDomain.Evidence, currentDomain.SetupInformation);
1063 ad2 = AppDomain.CreateDomain ("AppDomain 2", currentDomain.Evidence, currentDomain.SetupInformation);
1065 Thread a = new Thread (ThreadA);
1066 Thread b = new Thread (ThreadB);
1067 // execute on AppDomain 1 thread A
1068 // execute on AppDomain 2 thread B
1069 // execute on AppDomain 1 thread B - must have same ManagedThreadId as Ad 2 on thread B
1070 a.Start ();
1071 a.Join ();
1072 b.Start ();
1073 b.Join ();
1075 AppDomain.Unload (ad1);
1076 AppDomain.Unload (ad2);
1078 if (mbro.message != null)
1079 Assert.Fail (mbro.message);
1081 // 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);
1083 Assert.AreEqual ("AppDomain 1", mbro.ad_a1, "Name #1");
1084 Assert.AreEqual ("AppDomain 1", mbro.ad_b1, "Name #2");
1085 Assert.AreEqual ("AppDomain 2", mbro.ad_b2, "Name #3");
1087 Assert.AreNotEqual (mbro.id_a1, mbro.id_b1, "Id #1");
1088 Assert.AreNotEqual (mbro.id_a1, mbro.id_b2, "Id #2");
1089 Assert.AreEqual (mbro.id_b1, mbro.id_b2, "Id #3");
1091 Assert.AreNotEqual (mbro.id_a1, Thread.CurrentThread.ManagedThreadId, "Id #4");
1092 Assert.AreNotEqual (mbro.id_b1, Thread.CurrentThread.ManagedThreadId, "Id #5");
1093 Assert.AreNotEqual (mbro.id_b2, Thread.CurrentThread.ManagedThreadId, "Id #6");
1094 Assert.AreNotEqual (mbro.ad_a1, AppDomain.CurrentDomain.FriendlyName, "Name #4");
1095 Assert.AreNotEqual (mbro.ad_b1, AppDomain.CurrentDomain.FriendlyName, "Name #5");
1096 Assert.AreNotEqual (mbro.ad_b2, AppDomain.CurrentDomain.FriendlyName, "Name #6");
1098 #endif
1099 void A1 ()
1101 mbro.id_a1 = Thread.CurrentThread.ManagedThreadId;
1102 mbro.ad_a1 = AppDomain.CurrentDomain.FriendlyName;
1105 void B2 ()
1107 mbro.id_b2 = Thread.CurrentThread.ManagedThreadId;
1108 mbro.ad_b2 = AppDomain.CurrentDomain.FriendlyName;
1111 void B1 ()
1113 mbro.id_b1 = Thread.CurrentThread.ManagedThreadId;
1114 mbro.ad_b1 = AppDomain.CurrentDomain.FriendlyName;
1117 void ThreadA (object obj)
1119 // Console.WriteLine ("ThreadA");
1120 try {
1121 ad1.DoCallBack (A1);
1122 } catch (Exception ex) {
1123 mbro.message = string.Format ("ThreadA exception: {0}", ex);
1125 // Console.WriteLine ("ThreadA Done");
1128 void ThreadB (object obj)
1130 // Console.WriteLine ("ThreadB");
1131 try {
1132 ad2.DoCallBack (B2);
1133 ad1.DoCallBack (B1);
1134 } catch (Exception ex) {
1135 mbro.message = string.Format ("ThreadB exception: {0}", ex);
1137 // Console.WriteLine ("ThreadB Done");
1141 [TestFixture]
1142 public class ThreadApartmentTest
1144 void Start ()
1148 [Test] // bug #81658
1149 public void ApartmentState_StoppedThread ()
1151 Thread t1 = new Thread (new ThreadStart (Start));
1152 t1.Start ();
1153 t1.Join ();
1154 try {
1155 ApartmentState state = t1.ApartmentState;
1156 Assert.Fail ("#A1: " + state.ToString ());
1157 } catch (ThreadStateException ex) {
1158 Assert.AreEqual (typeof (ThreadStateException), ex.GetType (), "#A2");
1159 Assert.IsNull (ex.InnerException, "#A3");
1160 Assert.IsNotNull (ex.Message, "#A4");
1163 Thread t2 = new Thread (new ThreadStart (Start));
1164 t2.IsBackground = true;
1165 t2.Start ();
1166 t2.Join ();
1167 try {
1168 ApartmentState state = t2.ApartmentState;
1169 Assert.Fail ("#B1: " + state.ToString ());
1170 } catch (ThreadStateException ex) {
1171 Assert.AreEqual (typeof (ThreadStateException), ex.GetType (), "#B2");
1172 Assert.IsNull (ex.InnerException, "#B3");
1173 Assert.IsNotNull (ex.Message, "#B4");
1177 [Test]
1178 public void ApartmentState_BackGround ()
1180 Thread t1 = new Thread (new ThreadStart (Start));
1181 t1.IsBackground = true;
1182 Assert.AreEqual (ApartmentState.Unknown, t1.ApartmentState, "#1");
1183 t1.ApartmentState = ApartmentState.STA;
1184 Assert.AreEqual (ApartmentState.STA, t1.ApartmentState, "#2");
1187 [Test]
1188 public void TestApartmentState ()
1190 Thread t1 = new Thread (new ThreadStart (Start));
1191 Thread t2 = new Thread (new ThreadStart (Start));
1192 Thread t3 = new Thread (new ThreadStart (Start));
1194 Assert.AreEqual (ApartmentState.Unknown, t1.ApartmentState, "Thread1 Default");
1195 Assert.AreEqual (ApartmentState.Unknown, t2.ApartmentState, "Thread2 Default");
1196 Assert.AreEqual (ApartmentState.Unknown, t3.ApartmentState, "Thread3 Default");
1198 t1.ApartmentState = ApartmentState.STA;
1199 Assert.AreEqual (ApartmentState.STA, t1.ApartmentState, "Thread1 Set Once");
1200 t1.ApartmentState = ApartmentState.MTA;
1201 Assert.AreEqual (ApartmentState.STA, t1.ApartmentState, "Thread1 Set Twice");
1203 t2.ApartmentState = ApartmentState.MTA;
1204 Assert.AreEqual (ApartmentState.MTA, t2.ApartmentState, "Thread2 Set Once");
1205 t2.ApartmentState = ApartmentState.STA;
1206 Assert.AreEqual (ApartmentState.MTA, t2.ApartmentState, "Thread2 Set Twice");
1208 bool exception_occured = false;
1209 try {
1210 t3.ApartmentState = ApartmentState.Unknown;
1212 catch (Exception) {
1213 exception_occured = true;
1215 Assert.AreEqual (ApartmentState.Unknown, t3.ApartmentState, "Thread3 Set Invalid");
1216 Assert.IsFalse (exception_occured, "Thread3 Set Invalid Exception Occured");
1218 t1.Start ();
1219 exception_occured = false;
1220 try {
1221 t1.ApartmentState = ApartmentState.STA;
1223 catch (Exception) {
1224 exception_occured = true;
1226 Assert.IsTrue (exception_occured, "Thread1 Started Invalid Exception Occured");
1229 [Test]
1230 public void TestSetApartmentStateSameState ()
1232 Thread t1 = new Thread (new ThreadStart (Start));
1233 t1.SetApartmentState (ApartmentState.STA);
1234 Assert.AreEqual (ApartmentState.STA, t1.ApartmentState, "Thread1 Set Once");
1236 t1.SetApartmentState (ApartmentState.STA);
1237 Assert.AreEqual (ApartmentState.STA, t1.ApartmentState, "Thread1 Set twice");
1240 [Test]
1241 [ExpectedException(typeof(InvalidOperationException))]
1242 public void TestSetApartmentStateDiffState ()
1244 Thread t1 = new Thread (new ThreadStart (Start));
1245 t1.SetApartmentState (ApartmentState.STA);
1246 Assert.AreEqual (ApartmentState.STA, t1.ApartmentState, "Thread1 Set Once");
1248 t1.SetApartmentState (ApartmentState.MTA);
1251 [Test]
1252 public void TestTrySetApartmentState ()
1254 Thread t1 = new Thread (new ThreadStart (Start));
1255 t1.SetApartmentState (ApartmentState.STA);
1256 Assert.AreEqual (ApartmentState.STA, t1.ApartmentState, "#1");
1258 bool result = t1.TrySetApartmentState (ApartmentState.MTA);
1259 Assert.IsFalse (result, "#2");
1261 result = t1.TrySetApartmentState (ApartmentState.STA);
1262 Assert.IsTrue (result, "#3");
1265 [Test]
1266 public void TestTrySetApartmentStateRunning ()
1268 Thread t1 = new Thread (new ThreadStart (Start));
1269 t1.SetApartmentState (ApartmentState.STA);
1270 Assert.AreEqual (ApartmentState.STA, t1.ApartmentState, "#1");
1272 t1.Start ();
1274 try {
1275 t1.TrySetApartmentState (ApartmentState.STA);
1276 Assert.Fail ("#2");
1277 } catch (ThreadStateException) {
1280 t1.Join ();
1283 [Test]
1284 public void Volatile () {
1285 double v3 = 55667;
1286 Thread.VolatileWrite (ref v3, double.MaxValue);
1287 Assert.AreEqual (v3, double.MaxValue);
1289 float v4 = 1;
1290 Thread.VolatileWrite (ref v4, float.MaxValue);
1291 Assert.AreEqual (v4, float.MaxValue);
1294 [Test]
1295 public void Culture ()
1297 Assert.IsNotNull (Thread.CurrentThread.CurrentCulture, "CurrentCulture");
1298 Assert.IsNotNull (Thread.CurrentThread.CurrentUICulture, "CurrentUICulture");
1301 [Test]
1302 public void ThreadStartSimple ()
1304 int i = 0;
1305 Thread t = new Thread (delegate () {
1306 // ensure the NSAutoreleasePool works
1307 i++;
1309 t.Start ();
1310 t.Join ();
1311 Assert.AreEqual (1, i, "ThreadStart");
1314 [Test]
1315 public void ParametrizedThreadStart ()
1317 int i = 0;
1318 object arg = null;
1319 Thread t = new Thread (delegate (object obj) {
1320 // ensure the NSAutoreleasePool works
1321 i++;
1322 arg = obj;
1324 t.Start (this);
1325 t.Join ();
1327 Assert.AreEqual (1, i, "ParametrizedThreadStart");
1328 Assert.AreEqual (this, arg, "obj");
1331 [Test]
1332 public void SetNameTpThread () {
1333 ThreadPool.QueueUserWorkItem(new WaitCallback(ThreadProc));
1336 static void ThreadProc(Object stateInfo) {
1337 Thread.CurrentThread.Name = "My Worker";
1340 [Test]
1341 public void GetStackTraces () {
1342 var m = typeof (Thread).GetMethod ("Mono_GetStackTraces", BindingFlags.NonPublic|BindingFlags.Static);
1343 if (m != null) {
1344 var res = (Dictionary<Thread,SD.StackTrace>)typeof (Thread).GetMethod ("Mono_GetStackTraces", BindingFlags.NonPublic|BindingFlags.Static).Invoke (null, null);
1345 foreach (var t in res.Keys) {
1346 var st = res [t].ToString ();
1352 public class TestUtil
1354 public static void WaitForNotAlive (Thread t, string s)
1356 WhileAlive (t, true, s);
1359 public static void WaitForAlive (Thread t, string s)
1361 WhileAlive (t, false, s);
1364 public static bool WaitForAliveOrStop (Thread t, string s)
1366 return WhileAliveOrStop (t, false, s);
1369 public static void WhileAlive (Thread t, bool alive, string s)
1371 var sw = SD.Stopwatch.StartNew ();
1372 while (t.IsAlive == alive) {
1373 if (sw.Elapsed.TotalSeconds > 10) {
1374 if (alive) Assert.Fail ("Timeout while waiting for not alive state. " + s);
1375 else Assert.Fail ("Timeout while waiting for alive state. " + s);
1380 public static bool WhileAliveOrStop (Thread t, bool alive, string s)
1382 var sw = SD.Stopwatch.StartNew ();
1383 while (t.IsAlive == alive) {
1384 if (t.ThreadState == ThreadState.Stopped)
1385 return false;
1387 if (sw.Elapsed.TotalSeconds > 10) {
1388 if (alive) Assert.Fail ("Timeout while waiting for not alive state. " + s);
1389 else Assert.Fail ("Timeout while waiting for alive state. " + s);
1393 return true;