ThreadTest.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  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. public class ThreadedPrincipalTest : Assertion {
  17. public static void NoPrincipal ()
  18. {
  19. AppDomain.CurrentDomain.SetPrincipalPolicy (PrincipalPolicy.NoPrincipal);
  20. IPrincipal p = Thread.CurrentPrincipal;
  21. AssertNull ("Thread.CurrentPrincipal-1", p);
  22. Thread.CurrentPrincipal = new GenericPrincipal (new GenericIdentity ("mono"), null);
  23. AssertNotNull ("Thread.CurrentPrincipal-2", Thread.CurrentPrincipal);
  24. Thread.CurrentPrincipal = null;
  25. AssertNull ("Thread.CurrentPrincipal-3", Thread.CurrentPrincipal);
  26. // in this case we can return to null
  27. }
  28. public static void UnauthenticatedPrincipal ()
  29. {
  30. AppDomain.CurrentDomain.SetPrincipalPolicy (PrincipalPolicy.UnauthenticatedPrincipal);
  31. IPrincipal p = Thread.CurrentPrincipal;
  32. AssertNotNull ("Thread.CurrentPrincipal", p);
  33. Assert ("Type", (p is GenericPrincipal));
  34. AssertEquals ("Name", String.Empty, p.Identity.Name);
  35. AssertEquals ("AuthenticationType", String.Empty, p.Identity.AuthenticationType);
  36. Assert ("IsAuthenticated", !p.Identity.IsAuthenticated);
  37. Thread.CurrentPrincipal = new GenericPrincipal (new GenericIdentity ("mono"), null);
  38. AssertNotNull ("Thread.CurrentPrincipal-2", Thread.CurrentPrincipal);
  39. Thread.CurrentPrincipal = null;
  40. AssertNotNull ("Thread.CurrentPrincipal-3", Thread.CurrentPrincipal);
  41. // in this case we can't return to null
  42. }
  43. public static void WindowsPrincipal ()
  44. {
  45. AppDomain.CurrentDomain.SetPrincipalPolicy (PrincipalPolicy.WindowsPrincipal);
  46. IPrincipal p = Thread.CurrentPrincipal;
  47. AssertNotNull ("Thread.CurrentPrincipal", p);
  48. Assert ("Type", (p is WindowsPrincipal));
  49. Assert ("Name", (p.Identity.Name.IndexOf (@"\") > 0)); // DOMAIN\User
  50. AssertEquals ("AuthenticationType", "NTLM", p.Identity.AuthenticationType);
  51. Assert ("IsAuthenticated", p.Identity.IsAuthenticated);
  52. // note: we can switch from a WindowsPrincipal to a GenericPrincipal
  53. Thread.CurrentPrincipal = new GenericPrincipal (new GenericIdentity ("mono"), null);
  54. AssertNotNull ("Thread.CurrentPrincipal-2", Thread.CurrentPrincipal);
  55. Thread.CurrentPrincipal = null;
  56. AssertNotNull ("Thread.CurrentPrincipal-3", Thread.CurrentPrincipal);
  57. // in this case we can't return to null
  58. }
  59. }
  60. [TestFixture]
  61. public class ThreadTest : Assertion {
  62. //Some Classes to test as threads
  63. private class C1Test
  64. {
  65. public int cnt;
  66. public Thread thread1;
  67. public bool endm1;
  68. public bool endm2;
  69. public C1Test()
  70. {
  71. thread1 = (Thread)null;
  72. this.cnt = 0;
  73. endm1 = endm2 = false;
  74. }
  75. public void TestMethod()
  76. {
  77. while (cnt < 10)
  78. {
  79. cnt++;
  80. }
  81. endm1 = true;
  82. }
  83. public void TestMethod2()
  84. {
  85. if (!(thread1==(Thread)null) )
  86. {
  87. thread1.Join();
  88. }
  89. endm2 = true;
  90. }
  91. }
  92. private class C2Test
  93. {
  94. public int cnt;
  95. public bool run = false;
  96. public C2Test()
  97. {
  98. this.cnt = 0;
  99. }
  100. public void TestMethod()
  101. {
  102. run = true;
  103. while (true)
  104. {
  105. if (cnt < 1000)
  106. cnt++;
  107. else
  108. cnt = 0;
  109. }
  110. }
  111. }
  112. private class C3Test
  113. {
  114. public C1Test sub_class;
  115. public Thread sub_thread;
  116. public C3Test()
  117. {
  118. sub_class = new C1Test();
  119. sub_thread = new Thread(new ThreadStart(sub_class.TestMethod));
  120. }
  121. public void TestMethod1()
  122. {
  123. sub_thread.Start();
  124. sub_thread.Abort();
  125. }
  126. }
  127. private class C4Test
  128. {
  129. public C1Test class1;
  130. public C1Test class2;
  131. public Thread thread1;
  132. public Thread thread2;
  133. public bool T1ON ;
  134. public bool T2ON ;
  135. public C4Test()
  136. {
  137. T1ON = false;
  138. T2ON = false;
  139. class1 = new C1Test();
  140. class2 = new C1Test();
  141. thread1 = new Thread(new ThreadStart(class1.TestMethod));
  142. thread2 = new Thread(new ThreadStart(class2.TestMethod));
  143. }
  144. public void TestMethod1()
  145. {
  146. thread1.Start();
  147. while (!thread1.IsAlive);
  148. T1ON = true;
  149. thread2.Start();
  150. while (!thread2.IsAlive);
  151. T2ON = true;
  152. thread1.Abort();
  153. while (thread1.IsAlive);
  154. T1ON = false;
  155. thread2.Abort();
  156. while (thread2.IsAlive);
  157. T2ON = false;
  158. }
  159. public void TestMethod2()
  160. {
  161. thread1.Start();
  162. thread1.Join();
  163. }
  164. }
  165. [Ignore("these tests fail randomly")]
  166. public void TestCtor1()
  167. {
  168. C1Test test1 = new C1Test();
  169. try
  170. {
  171. Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
  172. }
  173. catch (Exception e)
  174. {
  175. Fail ("#01 Unexpected Exception Thrown: " + e.ToString ());
  176. }
  177. }
  178. [Ignore("these tests fail randomly")]
  179. public void TestStart()
  180. {
  181. {
  182. C1Test test1 = new C1Test();
  183. Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
  184. try
  185. {
  186. TestThread.Start();
  187. }
  188. catch (Exception e)
  189. {
  190. Fail ("#12 Unexpected Exception Thrown: " + e.ToString ());
  191. }
  192. TestThread.Join();
  193. AssertEquals("#13 Thread Not started: ", 10,test1.cnt);
  194. }
  195. {
  196. bool errorThrown = false;
  197. C2Test test1 = new C2Test();
  198. Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
  199. TestThread.Start();
  200. TestThread.Abort();
  201. try
  202. {
  203. TestThread.Start();
  204. }
  205. catch(ThreadStateException)
  206. {
  207. errorThrown = true;
  208. }
  209. Assert ("#14 no ThreadStateException trown", errorThrown);
  210. }
  211. {
  212. C2Test test1 = new C2Test();
  213. Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
  214. TestThread.Start();
  215. while(!test1.run);
  216. bool started = (TestThread.ThreadState == ThreadState.Running);
  217. AssertEquals("#15 Thread Is not in the correct state: ", started , test1.run);
  218. TestThread.Abort();
  219. }
  220. }
  221. [Ignore("these tests fail randomly")]
  222. public void TestApartment()
  223. {
  224. C2Test test1 = new C2Test();
  225. Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
  226. ApartmentState before = TestThread.ApartmentState;
  227. TestThread.Start();
  228. while(!TestThread.IsAlive);
  229. ApartmentState after = TestThread.ApartmentState;
  230. TestThread.Abort();
  231. AssertEquals("#21 Apartment State Changed when not needed",before,after);
  232. }
  233. [Ignore("these tests fail randomly")]
  234. public void TestApartmentState()
  235. {
  236. C2Test test1 = new C2Test();
  237. Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
  238. ApartmentState before = TestThread.ApartmentState;
  239. TestThread.Start();
  240. while(!TestThread.IsAlive);
  241. ApartmentState after = TestThread.ApartmentState;
  242. TestThread.Abort();
  243. AssertEquals("#31 Apartment State Changed when not needed: ",before,after);
  244. }
  245. [Ignore("these tests fail randomly")]
  246. public void TestPriority1()
  247. {
  248. C2Test test1 = new C2Test();
  249. Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
  250. TestThread.Priority=ThreadPriority.BelowNormal;
  251. ThreadPriority after = TestThread.Priority;
  252. TestThread.Start();
  253. while(!TestThread.IsAlive);
  254. ThreadPriority before = TestThread.Priority;
  255. TestThread.Abort();
  256. AssertEquals("#41 Unexpected Priority Change: ",before,after);
  257. }
  258. [Ignore("these tests fail randomly")]
  259. public void TestPriority2()
  260. {
  261. C2Test test1 = new C2Test();
  262. Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
  263. AssertEquals("#42 Incorrect Priority in New thread: ",ThreadPriority.Normal, TestThread.Priority);
  264. TestThread.Start();
  265. while(!TestThread.IsAlive);
  266. AssertEquals("#43 Incorrect Priority in Started thread: ",ThreadPriority.Normal, TestThread.Priority);
  267. TestThread.Abort();
  268. AssertEquals("#44 Incorrect Priority in Aborted thread: ",ThreadPriority.Normal, TestThread.Priority);
  269. }
  270. [Ignore("these tests fail randomly")]
  271. public void TestPriority3()
  272. {
  273. C2Test test1 = new C2Test();
  274. Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
  275. TestThread.Start();
  276. TestThread.Priority = ThreadPriority.Lowest;
  277. AssertEquals("#45A Incorrect Priority:",ThreadPriority.Lowest,TestThread.Priority);
  278. TestThread.Priority = ThreadPriority.BelowNormal;
  279. AssertEquals("#45B Incorrect Priority:",ThreadPriority.BelowNormal,TestThread.Priority);
  280. TestThread.Priority = ThreadPriority.Normal;
  281. AssertEquals("#45C Incorrect Priority:",ThreadPriority.Normal,TestThread.Priority);
  282. TestThread.Priority = ThreadPriority.AboveNormal;
  283. AssertEquals("#45D Incorrect Priority:",ThreadPriority.AboveNormal,TestThread.Priority);
  284. TestThread.Priority = ThreadPriority.Highest;
  285. AssertEquals("#45E Incorrect Priority:",ThreadPriority.Highest,TestThread.Priority);
  286. TestThread.Abort();
  287. }
  288. [Ignore("these tests fail randomly")]
  289. public void TestIsBackground1()
  290. {
  291. C2Test test1 = new C2Test();
  292. Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
  293. TestThread.Start();
  294. while(!TestThread.IsAlive);
  295. bool state = TestThread.IsBackground;
  296. TestThread.Abort();
  297. Assert("#51 IsBackground not set at the default state: ",!(state));
  298. }
  299. [Ignore("these tests fail randomly")]
  300. public void TestIsBackground2()
  301. {
  302. C2Test test1 = new C2Test();
  303. Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
  304. TestThread.IsBackground = true;
  305. TestThread.Start();
  306. TestThread.Abort();
  307. Assert("#52 Is Background Changed ot Start ",TestThread.IsBackground);
  308. }
  309. [Ignore("these tests fail randomly")]
  310. public void TestName()
  311. {
  312. C2Test test1 = new C2Test();
  313. Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
  314. TestThread.Start();
  315. while(!TestThread.IsAlive);
  316. string name = TestThread.Name;
  317. AssertEquals("#61 Name set when mustn't be set: ", name, (string)null);
  318. string newname = "Testing....";
  319. TestThread.Name = newname;
  320. AssertEquals("#62 Name not set when must be set: ",TestThread.Name,newname);
  321. TestThread.Abort();
  322. }
  323. [Ignore("these tests fail randomly")]
  324. public void TestNestedThreads1()
  325. {
  326. C3Test test1 = new C3Test();
  327. Thread TestThread = new Thread(new ThreadStart(test1.TestMethod1));
  328. try
  329. {
  330. TestThread.Start();
  331. while(!TestThread.IsAlive);
  332. TestThread.Abort();
  333. }
  334. catch(Exception e)
  335. {
  336. Fail("#71 Unexpected Exception" + e.Message);
  337. }
  338. }
  339. [Ignore("causes seg fault on mono")]
  340. public void TestNestedThreads2()
  341. {
  342. C4Test test1 = new C4Test();
  343. test1.thread1.Start();
  344. test1.thread1.Abort();
  345. while(test1.thread1.IsAlive);
  346. Thread TestThread = new Thread(new ThreadStart(test1.TestMethod1));
  347. try
  348. {
  349. TestThread.Start();
  350. TestThread.Abort();
  351. }
  352. catch(Exception e)
  353. {
  354. Fail("#81 Unexpected Exception" + e.ToString());
  355. }
  356. }
  357. [Ignore("these tests fail randomly")]
  358. public void TestJoin1()
  359. {
  360. C1Test test1 = new C1Test();
  361. C1Test test2 = new C1Test();
  362. Thread thread1 = new Thread(new ThreadStart(test1.TestMethod));
  363. Thread thread2 = new Thread(new ThreadStart(test1.TestMethod2));
  364. try
  365. {
  366. thread1.Start();
  367. thread2.Start();
  368. thread2.Join();
  369. }
  370. catch(Exception e)
  371. {
  372. Fail("#91 Unexpected Exception " + e.ToString());
  373. }
  374. finally
  375. {
  376. thread1.Abort();
  377. thread2.Abort();
  378. }
  379. }
  380. [Ignore("these tests fail randomly")]
  381. public void TestThreadState()
  382. {
  383. //TODO: Test The rest of the possible transitions
  384. C2Test test1 = new C2Test();
  385. Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
  386. AssertEquals("#101 Wrong Thread State",ThreadState.Unstarted,TestThread.ThreadState);
  387. TestThread.Start();
  388. //while(!TestThread.IsAlive); //In the MS Documentation this is not necessary
  389. //but in the MS SDK it is
  390. AssertEquals("#102 Wrong Thread State", ThreadState.Running | ThreadState.Unstarted ,TestThread.ThreadState);
  391. TestThread.Abort();
  392. while(TestThread.IsAlive);
  393. // Docs say state will be Stopped, but Aborted happens sometimes (?)
  394. Assert("#103 Wrong Thread State", ThreadState.Stopped == TestThread.ThreadState
  395. || ThreadState.Aborted == TestThread.ThreadState);
  396. }
  397. [Test]
  398. public void CurrentPrincipal_PrincipalPolicy_NoPrincipal ()
  399. {
  400. // note: switching from PrincipalPolicy won't work inside the same thread
  401. // because as soon as a Principal object is created the Policy doesn't matter anymore
  402. Thread t = new Thread (new ThreadStart (ThreadedPrincipalTest.NoPrincipal));
  403. t.Start ();
  404. while (t.IsAlive) {}
  405. }
  406. [Test]
  407. public void CurrentPrincipal_PrincipalPolicy_UnauthenticatedPrincipal ()
  408. {
  409. // note: switching from PrincipalPolicy won't work inside the same thread
  410. // because as soon as a Principal object is created the Policy doesn't matter anymore
  411. Thread t = new Thread (new ThreadStart (ThreadedPrincipalTest.UnauthenticatedPrincipal));
  412. t.Start ();
  413. while (t.IsAlive) {}
  414. }
  415. [Test]
  416. [Ignore ("WindowsPrincipal is not yet supported on Mono")]
  417. public void CurrentPrincipal_PrincipalPolicy_WindowsPrincipal ()
  418. {
  419. // note: switching from PrincipalPolicy won't work inside the same thread
  420. // because as soon as a Principal object is created the Policy doesn't matter anymore
  421. Thread t = new Thread (new ThreadStart (ThreadedPrincipalTest.WindowsPrincipal));
  422. t.Start ();
  423. while (t.IsAlive) {}
  424. }
  425. }
  426. }