ThreadTest.cs 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854
  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. //Some Classes to test as threads
  79. private class C1Test
  80. {
  81. public int cnt;
  82. public Thread thread1;
  83. public bool endm1;
  84. public bool endm2;
  85. public C1Test()
  86. {
  87. thread1 = (Thread)null;
  88. this.cnt = 0;
  89. endm1 = endm2 = false;
  90. }
  91. public void TestMethod()
  92. {
  93. while (cnt < 10)
  94. {
  95. cnt++;
  96. }
  97. endm1 = true;
  98. }
  99. public void TestMethod2()
  100. {
  101. if (!(thread1==(Thread)null) )
  102. {
  103. thread1.Join();
  104. }
  105. endm2 = true;
  106. }
  107. }
  108. private class C2Test
  109. {
  110. public int cnt;
  111. public bool run = false;
  112. public C2Test()
  113. {
  114. this.cnt = 0;
  115. }
  116. public void TestMethod()
  117. {
  118. run = true;
  119. while (true)
  120. {
  121. if (cnt < 1000)
  122. cnt++;
  123. else
  124. cnt = 0;
  125. }
  126. }
  127. }
  128. private class C3Test
  129. {
  130. public C1Test sub_class;
  131. public Thread sub_thread;
  132. public C3Test()
  133. {
  134. sub_class = new C1Test();
  135. sub_thread = new Thread(new ThreadStart(sub_class.TestMethod));
  136. }
  137. public void TestMethod1()
  138. {
  139. sub_thread.Start();
  140. Thread.Sleep (100);
  141. sub_thread.Abort();
  142. }
  143. }
  144. private class C4Test
  145. {
  146. public C1Test class1;
  147. public C1Test class2;
  148. public Thread thread1;
  149. public Thread thread2;
  150. public bool T1ON ;
  151. public bool T2ON ;
  152. public C4Test()
  153. {
  154. T1ON = false;
  155. T2ON = false;
  156. class1 = new C1Test();
  157. class2 = new C1Test();
  158. thread1 = new Thread(new ThreadStart(class1.TestMethod));
  159. thread2 = new Thread(new ThreadStart(class2.TestMethod));
  160. }
  161. public void TestMethod1()
  162. {
  163. thread1.Start();
  164. TestUtil.WaitForAlive (thread1, "wait1");
  165. T1ON = true;
  166. thread2.Start();
  167. TestUtil.WaitForAlive (thread2, "wait2");
  168. T2ON = true;
  169. thread1.Abort();
  170. TestUtil.WaitForNotAlive (thread1, "wait3");
  171. T1ON = false;
  172. thread2.Abort();
  173. TestUtil.WaitForNotAlive (thread2, "wait4");
  174. T2ON = false;
  175. }
  176. public void TestMethod2()
  177. {
  178. thread1.Start();
  179. thread1.Join();
  180. }
  181. }
  182. [Test]
  183. public void TestCtor1()
  184. {
  185. C1Test test1 = new C1Test();
  186. Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
  187. }
  188. [Test] // bug #325566
  189. public void GetHashCodeTest ()
  190. {
  191. C1Test test1 = new C1Test ();
  192. Thread tA = new Thread (new ThreadStart (test1.TestMethod));
  193. int hA1 = tA.GetHashCode ();
  194. #if NET_2_0
  195. Assert.IsTrue (hA1 > 0, "#A1");
  196. #endif
  197. tA.Start ();
  198. int hA2 = tA.GetHashCode ();
  199. Assert.AreEqual (hA1, hA2, "#A2");
  200. tA.Join ();
  201. int hA3 = tA.GetHashCode ();
  202. Assert.AreEqual (hA1, hA3, "#A3");
  203. #if NET_2_0
  204. Assert.AreEqual (hA1, tA.ManagedThreadId, "#A4");
  205. #endif
  206. test1 = new C1Test ();
  207. Thread tB = new Thread (new ThreadStart (test1.TestMethod));
  208. int hB1 = tB.GetHashCode ();
  209. #if NET_2_0
  210. Assert.IsTrue (hB1 > 0, "#B1");
  211. #endif
  212. tB.Start ();
  213. int hB2 = tB.GetHashCode ();
  214. Assert.AreEqual (hB1, hB2, "#B2");
  215. tB.Join ();
  216. int hB3 = tB.GetHashCode ();
  217. Assert.AreEqual (hB1, hB3, "#B3");
  218. #if NET_2_0
  219. Assert.AreEqual (hB1, tB.ManagedThreadId, "#B4");
  220. #endif
  221. Assert.IsFalse (hA2 == hB2, "#B5");
  222. }
  223. #if NET_2_0
  224. [Test] // bug #82700
  225. public void ManagedThreadId ()
  226. {
  227. C1Test test1 = new C1Test ();
  228. Thread t1 = new Thread (new ThreadStart (test1.TestMethod));
  229. int mtA1 = t1.ManagedThreadId;
  230. t1.Start ();
  231. int mtA2 = t1.ManagedThreadId;
  232. t1.Join ();
  233. int mtA3 = t1.ManagedThreadId;
  234. Assert.AreEqual (mtA1, mtA2, "#A1");
  235. Assert.AreEqual (mtA2, mtA3, "#A2");
  236. test1 = new C1Test ();
  237. Thread t2 = new Thread (new ThreadStart (test1.TestMethod));
  238. int mtB1 = t2.ManagedThreadId;
  239. t2.Start ();
  240. int mtB2 = t2.ManagedThreadId;
  241. t2.Join ();
  242. int mtB3 = t2.ManagedThreadId;
  243. Assert.AreEqual (mtB1, mtB2, "#B1");
  244. Assert.AreEqual (mtB2, mtB3, "#B2");
  245. Assert.IsFalse (mtB1 == mtA1, "#B3");
  246. }
  247. #endif
  248. [Test]
  249. public void TestStart()
  250. {
  251. {
  252. C1Test test1 = new C1Test();
  253. Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
  254. TestThread.Start();
  255. TestThread.Join();
  256. Assert.AreEqual (10, test1.cnt, "#1");
  257. }
  258. {
  259. C2Test test1 = new C2Test();
  260. Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
  261. TestThread.Start();
  262. TestThread.Abort();
  263. try {
  264. TestThread.Start();
  265. Assert.Fail ("#2");
  266. } catch (ThreadStateException) {
  267. }
  268. }
  269. {
  270. C2Test test1 = new C2Test();
  271. Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
  272. TestThread.Start();
  273. while (!test1.run) {
  274. }
  275. bool started = (TestThread.ThreadState == ThreadState.Running);
  276. Assert.AreEqual (started, test1.run, "#15 Thread Is not in the correct state: ");
  277. TestThread.Abort();
  278. }
  279. }
  280. [Test]
  281. public void TestApartmentState ()
  282. {
  283. C2Test test1 = new C2Test();
  284. Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
  285. Assert.AreEqual (ApartmentState.Unknown, TestThread.ApartmentState, "#1");
  286. TestThread.Start();
  287. TestUtil.WaitForAlive (TestThread, "wait5");
  288. #if NET_2_0
  289. Assert.AreEqual (ApartmentState.MTA, TestThread.ApartmentState, "#2");
  290. #else
  291. Assert.AreEqual (ApartmentState.Unknown, TestThread.ApartmentState, "#3");
  292. #endif
  293. TestThread.Abort();
  294. }
  295. [Test]
  296. public void TestPriority1()
  297. {
  298. C2Test test1 = new C2Test();
  299. Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
  300. try {
  301. TestThread.Priority=ThreadPriority.BelowNormal;
  302. ThreadPriority after = TestThread.Priority;
  303. TestThread.Start();
  304. TestUtil.WaitForAlive (TestThread, "wait7");
  305. ThreadPriority before = TestThread.Priority;
  306. Assert.AreEqual (before, after, "#41 Unexpected Priority Change: ");
  307. } finally {
  308. TestThread.Abort();
  309. }
  310. }
  311. [Test]
  312. [Category ("NotDotNet")] // on MS, Thread is still in AbortRequested state when Start is invoked
  313. public void AbortUnstarted ()
  314. {
  315. C2Test test1 = new C2Test();
  316. Thread th = new Thread (new ThreadStart (test1.TestMethod));
  317. th.Abort ();
  318. th.Start ();
  319. }
  320. [Test]
  321. [Category ("NotDotNet")] // on MS, ThreadState is immediately Stopped after Abort
  322. [Category ("NotWorking")] // this is a MonoTODO -> no support for Priority
  323. public void TestPriority2()
  324. {
  325. C2Test test1 = new C2Test();
  326. Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
  327. try {
  328. Assert.AreEqual (ThreadPriority.Normal, TestThread.Priority, "#42 Incorrect Priority in New thread: ");
  329. TestThread.Start();
  330. TestUtil.WaitForAliveOrStop (TestThread, "wait8");
  331. Assert.AreEqual (ThreadPriority.Normal, TestThread.Priority, "#43 Incorrect Priority in Started thread: ");
  332. } finally {
  333. TestThread.Abort();
  334. }
  335. Assert.AreEqual (ThreadPriority.Normal, TestThread.Priority, "#44 Incorrect Priority in Aborted thread: ");
  336. }
  337. [Test]
  338. [Category ("NotWorking")] // this is a MonoTODO -> no support for Priority
  339. public void TestPriority3()
  340. {
  341. C2Test test1 = new C2Test();
  342. Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
  343. try {
  344. TestThread.Start();
  345. TestThread.Priority = ThreadPriority.Lowest;
  346. Assert.AreEqual (ThreadPriority.Lowest, TestThread.Priority, "#45A Incorrect Priority:");
  347. TestThread.Priority = ThreadPriority.BelowNormal;
  348. Assert.AreEqual (ThreadPriority.BelowNormal, TestThread.Priority, "#45B Incorrect Priority:");
  349. TestThread.Priority = ThreadPriority.Normal;
  350. Assert.AreEqual (ThreadPriority.Normal, TestThread.Priority, "#45C Incorrect Priority:");
  351. TestThread.Priority = ThreadPriority.AboveNormal;
  352. Assert.AreEqual (ThreadPriority.AboveNormal, TestThread.Priority, "#45D Incorrect Priority:");
  353. TestThread.Priority = ThreadPriority.Highest;
  354. Assert.AreEqual (ThreadPriority.Highest, TestThread.Priority, "#45E Incorrect Priority:");
  355. }
  356. finally {
  357. TestThread.Abort();
  358. }
  359. }
  360. [Test]
  361. public void TestIsBackground1 ()
  362. {
  363. C2Test test1 = new C2Test();
  364. Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
  365. try {
  366. TestThread.Start();
  367. TestUtil.WaitForAlive (TestThread, "wait9");
  368. bool state = TestThread.IsBackground;
  369. Assert.IsFalse (state, "#51 IsBackground not set at the default state: ");
  370. } finally {
  371. TestThread.Abort();
  372. }
  373. }
  374. [Test]
  375. [Category ("NotDotNet")] // on MS, ThreadState is immediately Stopped after Abort
  376. public void TestIsBackground2 ()
  377. {
  378. C2Test test1 = new C2Test();
  379. Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
  380. TestThread.IsBackground = true;
  381. try {
  382. TestThread.Start();
  383. } finally {
  384. TestThread.Abort();
  385. }
  386. Assert.IsTrue (TestThread.IsBackground, "#52 Is Background Changed to Start ");
  387. }
  388. [Test]
  389. public void TestName()
  390. {
  391. C2Test test1 = new C2Test();
  392. Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
  393. try {
  394. TestThread.Start();
  395. TestUtil.WaitForAlive (TestThread, "wait10");
  396. string name = TestThread.Name;
  397. Assert.IsNull (name, "#61 Name set when mustn't be set: ");
  398. string newname = "Testing....";
  399. TestThread.Name = newname;
  400. Assert.AreEqual (newname, TestThread.Name, "#62 Name not set when must be set: ");
  401. } finally {
  402. TestThread.Abort();
  403. }
  404. }
  405. [Test]
  406. //[Category("NotDotNet")]
  407. public void TestNestedThreads1()
  408. {
  409. C3Test test1 = new C3Test();
  410. Thread TestThread = new Thread(new ThreadStart(test1.TestMethod1));
  411. try {
  412. TestThread.Start();
  413. TestUtil.WaitForAlive (TestThread, "wait11");
  414. } finally {
  415. TestThread.Abort();
  416. }
  417. }
  418. [Test]
  419. public void TestNestedThreads2()
  420. {
  421. C4Test test1 = new C4Test();
  422. Thread TestThread = new Thread(new ThreadStart(test1.TestMethod1));
  423. try {
  424. TestThread.Start();
  425. } finally {
  426. TestThread.Abort();
  427. }
  428. }
  429. [Test]
  430. public void TestJoin1()
  431. {
  432. C1Test test1 = new C1Test();
  433. C1Test test2 = new C1Test();
  434. Thread thread1 = new Thread(new ThreadStart(test1.TestMethod));
  435. Thread thread2 = new Thread(new ThreadStart(test1.TestMethod2));
  436. try {
  437. thread1.Start();
  438. thread2.Start();
  439. thread2.Join();
  440. } finally {
  441. thread1.Abort();
  442. thread2.Abort();
  443. }
  444. }
  445. [Test]
  446. public void TestThreadState()
  447. {
  448. //TODO: Test The rest of the possible transitions
  449. C2Test test1 = new C2Test();
  450. Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
  451. Assert.AreEqual (ThreadState.Unstarted, TestThread.ThreadState, "#101 Wrong Thread State");
  452. try {
  453. TestThread.Start();
  454. //while(!TestThread.IsAlive); //In the MS Documentation this is not necessary
  455. //but in the MS SDK it is
  456. Assert.IsTrue (TestThread.ThreadState == ThreadState.Running || (TestThread.ThreadState & ThreadState.Unstarted) != 0,
  457. "#102 Wrong Thread State: " + TestThread.ThreadState.ToString ());
  458. } finally {
  459. TestThread.Abort();
  460. }
  461. TestUtil.WaitForNotAlive (TestThread, "wait12");
  462. // Docs say state will be Stopped, but Aborted happens sometimes (?)
  463. Assert.IsTrue ((ThreadState.Stopped & TestThread.ThreadState) != 0 || (ThreadState.Aborted & TestThread.ThreadState) != 0,
  464. "#103 Wrong Thread State: " + TestThread.ThreadState.ToString ());
  465. }
  466. [Test]
  467. [Ignore ("see comment below.")]
  468. public void CurrentPrincipal_PrincipalPolicy_NoPrincipal ()
  469. {
  470. // note: switching from PrincipalPolicy won't work inside the same thread
  471. // because as soon as a Principal object is created the Policy doesn't matter anymore
  472. Thread t = new Thread (new ThreadStart (ThreadedPrincipalTest.NoPrincipal));
  473. try {
  474. t.Start ();
  475. t.Join ();
  476. } catch {
  477. t.Abort ();
  478. }
  479. }
  480. #if !TARGET_JVM // AppDomain.SetPrincipalPolicy not supported for TARGET_JVM
  481. [Test]
  482. [Ignore ("see comment below.")]
  483. public void CurrentPrincipal_PrincipalPolicy_UnauthenticatedPrincipal ()
  484. {
  485. // note: switching from PrincipalPolicy won't work inside the same thread
  486. // because as soon as a Principal object is created the Policy doesn't matter anymore
  487. Thread t = new Thread (new ThreadStart (ThreadedPrincipalTest.UnauthenticatedPrincipal));
  488. try {
  489. t.Start ();
  490. t.Join ();
  491. } catch {
  492. t.Abort ();
  493. }
  494. }
  495. [Test]
  496. public void CurrentPrincipal_PrincipalPolicy_WindowsPrincipal ()
  497. {
  498. // note: switching from PrincipalPolicy won't work inside the same thread
  499. // because as soon as a Principal object is created the Policy doesn't matter anymore
  500. Thread t = new Thread (new ThreadStart (ThreadedPrincipalTest.WindowsPrincipal));
  501. try {
  502. t.Start ();
  503. t.Join ();
  504. } catch {
  505. t.Abort ();
  506. }
  507. }
  508. #endif // TARGET_JVM
  509. [Test]
  510. public void IPrincipal_CopyOnNewThread ()
  511. {
  512. Thread.CurrentPrincipal = new GenericPrincipal (new GenericIdentity ("bad"), null);
  513. Thread t = new Thread (new ThreadStart (ThreadedPrincipalTest.CopyOnNewThread));
  514. try {
  515. Thread.CurrentPrincipal = new GenericPrincipal (new GenericIdentity ("good"), null);
  516. t.Start ();
  517. t.Join ();
  518. } catch {
  519. t.Abort ();
  520. }
  521. }
  522. int counter = 0;
  523. [Test]
  524. public void TestSuspend ()
  525. {
  526. Thread t = new Thread (new ThreadStart (DoCount));
  527. t.IsBackground = true;
  528. t.Start ();
  529. CheckIsRunning ("t1", t);
  530. t.Suspend ();
  531. WaitSuspended ("t2", t);
  532. CheckIsNotRunning ("t3", t);
  533. t.Resume ();
  534. WaitResumed ("t4", t);
  535. CheckIsRunning ("t5", t);
  536. t.Abort ();
  537. TestUtil.WaitForNotAlive (t, "wait13");
  538. CheckIsNotRunning ("t6", t);
  539. }
  540. [Test]
  541. [Category("NotDotNet")] // On MS, ThreadStateException is thrown on Abort: "Thread is suspended; attempting to abort"
  542. public void TestSuspendAbort ()
  543. {
  544. Thread t = new Thread (new ThreadStart (DoCount));
  545. t.IsBackground = true;
  546. t.Start ();
  547. CheckIsRunning ("t1", t);
  548. t.Suspend ();
  549. WaitSuspended ("t2", t);
  550. CheckIsNotRunning ("t3", t);
  551. t.Abort ();
  552. int n=0;
  553. while (t.IsAlive && n < 200) {
  554. Thread.Sleep (10);
  555. n++;
  556. }
  557. Assert.IsTrue (n < 200, "Timeout while waiting for abort");
  558. CheckIsNotRunning ("t6", t);
  559. }
  560. void CheckIsRunning (string s, Thread t)
  561. {
  562. int c = counter;
  563. Thread.Sleep (100);
  564. Assert.IsTrue (counter > c, s);
  565. }
  566. void CheckIsNotRunning (string s, Thread t)
  567. {
  568. int c = counter;
  569. Thread.Sleep (100);
  570. Assert.AreEqual (counter, c, s);
  571. }
  572. void WaitSuspended (string s, Thread t)
  573. {
  574. int n=0;
  575. ThreadState state = t.ThreadState;
  576. while ((state & ThreadState.Suspended) == 0) {
  577. Assert.IsTrue ((state & ThreadState.SuspendRequested) != 0, s + ": expected SuspendRequested state");
  578. Thread.Sleep (10);
  579. n++;
  580. Assert.IsTrue (n < 100, s + ": failed to suspend");
  581. state = t.ThreadState;
  582. }
  583. Assert.IsTrue ((state & ThreadState.SuspendRequested) == 0, s + ": SuspendRequested state not expected");
  584. }
  585. void WaitResumed (string s, Thread t)
  586. {
  587. int n=0;
  588. while ((t.ThreadState & ThreadState.Suspended) != 0) {
  589. Thread.Sleep (10);
  590. n++;
  591. Assert.IsTrue (n < 100, s + ": failed to resume");
  592. }
  593. }
  594. public void DoCount ()
  595. {
  596. while (true) {
  597. counter++;
  598. Thread.Sleep (1);
  599. }
  600. }
  601. }
  602. [TestFixture]
  603. public class ThreadStateTest {
  604. void Start ()
  605. {
  606. }
  607. [Test] // bug #81720
  608. public void IsBackGround ()
  609. {
  610. Thread t1 = new Thread (new ThreadStart (Start));
  611. Assert.AreEqual (ThreadState.Unstarted, t1.ThreadState, "#A1");
  612. Assert.IsFalse (t1.IsBackground, "#A2");
  613. t1.Start ();
  614. t1.Join ();
  615. Assert.AreEqual (ThreadState.Stopped, t1.ThreadState, "#A3");
  616. try {
  617. bool isBackGround = t1.IsBackground;
  618. Assert.Fail ("#A4: " + isBackGround.ToString ());
  619. } catch (ThreadStateException ex) {
  620. Assert.AreEqual (typeof (ThreadStateException), ex.GetType (), "#A5");
  621. Assert.IsNull (ex.InnerException, "#A6");
  622. Assert.IsNotNull (ex.Message, "#A7");
  623. }
  624. Thread t2 = new Thread (new ThreadStart (Start));
  625. Assert.AreEqual (ThreadState.Unstarted, t2.ThreadState, "#B1");
  626. t2.IsBackground = true;
  627. Assert.AreEqual (ThreadState.Unstarted | ThreadState.Background, t2.ThreadState, "#B2");
  628. Assert.IsTrue (t2.IsBackground, "#B3");
  629. t2.Start ();
  630. t2.Join ();
  631. Assert.AreEqual (ThreadState.Stopped, t2.ThreadState, "#B4");
  632. try {
  633. bool isBackGround = t2.IsBackground;
  634. Assert.Fail ("#B5: " + isBackGround.ToString ());
  635. } catch (ThreadStateException ex) {
  636. Assert.AreEqual (typeof (ThreadStateException), ex.GetType (), "#B6");
  637. Assert.IsNull (ex.InnerException, "#B7");
  638. Assert.IsNotNull (ex.Message, "#B8");
  639. }
  640. }
  641. }
  642. [TestFixture]
  643. public class ThreadApartmentTest
  644. {
  645. void Start ()
  646. {
  647. }
  648. [Test] // bug #81658
  649. public void ApartmentState_StoppedThread ()
  650. {
  651. Thread t1 = new Thread (new ThreadStart (Start));
  652. t1.Start ();
  653. t1.Join ();
  654. try {
  655. ApartmentState state = t1.ApartmentState;
  656. Assert.Fail ("#A1: " + state.ToString ());
  657. } catch (ThreadStateException ex) {
  658. Assert.AreEqual (typeof (ThreadStateException), ex.GetType (), "#A2");
  659. Assert.IsNull (ex.InnerException, "#A3");
  660. Assert.IsNotNull (ex.Message, "#A4");
  661. }
  662. Thread t2 = new Thread (new ThreadStart (Start));
  663. t2.IsBackground = true;
  664. t2.Start ();
  665. t2.Join ();
  666. try {
  667. ApartmentState state = t2.ApartmentState;
  668. Assert.Fail ("#B1: " + state.ToString ());
  669. } catch (ThreadStateException ex) {
  670. Assert.AreEqual (typeof (ThreadStateException), ex.GetType (), "#B2");
  671. Assert.IsNull (ex.InnerException, "#B3");
  672. Assert.IsNotNull (ex.Message, "#B4");
  673. }
  674. }
  675. [Test]
  676. public void ApartmentState_BackGround ()
  677. {
  678. Thread t1 = new Thread (new ThreadStart (Start));
  679. t1.IsBackground = true;
  680. Assert.AreEqual (ApartmentState.Unknown, t1.ApartmentState, "#1");
  681. t1.ApartmentState = ApartmentState.STA;
  682. Assert.AreEqual (ApartmentState.STA, t1.ApartmentState, "#2");
  683. }
  684. [Test]
  685. public void TestApartmentState ()
  686. {
  687. Thread t1 = new Thread (new ThreadStart (Start));
  688. Thread t2 = new Thread (new ThreadStart (Start));
  689. Thread t3 = new Thread (new ThreadStart (Start));
  690. Assert.AreEqual (ApartmentState.Unknown, t1.ApartmentState, "Thread1 Default");
  691. Assert.AreEqual (ApartmentState.Unknown, t2.ApartmentState, "Thread2 Default");
  692. Assert.AreEqual (ApartmentState.Unknown, t3.ApartmentState, "Thread3 Default");
  693. t1.ApartmentState = ApartmentState.STA;
  694. Assert.AreEqual (ApartmentState.STA, t1.ApartmentState, "Thread1 Set Once");
  695. t1.ApartmentState = ApartmentState.MTA;
  696. Assert.AreEqual (ApartmentState.STA, t1.ApartmentState, "Thread1 Set Twice");
  697. t2.ApartmentState = ApartmentState.MTA;
  698. Assert.AreEqual (ApartmentState.MTA, t2.ApartmentState, "Thread2 Set Once");
  699. t2.ApartmentState = ApartmentState.STA;
  700. Assert.AreEqual (ApartmentState.MTA, t2.ApartmentState, "Thread2 Set Twice");
  701. bool exception_occured = false;
  702. try {
  703. t3.ApartmentState = ApartmentState.Unknown;
  704. }
  705. catch (Exception) {
  706. exception_occured = true;
  707. }
  708. Assert.AreEqual (ApartmentState.Unknown, t3.ApartmentState, "Thread3 Set Invalid");
  709. #if NET_2_0
  710. Assert.IsFalse (exception_occured, "Thread3 Set Invalid Exception Occured");
  711. #else
  712. Assert.IsTrue (exception_occured, "Thread3 Set Invalid Exception Occured");
  713. #endif
  714. t1.Start ();
  715. exception_occured = false;
  716. try {
  717. t1.ApartmentState = ApartmentState.STA;
  718. }
  719. catch (Exception) {
  720. exception_occured = true;
  721. }
  722. Assert.IsTrue (exception_occured, "Thread1 Started Invalid Exception Occured");
  723. }
  724. }
  725. public class TestUtil
  726. {
  727. public static void WaitForNotAlive (Thread t, string s)
  728. {
  729. WhileAlive (t, true, s);
  730. }
  731. public static void WaitForAlive (Thread t, string s)
  732. {
  733. WhileAlive (t, false, s);
  734. }
  735. public static bool WaitForAliveOrStop (Thread t, string s)
  736. {
  737. return WhileAliveOrStop (t, false, s);
  738. }
  739. public static void WhileAlive (Thread t, bool alive, string s)
  740. {
  741. DateTime ti = DateTime.Now;
  742. while (t.IsAlive == alive) {
  743. if ((DateTime.Now - ti).TotalSeconds > 10) {
  744. if (alive) Assert.Fail ("Timeout while waiting for not alive state. " + s);
  745. else Assert.Fail ("Timeout while waiting for alive state. " + s);
  746. }
  747. }
  748. }
  749. public static bool WhileAliveOrStop (Thread t, bool alive, string s)
  750. {
  751. DateTime ti = DateTime.Now;
  752. while (t.IsAlive == alive) {
  753. if (t.ThreadState == ThreadState.Stopped)
  754. return false;
  755. if ((DateTime.Now - ti).TotalSeconds > 10) {
  756. if (alive) Assert.Fail ("Timeout while waiting for not alive state. " + s);
  757. else Assert.Fail ("Timeout while waiting for alive state. " + s);
  758. }
  759. }
  760. return true;
  761. }
  762. }
  763. }