ThreadTest.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683
  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 NUnit.Framework;
  12. using System;
  13. using System.Security.Principal;
  14. using System.Threading;
  15. namespace MonoTests.System.Threading {
  16. // These tests seem to hang the 2.0 framework. So they are disabled for now
  17. // Don't reenable them until you can run a few thousand times on an SMP box.
  18. [Category ("NotWorking")]
  19. public class ThreadedPrincipalTest : Assertion {
  20. public static void NoPrincipal ()
  21. {
  22. AppDomain.CurrentDomain.SetPrincipalPolicy (PrincipalPolicy.NoPrincipal);
  23. IPrincipal p = Thread.CurrentPrincipal;
  24. AssertNull ("Thread.CurrentPrincipal-1", p);
  25. Thread.CurrentPrincipal = new GenericPrincipal (new GenericIdentity ("mono"), null);
  26. AssertNotNull ("Thread.CurrentPrincipal-2", Thread.CurrentPrincipal);
  27. Thread.CurrentPrincipal = null;
  28. AssertNull ("Thread.CurrentPrincipal-3", Thread.CurrentPrincipal);
  29. // in this case we can return to null
  30. }
  31. public static void UnauthenticatedPrincipal ()
  32. {
  33. AppDomain.CurrentDomain.SetPrincipalPolicy (PrincipalPolicy.UnauthenticatedPrincipal);
  34. IPrincipal p = Thread.CurrentPrincipal;
  35. AssertNotNull ("Thread.CurrentPrincipal", p);
  36. Assert ("Type", (p is GenericPrincipal));
  37. AssertEquals ("Name", String.Empty, p.Identity.Name);
  38. AssertEquals ("AuthenticationType", String.Empty, p.Identity.AuthenticationType);
  39. Assert ("IsAuthenticated", !p.Identity.IsAuthenticated);
  40. Thread.CurrentPrincipal = new GenericPrincipal (new GenericIdentity ("mono"), null);
  41. AssertNotNull ("Thread.CurrentPrincipal-2", Thread.CurrentPrincipal);
  42. Thread.CurrentPrincipal = null;
  43. AssertNotNull ("Thread.CurrentPrincipal-3", Thread.CurrentPrincipal);
  44. // in this case we can't return to null
  45. }
  46. public static void WindowsPrincipal ()
  47. {
  48. AppDomain.CurrentDomain.SetPrincipalPolicy (PrincipalPolicy.WindowsPrincipal);
  49. IPrincipal p = Thread.CurrentPrincipal;
  50. AssertNotNull ("Thread.CurrentPrincipal", p);
  51. Assert ("Type", (p is WindowsPrincipal));
  52. AssertNotNull ("Name", p.Identity.Name);
  53. AssertNotNull ("AuthenticationType", p.Identity.AuthenticationType);
  54. Assert ("IsAuthenticated", p.Identity.IsAuthenticated);
  55. // note: we can switch from a WindowsPrincipal to a GenericPrincipal
  56. Thread.CurrentPrincipal = new GenericPrincipal (new GenericIdentity ("mono"), null);
  57. AssertNotNull ("Thread.CurrentPrincipal-2", Thread.CurrentPrincipal);
  58. Thread.CurrentPrincipal = null;
  59. AssertNotNull ("Thread.CurrentPrincipal-3", Thread.CurrentPrincipal);
  60. // in this case we can't return to null
  61. }
  62. public static void CopyOnNewThread ()
  63. {
  64. AssertNotNull ("Thread.CurrentPrincipal", Thread.CurrentPrincipal);
  65. AssertEquals ("Identity", "good", Thread.CurrentPrincipal.Identity.Name);
  66. }
  67. }
  68. [TestFixture]
  69. [Category ("NotWorking")]
  70. public class ThreadTest : Assertion {
  71. //Some Classes to test as threads
  72. private class C1Test
  73. {
  74. public int cnt;
  75. public Thread thread1;
  76. public bool endm1;
  77. public bool endm2;
  78. public C1Test()
  79. {
  80. thread1 = (Thread)null;
  81. this.cnt = 0;
  82. endm1 = endm2 = false;
  83. }
  84. public void TestMethod()
  85. {
  86. while (cnt < 10)
  87. {
  88. cnt++;
  89. }
  90. endm1 = true;
  91. }
  92. public void TestMethod2()
  93. {
  94. if (!(thread1==(Thread)null) )
  95. {
  96. thread1.Join();
  97. }
  98. endm2 = true;
  99. }
  100. }
  101. private class C2Test
  102. {
  103. public int cnt;
  104. public bool run = false;
  105. public C2Test()
  106. {
  107. this.cnt = 0;
  108. }
  109. public void TestMethod()
  110. {
  111. run = true;
  112. while (true)
  113. {
  114. if (cnt < 1000)
  115. cnt++;
  116. else
  117. cnt = 0;
  118. }
  119. }
  120. }
  121. private class C3Test
  122. {
  123. public C1Test sub_class;
  124. public Thread sub_thread;
  125. public C3Test()
  126. {
  127. sub_class = new C1Test();
  128. sub_thread = new Thread(new ThreadStart(sub_class.TestMethod));
  129. }
  130. public void TestMethod1()
  131. {
  132. sub_thread.Start();
  133. Thread.Sleep (100);
  134. sub_thread.Abort();
  135. }
  136. }
  137. private class C4Test
  138. {
  139. public C1Test class1;
  140. public C1Test class2;
  141. public Thread thread1;
  142. public Thread thread2;
  143. public bool T1ON ;
  144. public bool T2ON ;
  145. public C4Test()
  146. {
  147. T1ON = false;
  148. T2ON = false;
  149. class1 = new C1Test();
  150. class2 = new C1Test();
  151. thread1 = new Thread(new ThreadStart(class1.TestMethod));
  152. thread2 = new Thread(new ThreadStart(class2.TestMethod));
  153. }
  154. public void TestMethod1()
  155. {
  156. thread1.Start();
  157. TestUtil.WaitForAlive (thread1, "wait1");
  158. T1ON = true;
  159. thread2.Start();
  160. TestUtil.WaitForAlive (thread2, "wait2");
  161. T2ON = true;
  162. thread1.Abort();
  163. TestUtil.WaitForNotAlive (thread1, "wait3");
  164. T1ON = false;
  165. thread2.Abort();
  166. TestUtil.WaitForNotAlive (thread2, "wait4");
  167. T2ON = false;
  168. }
  169. public void TestMethod2()
  170. {
  171. thread1.Start();
  172. thread1.Join();
  173. }
  174. }
  175. public void TestCtor1()
  176. {
  177. C1Test test1 = new C1Test();
  178. try
  179. {
  180. Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
  181. }
  182. catch (Exception e)
  183. {
  184. Fail ("#01 Unexpected Exception Thrown: " + e.ToString ());
  185. }
  186. }
  187. [Category("NotDotNet")]
  188. public void TestStart()
  189. {
  190. {
  191. C1Test test1 = new C1Test();
  192. Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
  193. try
  194. {
  195. TestThread.Start();
  196. }
  197. catch (Exception e)
  198. {
  199. Fail ("#12 Unexpected Exception Thrown: " + e.ToString ());
  200. }
  201. TestThread.Join();
  202. AssertEquals("#13 Thread Not started: ", 10,test1.cnt);
  203. }
  204. {
  205. bool errorThrown = false;
  206. C2Test test1 = new C2Test();
  207. Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
  208. TestThread.Start();
  209. TestThread.Abort();
  210. try
  211. {
  212. TestThread.Start();
  213. }
  214. catch(ThreadStateException)
  215. {
  216. errorThrown = true;
  217. }
  218. Assert ("#14 no ThreadStateException trown", errorThrown);
  219. }
  220. {
  221. C2Test test1 = new C2Test();
  222. Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
  223. TestThread.Start();
  224. while(!test1.run);
  225. bool started = (TestThread.ThreadState == ThreadState.Running);
  226. AssertEquals("#15 Thread Is not in the correct state: ", started , test1.run);
  227. TestThread.Abort();
  228. }
  229. }
  230. public void TestApartment()
  231. {
  232. C2Test test1 = new C2Test();
  233. Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
  234. ApartmentState before = TestThread.ApartmentState;
  235. TestThread.Start();
  236. TestUtil.WaitForAlive (TestThread, "wait5");
  237. ApartmentState after = TestThread.ApartmentState;
  238. TestThread.Abort();
  239. AssertEquals("#21 Apartment State Changed when not needed",before,after);
  240. }
  241. [Category("NotDotNet")]
  242. public void TestApartmentState()
  243. {
  244. C2Test test1 = new C2Test();
  245. Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
  246. ApartmentState before = TestThread.ApartmentState;
  247. TestThread.Start();
  248. TestUtil.WaitForAlive (TestThread, "wait6");
  249. ApartmentState after = TestThread.ApartmentState;
  250. TestThread.Abort();
  251. AssertEquals("#31 Apartment State Changed when not needed: ",before,after);
  252. }
  253. public void TestPriority1()
  254. {
  255. C2Test test1 = new C2Test();
  256. Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
  257. try {
  258. TestThread.Priority=ThreadPriority.BelowNormal;
  259. ThreadPriority after = TestThread.Priority;
  260. TestThread.Start();
  261. TestUtil.WaitForAlive (TestThread, "wait7");
  262. ThreadPriority before = TestThread.Priority;
  263. AssertEquals("#41 Unexpected Priority Change: ",before,after);
  264. }
  265. finally {
  266. TestThread.Abort();
  267. }
  268. }
  269. [Test]
  270. public void AbortUnstarted ()
  271. {
  272. C2Test test1 = new C2Test();
  273. Thread th = new Thread (new ThreadStart (test1.TestMethod));
  274. th.Abort ();
  275. th.Start ();
  276. }
  277. [Category("NotWorking")] // this is a MonoTODO -> no support for Priority
  278. public void TestPriority2()
  279. {
  280. C2Test test1 = new C2Test();
  281. Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
  282. try {
  283. AssertEquals("#42 Incorrect Priority in New thread: ",ThreadPriority.Normal, TestThread.Priority);
  284. TestThread.Start();
  285. TestUtil.WaitForAliveOrStop (TestThread, "wait8");
  286. AssertEquals("#43 Incorrect Priority in Started thread: ",ThreadPriority.Normal, TestThread.Priority);
  287. }
  288. finally {
  289. TestThread.Abort();
  290. }
  291. AssertEquals("#44 Incorrect Priority in Aborted thread: ",ThreadPriority.Normal, TestThread.Priority);
  292. }
  293. [Category("NotWorking")] // this is a MonoTODO -> no support for Priority
  294. public void TestPriority3()
  295. {
  296. C2Test test1 = new C2Test();
  297. Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
  298. try {
  299. TestThread.Start();
  300. TestThread.Priority = ThreadPriority.Lowest;
  301. AssertEquals("#45A Incorrect Priority:",ThreadPriority.Lowest,TestThread.Priority);
  302. TestThread.Priority = ThreadPriority.BelowNormal;
  303. AssertEquals("#45B Incorrect Priority:",ThreadPriority.BelowNormal,TestThread.Priority);
  304. TestThread.Priority = ThreadPriority.Normal;
  305. AssertEquals("#45C Incorrect Priority:",ThreadPriority.Normal,TestThread.Priority);
  306. TestThread.Priority = ThreadPriority.AboveNormal;
  307. AssertEquals("#45D Incorrect Priority:",ThreadPriority.AboveNormal,TestThread.Priority);
  308. TestThread.Priority = ThreadPriority.Highest;
  309. AssertEquals("#45E Incorrect Priority:",ThreadPriority.Highest,TestThread.Priority);
  310. }
  311. finally {
  312. TestThread.Abort();
  313. }
  314. }
  315. public void TestIsBackground1()
  316. {
  317. C2Test test1 = new C2Test();
  318. Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
  319. try {
  320. TestThread.Start();
  321. TestUtil.WaitForAlive (TestThread, "wait9");
  322. bool state = TestThread.IsBackground;
  323. Assert("#51 IsBackground not set at the default state: ",!(state));
  324. }
  325. finally {
  326. TestThread.Abort();
  327. }
  328. }
  329. public void TestIsBackground2()
  330. {
  331. C2Test test1 = new C2Test();
  332. Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
  333. TestThread.IsBackground = true;
  334. try {
  335. TestThread.Start();
  336. }
  337. finally {
  338. TestThread.Abort();
  339. }
  340. Assert("#52 Is Background Changed ot Start ",TestThread.IsBackground);
  341. }
  342. public void TestName()
  343. {
  344. C2Test test1 = new C2Test();
  345. Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
  346. try {
  347. TestThread.Start();
  348. TestUtil.WaitForAlive (TestThread, "wait10");
  349. string name = TestThread.Name;
  350. AssertEquals("#61 Name set when mustn't be set: ", name, (string)null);
  351. string newname = "Testing....";
  352. TestThread.Name = newname;
  353. AssertEquals("#62 Name not set when must be set: ",TestThread.Name,newname);
  354. }
  355. finally {
  356. TestThread.Abort();
  357. }
  358. }
  359. [Category("NotDotNet")]
  360. public void TestNestedThreads1()
  361. {
  362. C3Test test1 = new C3Test();
  363. Thread TestThread = new Thread(new ThreadStart(test1.TestMethod1));
  364. try {
  365. TestThread.Start();
  366. TestUtil.WaitForAlive (TestThread, "wait11");
  367. }
  368. catch(Exception e) {
  369. Fail("#71 Unexpected Exception" + e.Message);
  370. }
  371. finally {
  372. TestThread.Abort();
  373. }
  374. }
  375. public void TestNestedThreads2()
  376. {
  377. C4Test test1 = new C4Test();
  378. Thread TestThread = new Thread(new ThreadStart(test1.TestMethod1));
  379. try {
  380. TestThread.Start();
  381. }
  382. catch(Exception e) {
  383. Fail("#81 Unexpected Exception" + e.ToString());
  384. }
  385. finally {
  386. TestThread.Abort();
  387. }
  388. }
  389. public void TestJoin1()
  390. {
  391. C1Test test1 = new C1Test();
  392. C1Test test2 = new C1Test();
  393. Thread thread1 = new Thread(new ThreadStart(test1.TestMethod));
  394. Thread thread2 = new Thread(new ThreadStart(test1.TestMethod2));
  395. try
  396. {
  397. thread1.Start();
  398. thread2.Start();
  399. thread2.Join();
  400. }
  401. catch(Exception e)
  402. {
  403. Fail("#91 Unexpected Exception " + e.ToString());
  404. }
  405. finally
  406. {
  407. thread1.Abort();
  408. thread2.Abort();
  409. }
  410. }
  411. public void TestThreadState()
  412. {
  413. //TODO: Test The rest of the possible transitions
  414. C2Test test1 = new C2Test();
  415. Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
  416. AssertEquals("#101 Wrong Thread State",ThreadState.Unstarted,TestThread.ThreadState);
  417. try {
  418. TestThread.Start();
  419. //while(!TestThread.IsAlive); //In the MS Documentation this is not necessary
  420. //but in the MS SDK it is
  421. Assert("#102 Wrong Thread State: " + TestThread.ThreadState.ToString(), TestThread.ThreadState == ThreadState.Running || (TestThread.ThreadState & ThreadState.Unstarted) != 0);
  422. }
  423. finally {
  424. TestThread.Abort();
  425. }
  426. TestUtil.WaitForNotAlive (TestThread, "wait12");
  427. // Docs say state will be Stopped, but Aborted happens sometimes (?)
  428. Assert("#103 Wrong Thread State: " + TestThread.ThreadState.ToString(), (ThreadState.Stopped & TestThread.ThreadState) != 0
  429. || (ThreadState.Aborted & TestThread.ThreadState) != 0);
  430. }
  431. [Test]
  432. public void CurrentPrincipal_PrincipalPolicy_NoPrincipal ()
  433. {
  434. // note: switching from PrincipalPolicy won't work inside the same thread
  435. // because as soon as a Principal object is created the Policy doesn't matter anymore
  436. Thread t = new Thread (new ThreadStart (ThreadedPrincipalTest.NoPrincipal));
  437. try {
  438. t.Start ();
  439. t.Join ();
  440. }
  441. catch {
  442. t.Abort ();
  443. }
  444. }
  445. [Test]
  446. public void CurrentPrincipal_PrincipalPolicy_UnauthenticatedPrincipal ()
  447. {
  448. // note: switching from PrincipalPolicy won't work inside the same thread
  449. // because as soon as a Principal object is created the Policy doesn't matter anymore
  450. Thread t = new Thread (new ThreadStart (ThreadedPrincipalTest.UnauthenticatedPrincipal));
  451. try {
  452. t.Start ();
  453. t.Join ();
  454. }
  455. catch {
  456. t.Abort ();
  457. }
  458. }
  459. [Test]
  460. public void CurrentPrincipal_PrincipalPolicy_WindowsPrincipal ()
  461. {
  462. // note: switching from PrincipalPolicy won't work inside the same thread
  463. // because as soon as a Principal object is created the Policy doesn't matter anymore
  464. Thread t = new Thread (new ThreadStart (ThreadedPrincipalTest.WindowsPrincipal));
  465. try {
  466. t.Start ();
  467. t.Join ();
  468. }
  469. catch {
  470. t.Abort ();
  471. }
  472. }
  473. [Test]
  474. public void IPrincipal_CopyOnNewThread ()
  475. {
  476. Thread.CurrentPrincipal = new GenericPrincipal (new GenericIdentity ("bad"), null);
  477. Thread t = new Thread (new ThreadStart (ThreadedPrincipalTest.CopyOnNewThread));
  478. try {
  479. Thread.CurrentPrincipal = new GenericPrincipal (new GenericIdentity ("good"), null);
  480. t.Start ();
  481. t.Join ();
  482. }
  483. catch {
  484. t.Abort ();
  485. }
  486. }
  487. int counter = 0;
  488. [Category("NotDotNet")]
  489. public void TestSuspend ()
  490. {
  491. Thread t = new Thread (new ThreadStart (DoCount));
  492. t.IsBackground = true;
  493. t.Start ();
  494. CheckIsRunning ("t1", t);
  495. t.Suspend ();
  496. WaitSuspended ("t2", t);
  497. CheckIsNotRunning ("t3", t);
  498. t.Resume ();
  499. WaitResumed ("t4", t);
  500. CheckIsRunning ("t5", t);
  501. t.Abort ();
  502. TestUtil.WaitForNotAlive (t, "wait13");
  503. CheckIsNotRunning ("t6", t);
  504. }
  505. [Category("NotDotNet")]
  506. [Category("NotWorking")]
  507. public void TestSuspendAbort ()
  508. {
  509. Thread t = new Thread (new ThreadStart (DoCount));
  510. t.IsBackground = true;
  511. t.Start ();
  512. CheckIsRunning ("t1", t);
  513. t.Suspend ();
  514. WaitSuspended ("t2", t);
  515. CheckIsNotRunning ("t3", t);
  516. t.Abort ();
  517. int n=0;
  518. while (t.IsAlive && n < 200) {
  519. Thread.Sleep (10);
  520. n++;
  521. }
  522. Assert ("Timeout while waiting for abort", n < 200);
  523. CheckIsNotRunning ("t6", t);
  524. }
  525. void CheckIsRunning (string s, Thread t)
  526. {
  527. int c = counter;
  528. Thread.Sleep (100);
  529. Assert (s, counter > c);
  530. }
  531. void CheckIsNotRunning (string s, Thread t)
  532. {
  533. int c = counter;
  534. Thread.Sleep (100);
  535. Assert (s, counter == c);
  536. }
  537. void WaitSuspended (string s, Thread t)
  538. {
  539. int n=0;
  540. ThreadState state = t.ThreadState;
  541. while ((state & ThreadState.Suspended) == 0) {
  542. Assert (s + ": expected SuspendRequested state", (state & ThreadState.SuspendRequested) != 0);
  543. Thread.Sleep (10);
  544. n++;
  545. Assert (s + ": failed to suspend", n < 100);
  546. state = t.ThreadState;
  547. }
  548. Assert (s + ": SuspendRequested state not expected", (state & ThreadState.SuspendRequested) == 0);
  549. }
  550. void WaitResumed (string s, Thread t)
  551. {
  552. int n=0;
  553. while ((t.ThreadState & ThreadState.Suspended) != 0) {
  554. Thread.Sleep (10);
  555. n++;
  556. Assert (s + ": failed to resume", n < 100);
  557. }
  558. }
  559. public void DoCount ()
  560. {
  561. while (true) {
  562. counter++;
  563. Thread.Sleep (1);
  564. }
  565. }
  566. }
  567. public class TestUtil
  568. {
  569. public static void WaitForNotAlive (Thread t, string s)
  570. {
  571. WhileAlive (t, true, s);
  572. }
  573. public static void WaitForAlive (Thread t, string s)
  574. {
  575. WhileAlive (t, false, s);
  576. }
  577. public static bool WaitForAliveOrStop (Thread t, string s)
  578. {
  579. return WhileAliveOrStop (t, false, s);
  580. }
  581. public static void WhileAlive (Thread t, bool alive, string s)
  582. {
  583. DateTime ti = DateTime.Now;
  584. while (t.IsAlive == alive) {
  585. if ((DateTime.Now - ti).TotalSeconds > 10) {
  586. if (alive) Assertion.Fail ("Timeout while waiting for not alive state. " + s);
  587. else Assertion.Fail ("Timeout while waiting for alive state. " + s);
  588. }
  589. }
  590. }
  591. public static bool WhileAliveOrStop (Thread t, bool alive, string s)
  592. {
  593. DateTime ti = DateTime.Now;
  594. while (t.IsAlive == alive) {
  595. if (t.ThreadState == ThreadState.Stopped)
  596. return false;
  597. if ((DateTime.Now - ti).TotalSeconds > 10) {
  598. if (alive) Assertion.Fail ("Timeout while waiting for not alive state. " + s);
  599. else Assertion.Fail ("Timeout while waiting for alive state. " + s);
  600. }
  601. }
  602. return true;
  603. }
  604. }
  605. }