ThreadTest.cs 37 KB

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