ThreadTest.cs 36 KB

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