ThreadTest.cs 35 KB

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