ThreadTest.cs 37 KB

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