ThreadTest.cs 38 KB

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