ThreadTest.cs 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. // ThreadTest.cs - NUnit Test Cases for the System.Threading.Thread class
  2. //
  3. // Eduardo Garcia Cebollero ([email protected])
  4. //
  5. // (C) Eduardo Garcia Cebollero.
  6. // (C) Ximian, Inc. http://www.ximian.com
  7. //
  8. using NUnit.Framework;
  9. using System;
  10. using System.Threading;
  11. namespace MonoTests.System.Threading
  12. {
  13. public class ThreadTest : TestCase
  14. {
  15. //Some Classes to test as threads
  16. private class C1Test
  17. {
  18. public int cnt;
  19. public Thread thread1;
  20. public bool endm1;
  21. public bool endm2;
  22. public C1Test()
  23. {
  24. thread1 = (Thread)null;
  25. this.cnt = 0;
  26. endm1 = endm2 = false;
  27. }
  28. public void TestMethod()
  29. {
  30. while (cnt < 10)
  31. {
  32. cnt++;
  33. }
  34. endm1 = true;
  35. }
  36. public void TestMethod2()
  37. {
  38. if (!(thread1==(Thread)null) )
  39. {
  40. thread1.Join();
  41. }
  42. endm2 = true;
  43. }
  44. }
  45. private class C2Test
  46. {
  47. public int cnt;
  48. public bool run = false;
  49. public C2Test()
  50. {
  51. this.cnt = 0;
  52. }
  53. public void TestMethod()
  54. {
  55. run = true;
  56. while (true)
  57. {
  58. if (cnt < 1000)
  59. cnt++;
  60. else
  61. cnt = 0;
  62. }
  63. }
  64. }
  65. private class C3Test
  66. {
  67. public C1Test sub_class;
  68. public Thread sub_thread;
  69. public C3Test()
  70. {
  71. sub_class = new C1Test();
  72. sub_thread = new Thread(new ThreadStart(sub_class.TestMethod));
  73. }
  74. public void TestMethod1()
  75. {
  76. sub_thread.Start();
  77. sub_thread.Abort();
  78. }
  79. }
  80. private class C4Test
  81. {
  82. public C1Test class1;
  83. public C1Test class2;
  84. public Thread thread1;
  85. public Thread thread2;
  86. public bool T1ON ;
  87. public bool T2ON ;
  88. public C4Test()
  89. {
  90. T1ON = false;
  91. T2ON = false;
  92. class1 = new C1Test();
  93. class2 = new C1Test();
  94. thread1 = new Thread(new ThreadStart(class1.TestMethod));
  95. thread2 = new Thread(new ThreadStart(class2.TestMethod));
  96. }
  97. public void TestMethod1()
  98. {
  99. thread1.Start();
  100. while (!thread1.IsAlive);
  101. T1ON = true;
  102. thread2.Start();
  103. while (!thread2.IsAlive);
  104. T2ON = true;
  105. thread1.Abort();
  106. while (thread1.IsAlive);
  107. T1ON = false;
  108. thread2.Abort();
  109. while (thread2.IsAlive);
  110. T2ON = false;
  111. }
  112. public void TestMethod2()
  113. {
  114. thread1.Start();
  115. thread1.Join();
  116. }
  117. }
  118. public void TestCtor1()
  119. {
  120. C1Test test1 = new C1Test();
  121. try
  122. {
  123. Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
  124. }
  125. catch (Exception e)
  126. {
  127. Fail ("#01 Unexpected Exception Thrown: " + e.ToString ());
  128. }
  129. }
  130. public void TestStart()
  131. {
  132. {
  133. C1Test test1 = new C1Test();
  134. Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
  135. try
  136. {
  137. TestThread.Start();
  138. }
  139. catch (Exception e)
  140. {
  141. Fail ("#12 Unexpected Exception Thrown: " + e.ToString ());
  142. }
  143. TestThread.Join();
  144. AssertEquals("#13 Thread Not started: ", 10,test1.cnt);
  145. }
  146. {
  147. bool errorThrown = false;
  148. C2Test test1 = new C2Test();
  149. Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
  150. TestThread.Start();
  151. TestThread.Abort();
  152. try
  153. {
  154. TestThread.Start();
  155. }
  156. catch(ThreadStateException)
  157. {
  158. errorThrown = true;
  159. }
  160. Assert ("#14 no ThreadStateException trown", errorThrown);
  161. }
  162. {
  163. C2Test test1 = new C2Test();
  164. Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
  165. TestThread.Start();
  166. while(!test1.run);
  167. bool started = (TestThread.ThreadState == ThreadState.Running);
  168. AssertEquals("#15 Thread Is not in the correct state: ", started , test1.run);
  169. TestThread.Abort();
  170. }
  171. }
  172. public void TestApartment()
  173. {
  174. C2Test test1 = new C2Test();
  175. Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
  176. ApartmentState before = TestThread.ApartmentState;
  177. TestThread.Start();
  178. while(!TestThread.IsAlive);
  179. ApartmentState after = TestThread.ApartmentState;
  180. TestThread.Abort();
  181. AssertEquals("#21 Apartment State Changed when not needed",before,after);
  182. }
  183. public void TestApartmentState()
  184. {
  185. C2Test test1 = new C2Test();
  186. Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
  187. ApartmentState before = TestThread.ApartmentState;
  188. TestThread.Start();
  189. while(!TestThread.IsAlive);
  190. ApartmentState after = TestThread.ApartmentState;
  191. TestThread.Abort();
  192. AssertEquals("#31 Apartment State Changed when not needed: ",before,after);
  193. }
  194. public void TestPriority1()
  195. {
  196. C2Test test1 = new C2Test();
  197. Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
  198. TestThread.Priority=ThreadPriority.BelowNormal;
  199. ThreadPriority after = TestThread.Priority;
  200. TestThread.Start();
  201. while(!TestThread.IsAlive);
  202. ThreadPriority before = TestThread.Priority;
  203. TestThread.Abort();
  204. AssertEquals("#41 Unexpected Priority Change: ",before,after);
  205. }
  206. public void TestPriority2()
  207. {
  208. C2Test test1 = new C2Test();
  209. Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
  210. AssertEquals("#42 Incorrect Priority in New thread: ",ThreadPriority.Normal, TestThread.Priority);
  211. TestThread.Start();
  212. while(!TestThread.IsAlive);
  213. AssertEquals("#43 Incorrect Priority in Started thread: ",ThreadPriority.Normal, TestThread.Priority);
  214. TestThread.Abort();
  215. AssertEquals("#44 Incorrect Priority in Aborted thread: ",ThreadPriority.Normal, TestThread.Priority);
  216. }
  217. public void TestPriority3()
  218. {
  219. C2Test test1 = new C2Test();
  220. Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
  221. TestThread.Start();
  222. TestThread.Priority = ThreadPriority.Lowest;
  223. AssertEquals("#45A Incorrect Priority:",ThreadPriority.Lowest,TestThread.Priority);
  224. TestThread.Priority = ThreadPriority.BelowNormal;
  225. AssertEquals("#45B Incorrect Priority:",ThreadPriority.BelowNormal,TestThread.Priority);
  226. TestThread.Priority = ThreadPriority.Normal;
  227. AssertEquals("#45C Incorrect Priority:",ThreadPriority.Normal,TestThread.Priority);
  228. TestThread.Priority = ThreadPriority.AboveNormal;
  229. AssertEquals("#45D Incorrect Priority:",ThreadPriority.AboveNormal,TestThread.Priority);
  230. TestThread.Priority = ThreadPriority.Highest;
  231. AssertEquals("#45E Incorrect Priority:",ThreadPriority.Highest,TestThread.Priority);
  232. TestThread.Abort();
  233. }
  234. public void TestIsBackground1()
  235. {
  236. C2Test test1 = new C2Test();
  237. Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
  238. TestThread.Start();
  239. while(!TestThread.IsAlive);
  240. bool state = TestThread.IsBackground;
  241. TestThread.Abort();
  242. Assert("#51 IsBackground not set at the default state: ",!(state));
  243. }
  244. public void TestIsBackground2()
  245. {
  246. C2Test test1 = new C2Test();
  247. Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
  248. TestThread.IsBackground = true;
  249. TestThread.Start();
  250. TestThread.Abort();
  251. Assert("#52 Is Background Changed ot Start ",TestThread.IsBackground);
  252. }
  253. public void TestName()
  254. {
  255. C2Test test1 = new C2Test();
  256. Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
  257. TestThread.Start();
  258. while(!TestThread.IsAlive);
  259. string name = TestThread.Name;
  260. AssertEquals("#61 Name set when mustn't be set: ", name, (string)null);
  261. string newname = "Testing....";
  262. TestThread.Name = newname;
  263. AssertEquals("#62 Name not set when must be set: ",TestThread.Name,newname);
  264. TestThread.Abort();
  265. }
  266. public void TestNestedThreads1()
  267. {
  268. C3Test test1 = new C3Test();
  269. Thread TestThread = new Thread(new ThreadStart(test1.TestMethod1));
  270. try
  271. {
  272. TestThread.Start();
  273. while(!TestThread.IsAlive);
  274. TestThread.Abort();
  275. }
  276. catch(Exception e)
  277. {
  278. Fail("#71 Unexpected Exception" + e.Message);
  279. }
  280. }
  281. public void TestNestedThreads2()
  282. {
  283. C4Test test1 = new C4Test();
  284. test1.thread1.Start();
  285. test1.thread1.Abort();
  286. while(test1.thread1.IsAlive);
  287. Thread TestThread = new Thread(new ThreadStart(test1.TestMethod1));
  288. try
  289. {
  290. TestThread.Start();
  291. TestThread.Abort();
  292. }
  293. catch(Exception e)
  294. {
  295. Fail("#81 Unexpected Exception" + e.ToString());
  296. }
  297. }
  298. public void TestJoin1()
  299. {
  300. C1Test test1 = new C1Test();
  301. C1Test test2 = new C1Test();
  302. Thread thread1 = new Thread(new ThreadStart(test1.TestMethod));
  303. Thread thread2 = new Thread(new ThreadStart(test1.TestMethod2));
  304. try
  305. {
  306. thread1.Start();
  307. thread2.Start();
  308. thread2.Join();
  309. }
  310. catch(Exception e)
  311. {
  312. Fail("#91 Unexpected Exception " + e.ToString());
  313. }
  314. finally
  315. {
  316. thread1.Abort();
  317. thread2.Abort();
  318. }
  319. }
  320. public void TestThreadState()
  321. {
  322. //TODO: Test The rest of the possible transitions
  323. C2Test test1 = new C2Test();
  324. Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
  325. AssertEquals("#101 Wrong Thread State",ThreadState.Unstarted,TestThread.ThreadState);
  326. TestThread.Start();
  327. //while(!TestThread.IsAlive); //In the MS Documentation this is not necessary
  328. //but in the MS SDK it is
  329. AssertEquals("#102 Wrong Thread State", ThreadState.Running | ThreadState.Unstarted ,TestThread.ThreadState);
  330. TestThread.Abort();
  331. while(TestThread.IsAlive);
  332. AssertEquals("#103 Wrong Thread State",ThreadState.Aborted,TestThread.ThreadState);
  333. }
  334. }
  335. }