ThreadTest.cs 23 KB

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