ThreadTest.cs 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060
  1. // ThreadTest.cs - NUnit Test Cases for the System.Threading.Thread class
  2. //
  3. // Authors
  4. // Eduardo Garcia Cebollero ([email protected])
  5. // Sebastien Pouliot <[email protected]>
  6. //
  7. // (C) Eduardo Garcia Cebollero.
  8. // (C) Ximian, Inc. http://www.ximian.com
  9. // (C) 2004 Novell (http://www.novell.com)
  10. //
  11. using System;
  12. using System.Globalization;
  13. using System.Security.Principal;
  14. using System.Threading;
  15. using NUnit.Framework;
  16. namespace MonoTests.System.Threading
  17. {
  18. // These tests seem to hang the 2.0 framework. So they are disabled for now
  19. // Don't reenable them until you can run a few thousand times on an SMP box.
  20. [Category ("NotWorking")]
  21. public class ThreadedPrincipalTest
  22. {
  23. public static void NoPrincipal ()
  24. {
  25. #if !TARGET_JVM // AppDomain.SetPrincipalPolicy not supported for TARGET_JVM
  26. AppDomain.CurrentDomain.SetPrincipalPolicy (PrincipalPolicy.NoPrincipal);
  27. #endif
  28. IPrincipal p = Thread.CurrentPrincipal;
  29. Assert.IsNull (p, "#1");
  30. Thread.CurrentPrincipal = new GenericPrincipal (new GenericIdentity ("mono"), null);
  31. Assert.IsNotNull (Thread.CurrentPrincipal, "#2");
  32. Thread.CurrentPrincipal = null;
  33. Assert.IsNull (Thread.CurrentPrincipal, "#3");
  34. // in this case we can return to null
  35. }
  36. #if !TARGET_JVM // AppDomain.SetPrincipalPolicy not supported for TARGET_JVM
  37. public static void UnauthenticatedPrincipal ()
  38. {
  39. AppDomain.CurrentDomain.SetPrincipalPolicy (PrincipalPolicy.UnauthenticatedPrincipal);
  40. IPrincipal p = Thread.CurrentPrincipal;
  41. Assert.IsNotNull (p, "#1");
  42. Assert.IsTrue ((p is GenericPrincipal), "#2");
  43. Assert.AreEqual (String.Empty, p.Identity.Name, "#3");
  44. Assert.AreEqual (String.Empty, p.Identity.AuthenticationType, "#4");
  45. Assert.IsFalse (p.Identity.IsAuthenticated, "#5");
  46. Thread.CurrentPrincipal = new GenericPrincipal (new GenericIdentity ("mono"), null);
  47. Assert.IsNotNull (Thread.CurrentPrincipal, "#6");
  48. Thread.CurrentPrincipal = null;
  49. Assert.IsNotNull (Thread.CurrentPrincipal, "#7");
  50. // in this case we can't return to null
  51. }
  52. public static void WindowsPrincipal ()
  53. {
  54. AppDomain.CurrentDomain.SetPrincipalPolicy (PrincipalPolicy.WindowsPrincipal);
  55. IPrincipal p = Thread.CurrentPrincipal;
  56. Assert.IsNotNull (p, "#1");
  57. Assert.IsTrue ((p is WindowsPrincipal), "#2");
  58. Assert.IsNotNull (p.Identity.Name, "#3");
  59. Assert.IsNotNull (p.Identity.AuthenticationType, "#4");
  60. Assert.IsTrue (p.Identity.IsAuthenticated, "#5");
  61. // note: we can switch from a WindowsPrincipal to a GenericPrincipal
  62. Thread.CurrentPrincipal = new GenericPrincipal (new GenericIdentity ("mono"), null);
  63. Assert.IsNotNull (Thread.CurrentPrincipal, "#6");
  64. Thread.CurrentPrincipal = null;
  65. Assert.IsNotNull (Thread.CurrentPrincipal, "#7");
  66. // in this case we can't return to null
  67. }
  68. #endif // TARGET_JVM
  69. public static void CopyOnNewThread ()
  70. {
  71. Assert.IsNotNull (Thread.CurrentPrincipal, "#1");
  72. Assert.AreEqual ("good", Thread.CurrentPrincipal.Identity.Name, "#2");
  73. }
  74. }
  75. [TestFixture]
  76. public class ThreadTest
  77. {
  78. TimeSpan Infinite = new TimeSpan (-10000); // -10000 ticks == -1 ms
  79. TimeSpan SmallNegative = new TimeSpan (-2); // between 0 and -1.0 (infinite) ms
  80. TimeSpan Negative = new TimeSpan (-20000); // really negative
  81. TimeSpan MaxValue = TimeSpan.FromMilliseconds ((long) Int32.MaxValue);
  82. TimeSpan TooLarge = TimeSpan.FromMilliseconds ((long) Int32.MaxValue + 1);
  83. static bool is_win32;
  84. static bool is_mono;
  85. static ThreadTest ()
  86. {
  87. switch (Environment.OSVersion.Platform) {
  88. case PlatformID.Win32NT:
  89. case PlatformID.Win32S:
  90. case PlatformID.Win32Windows:
  91. case PlatformID.WinCE:
  92. is_win32 = true;
  93. break;
  94. }
  95. // check a class in mscorlib to determine if we're running on Mono
  96. if (Type.GetType ("System.MonoType", false) != null)
  97. is_mono = true;
  98. }
  99. //Some Classes to test as threads
  100. private class C1Test
  101. {
  102. public int cnt;
  103. public Thread thread1;
  104. public bool endm1;
  105. public bool endm2;
  106. public C1Test()
  107. {
  108. thread1 = (Thread)null;
  109. this.cnt = 0;
  110. endm1 = endm2 = false;
  111. }
  112. public void TestMethod()
  113. {
  114. while (cnt < 10)
  115. {
  116. cnt++;
  117. }
  118. endm1 = true;
  119. }
  120. public void TestMethod2()
  121. {
  122. if (!(thread1==(Thread)null) )
  123. {
  124. thread1.Join();
  125. }
  126. endm2 = true;
  127. }
  128. }
  129. private class C2Test
  130. {
  131. public int cnt;
  132. public bool run = false;
  133. public C2Test()
  134. {
  135. this.cnt = 0;
  136. }
  137. public void TestMethod()
  138. {
  139. run = true;
  140. while (true)
  141. {
  142. if (cnt < 1000)
  143. cnt++;
  144. else
  145. cnt = 0;
  146. }
  147. }
  148. }
  149. private class C3Test
  150. {
  151. public C1Test sub_class;
  152. public Thread sub_thread;
  153. public C3Test()
  154. {
  155. sub_class = new C1Test();
  156. sub_thread = new Thread(new ThreadStart(sub_class.TestMethod));
  157. }
  158. public void TestMethod1()
  159. {
  160. sub_thread.Start();
  161. Thread.Sleep (100);
  162. sub_thread.Abort();
  163. }
  164. }
  165. private class C4Test
  166. {
  167. public C1Test class1;
  168. public C1Test class2;
  169. public Thread thread1;
  170. public Thread thread2;
  171. public bool T1ON ;
  172. public bool T2ON ;
  173. public C4Test()
  174. {
  175. T1ON = false;
  176. T2ON = false;
  177. class1 = new C1Test();
  178. class2 = new C1Test();
  179. thread1 = new Thread(new ThreadStart(class1.TestMethod));
  180. thread2 = new Thread(new ThreadStart(class2.TestMethod));
  181. }
  182. public void TestMethod1()
  183. {
  184. thread1.Start();
  185. TestUtil.WaitForAlive (thread1, "wait1");
  186. T1ON = true;
  187. thread2.Start();
  188. TestUtil.WaitForAlive (thread2, "wait2");
  189. T2ON = true;
  190. thread1.Abort();
  191. TestUtil.WaitForNotAlive (thread1, "wait3");
  192. T1ON = false;
  193. thread2.Abort();
  194. TestUtil.WaitForNotAlive (thread2, "wait4");
  195. T2ON = false;
  196. }
  197. public void TestMethod2()
  198. {
  199. thread1.Start();
  200. thread1.Join();
  201. }
  202. }
  203. [Test]
  204. public void TestCtor1()
  205. {
  206. C1Test test1 = new C1Test();
  207. Thread t = new Thread (new ThreadStart (test1.TestMethod));
  208. Assert.IsTrue (t.CurrentCulture.IsReadOnly, "CurrentCulture.IsReadOnly");
  209. Assert.IsFalse (t.IsAlive, "IsAlive");
  210. Assert.IsFalse (t.IsBackground, "IsBackground");
  211. Assert.IsNull (t.Name, "Name");
  212. Assert.AreEqual (ThreadState.Unstarted, t.ThreadState, "ThreadState");
  213. }
  214. [Test]
  215. [Category ("NotWorking")] // we're not sharing (read-only) CultureInfo
  216. public void CultureInfo_Shared_Across_Threads ()
  217. {
  218. Thread t = new Thread (TestCtor1);
  219. Assert.AreSame (t.CurrentCulture, t.CurrentUICulture, "Culture");
  220. Assert.AreSame (t.CurrentCulture, CultureInfo.CurrentCulture, "CultureInfo.CurrentCulture");
  221. Assert.AreSame (t.CurrentUICulture, CultureInfo.CurrentUICulture, "CultureInfo.CurrentUICulture");
  222. Assert.AreSame (t.CurrentCulture, Thread.CurrentThread.CurrentCulture, "Thread.CurrentThread.CurrentCulture");
  223. Assert.AreSame (t.CurrentUICulture, Thread.CurrentThread.CurrentUICulture, "Thread.CurrentThread.CurrentUICulture");
  224. }
  225. [Test] // bug #325566
  226. public void GetHashCodeTest ()
  227. {
  228. C1Test test1 = new C1Test ();
  229. Thread tA = new Thread (new ThreadStart (test1.TestMethod));
  230. int hA1 = tA.GetHashCode ();
  231. #if NET_2_0
  232. Assert.IsTrue (hA1 > 0, "#A1");
  233. #endif
  234. tA.Start ();
  235. int hA2 = tA.GetHashCode ();
  236. Assert.AreEqual (hA1, hA2, "#A2");
  237. tA.Join ();
  238. int hA3 = tA.GetHashCode ();
  239. Assert.AreEqual (hA1, hA3, "#A3");
  240. #if NET_2_0
  241. Assert.AreEqual (hA1, tA.ManagedThreadId, "#A4");
  242. #endif
  243. test1 = new C1Test ();
  244. Thread tB = new Thread (new ThreadStart (test1.TestMethod));
  245. int hB1 = tB.GetHashCode ();
  246. #if NET_2_0
  247. Assert.IsTrue (hB1 > 0, "#B1");
  248. #endif
  249. tB.Start ();
  250. int hB2 = tB.GetHashCode ();
  251. Assert.AreEqual (hB1, hB2, "#B2");
  252. tB.Join ();
  253. int hB3 = tB.GetHashCode ();
  254. Assert.AreEqual (hB1, hB3, "#B3");
  255. #if NET_2_0
  256. Assert.AreEqual (hB1, tB.ManagedThreadId, "#B4");
  257. #endif
  258. Assert.IsFalse (hA2 == hB2, "#B5");
  259. }
  260. #if NET_2_0
  261. [Test] // bug #82700
  262. public void ManagedThreadId ()
  263. {
  264. C1Test test1 = new C1Test ();
  265. Thread t1 = new Thread (new ThreadStart (test1.TestMethod));
  266. int mtA1 = t1.ManagedThreadId;
  267. t1.Start ();
  268. int mtA2 = t1.ManagedThreadId;
  269. t1.Join ();
  270. int mtA3 = t1.ManagedThreadId;
  271. Assert.AreEqual (mtA1, mtA2, "#A1");
  272. Assert.AreEqual (mtA2, mtA3, "#A2");
  273. test1 = new C1Test ();
  274. Thread t2 = new Thread (new ThreadStart (test1.TestMethod));
  275. int mtB1 = t2.ManagedThreadId;
  276. t2.Start ();
  277. int mtB2 = t2.ManagedThreadId;
  278. t2.Join ();
  279. int mtB3 = t2.ManagedThreadId;
  280. Assert.AreEqual (mtB1, mtB2, "#B1");
  281. Assert.AreEqual (mtB2, mtB3, "#B2");
  282. Assert.IsFalse (mtB1 == mtA1, "#B3");
  283. }
  284. #endif
  285. [Test]
  286. [Category ("NotDotNet")] // it hangs.
  287. public void TestStart()
  288. {
  289. if (is_win32 && is_mono)
  290. Assert.Fail ("This test fails on Win32. The test should be fixed.");
  291. {
  292. C1Test test1 = new C1Test();
  293. Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
  294. TestThread.Start();
  295. TestThread.Join();
  296. Assert.AreEqual (10, test1.cnt, "#1");
  297. }
  298. {
  299. C2Test test1 = new C2Test();
  300. Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
  301. TestThread.Start();
  302. TestThread.Abort();
  303. try {
  304. TestThread.Start();
  305. Assert.Fail ("#2");
  306. } catch (ThreadStateException) {
  307. }
  308. }
  309. {
  310. C2Test test1 = new C2Test();
  311. Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
  312. TestThread.Start();
  313. while (!test1.run) {
  314. }
  315. bool started = (TestThread.ThreadState == ThreadState.Running);
  316. Assert.AreEqual (started, test1.run, "#15 Thread Is not in the correct state: ");
  317. TestThread.Abort();
  318. }
  319. }
  320. [Test]
  321. public void TestApartmentState ()
  322. {
  323. if (is_win32 && is_mono)
  324. Assert.Fail ("This test fails on mono on win32. Our runtime should be fixed.");
  325. C2Test test1 = new C2Test();
  326. Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
  327. Assert.AreEqual (ApartmentState.Unknown, TestThread.ApartmentState, "#1");
  328. TestThread.Start();
  329. TestUtil.WaitForAlive (TestThread, "wait5");
  330. #if NET_2_0
  331. Assert.AreEqual (ApartmentState.MTA, TestThread.ApartmentState, "#2");
  332. #else
  333. Assert.AreEqual (ApartmentState.Unknown, TestThread.ApartmentState, "#3");
  334. #endif
  335. TestThread.Abort();
  336. }
  337. [Test]
  338. public void TestPriority1()
  339. {
  340. if (is_win32 && is_mono)
  341. Assert.Fail ("This test fails on mono on Win32. Our runtime should be fixed.");
  342. C2Test test1 = new C2Test();
  343. Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
  344. try {
  345. TestThread.Priority=ThreadPriority.BelowNormal;
  346. ThreadPriority after = TestThread.Priority;
  347. TestThread.Start();
  348. TestUtil.WaitForAlive (TestThread, "wait7");
  349. ThreadPriority before = TestThread.Priority;
  350. Assert.AreEqual (before, after, "#41 Unexpected Priority Change: ");
  351. } finally {
  352. TestThread.Abort();
  353. }
  354. }
  355. [Test]
  356. [Category ("NotDotNet")] // on MS, Thread is still in AbortRequested state when Start is invoked
  357. public void AbortUnstarted ()
  358. {
  359. C2Test test1 = new C2Test();
  360. Thread th = new Thread (new ThreadStart (test1.TestMethod));
  361. th.Abort ();
  362. th.Start ();
  363. }
  364. [Test]
  365. [Category ("NotDotNet")] // on MS, ThreadState is immediately Stopped after Abort
  366. [Category ("NotWorking")] // this is a MonoTODO -> no support for Priority
  367. public void TestPriority2()
  368. {
  369. C2Test test1 = new C2Test();
  370. Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
  371. try {
  372. Assert.AreEqual (ThreadPriority.Normal, TestThread.Priority, "#42 Incorrect Priority in New thread: ");
  373. TestThread.Start();
  374. TestUtil.WaitForAliveOrStop (TestThread, "wait8");
  375. Assert.AreEqual (ThreadPriority.Normal, TestThread.Priority, "#43 Incorrect Priority in Started thread: ");
  376. } finally {
  377. TestThread.Abort();
  378. }
  379. Assert.AreEqual (ThreadPriority.Normal, TestThread.Priority, "#44 Incorrect Priority in Aborted thread: ");
  380. }
  381. [Test]
  382. [Category ("NotWorking")] // this is a MonoTODO -> no support for Priority
  383. public void TestPriority3()
  384. {
  385. C2Test test1 = new C2Test();
  386. Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
  387. try {
  388. TestThread.Start();
  389. TestThread.Priority = ThreadPriority.Lowest;
  390. Assert.AreEqual (ThreadPriority.Lowest, TestThread.Priority, "#45A Incorrect Priority:");
  391. TestThread.Priority = ThreadPriority.BelowNormal;
  392. Assert.AreEqual (ThreadPriority.BelowNormal, TestThread.Priority, "#45B Incorrect Priority:");
  393. TestThread.Priority = ThreadPriority.Normal;
  394. Assert.AreEqual (ThreadPriority.Normal, TestThread.Priority, "#45C Incorrect Priority:");
  395. TestThread.Priority = ThreadPriority.AboveNormal;
  396. Assert.AreEqual (ThreadPriority.AboveNormal, TestThread.Priority, "#45D Incorrect Priority:");
  397. TestThread.Priority = ThreadPriority.Highest;
  398. Assert.AreEqual (ThreadPriority.Highest, TestThread.Priority, "#45E Incorrect Priority:");
  399. }
  400. finally {
  401. TestThread.Abort();
  402. }
  403. }
  404. [Test]
  405. public void TestIsBackground1 ()
  406. {
  407. if (is_win32 && is_mono)
  408. Assert.Fail ("This test fails on mono on Win32. Our runtime should be fixed.");
  409. C2Test test1 = new C2Test();
  410. Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
  411. try {
  412. TestThread.Start();
  413. TestUtil.WaitForAlive (TestThread, "wait9");
  414. bool state = TestThread.IsBackground;
  415. Assert.IsFalse (state, "#51 IsBackground not set at the default state: ");
  416. } finally {
  417. TestThread.Abort();
  418. }
  419. }
  420. [Test]
  421. [Category ("NotDotNet")] // on MS, ThreadState is immediately Stopped after Abort
  422. public void TestIsBackground2 ()
  423. {
  424. C2Test test1 = new C2Test();
  425. Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
  426. TestThread.IsBackground = true;
  427. try {
  428. TestThread.Start();
  429. } finally {
  430. TestThread.Abort();
  431. }
  432. Assert.IsTrue (TestThread.IsBackground, "#52 Is Background Changed to Start ");
  433. }
  434. [Test]
  435. public void TestName()
  436. {
  437. if (is_win32 && is_mono)
  438. Assert.Fail ("This test fails on mono on Win32. Our runtime should be fixed.");
  439. C2Test test1 = new C2Test();
  440. Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
  441. try {
  442. TestThread.Start();
  443. TestUtil.WaitForAlive (TestThread, "wait10");
  444. string name = TestThread.Name;
  445. Assert.IsNull (name, "#61 Name set when mustn't be set: ");
  446. string newname = "Testing....";
  447. TestThread.Name = newname;
  448. Assert.AreEqual (newname, TestThread.Name, "#62 Name not set when must be set: ");
  449. } finally {
  450. TestThread.Abort();
  451. }
  452. }
  453. [Test]
  454. public void Name ()
  455. {
  456. Thread t = new Thread (new ThreadStart (Name));
  457. Assert.IsNull (t.Name, "Name-1");
  458. t.Name = null;
  459. Assert.IsNull (t.Name, "Name-2");
  460. }
  461. [Test]
  462. [ExpectedException (typeof (InvalidOperationException))]
  463. public void ReName ()
  464. {
  465. Thread t = new Thread (new ThreadStart (ReName));
  466. t.Name = "a";
  467. t.Name = "b";
  468. }
  469. [Test]
  470. public void TestNestedThreads1()
  471. {
  472. C3Test test1 = new C3Test();
  473. Thread TestThread = new Thread(new ThreadStart(test1.TestMethod1));
  474. try {
  475. TestThread.Start();
  476. TestUtil.WaitForAlive (TestThread, "wait11");
  477. } finally {
  478. TestThread.Abort();
  479. }
  480. }
  481. [Test]
  482. public void TestNestedThreads2()
  483. {
  484. C4Test test1 = new C4Test();
  485. Thread TestThread = new Thread(new ThreadStart(test1.TestMethod1));
  486. try {
  487. TestThread.Start();
  488. } finally {
  489. TestThread.Abort();
  490. }
  491. }
  492. [Test]
  493. public void TestJoin1()
  494. {
  495. C1Test test1 = new C1Test();
  496. C1Test test2 = new C1Test();
  497. Thread thread1 = new Thread(new ThreadStart(test1.TestMethod));
  498. Thread thread2 = new Thread(new ThreadStart(test1.TestMethod2));
  499. try {
  500. thread1.Start();
  501. thread2.Start();
  502. thread2.Join();
  503. } finally {
  504. thread1.Abort();
  505. thread2.Abort();
  506. }
  507. }
  508. [Test]
  509. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  510. public void Join_Int32_Negative ()
  511. {
  512. // -1 is Timeout.Infinite
  513. Thread.CurrentThread.Join (-2);
  514. }
  515. [Test]
  516. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  517. public void Join_TimeSpan_Negative ()
  518. {
  519. Thread.CurrentThread.Join (Negative);
  520. }
  521. [Test]
  522. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  523. public void Join_TimeSpan_TooLarge ()
  524. {
  525. Thread.CurrentThread.Join (TooLarge);
  526. }
  527. [Test]
  528. public void Join_TimeSpan_SmallNegative ()
  529. {
  530. Thread.CurrentThread.Join (SmallNegative);
  531. }
  532. [Test]
  533. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  534. public void Sleep_Int32_Negative ()
  535. {
  536. // -1 is Timeout.Infinite
  537. Thread.Sleep (-2);
  538. }
  539. [Test]
  540. public void Sleep_TimeSpan_SmallNegative ()
  541. {
  542. Thread.Sleep (SmallNegative);
  543. }
  544. [Test]
  545. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  546. public void Sleep_TimeSpan_Negative ()
  547. {
  548. Thread.Sleep (Negative);
  549. }
  550. [Test]
  551. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  552. public void Sleep_TimeSpan_TooLarge ()
  553. {
  554. Thread.Sleep (TooLarge);
  555. }
  556. [Test]
  557. public void SpinWait ()
  558. {
  559. // no exception for negative numbers
  560. Thread.SpinWait (Int32.MinValue);
  561. Thread.SpinWait (0);
  562. }
  563. [Test]
  564. public void TestThreadState ()
  565. {
  566. if (is_win32 && is_mono)
  567. Assert.Fail ("This test fails on mono on Win32. Our runtime should be fixed.");
  568. //TODO: Test The rest of the possible transitions
  569. C2Test test1 = new C2Test();
  570. Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
  571. Assert.AreEqual (ThreadState.Unstarted, TestThread.ThreadState, "#101 Wrong Thread State");
  572. try {
  573. TestThread.Start();
  574. //while(!TestThread.IsAlive); //In the MS Documentation this is not necessary
  575. //but in the MS SDK it is
  576. Assert.IsTrue (TestThread.ThreadState == ThreadState.Running || (TestThread.ThreadState & ThreadState.Unstarted) != 0,
  577. "#102 Wrong Thread State: " + TestThread.ThreadState.ToString ());
  578. } finally {
  579. TestThread.Abort();
  580. }
  581. TestUtil.WaitForNotAlive (TestThread, "wait12");
  582. // Docs say state will be Stopped, but Aborted happens sometimes (?)
  583. Assert.IsTrue ((ThreadState.Stopped & TestThread.ThreadState) != 0 || (ThreadState.Aborted & TestThread.ThreadState) != 0,
  584. "#103 Wrong Thread State: " + TestThread.ThreadState.ToString ());
  585. }
  586. [Test]
  587. [Ignore ("see comment below.")]
  588. public void CurrentPrincipal_PrincipalPolicy_NoPrincipal ()
  589. {
  590. // note: switching from PrincipalPolicy won't work inside the same thread
  591. // because as soon as a Principal object is created the Policy doesn't matter anymore
  592. Thread t = new Thread (new ThreadStart (ThreadedPrincipalTest.NoPrincipal));
  593. try {
  594. t.Start ();
  595. t.Join ();
  596. } catch {
  597. t.Abort ();
  598. }
  599. }
  600. #if !TARGET_JVM // AppDomain.SetPrincipalPolicy not supported for TARGET_JVM
  601. [Test]
  602. [Ignore ("see comment below.")]
  603. public void CurrentPrincipal_PrincipalPolicy_UnauthenticatedPrincipal ()
  604. {
  605. // note: switching from PrincipalPolicy won't work inside the same thread
  606. // because as soon as a Principal object is created the Policy doesn't matter anymore
  607. Thread t = new Thread (new ThreadStart (ThreadedPrincipalTest.UnauthenticatedPrincipal));
  608. try {
  609. t.Start ();
  610. t.Join ();
  611. } catch {
  612. t.Abort ();
  613. }
  614. }
  615. [Test]
  616. public void CurrentPrincipal_PrincipalPolicy_WindowsPrincipal ()
  617. {
  618. // note: switching from PrincipalPolicy won't work inside the same thread
  619. // because as soon as a Principal object is created the Policy doesn't matter anymore
  620. Thread t = new Thread (new ThreadStart (ThreadedPrincipalTest.WindowsPrincipal));
  621. try {
  622. t.Start ();
  623. t.Join ();
  624. } catch {
  625. t.Abort ();
  626. }
  627. }
  628. #endif // TARGET_JVM
  629. [Test]
  630. public void IPrincipal_CopyOnNewThread ()
  631. {
  632. Thread.CurrentPrincipal = new GenericPrincipal (new GenericIdentity ("bad"), null);
  633. Thread t = new Thread (new ThreadStart (ThreadedPrincipalTest.CopyOnNewThread));
  634. try {
  635. Thread.CurrentPrincipal = new GenericPrincipal (new GenericIdentity ("good"), null);
  636. t.Start ();
  637. t.Join ();
  638. } catch {
  639. t.Abort ();
  640. }
  641. }
  642. int counter = 0;
  643. [Test]
  644. public void TestSuspend ()
  645. {
  646. Thread t = new Thread (new ThreadStart (DoCount));
  647. t.IsBackground = true;
  648. t.Start ();
  649. CheckIsRunning ("t1", t);
  650. t.Suspend ();
  651. WaitSuspended ("t2", t);
  652. CheckIsNotRunning ("t3", t);
  653. t.Resume ();
  654. WaitResumed ("t4", t);
  655. CheckIsRunning ("t5", t);
  656. t.Abort ();
  657. TestUtil.WaitForNotAlive (t, "wait13");
  658. CheckIsNotRunning ("t6", t);
  659. }
  660. [Test]
  661. [Category("NotDotNet")] // On MS, ThreadStateException is thrown on Abort: "Thread is suspended; attempting to abort"
  662. public void TestSuspendAbort ()
  663. {
  664. if (is_win32 && is_mono)
  665. Assert.Fail ("This test fails on Win32. The test should be fixed.");
  666. Thread t = new Thread (new ThreadStart (DoCount));
  667. t.IsBackground = true;
  668. t.Start ();
  669. CheckIsRunning ("t1", t);
  670. t.Suspend ();
  671. WaitSuspended ("t2", t);
  672. CheckIsNotRunning ("t3", t);
  673. t.Abort ();
  674. int n=0;
  675. while (t.IsAlive && n < 200) {
  676. Thread.Sleep (10);
  677. n++;
  678. }
  679. Assert.IsTrue (n < 200, "Timeout while waiting for abort");
  680. CheckIsNotRunning ("t6", t);
  681. }
  682. [Test]
  683. public void Test_Interrupt ()
  684. {
  685. bool interruptedExceptionThrown = false;
  686. ThreadPool.QueueUserWorkItem (Test_Interrupt_Worker, Thread.CurrentThread);
  687. try {
  688. try {
  689. Thread.Sleep (3000);
  690. } finally {
  691. try {
  692. Thread.Sleep (0);
  693. } catch (ThreadInterruptedException) {
  694. Assert.Fail ("ThreadInterruptedException thrown twice");
  695. }
  696. }
  697. } catch (ThreadInterruptedException) {
  698. interruptedExceptionThrown = true;
  699. }
  700. Assert.IsTrue (interruptedExceptionThrown, "ThreadInterruptedException expected.");
  701. }
  702. [Test]
  703. [ExpectedException (typeof (ArgumentNullException))]
  704. public void TestQueueUserWorkItemNullCallback ()
  705. {
  706. ThreadPool.QueueUserWorkItem (null, null);
  707. }
  708. private void Test_Interrupt_Worker (object o)
  709. {
  710. Thread t = o as Thread;
  711. Thread.Sleep (100);
  712. t.Interrupt ();
  713. }
  714. [Test]
  715. public void Test_InterruptCurrentThread ()
  716. {
  717. bool interruptedExceptionThrown = false;
  718. try {
  719. try {
  720. Thread.CurrentThread.Interrupt ();
  721. } finally {
  722. try {
  723. Thread.Sleep (0);
  724. } catch (ThreadInterruptedException) {
  725. Assert.Fail ("ThreadInterruptedException should not be thrown.");
  726. }
  727. }
  728. } catch (ThreadInterruptedException) {
  729. interruptedExceptionThrown = true;
  730. }
  731. Assert.IsFalse (interruptedExceptionThrown, "ThreadInterruptedException should not be thrown.");
  732. }
  733. void CheckIsRunning (string s, Thread t)
  734. {
  735. int c = counter;
  736. Thread.Sleep (100);
  737. Assert.IsTrue (counter > c, s);
  738. }
  739. void CheckIsNotRunning (string s, Thread t)
  740. {
  741. int c = counter;
  742. Thread.Sleep (100);
  743. Assert.AreEqual (counter, c, s);
  744. }
  745. void WaitSuspended (string s, Thread t)
  746. {
  747. int n=0;
  748. ThreadState state = t.ThreadState;
  749. while ((state & ThreadState.Suspended) == 0) {
  750. Assert.IsTrue ((state & ThreadState.SuspendRequested) != 0, s + ": expected SuspendRequested state");
  751. Thread.Sleep (10);
  752. n++;
  753. Assert.IsTrue (n < 100, s + ": failed to suspend");
  754. state = t.ThreadState;
  755. }
  756. Assert.IsTrue ((state & ThreadState.SuspendRequested) == 0, s + ": SuspendRequested state not expected");
  757. }
  758. void WaitResumed (string s, Thread t)
  759. {
  760. int n=0;
  761. while ((t.ThreadState & ThreadState.Suspended) != 0) {
  762. Thread.Sleep (10);
  763. n++;
  764. Assert.IsTrue (n < 100, s + ": failed to resume");
  765. }
  766. }
  767. public void DoCount ()
  768. {
  769. while (true) {
  770. counter++;
  771. Thread.Sleep (1);
  772. }
  773. }
  774. }
  775. [TestFixture]
  776. public class ThreadStateTest {
  777. void Start ()
  778. {
  779. }
  780. [Test] // bug #81720
  781. public void IsBackGround ()
  782. {
  783. Thread t1 = new Thread (new ThreadStart (Start));
  784. Assert.AreEqual (ThreadState.Unstarted, t1.ThreadState, "#A1");
  785. Assert.IsFalse (t1.IsBackground, "#A2");
  786. t1.Start ();
  787. t1.Join ();
  788. Assert.AreEqual (ThreadState.Stopped, t1.ThreadState, "#A3");
  789. try {
  790. bool isBackGround = t1.IsBackground;
  791. Assert.Fail ("#A4: " + isBackGround.ToString ());
  792. } catch (ThreadStateException ex) {
  793. Assert.AreEqual (typeof (ThreadStateException), ex.GetType (), "#A5");
  794. Assert.IsNull (ex.InnerException, "#A6");
  795. Assert.IsNotNull (ex.Message, "#A7");
  796. }
  797. Thread t2 = new Thread (new ThreadStart (Start));
  798. Assert.AreEqual (ThreadState.Unstarted, t2.ThreadState, "#B1");
  799. t2.IsBackground = true;
  800. Assert.AreEqual (ThreadState.Unstarted | ThreadState.Background, t2.ThreadState, "#B2");
  801. Assert.IsTrue (t2.IsBackground, "#B3");
  802. t2.Start ();
  803. t2.Join ();
  804. Assert.AreEqual (ThreadState.Stopped, t2.ThreadState, "#B4");
  805. try {
  806. bool isBackGround = t2.IsBackground;
  807. Assert.Fail ("#B5: " + isBackGround.ToString ());
  808. } catch (ThreadStateException ex) {
  809. Assert.AreEqual (typeof (ThreadStateException), ex.GetType (), "#B6");
  810. Assert.IsNull (ex.InnerException, "#B7");
  811. Assert.IsNotNull (ex.Message, "#B8");
  812. }
  813. }
  814. }
  815. [TestFixture]
  816. public class ThreadApartmentTest
  817. {
  818. void Start ()
  819. {
  820. }
  821. [Test] // bug #81658
  822. public void ApartmentState_StoppedThread ()
  823. {
  824. Thread t1 = new Thread (new ThreadStart (Start));
  825. t1.Start ();
  826. t1.Join ();
  827. try {
  828. ApartmentState state = t1.ApartmentState;
  829. Assert.Fail ("#A1: " + state.ToString ());
  830. } catch (ThreadStateException ex) {
  831. Assert.AreEqual (typeof (ThreadStateException), ex.GetType (), "#A2");
  832. Assert.IsNull (ex.InnerException, "#A3");
  833. Assert.IsNotNull (ex.Message, "#A4");
  834. }
  835. Thread t2 = new Thread (new ThreadStart (Start));
  836. t2.IsBackground = true;
  837. t2.Start ();
  838. t2.Join ();
  839. try {
  840. ApartmentState state = t2.ApartmentState;
  841. Assert.Fail ("#B1: " + state.ToString ());
  842. } catch (ThreadStateException ex) {
  843. Assert.AreEqual (typeof (ThreadStateException), ex.GetType (), "#B2");
  844. Assert.IsNull (ex.InnerException, "#B3");
  845. Assert.IsNotNull (ex.Message, "#B4");
  846. }
  847. }
  848. [Test]
  849. public void ApartmentState_BackGround ()
  850. {
  851. Thread t1 = new Thread (new ThreadStart (Start));
  852. t1.IsBackground = true;
  853. Assert.AreEqual (ApartmentState.Unknown, t1.ApartmentState, "#1");
  854. t1.ApartmentState = ApartmentState.STA;
  855. Assert.AreEqual (ApartmentState.STA, t1.ApartmentState, "#2");
  856. }
  857. [Test]
  858. public void TestApartmentState ()
  859. {
  860. Thread t1 = new Thread (new ThreadStart (Start));
  861. Thread t2 = new Thread (new ThreadStart (Start));
  862. Thread t3 = new Thread (new ThreadStart (Start));
  863. Assert.AreEqual (ApartmentState.Unknown, t1.ApartmentState, "Thread1 Default");
  864. Assert.AreEqual (ApartmentState.Unknown, t2.ApartmentState, "Thread2 Default");
  865. Assert.AreEqual (ApartmentState.Unknown, t3.ApartmentState, "Thread3 Default");
  866. t1.ApartmentState = ApartmentState.STA;
  867. Assert.AreEqual (ApartmentState.STA, t1.ApartmentState, "Thread1 Set Once");
  868. t1.ApartmentState = ApartmentState.MTA;
  869. Assert.AreEqual (ApartmentState.STA, t1.ApartmentState, "Thread1 Set Twice");
  870. t2.ApartmentState = ApartmentState.MTA;
  871. Assert.AreEqual (ApartmentState.MTA, t2.ApartmentState, "Thread2 Set Once");
  872. t2.ApartmentState = ApartmentState.STA;
  873. Assert.AreEqual (ApartmentState.MTA, t2.ApartmentState, "Thread2 Set Twice");
  874. bool exception_occured = false;
  875. try {
  876. t3.ApartmentState = ApartmentState.Unknown;
  877. }
  878. catch (Exception) {
  879. exception_occured = true;
  880. }
  881. Assert.AreEqual (ApartmentState.Unknown, t3.ApartmentState, "Thread3 Set Invalid");
  882. #if NET_2_0
  883. Assert.IsFalse (exception_occured, "Thread3 Set Invalid Exception Occured");
  884. #else
  885. Assert.IsTrue (exception_occured, "Thread3 Set Invalid Exception Occured");
  886. #endif
  887. t1.Start ();
  888. exception_occured = false;
  889. try {
  890. t1.ApartmentState = ApartmentState.STA;
  891. }
  892. catch (Exception) {
  893. exception_occured = true;
  894. }
  895. Assert.IsTrue (exception_occured, "Thread1 Started Invalid Exception Occured");
  896. }
  897. }
  898. public class TestUtil
  899. {
  900. public static void WaitForNotAlive (Thread t, string s)
  901. {
  902. WhileAlive (t, true, s);
  903. }
  904. public static void WaitForAlive (Thread t, string s)
  905. {
  906. WhileAlive (t, false, s);
  907. }
  908. public static bool WaitForAliveOrStop (Thread t, string s)
  909. {
  910. return WhileAliveOrStop (t, false, s);
  911. }
  912. public static void WhileAlive (Thread t, bool alive, string s)
  913. {
  914. DateTime ti = DateTime.Now;
  915. while (t.IsAlive == alive) {
  916. if ((DateTime.Now - ti).TotalSeconds > 10) {
  917. if (alive) Assert.Fail ("Timeout while waiting for not alive state. " + s);
  918. else Assert.Fail ("Timeout while waiting for alive state. " + s);
  919. }
  920. }
  921. }
  922. public static bool WhileAliveOrStop (Thread t, bool alive, string s)
  923. {
  924. DateTime ti = DateTime.Now;
  925. while (t.IsAlive == alive) {
  926. if (t.ThreadState == ThreadState.Stopped)
  927. return false;
  928. if ((DateTime.Now - ti).TotalSeconds > 10) {
  929. if (alive) Assert.Fail ("Timeout while waiting for not alive state. " + s);
  930. else Assert.Fail ("Timeout while waiting for alive state. " + s);
  931. }
  932. }
  933. return true;
  934. }
  935. }
  936. }