ThreadTest.cs 16 KB

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