ThreadTest.cs 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163
  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. public class ThreadTest
  77. {
  78. TimeSpan Infinite = new TimeSpan (-10000); // -10000 ticks == -1 ms
  79. TimeSpan SmallNegative = new TimeSpan (-2); // between 0 and -1.0 (infinite) ms
  80. TimeSpan Negative = new TimeSpan (-20000); // really negative
  81. TimeSpan MaxValue = TimeSpan.FromMilliseconds ((long) Int32.MaxValue);
  82. TimeSpan TooLarge = TimeSpan.FromMilliseconds ((long) Int32.MaxValue + 1);
  83. static bool is_win32;
  84. static bool is_mono;
  85. static ThreadTest ()
  86. {
  87. switch (Environment.OSVersion.Platform) {
  88. case PlatformID.Win32NT:
  89. case PlatformID.Win32S:
  90. case PlatformID.Win32Windows:
  91. case PlatformID.WinCE:
  92. is_win32 = true;
  93. break;
  94. }
  95. // check a class in mscorlib to determine if we're running on Mono
  96. if (Type.GetType ("System.MonoType", false) != null)
  97. is_mono = true;
  98. }
  99. //Some Classes to test as threads
  100. private class C1Test
  101. {
  102. public int cnt;
  103. public Thread thread1;
  104. public bool endm1;
  105. public bool endm2;
  106. public C1Test()
  107. {
  108. thread1 = (Thread)null;
  109. this.cnt = 0;
  110. endm1 = endm2 = false;
  111. }
  112. public void TestMethod()
  113. {
  114. while (cnt < 10)
  115. {
  116. cnt++;
  117. }
  118. endm1 = true;
  119. }
  120. public void TestMethod2()
  121. {
  122. if (!(thread1==(Thread)null) )
  123. {
  124. thread1.Join();
  125. }
  126. endm2 = true;
  127. }
  128. }
  129. private class C2Test
  130. {
  131. public int cnt;
  132. public bool run = false;
  133. public C2Test()
  134. {
  135. this.cnt = 0;
  136. }
  137. public void TestMethod()
  138. {
  139. run = true;
  140. while (true)
  141. {
  142. if (cnt < 1000)
  143. cnt++;
  144. else
  145. cnt = 0;
  146. }
  147. }
  148. }
  149. private class C3Test
  150. {
  151. public C1Test sub_class;
  152. public Thread sub_thread;
  153. public C3Test()
  154. {
  155. sub_class = new C1Test();
  156. sub_thread = new Thread(new ThreadStart(sub_class.TestMethod));
  157. }
  158. public void TestMethod1()
  159. {
  160. sub_thread.Start();
  161. Thread.Sleep (100);
  162. sub_thread.Abort();
  163. }
  164. }
  165. private class C4Test
  166. {
  167. public C1Test class1;
  168. public C1Test class2;
  169. public Thread thread1;
  170. public Thread thread2;
  171. public bool T1ON ;
  172. public bool T2ON ;
  173. public C4Test()
  174. {
  175. T1ON = false;
  176. T2ON = false;
  177. class1 = new C1Test();
  178. class2 = new C1Test();
  179. thread1 = new Thread(new ThreadStart(class1.TestMethod));
  180. thread2 = new Thread(new ThreadStart(class2.TestMethod));
  181. }
  182. public void TestMethod1()
  183. {
  184. thread1.Start();
  185. TestUtil.WaitForAlive (thread1, "wait1");
  186. T1ON = true;
  187. thread2.Start();
  188. TestUtil.WaitForAlive (thread2, "wait2");
  189. T2ON = true;
  190. thread1.Abort();
  191. TestUtil.WaitForNotAlive (thread1, "wait3");
  192. T1ON = false;
  193. thread2.Abort();
  194. TestUtil.WaitForNotAlive (thread2, "wait4");
  195. T2ON = false;
  196. }
  197. public void TestMethod2()
  198. {
  199. thread1.Start();
  200. thread1.Join();
  201. }
  202. }
  203. [Test]
  204. public void TestCtor1()
  205. {
  206. C1Test test1 = new C1Test();
  207. Thread t = new Thread (new ThreadStart (test1.TestMethod));
  208. Assert.IsTrue (t.CurrentCulture.IsReadOnly, "CurrentCulture.IsReadOnly");
  209. Assert.IsFalse (t.IsAlive, "IsAlive");
  210. Assert.IsFalse (t.IsBackground, "IsBackground");
  211. Assert.IsNull (t.Name, "Name");
  212. Assert.AreEqual (ThreadState.Unstarted, t.ThreadState, "ThreadState");
  213. }
  214. [Test]
  215. [Category ("NotWorking")] // we're not sharing (read-only) CultureInfo
  216. public void CultureInfo_Shared_Across_Threads ()
  217. {
  218. Thread t = new Thread (TestCtor1);
  219. Assert.AreSame (t.CurrentCulture, t.CurrentUICulture, "Culture");
  220. Assert.AreSame (t.CurrentCulture, CultureInfo.CurrentCulture, "CultureInfo.CurrentCulture");
  221. Assert.AreSame (t.CurrentUICulture, CultureInfo.CurrentUICulture, "CultureInfo.CurrentUICulture");
  222. Assert.AreSame (t.CurrentCulture, Thread.CurrentThread.CurrentCulture, "Thread.CurrentThread.CurrentCulture");
  223. Assert.AreSame (t.CurrentUICulture, Thread.CurrentThread.CurrentUICulture, "Thread.CurrentThread.CurrentUICulture");
  224. }
  225. [Test] // bug #325566
  226. public void GetHashCodeTest ()
  227. {
  228. C1Test test1 = new C1Test ();
  229. Thread tA = new Thread (new ThreadStart (test1.TestMethod));
  230. int hA1 = tA.GetHashCode ();
  231. #if NET_2_0
  232. Assert.IsTrue (hA1 > 0, "#A1");
  233. #endif
  234. tA.Start ();
  235. int hA2 = tA.GetHashCode ();
  236. Assert.AreEqual (hA1, hA2, "#A2");
  237. tA.Join ();
  238. int hA3 = tA.GetHashCode ();
  239. Assert.AreEqual (hA1, hA3, "#A3");
  240. #if NET_2_0
  241. Assert.AreEqual (hA1, tA.ManagedThreadId, "#A4");
  242. #endif
  243. test1 = new C1Test ();
  244. Thread tB = new Thread (new ThreadStart (test1.TestMethod));
  245. int hB1 = tB.GetHashCode ();
  246. #if NET_2_0
  247. Assert.IsTrue (hB1 > 0, "#B1");
  248. #endif
  249. tB.Start ();
  250. int hB2 = tB.GetHashCode ();
  251. Assert.AreEqual (hB1, hB2, "#B2");
  252. tB.Join ();
  253. int hB3 = tB.GetHashCode ();
  254. Assert.AreEqual (hB1, hB3, "#B3");
  255. #if NET_2_0
  256. Assert.AreEqual (hB1, tB.ManagedThreadId, "#B4");
  257. #endif
  258. Assert.IsFalse (hA2 == hB2, "#B5");
  259. }
  260. #if NET_2_0
  261. [Test] // bug #82700
  262. public void ManagedThreadId ()
  263. {
  264. C1Test test1 = new C1Test ();
  265. Thread t1 = new Thread (new ThreadStart (test1.TestMethod));
  266. int mtA1 = t1.ManagedThreadId;
  267. t1.Start ();
  268. int mtA2 = t1.ManagedThreadId;
  269. t1.Join ();
  270. int mtA3 = t1.ManagedThreadId;
  271. Assert.AreEqual (mtA1, mtA2, "#A1");
  272. Assert.AreEqual (mtA2, mtA3, "#A2");
  273. test1 = new C1Test ();
  274. Thread t2 = new Thread (new ThreadStart (test1.TestMethod));
  275. int mtB1 = t2.ManagedThreadId;
  276. t2.Start ();
  277. int mtB2 = t2.ManagedThreadId;
  278. t2.Join ();
  279. int mtB3 = t2.ManagedThreadId;
  280. Assert.AreEqual (mtB1, mtB2, "#B1");
  281. Assert.AreEqual (mtB2, mtB3, "#B2");
  282. Assert.IsFalse (mtB1 == mtA1, "#B3");
  283. }
  284. #endif
  285. [Test]
  286. [Category ("NotDotNet")] // it hangs.
  287. public void TestStart()
  288. {
  289. if (is_win32 && is_mono)
  290. Assert.Fail ("This test fails on Win32. The test should be fixed.");
  291. {
  292. C1Test test1 = new C1Test();
  293. Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
  294. TestThread.Start();
  295. TestThread.Join();
  296. Assert.AreEqual (10, test1.cnt, "#1");
  297. }
  298. {
  299. C2Test test1 = new C2Test();
  300. Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
  301. TestThread.Start();
  302. TestThread.Abort();
  303. try {
  304. TestThread.Start();
  305. Assert.Fail ("#2");
  306. } catch (ThreadStateException) {
  307. }
  308. }
  309. {
  310. C2Test test1 = new C2Test();
  311. Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
  312. TestThread.Start();
  313. while (!test1.run) {
  314. }
  315. bool started = (TestThread.ThreadState == ThreadState.Running);
  316. Assert.AreEqual (started, test1.run, "#15 Thread Is not in the correct state: ");
  317. TestThread.Abort();
  318. }
  319. }
  320. [Test]
  321. public void TestApartmentState ()
  322. {
  323. if (is_win32 && is_mono)
  324. Assert.Fail ("This test fails on mono on win32. Our runtime should be fixed.");
  325. C2Test test1 = new C2Test();
  326. Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
  327. Assert.AreEqual (ApartmentState.Unknown, TestThread.ApartmentState, "#1");
  328. TestThread.Start();
  329. TestUtil.WaitForAlive (TestThread, "wait5");
  330. #if NET_2_0
  331. Assert.AreEqual (ApartmentState.MTA, TestThread.ApartmentState, "#2");
  332. #else
  333. Assert.AreEqual (ApartmentState.Unknown, TestThread.ApartmentState, "#3");
  334. #endif
  335. TestThread.Abort();
  336. }
  337. [Test]
  338. public void TestPriority1()
  339. {
  340. if (is_win32 && is_mono)
  341. Assert.Fail ("This test fails on mono on Win32. Our runtime should be fixed.");
  342. C2Test test1 = new C2Test();
  343. Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
  344. try {
  345. TestThread.Priority=ThreadPriority.BelowNormal;
  346. ThreadPriority after = TestThread.Priority;
  347. TestThread.Start();
  348. TestUtil.WaitForAlive (TestThread, "wait7");
  349. ThreadPriority before = TestThread.Priority;
  350. Assert.AreEqual (before, after, "#41 Unexpected Priority Change: ");
  351. } finally {
  352. TestThread.Abort();
  353. }
  354. }
  355. [Test]
  356. [Category ("NotDotNet")] // on MS, Thread is still in AbortRequested state when Start is invoked
  357. public void AbortUnstarted ()
  358. {
  359. C2Test test1 = new C2Test();
  360. Thread th = new Thread (new ThreadStart (test1.TestMethod));
  361. th.Abort ();
  362. th.Start ();
  363. }
  364. [Test]
  365. [Category ("NotDotNet")] // on MS, ThreadState is immediately Stopped after Abort
  366. [Category ("NotWorking")] // this is a MonoTODO -> no support for Priority
  367. public void TestPriority2()
  368. {
  369. C2Test test1 = new C2Test();
  370. Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
  371. try {
  372. Assert.AreEqual (ThreadPriority.Normal, TestThread.Priority, "#42 Incorrect Priority in New thread: ");
  373. TestThread.Start();
  374. TestUtil.WaitForAliveOrStop (TestThread, "wait8");
  375. Assert.AreEqual (ThreadPriority.Normal, TestThread.Priority, "#43 Incorrect Priority in Started thread: ");
  376. } finally {
  377. TestThread.Abort();
  378. }
  379. Assert.AreEqual (ThreadPriority.Normal, TestThread.Priority, "#44 Incorrect Priority in Aborted thread: ");
  380. }
  381. [Test]
  382. [Category ("NotWorking")] // this is a MonoTODO -> no support for Priority
  383. public void TestPriority3()
  384. {
  385. C2Test test1 = new C2Test();
  386. Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
  387. try {
  388. TestThread.Start();
  389. TestThread.Priority = ThreadPriority.Lowest;
  390. Assert.AreEqual (ThreadPriority.Lowest, TestThread.Priority, "#45A Incorrect Priority:");
  391. TestThread.Priority = ThreadPriority.BelowNormal;
  392. Assert.AreEqual (ThreadPriority.BelowNormal, TestThread.Priority, "#45B Incorrect Priority:");
  393. TestThread.Priority = ThreadPriority.Normal;
  394. Assert.AreEqual (ThreadPriority.Normal, TestThread.Priority, "#45C Incorrect Priority:");
  395. TestThread.Priority = ThreadPriority.AboveNormal;
  396. Assert.AreEqual (ThreadPriority.AboveNormal, TestThread.Priority, "#45D Incorrect Priority:");
  397. TestThread.Priority = ThreadPriority.Highest;
  398. Assert.AreEqual (ThreadPriority.Highest, TestThread.Priority, "#45E Incorrect Priority:");
  399. }
  400. finally {
  401. TestThread.Abort();
  402. }
  403. }
  404. [Test]
  405. public void TestUndivisibleByPageSizeMaxStackSize ()
  406. {
  407. const int undivisible_stacksize = 1048573;
  408. var thread = new Thread (new ThreadStart (delegate {}), undivisible_stacksize);
  409. thread.Start ();
  410. thread.Join ();
  411. }
  412. [Test]
  413. public void TestIsBackground1 ()
  414. {
  415. if (is_win32 && is_mono)
  416. Assert.Fail ("This test fails on mono on Win32. Our runtime should be fixed.");
  417. C2Test test1 = new C2Test();
  418. Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
  419. try {
  420. TestThread.Start();
  421. TestUtil.WaitForAlive (TestThread, "wait9");
  422. bool state = TestThread.IsBackground;
  423. Assert.IsFalse (state, "#51 IsBackground not set at the default state: ");
  424. } finally {
  425. TestThread.Abort();
  426. }
  427. }
  428. [Test]
  429. [Category ("NotDotNet")] // on MS, ThreadState is immediately Stopped after Abort
  430. public void TestIsBackground2 ()
  431. {
  432. C2Test test1 = new C2Test();
  433. Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
  434. TestThread.IsBackground = true;
  435. try {
  436. TestThread.Start();
  437. } finally {
  438. TestThread.Abort();
  439. }
  440. Assert.IsTrue (TestThread.IsBackground, "#52 Is Background Changed to Start ");
  441. }
  442. [Test]
  443. public void TestName()
  444. {
  445. if (is_win32 && is_mono)
  446. Assert.Fail ("This test fails on mono on Win32. Our runtime should be fixed.");
  447. C2Test test1 = new C2Test();
  448. Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
  449. try {
  450. TestThread.Start();
  451. TestUtil.WaitForAlive (TestThread, "wait10");
  452. string name = TestThread.Name;
  453. Assert.IsNull (name, "#61 Name set when mustn't be set: ");
  454. string newname = "Testing....";
  455. TestThread.Name = newname;
  456. Assert.AreEqual (newname, TestThread.Name, "#62 Name not set when must be set: ");
  457. } finally {
  458. TestThread.Abort();
  459. }
  460. }
  461. [Test]
  462. public void Name ()
  463. {
  464. Thread t = new Thread (new ThreadStart (Name));
  465. Assert.IsNull (t.Name, "Name-1");
  466. t.Name = null;
  467. Assert.IsNull (t.Name, "Name-2");
  468. }
  469. [Test]
  470. [ExpectedException (typeof (InvalidOperationException))]
  471. public void ReName ()
  472. {
  473. Thread t = new Thread (new ThreadStart (ReName));
  474. t.Name = "a";
  475. t.Name = "b";
  476. }
  477. [Test]
  478. public void TestNestedThreads1()
  479. {
  480. C3Test test1 = new C3Test();
  481. Thread TestThread = new Thread(new ThreadStart(test1.TestMethod1));
  482. try {
  483. TestThread.Start();
  484. TestUtil.WaitForAlive (TestThread, "wait11");
  485. } finally {
  486. TestThread.Abort();
  487. }
  488. }
  489. [Test]
  490. public void TestNestedThreads2()
  491. {
  492. C4Test test1 = new C4Test();
  493. Thread TestThread = new Thread(new ThreadStart(test1.TestMethod1));
  494. try {
  495. TestThread.Start();
  496. } finally {
  497. TestThread.Abort();
  498. }
  499. }
  500. [Test]
  501. public void TestJoin1()
  502. {
  503. C1Test test1 = new C1Test();
  504. C1Test test2 = new C1Test();
  505. Thread thread1 = new Thread(new ThreadStart(test1.TestMethod));
  506. Thread thread2 = new Thread(new ThreadStart(test1.TestMethod2));
  507. try {
  508. thread1.Start();
  509. thread2.Start();
  510. thread2.Join();
  511. } finally {
  512. thread1.Abort();
  513. thread2.Abort();
  514. }
  515. }
  516. [Test]
  517. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  518. public void Join_Int32_Negative ()
  519. {
  520. // -1 is Timeout.Infinite
  521. Thread.CurrentThread.Join (-2);
  522. }
  523. [Test]
  524. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  525. public void Join_TimeSpan_Negative ()
  526. {
  527. Thread.CurrentThread.Join (Negative);
  528. }
  529. [Test]
  530. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  531. public void Join_TimeSpan_TooLarge ()
  532. {
  533. Thread.CurrentThread.Join (TooLarge);
  534. }
  535. [Test]
  536. public void Join_TimeSpan_SmallNegative ()
  537. {
  538. Thread.CurrentThread.Join (SmallNegative);
  539. }
  540. [Test]
  541. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  542. public void Sleep_Int32_Negative ()
  543. {
  544. // -1 is Timeout.Infinite
  545. Thread.Sleep (-2);
  546. }
  547. [Test]
  548. public void Sleep_TimeSpan_SmallNegative ()
  549. {
  550. Thread.Sleep (SmallNegative);
  551. }
  552. [Test]
  553. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  554. public void Sleep_TimeSpan_Negative ()
  555. {
  556. Thread.Sleep (Negative);
  557. }
  558. [Test]
  559. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  560. public void Sleep_TimeSpan_TooLarge ()
  561. {
  562. Thread.Sleep (TooLarge);
  563. }
  564. [Test]
  565. public void SpinWait ()
  566. {
  567. // no exception for negative numbers
  568. Thread.SpinWait (Int32.MinValue);
  569. Thread.SpinWait (0);
  570. }
  571. [Test]
  572. public void TestThreadState ()
  573. {
  574. if (is_win32 && is_mono)
  575. Assert.Fail ("This test fails on mono on Win32. Our runtime should be fixed.");
  576. //TODO: Test The rest of the possible transitions
  577. C2Test test1 = new C2Test();
  578. Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
  579. Assert.AreEqual (ThreadState.Unstarted, TestThread.ThreadState, "#101 Wrong Thread State");
  580. try {
  581. TestThread.Start();
  582. //while(!TestThread.IsAlive); //In the MS Documentation this is not necessary
  583. //but in the MS SDK it is
  584. Assert.IsTrue (TestThread.ThreadState == ThreadState.Running || (TestThread.ThreadState & ThreadState.Unstarted) != 0,
  585. "#102 Wrong Thread State: " + TestThread.ThreadState.ToString ());
  586. } finally {
  587. TestThread.Abort();
  588. }
  589. TestUtil.WaitForNotAlive (TestThread, "wait12");
  590. // Docs say state will be Stopped, but Aborted happens sometimes (?)
  591. Assert.IsTrue ((ThreadState.Stopped & TestThread.ThreadState) != 0 || (ThreadState.Aborted & TestThread.ThreadState) != 0,
  592. "#103 Wrong Thread State: " + TestThread.ThreadState.ToString ());
  593. }
  594. [Test]
  595. [Ignore ("see comment below.")]
  596. public void CurrentPrincipal_PrincipalPolicy_NoPrincipal ()
  597. {
  598. // note: switching from PrincipalPolicy won't work inside the same thread
  599. // because as soon as a Principal object is created the Policy doesn't matter anymore
  600. Thread t = new Thread (new ThreadStart (ThreadedPrincipalTest.NoPrincipal));
  601. try {
  602. t.Start ();
  603. t.Join ();
  604. } catch {
  605. t.Abort ();
  606. }
  607. }
  608. #if !TARGET_JVM // AppDomain.SetPrincipalPolicy not supported for TARGET_JVM
  609. [Test]
  610. [Ignore ("see comment below.")]
  611. public void CurrentPrincipal_PrincipalPolicy_UnauthenticatedPrincipal ()
  612. {
  613. // note: switching from PrincipalPolicy won't work inside the same thread
  614. // because as soon as a Principal object is created the Policy doesn't matter anymore
  615. Thread t = new Thread (new ThreadStart (ThreadedPrincipalTest.UnauthenticatedPrincipal));
  616. try {
  617. t.Start ();
  618. t.Join ();
  619. } catch {
  620. t.Abort ();
  621. }
  622. }
  623. [Test]
  624. public void CurrentPrincipal_PrincipalPolicy_WindowsPrincipal ()
  625. {
  626. // note: switching from PrincipalPolicy won't work inside the same thread
  627. // because as soon as a Principal object is created the Policy doesn't matter anymore
  628. Thread t = new Thread (new ThreadStart (ThreadedPrincipalTest.WindowsPrincipal));
  629. try {
  630. t.Start ();
  631. t.Join ();
  632. } catch {
  633. t.Abort ();
  634. }
  635. }
  636. #endif // TARGET_JVM
  637. [Test]
  638. public void IPrincipal_CopyOnNewThread ()
  639. {
  640. Thread.CurrentPrincipal = new GenericPrincipal (new GenericIdentity ("bad"), null);
  641. Thread t = new Thread (new ThreadStart (ThreadedPrincipalTest.CopyOnNewThread));
  642. try {
  643. Thread.CurrentPrincipal = new GenericPrincipal (new GenericIdentity ("good"), null);
  644. t.Start ();
  645. t.Join ();
  646. } catch {
  647. t.Abort ();
  648. }
  649. }
  650. int counter = 0;
  651. [Test]
  652. public void TestSuspend ()
  653. {
  654. Thread t = new Thread (new ThreadStart (DoCount));
  655. t.IsBackground = true;
  656. t.Start ();
  657. CheckIsRunning ("t1", t);
  658. t.Suspend ();
  659. WaitSuspended ("t2", t);
  660. CheckIsNotRunning ("t3", t);
  661. t.Resume ();
  662. WaitResumed ("t4", t);
  663. CheckIsRunning ("t5", t);
  664. t.Abort ();
  665. TestUtil.WaitForNotAlive (t, "wait13");
  666. CheckIsNotRunning ("t6", t);
  667. }
  668. [Test]
  669. [Category("NotDotNet")] // On MS, ThreadStateException is thrown on Abort: "Thread is suspended; attempting to abort"
  670. public void TestSuspendAbort ()
  671. {
  672. if (is_win32 && is_mono)
  673. Assert.Fail ("This test fails on Win32. The test should be fixed.");
  674. Thread t = new Thread (new ThreadStart (DoCount));
  675. t.IsBackground = true;
  676. t.Start ();
  677. CheckIsRunning ("t1", t);
  678. t.Suspend ();
  679. WaitSuspended ("t2", t);
  680. CheckIsNotRunning ("t3", t);
  681. t.Abort ();
  682. int n=0;
  683. while (t.IsAlive && n < 200) {
  684. Thread.Sleep (10);
  685. n++;
  686. }
  687. Assert.IsTrue (n < 200, "Timeout while waiting for abort");
  688. CheckIsNotRunning ("t6", t);
  689. }
  690. [Test]
  691. public void Test_Interrupt ()
  692. {
  693. bool interruptedExceptionThrown = false;
  694. ThreadPool.QueueUserWorkItem (Test_Interrupt_Worker, Thread.CurrentThread);
  695. try {
  696. try {
  697. Thread.Sleep (3000);
  698. } finally {
  699. try {
  700. Thread.Sleep (0);
  701. } catch (ThreadInterruptedException) {
  702. Assert.Fail ("ThreadInterruptedException thrown twice");
  703. }
  704. }
  705. } catch (ThreadInterruptedException) {
  706. interruptedExceptionThrown = true;
  707. }
  708. Assert.IsTrue (interruptedExceptionThrown, "ThreadInterruptedException expected.");
  709. }
  710. [Test]
  711. [ExpectedException (typeof (ArgumentNullException))]
  712. public void TestQueueUserWorkItemNullCallback ()
  713. {
  714. ThreadPool.QueueUserWorkItem (null, null);
  715. }
  716. private void Test_Interrupt_Worker (object o)
  717. {
  718. Thread t = o as Thread;
  719. Thread.Sleep (100);
  720. t.Interrupt ();
  721. }
  722. [Test]
  723. [Category ("NotDotNet")] // it crashes nunit.
  724. public void Test_InterruptCurrentThread ()
  725. {
  726. bool interruptedExceptionThrown = false;
  727. Thread.CurrentThread.Interrupt ();
  728. try {
  729. Thread.Sleep (0);
  730. Assert.Fail ();
  731. } catch (ThreadInterruptedException) {
  732. }
  733. }
  734. void CheckIsRunning (string s, Thread t)
  735. {
  736. int c = counter;
  737. Thread.Sleep (100);
  738. Assert.IsTrue (counter > c, s);
  739. }
  740. void CheckIsNotRunning (string s, Thread t)
  741. {
  742. int c = counter;
  743. Thread.Sleep (100);
  744. Assert.AreEqual (counter, c, s);
  745. }
  746. void WaitSuspended (string s, Thread t)
  747. {
  748. int n=0;
  749. ThreadState state = t.ThreadState;
  750. while ((state & ThreadState.Suspended) == 0) {
  751. Assert.IsTrue ((state & ThreadState.SuspendRequested) != 0, s + ": expected SuspendRequested state");
  752. Thread.Sleep (10);
  753. n++;
  754. Assert.IsTrue (n < 100, s + ": failed to suspend");
  755. state = t.ThreadState;
  756. }
  757. Assert.IsTrue ((state & ThreadState.SuspendRequested) == 0, s + ": SuspendRequested state not expected");
  758. }
  759. void WaitResumed (string s, Thread t)
  760. {
  761. int n=0;
  762. while ((t.ThreadState & ThreadState.Suspended) != 0) {
  763. Thread.Sleep (10);
  764. n++;
  765. Assert.IsTrue (n < 100, s + ": failed to resume");
  766. }
  767. }
  768. public void DoCount ()
  769. {
  770. while (true) {
  771. counter++;
  772. Thread.Sleep (1);
  773. }
  774. }
  775. }
  776. [TestFixture]
  777. public class ThreadStateTest {
  778. void Start ()
  779. {
  780. }
  781. [Test] // bug #81720
  782. public void IsBackGround ()
  783. {
  784. Thread t1 = new Thread (new ThreadStart (Start));
  785. Assert.AreEqual (ThreadState.Unstarted, t1.ThreadState, "#A1");
  786. Assert.IsFalse (t1.IsBackground, "#A2");
  787. t1.Start ();
  788. t1.Join ();
  789. Assert.AreEqual (ThreadState.Stopped, t1.ThreadState, "#A3");
  790. try {
  791. bool isBackGround = t1.IsBackground;
  792. Assert.Fail ("#A4: " + isBackGround.ToString ());
  793. } catch (ThreadStateException ex) {
  794. Assert.AreEqual (typeof (ThreadStateException), ex.GetType (), "#A5");
  795. Assert.IsNull (ex.InnerException, "#A6");
  796. Assert.IsNotNull (ex.Message, "#A7");
  797. }
  798. Thread t2 = new Thread (new ThreadStart (Start));
  799. Assert.AreEqual (ThreadState.Unstarted, t2.ThreadState, "#B1");
  800. t2.IsBackground = true;
  801. Assert.AreEqual (ThreadState.Unstarted | ThreadState.Background, t2.ThreadState, "#B2");
  802. Assert.IsTrue (t2.IsBackground, "#B3");
  803. t2.Start ();
  804. t2.Join ();
  805. Assert.AreEqual (ThreadState.Stopped, t2.ThreadState, "#B4");
  806. try {
  807. bool isBackGround = t2.IsBackground;
  808. Assert.Fail ("#B5: " + isBackGround.ToString ());
  809. } catch (ThreadStateException ex) {
  810. Assert.AreEqual (typeof (ThreadStateException), ex.GetType (), "#B6");
  811. Assert.IsNull (ex.InnerException, "#B7");
  812. Assert.IsNotNull (ex.Message, "#B8");
  813. }
  814. }
  815. }
  816. [TestFixture]
  817. [Serializable]
  818. public class ThreadTest_ManagedThreadId
  819. {
  820. AppDomain ad1;
  821. AppDomain ad2;
  822. MBRO mbro = new MBRO ();
  823. class MBRO : MarshalByRefObject {
  824. public int id_a1;
  825. public int id_b1;
  826. public int id_b2;
  827. public string ad_a1;
  828. public string ad_b1;
  829. public string ad_b2;
  830. public string message;
  831. }
  832. [Test]
  833. public void ManagedThreadId_AppDomains ()
  834. {
  835. AppDomain currentDomain = AppDomain.CurrentDomain;
  836. ad1 = AppDomain.CreateDomain ("AppDomain 1", currentDomain.Evidence, currentDomain.SetupInformation);
  837. ad2 = AppDomain.CreateDomain ("AppDomain 2", currentDomain.Evidence, currentDomain.SetupInformation);
  838. Thread a = new Thread (ThreadA);
  839. Thread b = new Thread (ThreadB);
  840. // execute on AppDomain 1 thread A
  841. // execute on AppDomain 2 thread B
  842. // execute on AppDomain 1 thread B - must have same ManagedThreadId as Ad 2 on thread B
  843. a.Start ();
  844. a.Join ();
  845. b.Start ();
  846. b.Join ();
  847. AppDomain.Unload (ad1);
  848. AppDomain.Unload (ad2);
  849. if (mbro.message != null)
  850. Assert.Fail (mbro.message);
  851. // Console.WriteLine ("Done id_a1: {0} id_b1: {1} id_b2: {2} ad_a1: {3} ad_b1: {4} ad_b2: {5}", mbro.id_a1, mbro.id_b1, mbro.id_b2, mbro.ad_a1, mbro.ad_b1, mbro.ad_b2);
  852. Assert.AreEqual ("AppDomain 1", mbro.ad_a1, "Name #1");
  853. Assert.AreEqual ("AppDomain 1", mbro.ad_b1, "Name #2");
  854. Assert.AreEqual ("AppDomain 2", mbro.ad_b2, "Name #3");
  855. Assert.AreNotEqual (mbro.id_a1, mbro.id_b1, "Id #1");
  856. Assert.AreNotEqual (mbro.id_a1, mbro.id_b2, "Id #2");
  857. Assert.AreEqual (mbro.id_b1, mbro.id_b2, "Id #3");
  858. Assert.AreNotEqual (mbro.id_a1, Thread.CurrentThread.ManagedThreadId, "Id #4");
  859. Assert.AreNotEqual (mbro.id_b1, Thread.CurrentThread.ManagedThreadId, "Id #5");
  860. Assert.AreNotEqual (mbro.id_b2, Thread.CurrentThread.ManagedThreadId, "Id #6");
  861. Assert.AreNotEqual (mbro.ad_a1, AppDomain.CurrentDomain.FriendlyName, "Name #4");
  862. Assert.AreNotEqual (mbro.ad_b1, AppDomain.CurrentDomain.FriendlyName, "Name #5");
  863. Assert.AreNotEqual (mbro.ad_b2, AppDomain.CurrentDomain.FriendlyName, "Name #6");
  864. }
  865. void A1 ()
  866. {
  867. mbro.id_a1 = Thread.CurrentThread.ManagedThreadId;
  868. mbro.ad_a1 = AppDomain.CurrentDomain.FriendlyName;
  869. }
  870. void B2 ()
  871. {
  872. mbro.id_b2 = Thread.CurrentThread.ManagedThreadId;
  873. mbro.ad_b2 = AppDomain.CurrentDomain.FriendlyName;
  874. }
  875. void B1 ()
  876. {
  877. mbro.id_b1 = Thread.CurrentThread.ManagedThreadId;
  878. mbro.ad_b1 = AppDomain.CurrentDomain.FriendlyName;
  879. }
  880. void ThreadA (object obj)
  881. {
  882. // Console.WriteLine ("ThreadA");
  883. try {
  884. ad1.DoCallBack (A1);
  885. } catch (Exception ex) {
  886. mbro.message = string.Format ("ThreadA exception: {0}", ex);
  887. }
  888. // Console.WriteLine ("ThreadA Done");
  889. }
  890. void ThreadB (object obj)
  891. {
  892. // Console.WriteLine ("ThreadB");
  893. try {
  894. ad2.DoCallBack (B2);
  895. ad1.DoCallBack (B1);
  896. } catch (Exception ex) {
  897. mbro.message = string.Format ("ThreadB exception: {0}", ex);
  898. }
  899. // Console.WriteLine ("ThreadB Done");
  900. }
  901. }
  902. [TestFixture]
  903. public class ThreadApartmentTest
  904. {
  905. void Start ()
  906. {
  907. }
  908. [Test] // bug #81658
  909. public void ApartmentState_StoppedThread ()
  910. {
  911. Thread t1 = new Thread (new ThreadStart (Start));
  912. t1.Start ();
  913. t1.Join ();
  914. try {
  915. ApartmentState state = t1.ApartmentState;
  916. Assert.Fail ("#A1: " + state.ToString ());
  917. } catch (ThreadStateException ex) {
  918. Assert.AreEqual (typeof (ThreadStateException), ex.GetType (), "#A2");
  919. Assert.IsNull (ex.InnerException, "#A3");
  920. Assert.IsNotNull (ex.Message, "#A4");
  921. }
  922. Thread t2 = new Thread (new ThreadStart (Start));
  923. t2.IsBackground = true;
  924. t2.Start ();
  925. t2.Join ();
  926. try {
  927. ApartmentState state = t2.ApartmentState;
  928. Assert.Fail ("#B1: " + state.ToString ());
  929. } catch (ThreadStateException ex) {
  930. Assert.AreEqual (typeof (ThreadStateException), ex.GetType (), "#B2");
  931. Assert.IsNull (ex.InnerException, "#B3");
  932. Assert.IsNotNull (ex.Message, "#B4");
  933. }
  934. }
  935. [Test]
  936. public void ApartmentState_BackGround ()
  937. {
  938. Thread t1 = new Thread (new ThreadStart (Start));
  939. t1.IsBackground = true;
  940. Assert.AreEqual (ApartmentState.Unknown, t1.ApartmentState, "#1");
  941. t1.ApartmentState = ApartmentState.STA;
  942. Assert.AreEqual (ApartmentState.STA, t1.ApartmentState, "#2");
  943. }
  944. [Test]
  945. public void TestApartmentState ()
  946. {
  947. Thread t1 = new Thread (new ThreadStart (Start));
  948. Thread t2 = new Thread (new ThreadStart (Start));
  949. Thread t3 = new Thread (new ThreadStart (Start));
  950. Assert.AreEqual (ApartmentState.Unknown, t1.ApartmentState, "Thread1 Default");
  951. Assert.AreEqual (ApartmentState.Unknown, t2.ApartmentState, "Thread2 Default");
  952. Assert.AreEqual (ApartmentState.Unknown, t3.ApartmentState, "Thread3 Default");
  953. t1.ApartmentState = ApartmentState.STA;
  954. Assert.AreEqual (ApartmentState.STA, t1.ApartmentState, "Thread1 Set Once");
  955. t1.ApartmentState = ApartmentState.MTA;
  956. Assert.AreEqual (ApartmentState.STA, t1.ApartmentState, "Thread1 Set Twice");
  957. t2.ApartmentState = ApartmentState.MTA;
  958. Assert.AreEqual (ApartmentState.MTA, t2.ApartmentState, "Thread2 Set Once");
  959. t2.ApartmentState = ApartmentState.STA;
  960. Assert.AreEqual (ApartmentState.MTA, t2.ApartmentState, "Thread2 Set Twice");
  961. bool exception_occured = false;
  962. try {
  963. t3.ApartmentState = ApartmentState.Unknown;
  964. }
  965. catch (Exception) {
  966. exception_occured = true;
  967. }
  968. Assert.AreEqual (ApartmentState.Unknown, t3.ApartmentState, "Thread3 Set Invalid");
  969. #if NET_2_0
  970. Assert.IsFalse (exception_occured, "Thread3 Set Invalid Exception Occured");
  971. #else
  972. Assert.IsTrue (exception_occured, "Thread3 Set Invalid Exception Occured");
  973. #endif
  974. t1.Start ();
  975. exception_occured = false;
  976. try {
  977. t1.ApartmentState = ApartmentState.STA;
  978. }
  979. catch (Exception) {
  980. exception_occured = true;
  981. }
  982. Assert.IsTrue (exception_occured, "Thread1 Started Invalid Exception Occured");
  983. }
  984. }
  985. public class TestUtil
  986. {
  987. public static void WaitForNotAlive (Thread t, string s)
  988. {
  989. WhileAlive (t, true, s);
  990. }
  991. public static void WaitForAlive (Thread t, string s)
  992. {
  993. WhileAlive (t, false, s);
  994. }
  995. public static bool WaitForAliveOrStop (Thread t, string s)
  996. {
  997. return WhileAliveOrStop (t, false, s);
  998. }
  999. public static void WhileAlive (Thread t, bool alive, string s)
  1000. {
  1001. DateTime ti = DateTime.Now;
  1002. while (t.IsAlive == alive) {
  1003. if ((DateTime.Now - ti).TotalSeconds > 10) {
  1004. if (alive) Assert.Fail ("Timeout while waiting for not alive state. " + s);
  1005. else Assert.Fail ("Timeout while waiting for alive state. " + s);
  1006. }
  1007. }
  1008. }
  1009. public static bool WhileAliveOrStop (Thread t, bool alive, string s)
  1010. {
  1011. DateTime ti = DateTime.Now;
  1012. while (t.IsAlive == alive) {
  1013. if (t.ThreadState == ThreadState.Stopped)
  1014. return false;
  1015. if ((DateTime.Now - ti).TotalSeconds > 10) {
  1016. if (alive) Assert.Fail ("Timeout while waiting for not alive state. " + s);
  1017. else Assert.Fail ("Timeout while waiting for alive state. " + s);
  1018. }
  1019. }
  1020. return true;
  1021. }
  1022. }
  1023. }