ThreadTest.cs 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053
  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. private void Test_Interrupt_Worker (object o)
  703. {
  704. Thread t = o as Thread;
  705. Thread.Sleep (100);
  706. t.Interrupt ();
  707. }
  708. [Test]
  709. public void Test_InterruptCurrentThread ()
  710. {
  711. bool interruptedExceptionThrown = false;
  712. try {
  713. try {
  714. Thread.CurrentThread.Interrupt ();
  715. } finally {
  716. try {
  717. Thread.Sleep (0);
  718. } catch (ThreadInterruptedException) {
  719. Assert.Fail ("ThreadInterruptedException should not be thrown.");
  720. }
  721. }
  722. } catch (ThreadInterruptedException) {
  723. interruptedExceptionThrown = true;
  724. }
  725. Assert.IsFalse (interruptedExceptionThrown, "ThreadInterruptedException should not be thrown.");
  726. }
  727. void CheckIsRunning (string s, Thread t)
  728. {
  729. int c = counter;
  730. Thread.Sleep (100);
  731. Assert.IsTrue (counter > c, s);
  732. }
  733. void CheckIsNotRunning (string s, Thread t)
  734. {
  735. int c = counter;
  736. Thread.Sleep (100);
  737. Assert.AreEqual (counter, c, s);
  738. }
  739. void WaitSuspended (string s, Thread t)
  740. {
  741. int n=0;
  742. ThreadState state = t.ThreadState;
  743. while ((state & ThreadState.Suspended) == 0) {
  744. Assert.IsTrue ((state & ThreadState.SuspendRequested) != 0, s + ": expected SuspendRequested state");
  745. Thread.Sleep (10);
  746. n++;
  747. Assert.IsTrue (n < 100, s + ": failed to suspend");
  748. state = t.ThreadState;
  749. }
  750. Assert.IsTrue ((state & ThreadState.SuspendRequested) == 0, s + ": SuspendRequested state not expected");
  751. }
  752. void WaitResumed (string s, Thread t)
  753. {
  754. int n=0;
  755. while ((t.ThreadState & ThreadState.Suspended) != 0) {
  756. Thread.Sleep (10);
  757. n++;
  758. Assert.IsTrue (n < 100, s + ": failed to resume");
  759. }
  760. }
  761. public void DoCount ()
  762. {
  763. while (true) {
  764. counter++;
  765. Thread.Sleep (1);
  766. }
  767. }
  768. }
  769. [TestFixture]
  770. public class ThreadStateTest {
  771. void Start ()
  772. {
  773. }
  774. [Test] // bug #81720
  775. public void IsBackGround ()
  776. {
  777. Thread t1 = new Thread (new ThreadStart (Start));
  778. Assert.AreEqual (ThreadState.Unstarted, t1.ThreadState, "#A1");
  779. Assert.IsFalse (t1.IsBackground, "#A2");
  780. t1.Start ();
  781. t1.Join ();
  782. Assert.AreEqual (ThreadState.Stopped, t1.ThreadState, "#A3");
  783. try {
  784. bool isBackGround = t1.IsBackground;
  785. Assert.Fail ("#A4: " + isBackGround.ToString ());
  786. } catch (ThreadStateException ex) {
  787. Assert.AreEqual (typeof (ThreadStateException), ex.GetType (), "#A5");
  788. Assert.IsNull (ex.InnerException, "#A6");
  789. Assert.IsNotNull (ex.Message, "#A7");
  790. }
  791. Thread t2 = new Thread (new ThreadStart (Start));
  792. Assert.AreEqual (ThreadState.Unstarted, t2.ThreadState, "#B1");
  793. t2.IsBackground = true;
  794. Assert.AreEqual (ThreadState.Unstarted | ThreadState.Background, t2.ThreadState, "#B2");
  795. Assert.IsTrue (t2.IsBackground, "#B3");
  796. t2.Start ();
  797. t2.Join ();
  798. Assert.AreEqual (ThreadState.Stopped, t2.ThreadState, "#B4");
  799. try {
  800. bool isBackGround = t2.IsBackground;
  801. Assert.Fail ("#B5: " + isBackGround.ToString ());
  802. } catch (ThreadStateException ex) {
  803. Assert.AreEqual (typeof (ThreadStateException), ex.GetType (), "#B6");
  804. Assert.IsNull (ex.InnerException, "#B7");
  805. Assert.IsNotNull (ex.Message, "#B8");
  806. }
  807. }
  808. }
  809. [TestFixture]
  810. public class ThreadApartmentTest
  811. {
  812. void Start ()
  813. {
  814. }
  815. [Test] // bug #81658
  816. public void ApartmentState_StoppedThread ()
  817. {
  818. Thread t1 = new Thread (new ThreadStart (Start));
  819. t1.Start ();
  820. t1.Join ();
  821. try {
  822. ApartmentState state = t1.ApartmentState;
  823. Assert.Fail ("#A1: " + state.ToString ());
  824. } catch (ThreadStateException ex) {
  825. Assert.AreEqual (typeof (ThreadStateException), ex.GetType (), "#A2");
  826. Assert.IsNull (ex.InnerException, "#A3");
  827. Assert.IsNotNull (ex.Message, "#A4");
  828. }
  829. Thread t2 = new Thread (new ThreadStart (Start));
  830. t2.IsBackground = true;
  831. t2.Start ();
  832. t2.Join ();
  833. try {
  834. ApartmentState state = t2.ApartmentState;
  835. Assert.Fail ("#B1: " + state.ToString ());
  836. } catch (ThreadStateException ex) {
  837. Assert.AreEqual (typeof (ThreadStateException), ex.GetType (), "#B2");
  838. Assert.IsNull (ex.InnerException, "#B3");
  839. Assert.IsNotNull (ex.Message, "#B4");
  840. }
  841. }
  842. [Test]
  843. public void ApartmentState_BackGround ()
  844. {
  845. Thread t1 = new Thread (new ThreadStart (Start));
  846. t1.IsBackground = true;
  847. Assert.AreEqual (ApartmentState.Unknown, t1.ApartmentState, "#1");
  848. t1.ApartmentState = ApartmentState.STA;
  849. Assert.AreEqual (ApartmentState.STA, t1.ApartmentState, "#2");
  850. }
  851. [Test]
  852. public void TestApartmentState ()
  853. {
  854. Thread t1 = new Thread (new ThreadStart (Start));
  855. Thread t2 = new Thread (new ThreadStart (Start));
  856. Thread t3 = new Thread (new ThreadStart (Start));
  857. Assert.AreEqual (ApartmentState.Unknown, t1.ApartmentState, "Thread1 Default");
  858. Assert.AreEqual (ApartmentState.Unknown, t2.ApartmentState, "Thread2 Default");
  859. Assert.AreEqual (ApartmentState.Unknown, t3.ApartmentState, "Thread3 Default");
  860. t1.ApartmentState = ApartmentState.STA;
  861. Assert.AreEqual (ApartmentState.STA, t1.ApartmentState, "Thread1 Set Once");
  862. t1.ApartmentState = ApartmentState.MTA;
  863. Assert.AreEqual (ApartmentState.STA, t1.ApartmentState, "Thread1 Set Twice");
  864. t2.ApartmentState = ApartmentState.MTA;
  865. Assert.AreEqual (ApartmentState.MTA, t2.ApartmentState, "Thread2 Set Once");
  866. t2.ApartmentState = ApartmentState.STA;
  867. Assert.AreEqual (ApartmentState.MTA, t2.ApartmentState, "Thread2 Set Twice");
  868. bool exception_occured = false;
  869. try {
  870. t3.ApartmentState = ApartmentState.Unknown;
  871. }
  872. catch (Exception) {
  873. exception_occured = true;
  874. }
  875. Assert.AreEqual (ApartmentState.Unknown, t3.ApartmentState, "Thread3 Set Invalid");
  876. #if NET_2_0
  877. Assert.IsFalse (exception_occured, "Thread3 Set Invalid Exception Occured");
  878. #else
  879. Assert.IsTrue (exception_occured, "Thread3 Set Invalid Exception Occured");
  880. #endif
  881. t1.Start ();
  882. exception_occured = false;
  883. try {
  884. t1.ApartmentState = ApartmentState.STA;
  885. }
  886. catch (Exception) {
  887. exception_occured = true;
  888. }
  889. Assert.IsTrue (exception_occured, "Thread1 Started Invalid Exception Occured");
  890. }
  891. }
  892. public class TestUtil
  893. {
  894. public static void WaitForNotAlive (Thread t, string s)
  895. {
  896. WhileAlive (t, true, s);
  897. }
  898. public static void WaitForAlive (Thread t, string s)
  899. {
  900. WhileAlive (t, false, s);
  901. }
  902. public static bool WaitForAliveOrStop (Thread t, string s)
  903. {
  904. return WhileAliveOrStop (t, false, s);
  905. }
  906. public static void WhileAlive (Thread t, bool alive, string s)
  907. {
  908. DateTime ti = DateTime.Now;
  909. while (t.IsAlive == alive) {
  910. if ((DateTime.Now - ti).TotalSeconds > 10) {
  911. if (alive) Assert.Fail ("Timeout while waiting for not alive state. " + s);
  912. else Assert.Fail ("Timeout while waiting for alive state. " + s);
  913. }
  914. }
  915. }
  916. public static bool WhileAliveOrStop (Thread t, bool alive, string s)
  917. {
  918. DateTime ti = DateTime.Now;
  919. while (t.IsAlive == alive) {
  920. if (t.ThreadState == ThreadState.Stopped)
  921. return false;
  922. if ((DateTime.Now - ti).TotalSeconds > 10) {
  923. if (alive) Assert.Fail ("Timeout while waiting for not alive state. " + s);
  924. else Assert.Fail ("Timeout while waiting for alive state. " + s);
  925. }
  926. }
  927. return true;
  928. }
  929. }
  930. }