ThreadTest.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762
  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.Security.Principal;
  13. using System.Threading;
  14. using NUnit.Framework;
  15. namespace MonoTests.System.Threading
  16. {
  17. // These tests seem to hang the 2.0 framework. So they are disabled for now
  18. // Don't reenable them until you can run a few thousand times on an SMP box.
  19. [Category ("NotWorking")]
  20. public class ThreadedPrincipalTest
  21. {
  22. public static void NoPrincipal ()
  23. {
  24. #if !TARGET_JVM // AppDomain.SetPrincipalPolicy not supported for TARGET_JVM
  25. AppDomain.CurrentDomain.SetPrincipalPolicy (PrincipalPolicy.NoPrincipal);
  26. #endif
  27. IPrincipal p = Thread.CurrentPrincipal;
  28. Assert.IsNull (p, "#1");
  29. Thread.CurrentPrincipal = new GenericPrincipal (new GenericIdentity ("mono"), null);
  30. Assert.IsNotNull (Thread.CurrentPrincipal, "#2");
  31. Thread.CurrentPrincipal = null;
  32. Assert.IsNull (Thread.CurrentPrincipal, "#3");
  33. // in this case we can return to null
  34. }
  35. #if !TARGET_JVM // AppDomain.SetPrincipalPolicy not supported for TARGET_JVM
  36. public static void UnauthenticatedPrincipal ()
  37. {
  38. AppDomain.CurrentDomain.SetPrincipalPolicy (PrincipalPolicy.UnauthenticatedPrincipal);
  39. IPrincipal p = Thread.CurrentPrincipal;
  40. Assert.IsNotNull (p, "#1");
  41. Assert.IsTrue ((p is GenericPrincipal), "#2");
  42. Assert.AreEqual (String.Empty, p.Identity.Name, "#3");
  43. Assert.AreEqual (String.Empty, p.Identity.AuthenticationType, "#4");
  44. Assert.IsFalse (p.Identity.IsAuthenticated, "#5");
  45. Thread.CurrentPrincipal = new GenericPrincipal (new GenericIdentity ("mono"), null);
  46. Assert.IsNotNull (Thread.CurrentPrincipal, "#6");
  47. Thread.CurrentPrincipal = null;
  48. Assert.IsNotNull (Thread.CurrentPrincipal, "#7");
  49. // in this case we can't return to null
  50. }
  51. public static void WindowsPrincipal ()
  52. {
  53. AppDomain.CurrentDomain.SetPrincipalPolicy (PrincipalPolicy.WindowsPrincipal);
  54. IPrincipal p = Thread.CurrentPrincipal;
  55. Assert.IsNotNull (p, "#1");
  56. Assert.IsTrue ((p is WindowsPrincipal), "#2");
  57. Assert.IsNotNull (p.Identity.Name, "#3");
  58. Assert.IsNotNull (p.Identity.AuthenticationType, "#4");
  59. Assert.IsTrue (p.Identity.IsAuthenticated, "#5");
  60. // note: we can switch from a WindowsPrincipal to a GenericPrincipal
  61. Thread.CurrentPrincipal = new GenericPrincipal (new GenericIdentity ("mono"), null);
  62. Assert.IsNotNull (Thread.CurrentPrincipal, "#6");
  63. Thread.CurrentPrincipal = null;
  64. Assert.IsNotNull (Thread.CurrentPrincipal, "#7");
  65. // in this case we can't return to null
  66. }
  67. #endif // TARGET_JVM
  68. public static void CopyOnNewThread ()
  69. {
  70. Assert.IsNotNull (Thread.CurrentPrincipal, "#1");
  71. Assert.AreEqual ("good", Thread.CurrentPrincipal.Identity.Name, "#2");
  72. }
  73. }
  74. [TestFixture]
  75. [Category ("NotWorking")]
  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. public void TestCtor1()
  183. {
  184. C1Test test1 = new C1Test();
  185. try
  186. {
  187. Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
  188. }
  189. catch (Exception e)
  190. {
  191. Assert.Fail ("#01 Unexpected Exception Thrown: " + e.ToString ());
  192. }
  193. }
  194. [Category("NotDotNet")]
  195. public void TestStart()
  196. {
  197. {
  198. C1Test test1 = new C1Test();
  199. Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
  200. try
  201. {
  202. TestThread.Start();
  203. }
  204. catch (Exception e)
  205. {
  206. Assert.Fail ("#12 Unexpected Exception Thrown: " + e.ToString ());
  207. }
  208. TestThread.Join();
  209. Assert.AreEqual (10, test1.cnt, "#13 Thread Not started");
  210. }
  211. {
  212. bool errorThrown = false;
  213. C2Test test1 = new C2Test();
  214. Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
  215. TestThread.Start();
  216. TestThread.Abort();
  217. try
  218. {
  219. TestThread.Start();
  220. }
  221. catch(ThreadStateException)
  222. {
  223. errorThrown = true;
  224. }
  225. Assert.IsTrue (errorThrown, "#14 no ThreadStateException trown");
  226. }
  227. {
  228. C2Test test1 = new C2Test();
  229. Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
  230. TestThread.Start();
  231. while(!test1.run);
  232. bool started = (TestThread.ThreadState == ThreadState.Running);
  233. Assert.AreEqual (started, test1.run, "#15 Thread Is not in the correct state: ");
  234. TestThread.Abort();
  235. }
  236. }
  237. public void TestApartment()
  238. {
  239. C2Test test1 = new C2Test();
  240. Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
  241. ApartmentState before = TestThread.ApartmentState;
  242. TestThread.Start();
  243. TestUtil.WaitForAlive (TestThread, "wait5");
  244. ApartmentState after = TestThread.ApartmentState;
  245. TestThread.Abort();
  246. Assert.AreEqual (before, after, "#21 Apartment State Changed when not needed");
  247. }
  248. [Category("NotDotNet")]
  249. public void TestApartmentState()
  250. {
  251. C2Test test1 = new C2Test();
  252. Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
  253. ApartmentState before = TestThread.ApartmentState;
  254. TestThread.Start();
  255. TestUtil.WaitForAlive (TestThread, "wait6");
  256. ApartmentState after = TestThread.ApartmentState;
  257. TestThread.Abort();
  258. Assert.AreEqual (before, after, "#31 Apartment State Changed when not needed: ");
  259. }
  260. public void TestPriority1()
  261. {
  262. C2Test test1 = new C2Test();
  263. Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
  264. try {
  265. TestThread.Priority=ThreadPriority.BelowNormal;
  266. ThreadPriority after = TestThread.Priority;
  267. TestThread.Start();
  268. TestUtil.WaitForAlive (TestThread, "wait7");
  269. ThreadPriority before = TestThread.Priority;
  270. Assert.AreEqual (before, after, "#41 Unexpected Priority Change: ");
  271. }
  272. finally {
  273. TestThread.Abort();
  274. }
  275. }
  276. [Test]
  277. public void AbortUnstarted ()
  278. {
  279. C2Test test1 = new C2Test();
  280. Thread th = new Thread (new ThreadStart (test1.TestMethod));
  281. th.Abort ();
  282. th.Start ();
  283. }
  284. [Category("NotWorking")] // this is a MonoTODO -> no support for Priority
  285. public void TestPriority2()
  286. {
  287. C2Test test1 = new C2Test();
  288. Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
  289. try {
  290. Assert.AreEqual (ThreadPriority.Normal, TestThread.Priority, "#42 Incorrect Priority in New thread: ");
  291. TestThread.Start();
  292. TestUtil.WaitForAliveOrStop (TestThread, "wait8");
  293. Assert.AreEqual (ThreadPriority.Normal, TestThread.Priority, "#43 Incorrect Priority in Started thread: ");
  294. }
  295. finally {
  296. TestThread.Abort();
  297. }
  298. Assert.AreEqual (ThreadPriority.Normal, TestThread.Priority, "#44 Incorrect Priority in Aborted thread: ");
  299. }
  300. [Category("NotWorking")] // this is a MonoTODO -> no support for Priority
  301. public void TestPriority3()
  302. {
  303. C2Test test1 = new C2Test();
  304. Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
  305. try {
  306. TestThread.Start();
  307. TestThread.Priority = ThreadPriority.Lowest;
  308. Assert.AreEqual (ThreadPriority.Lowest, TestThread.Priority, "#45A Incorrect Priority:");
  309. TestThread.Priority = ThreadPriority.BelowNormal;
  310. Assert.AreEqual (ThreadPriority.BelowNormal, TestThread.Priority, "#45B Incorrect Priority:");
  311. TestThread.Priority = ThreadPriority.Normal;
  312. Assert.AreEqual (ThreadPriority.Normal, TestThread.Priority, "#45C Incorrect Priority:");
  313. TestThread.Priority = ThreadPriority.AboveNormal;
  314. Assert.AreEqual (ThreadPriority.AboveNormal, TestThread.Priority, "#45D Incorrect Priority:");
  315. TestThread.Priority = ThreadPriority.Highest;
  316. Assert.AreEqual (ThreadPriority.Highest, TestThread.Priority, "#45E Incorrect Priority:");
  317. }
  318. finally {
  319. TestThread.Abort();
  320. }
  321. }
  322. public void TestIsBackground1()
  323. {
  324. C2Test test1 = new C2Test();
  325. Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
  326. try {
  327. TestThread.Start();
  328. TestUtil.WaitForAlive (TestThread, "wait9");
  329. bool state = TestThread.IsBackground;
  330. Assert.IsFalse (state, "#51 IsBackground not set at the default state: ");
  331. }
  332. finally {
  333. TestThread.Abort();
  334. }
  335. }
  336. public void TestIsBackground2()
  337. {
  338. C2Test test1 = new C2Test();
  339. Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
  340. TestThread.IsBackground = true;
  341. try {
  342. TestThread.Start();
  343. }
  344. finally {
  345. TestThread.Abort();
  346. }
  347. Assert.IsTrue (TestThread.IsBackground, "#52 Is Background Changed ot Start ");
  348. }
  349. public void TestName()
  350. {
  351. C2Test test1 = new C2Test();
  352. Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
  353. try {
  354. TestThread.Start();
  355. TestUtil.WaitForAlive (TestThread, "wait10");
  356. string name = TestThread.Name;
  357. Assert.IsNull (name, "#61 Name set when mustn't be set: ");
  358. string newname = "Testing....";
  359. TestThread.Name = newname;
  360. Assert.AreEqual (newname, TestThread.Name, "#62 Name not set when must be set: ");
  361. }
  362. finally {
  363. TestThread.Abort();
  364. }
  365. }
  366. [Category("NotDotNet")]
  367. public void TestNestedThreads1()
  368. {
  369. C3Test test1 = new C3Test();
  370. Thread TestThread = new Thread(new ThreadStart(test1.TestMethod1));
  371. try {
  372. TestThread.Start();
  373. TestUtil.WaitForAlive (TestThread, "wait11");
  374. }
  375. catch(Exception e) {
  376. Assert.Fail ("#71 Unexpected Exception" + e.Message);
  377. }
  378. finally {
  379. TestThread.Abort();
  380. }
  381. }
  382. public void TestNestedThreads2()
  383. {
  384. C4Test test1 = new C4Test();
  385. Thread TestThread = new Thread(new ThreadStart(test1.TestMethod1));
  386. try {
  387. TestThread.Start();
  388. }
  389. catch(Exception e) {
  390. Assert.Fail ("#81 Unexpected Exception" + e.ToString());
  391. }
  392. finally {
  393. TestThread.Abort();
  394. }
  395. }
  396. public void TestJoin1()
  397. {
  398. C1Test test1 = new C1Test();
  399. C1Test test2 = new C1Test();
  400. Thread thread1 = new Thread(new ThreadStart(test1.TestMethod));
  401. Thread thread2 = new Thread(new ThreadStart(test1.TestMethod2));
  402. try
  403. {
  404. thread1.Start();
  405. thread2.Start();
  406. thread2.Join();
  407. }
  408. catch(Exception e)
  409. {
  410. Assert.Fail ("#91 Unexpected Exception " + e.ToString());
  411. }
  412. finally
  413. {
  414. thread1.Abort();
  415. thread2.Abort();
  416. }
  417. }
  418. public void TestThreadState()
  419. {
  420. //TODO: Test The rest of the possible transitions
  421. C2Test test1 = new C2Test();
  422. Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
  423. Assert.AreEqual (ThreadState.Unstarted, TestThread.ThreadState, "#101 Wrong Thread State");
  424. try {
  425. TestThread.Start();
  426. //while(!TestThread.IsAlive); //In the MS Documentation this is not necessary
  427. //but in the MS SDK it is
  428. Assert.IsTrue (TestThread.ThreadState == ThreadState.Running || (TestThread.ThreadState & ThreadState.Unstarted) != 0,
  429. "#102 Wrong Thread State: " + TestThread.ThreadState.ToString ());
  430. }
  431. finally {
  432. TestThread.Abort();
  433. }
  434. TestUtil.WaitForNotAlive (TestThread, "wait12");
  435. // Docs say state will be Stopped, but Aborted happens sometimes (?)
  436. Assert.IsTrue ((ThreadState.Stopped & TestThread.ThreadState) != 0 || (ThreadState.Aborted & TestThread.ThreadState) != 0,
  437. "#103 Wrong Thread State: " + TestThread.ThreadState.ToString ());
  438. }
  439. [Test]
  440. [Ignore ("see comment below.")]
  441. public void CurrentPrincipal_PrincipalPolicy_NoPrincipal ()
  442. {
  443. // note: switching from PrincipalPolicy won't work inside the same thread
  444. // because as soon as a Principal object is created the Policy doesn't matter anymore
  445. Thread t = new Thread (new ThreadStart (ThreadedPrincipalTest.NoPrincipal));
  446. try {
  447. t.Start ();
  448. t.Join ();
  449. }
  450. catch {
  451. t.Abort ();
  452. }
  453. }
  454. #if !TARGET_JVM // AppDomain.SetPrincipalPolicy not supported for TARGET_JVM
  455. [Test]
  456. [Ignore ("see comment below.")]
  457. public void CurrentPrincipal_PrincipalPolicy_UnauthenticatedPrincipal ()
  458. {
  459. // note: switching from PrincipalPolicy won't work inside the same thread
  460. // because as soon as a Principal object is created the Policy doesn't matter anymore
  461. Thread t = new Thread (new ThreadStart (ThreadedPrincipalTest.UnauthenticatedPrincipal));
  462. try {
  463. t.Start ();
  464. t.Join ();
  465. }
  466. catch {
  467. t.Abort ();
  468. }
  469. }
  470. [Test]
  471. public void CurrentPrincipal_PrincipalPolicy_WindowsPrincipal ()
  472. {
  473. // note: switching from PrincipalPolicy won't work inside the same thread
  474. // because as soon as a Principal object is created the Policy doesn't matter anymore
  475. Thread t = new Thread (new ThreadStart (ThreadedPrincipalTest.WindowsPrincipal));
  476. try {
  477. t.Start ();
  478. t.Join ();
  479. }
  480. catch {
  481. t.Abort ();
  482. }
  483. }
  484. #endif // TARGET_JVM
  485. [Test]
  486. public void IPrincipal_CopyOnNewThread ()
  487. {
  488. Thread.CurrentPrincipal = new GenericPrincipal (new GenericIdentity ("bad"), null);
  489. Thread t = new Thread (new ThreadStart (ThreadedPrincipalTest.CopyOnNewThread));
  490. try {
  491. Thread.CurrentPrincipal = new GenericPrincipal (new GenericIdentity ("good"), null);
  492. t.Start ();
  493. t.Join ();
  494. }
  495. catch {
  496. t.Abort ();
  497. }
  498. }
  499. int counter = 0;
  500. [Category("NotDotNet")]
  501. public void TestSuspend ()
  502. {
  503. Thread t = new Thread (new ThreadStart (DoCount));
  504. t.IsBackground = true;
  505. t.Start ();
  506. CheckIsRunning ("t1", t);
  507. t.Suspend ();
  508. WaitSuspended ("t2", t);
  509. CheckIsNotRunning ("t3", t);
  510. t.Resume ();
  511. WaitResumed ("t4", t);
  512. CheckIsRunning ("t5", t);
  513. t.Abort ();
  514. TestUtil.WaitForNotAlive (t, "wait13");
  515. CheckIsNotRunning ("t6", t);
  516. }
  517. [Category("NotDotNet")]
  518. [Category("NotWorking")]
  519. public void TestSuspendAbort ()
  520. {
  521. Thread t = new Thread (new ThreadStart (DoCount));
  522. t.IsBackground = true;
  523. t.Start ();
  524. CheckIsRunning ("t1", t);
  525. t.Suspend ();
  526. WaitSuspended ("t2", t);
  527. CheckIsNotRunning ("t3", t);
  528. t.Abort ();
  529. int n=0;
  530. while (t.IsAlive && n < 200) {
  531. Thread.Sleep (10);
  532. n++;
  533. }
  534. Assert.IsTrue (n < 200, "Timeout while waiting for abort");
  535. CheckIsNotRunning ("t6", t);
  536. }
  537. void CheckIsRunning (string s, Thread t)
  538. {
  539. int c = counter;
  540. Thread.Sleep (100);
  541. Assert.IsTrue (counter > c, s);
  542. }
  543. void CheckIsNotRunning (string s, Thread t)
  544. {
  545. int c = counter;
  546. Thread.Sleep (100);
  547. Assert.AreEqual (counter, c, s);
  548. }
  549. void WaitSuspended (string s, Thread t)
  550. {
  551. int n=0;
  552. ThreadState state = t.ThreadState;
  553. while ((state & ThreadState.Suspended) == 0) {
  554. Assert.IsTrue ((state & ThreadState.SuspendRequested) != 0, s + ": expected SuspendRequested state");
  555. Thread.Sleep (10);
  556. n++;
  557. Assert.IsTrue (n < 100, s + ": failed to suspend");
  558. state = t.ThreadState;
  559. }
  560. Assert.IsTrue ((state & ThreadState.SuspendRequested) == 0, s + ": SuspendRequested state not expected");
  561. }
  562. void WaitResumed (string s, Thread t)
  563. {
  564. int n=0;
  565. while ((t.ThreadState & ThreadState.Suspended) != 0) {
  566. Thread.Sleep (10);
  567. n++;
  568. Assert.IsTrue (n < 100, s + ": failed to resume");
  569. }
  570. }
  571. public void DoCount ()
  572. {
  573. while (true) {
  574. counter++;
  575. Thread.Sleep (1);
  576. }
  577. }
  578. }
  579. [TestFixture]
  580. public class ThreadApartmentTest
  581. {
  582. void Start ()
  583. {
  584. }
  585. [Test] // bug #81658
  586. [Category ("NotWorking")]
  587. public void ApartmentState_StoppedThread ()
  588. {
  589. Thread t1 = new Thread (new ThreadStart (Start));
  590. t1.Start ();
  591. t1.Join ();
  592. Assert.AreEqual (ThreadState.Stopped, t1.ThreadState, "#1");
  593. try {
  594. ApartmentState state = t1.ApartmentState;
  595. Assert.Fail ("#2");
  596. } catch (ThreadStateException) {
  597. }
  598. }
  599. [Test]
  600. public void TestApartmentState ()
  601. {
  602. Thread t1 = new Thread (new ThreadStart (Start));
  603. Thread t2 = new Thread (new ThreadStart (Start));
  604. Thread t3 = new Thread (new ThreadStart (Start));
  605. Assert.AreEqual (ApartmentState.Unknown, t1.ApartmentState, "Thread1 Default");
  606. Assert.AreEqual (ApartmentState.Unknown, t2.ApartmentState, "Thread2 Default");
  607. Assert.AreEqual (ApartmentState.Unknown, t3.ApartmentState, "Thread3 Default");
  608. t1.ApartmentState = ApartmentState.STA;
  609. Assert.AreEqual (ApartmentState.STA, t1.ApartmentState, "Thread1 Set Once");
  610. t1.ApartmentState = ApartmentState.MTA;
  611. Assert.AreEqual (ApartmentState.STA, t1.ApartmentState, "Thread1 Set Twice");
  612. t2.ApartmentState = ApartmentState.MTA;
  613. Assert.AreEqual (ApartmentState.MTA, t2.ApartmentState, "Thread2 Set Once");
  614. t2.ApartmentState = ApartmentState.STA;
  615. Assert.AreEqual (ApartmentState.MTA, t2.ApartmentState, "Thread2 Set Twice");
  616. bool exception_occured = false;
  617. try {
  618. t3.ApartmentState = ApartmentState.Unknown;
  619. }
  620. catch (Exception) {
  621. exception_occured = true;
  622. }
  623. Assert.AreEqual (ApartmentState.Unknown, t3.ApartmentState, "Thread3 Set Invalid");
  624. #if NET_2_0
  625. Assert.IsFalse (exception_occured, "Thread3 Set Invalid Exception Occured");
  626. #else
  627. Assert.IsTrue (exception_occured, "Thread3 Set Invalid Exception Occured");
  628. #endif
  629. t1.Start ();
  630. exception_occured = false;
  631. try {
  632. t1.ApartmentState = ApartmentState.STA;
  633. }
  634. catch (Exception) {
  635. exception_occured = true;
  636. }
  637. Assert.IsTrue (exception_occured, "Thread1 Started Invalid Exception Occured");
  638. }
  639. }
  640. public class TestUtil
  641. {
  642. public static void WaitForNotAlive (Thread t, string s)
  643. {
  644. WhileAlive (t, true, s);
  645. }
  646. public static void WaitForAlive (Thread t, string s)
  647. {
  648. WhileAlive (t, false, s);
  649. }
  650. public static bool WaitForAliveOrStop (Thread t, string s)
  651. {
  652. return WhileAliveOrStop (t, false, s);
  653. }
  654. public static void WhileAlive (Thread t, bool alive, string s)
  655. {
  656. DateTime ti = DateTime.Now;
  657. while (t.IsAlive == alive) {
  658. if ((DateTime.Now - ti).TotalSeconds > 10) {
  659. if (alive) Assert.Fail ("Timeout while waiting for not alive state. " + s);
  660. else Assert.Fail ("Timeout while waiting for alive state. " + s);
  661. }
  662. }
  663. }
  664. public static bool WhileAliveOrStop (Thread t, bool alive, string s)
  665. {
  666. DateTime ti = DateTime.Now;
  667. while (t.IsAlive == alive) {
  668. if (t.ThreadState == ThreadState.Stopped)
  669. return false;
  670. if ((DateTime.Now - ti).TotalSeconds > 10) {
  671. if (alive) Assert.Fail ("Timeout while waiting for not alive state. " + s);
  672. else Assert.Fail ("Timeout while waiting for alive state. " + s);
  673. }
  674. }
  675. return true;
  676. }
  677. }
  678. }