ThreadTest.cs 24 KB

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