ThreadTest.cs 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377
  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. Task t = Task.Run (delegate () {
  819. Thread.CurrentThread.Name = "ThreadName1";
  820. Assert.AreEqual (Thread.CurrentThread.Name, "ThreadName1", "#1");
  821. try {
  822. Thread.CurrentThread.Name = "ThreadName2";
  823. Assert.Fail ("#2");
  824. } catch (InvalidOperationException) {
  825. }
  826. });
  827. t.Wait ();
  828. }
  829. void CheckIsRunning (string s, Thread t)
  830. {
  831. int c = counter;
  832. Thread.Sleep (100);
  833. Assert.IsTrue (counter > c, s);
  834. }
  835. void CheckIsNotRunning (string s, Thread t)
  836. {
  837. int c = counter;
  838. Thread.Sleep (100);
  839. Assert.AreEqual (counter, c, s);
  840. }
  841. void WaitSuspended (string s, Thread t)
  842. {
  843. int n=0;
  844. ThreadState state = t.ThreadState;
  845. while ((state & ThreadState.Suspended) == 0) {
  846. Assert.IsTrue ((state & ThreadState.SuspendRequested) != 0, s + ": expected SuspendRequested state");
  847. Thread.Sleep (10);
  848. n++;
  849. Assert.IsTrue (n < 100, s + ": failed to suspend");
  850. state = t.ThreadState;
  851. }
  852. Assert.IsTrue ((state & ThreadState.SuspendRequested) == 0, s + ": SuspendRequested state not expected");
  853. }
  854. void WaitResumed (string s, Thread t)
  855. {
  856. int n=0;
  857. while ((t.ThreadState & ThreadState.Suspended) != 0) {
  858. Thread.Sleep (10);
  859. n++;
  860. Assert.IsTrue (n < 100, s + ": failed to resume");
  861. }
  862. }
  863. public void DoCount ()
  864. {
  865. while (true) {
  866. counter++;
  867. Thread.Sleep (1);
  868. }
  869. }
  870. }
  871. [TestFixture]
  872. public class ThreadStateTest {
  873. void Start ()
  874. {
  875. }
  876. [Test] // bug #81720
  877. public void IsBackGround ()
  878. {
  879. Thread t1 = new Thread (new ThreadStart (Start));
  880. Assert.AreEqual (ThreadState.Unstarted, t1.ThreadState, "#A1");
  881. Assert.IsFalse (t1.IsBackground, "#A2");
  882. t1.Start ();
  883. t1.Join ();
  884. Assert.AreEqual (ThreadState.Stopped, t1.ThreadState, "#A3");
  885. try {
  886. bool isBackGround = t1.IsBackground;
  887. Assert.Fail ("#A4: " + isBackGround.ToString ());
  888. } catch (ThreadStateException ex) {
  889. Assert.AreEqual (typeof (ThreadStateException), ex.GetType (), "#A5");
  890. Assert.IsNull (ex.InnerException, "#A6");
  891. Assert.IsNotNull (ex.Message, "#A7");
  892. }
  893. Thread t2 = new Thread (new ThreadStart (Start));
  894. Assert.AreEqual (ThreadState.Unstarted, t2.ThreadState, "#B1");
  895. t2.IsBackground = true;
  896. Assert.AreEqual (ThreadState.Unstarted | ThreadState.Background, t2.ThreadState, "#B2");
  897. Assert.IsTrue (t2.IsBackground, "#B3");
  898. t2.Start ();
  899. t2.Join ();
  900. Assert.AreEqual (ThreadState.Stopped, t2.ThreadState, "#B4");
  901. try {
  902. bool isBackGround = t2.IsBackground;
  903. Assert.Fail ("#B5: " + isBackGround.ToString ());
  904. } catch (ThreadStateException ex) {
  905. Assert.AreEqual (typeof (ThreadStateException), ex.GetType (), "#B6");
  906. Assert.IsNull (ex.InnerException, "#B7");
  907. Assert.IsNotNull (ex.Message, "#B8");
  908. }
  909. }
  910. }
  911. [TestFixture]
  912. [Serializable]
  913. public class ThreadTest_ManagedThreadId
  914. {
  915. AppDomain ad1;
  916. AppDomain ad2;
  917. MBRO mbro = new MBRO ();
  918. class MBRO : MarshalByRefObject {
  919. public int id_a1;
  920. public int id_b1;
  921. public int id_b2;
  922. public string ad_a1;
  923. public string ad_b1;
  924. public string ad_b2;
  925. public string message;
  926. }
  927. #if !MOBILE
  928. [Test]
  929. public void ManagedThreadId_AppDomains ()
  930. {
  931. AppDomain currentDomain = AppDomain.CurrentDomain;
  932. ad1 = AppDomain.CreateDomain ("AppDomain 1", currentDomain.Evidence, currentDomain.SetupInformation);
  933. ad2 = AppDomain.CreateDomain ("AppDomain 2", currentDomain.Evidence, currentDomain.SetupInformation);
  934. Thread a = new Thread (ThreadA);
  935. Thread b = new Thread (ThreadB);
  936. // execute on AppDomain 1 thread A
  937. // execute on AppDomain 2 thread B
  938. // execute on AppDomain 1 thread B - must have same ManagedThreadId as Ad 2 on thread B
  939. a.Start ();
  940. a.Join ();
  941. b.Start ();
  942. b.Join ();
  943. AppDomain.Unload (ad1);
  944. AppDomain.Unload (ad2);
  945. if (mbro.message != null)
  946. Assert.Fail (mbro.message);
  947. // 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);
  948. Assert.AreEqual ("AppDomain 1", mbro.ad_a1, "Name #1");
  949. Assert.AreEqual ("AppDomain 1", mbro.ad_b1, "Name #2");
  950. Assert.AreEqual ("AppDomain 2", mbro.ad_b2, "Name #3");
  951. Assert.AreNotEqual (mbro.id_a1, mbro.id_b1, "Id #1");
  952. Assert.AreNotEqual (mbro.id_a1, mbro.id_b2, "Id #2");
  953. Assert.AreEqual (mbro.id_b1, mbro.id_b2, "Id #3");
  954. Assert.AreNotEqual (mbro.id_a1, Thread.CurrentThread.ManagedThreadId, "Id #4");
  955. Assert.AreNotEqual (mbro.id_b1, Thread.CurrentThread.ManagedThreadId, "Id #5");
  956. Assert.AreNotEqual (mbro.id_b2, Thread.CurrentThread.ManagedThreadId, "Id #6");
  957. Assert.AreNotEqual (mbro.ad_a1, AppDomain.CurrentDomain.FriendlyName, "Name #4");
  958. Assert.AreNotEqual (mbro.ad_b1, AppDomain.CurrentDomain.FriendlyName, "Name #5");
  959. Assert.AreNotEqual (mbro.ad_b2, AppDomain.CurrentDomain.FriendlyName, "Name #6");
  960. }
  961. #endif
  962. void A1 ()
  963. {
  964. mbro.id_a1 = Thread.CurrentThread.ManagedThreadId;
  965. mbro.ad_a1 = AppDomain.CurrentDomain.FriendlyName;
  966. }
  967. void B2 ()
  968. {
  969. mbro.id_b2 = Thread.CurrentThread.ManagedThreadId;
  970. mbro.ad_b2 = AppDomain.CurrentDomain.FriendlyName;
  971. }
  972. void B1 ()
  973. {
  974. mbro.id_b1 = Thread.CurrentThread.ManagedThreadId;
  975. mbro.ad_b1 = AppDomain.CurrentDomain.FriendlyName;
  976. }
  977. void ThreadA (object obj)
  978. {
  979. // Console.WriteLine ("ThreadA");
  980. try {
  981. ad1.DoCallBack (A1);
  982. } catch (Exception ex) {
  983. mbro.message = string.Format ("ThreadA exception: {0}", ex);
  984. }
  985. // Console.WriteLine ("ThreadA Done");
  986. }
  987. void ThreadB (object obj)
  988. {
  989. // Console.WriteLine ("ThreadB");
  990. try {
  991. ad2.DoCallBack (B2);
  992. ad1.DoCallBack (B1);
  993. } catch (Exception ex) {
  994. mbro.message = string.Format ("ThreadB exception: {0}", ex);
  995. }
  996. // Console.WriteLine ("ThreadB Done");
  997. }
  998. }
  999. [TestFixture]
  1000. public class ThreadApartmentTest
  1001. {
  1002. void Start ()
  1003. {
  1004. }
  1005. [Test] // bug #81658
  1006. public void ApartmentState_StoppedThread ()
  1007. {
  1008. Thread t1 = new Thread (new ThreadStart (Start));
  1009. t1.Start ();
  1010. t1.Join ();
  1011. try {
  1012. ApartmentState state = t1.ApartmentState;
  1013. Assert.Fail ("#A1: " + state.ToString ());
  1014. } catch (ThreadStateException ex) {
  1015. Assert.AreEqual (typeof (ThreadStateException), ex.GetType (), "#A2");
  1016. Assert.IsNull (ex.InnerException, "#A3");
  1017. Assert.IsNotNull (ex.Message, "#A4");
  1018. }
  1019. Thread t2 = new Thread (new ThreadStart (Start));
  1020. t2.IsBackground = true;
  1021. t2.Start ();
  1022. t2.Join ();
  1023. try {
  1024. ApartmentState state = t2.ApartmentState;
  1025. Assert.Fail ("#B1: " + state.ToString ());
  1026. } catch (ThreadStateException ex) {
  1027. Assert.AreEqual (typeof (ThreadStateException), ex.GetType (), "#B2");
  1028. Assert.IsNull (ex.InnerException, "#B3");
  1029. Assert.IsNotNull (ex.Message, "#B4");
  1030. }
  1031. }
  1032. [Test]
  1033. public void ApartmentState_BackGround ()
  1034. {
  1035. Thread t1 = new Thread (new ThreadStart (Start));
  1036. t1.IsBackground = true;
  1037. Assert.AreEqual (ApartmentState.Unknown, t1.ApartmentState, "#1");
  1038. t1.ApartmentState = ApartmentState.STA;
  1039. Assert.AreEqual (ApartmentState.STA, t1.ApartmentState, "#2");
  1040. }
  1041. [Test]
  1042. public void TestApartmentState ()
  1043. {
  1044. Thread t1 = new Thread (new ThreadStart (Start));
  1045. Thread t2 = new Thread (new ThreadStart (Start));
  1046. Thread t3 = new Thread (new ThreadStart (Start));
  1047. Assert.AreEqual (ApartmentState.Unknown, t1.ApartmentState, "Thread1 Default");
  1048. Assert.AreEqual (ApartmentState.Unknown, t2.ApartmentState, "Thread2 Default");
  1049. Assert.AreEqual (ApartmentState.Unknown, t3.ApartmentState, "Thread3 Default");
  1050. t1.ApartmentState = ApartmentState.STA;
  1051. Assert.AreEqual (ApartmentState.STA, t1.ApartmentState, "Thread1 Set Once");
  1052. t1.ApartmentState = ApartmentState.MTA;
  1053. Assert.AreEqual (ApartmentState.STA, t1.ApartmentState, "Thread1 Set Twice");
  1054. t2.ApartmentState = ApartmentState.MTA;
  1055. Assert.AreEqual (ApartmentState.MTA, t2.ApartmentState, "Thread2 Set Once");
  1056. t2.ApartmentState = ApartmentState.STA;
  1057. Assert.AreEqual (ApartmentState.MTA, t2.ApartmentState, "Thread2 Set Twice");
  1058. bool exception_occured = false;
  1059. try {
  1060. t3.ApartmentState = ApartmentState.Unknown;
  1061. }
  1062. catch (Exception) {
  1063. exception_occured = true;
  1064. }
  1065. Assert.AreEqual (ApartmentState.Unknown, t3.ApartmentState, "Thread3 Set Invalid");
  1066. Assert.IsFalse (exception_occured, "Thread3 Set Invalid Exception Occured");
  1067. t1.Start ();
  1068. exception_occured = false;
  1069. try {
  1070. t1.ApartmentState = ApartmentState.STA;
  1071. }
  1072. catch (Exception) {
  1073. exception_occured = true;
  1074. }
  1075. Assert.IsTrue (exception_occured, "Thread1 Started Invalid Exception Occured");
  1076. }
  1077. [Test]
  1078. public void TestSetApartmentStateSameState ()
  1079. {
  1080. Thread t1 = new Thread (new ThreadStart (Start));
  1081. t1.SetApartmentState (ApartmentState.STA);
  1082. Assert.AreEqual (ApartmentState.STA, t1.ApartmentState, "Thread1 Set Once");
  1083. t1.SetApartmentState (ApartmentState.STA);
  1084. Assert.AreEqual (ApartmentState.STA, t1.ApartmentState, "Thread1 Set twice");
  1085. }
  1086. [Test]
  1087. [ExpectedException(typeof(InvalidOperationException))]
  1088. public void TestSetApartmentStateDiffState ()
  1089. {
  1090. Thread t1 = new Thread (new ThreadStart (Start));
  1091. t1.SetApartmentState (ApartmentState.STA);
  1092. Assert.AreEqual (ApartmentState.STA, t1.ApartmentState, "Thread1 Set Once");
  1093. t1.SetApartmentState (ApartmentState.MTA);
  1094. }
  1095. [Test]
  1096. public void TestTrySetApartmentState ()
  1097. {
  1098. Thread t1 = new Thread (new ThreadStart (Start));
  1099. t1.SetApartmentState (ApartmentState.STA);
  1100. Assert.AreEqual (ApartmentState.STA, t1.ApartmentState, "#1");
  1101. bool result = t1.TrySetApartmentState (ApartmentState.MTA);
  1102. Assert.IsFalse (result, "#2");
  1103. result = t1.TrySetApartmentState (ApartmentState.STA);
  1104. Assert.IsTrue (result, "#3");
  1105. }
  1106. [Test]
  1107. public void TestTrySetApartmentStateRunning ()
  1108. {
  1109. Thread t1 = new Thread (new ThreadStart (Start));
  1110. t1.SetApartmentState (ApartmentState.STA);
  1111. Assert.AreEqual (ApartmentState.STA, t1.ApartmentState, "#1");
  1112. t1.Start ();
  1113. try {
  1114. t1.TrySetApartmentState (ApartmentState.STA);
  1115. Assert.Fail ("#2");
  1116. } catch (ThreadStateException) {
  1117. }
  1118. t1.Join ();
  1119. }
  1120. [Test]
  1121. public void Volatile () {
  1122. double v3 = 55667;
  1123. Thread.VolatileWrite (ref v3, double.MaxValue);
  1124. Assert.AreEqual (v3, double.MaxValue);
  1125. float v4 = 1;
  1126. Thread.VolatileWrite (ref v4, float.MaxValue);
  1127. Assert.AreEqual (v4, float.MaxValue);
  1128. }
  1129. [Test]
  1130. public void Culture ()
  1131. {
  1132. Assert.IsNotNull (Thread.CurrentThread.CurrentCulture, "CurrentCulture");
  1133. Assert.IsNotNull (Thread.CurrentThread.CurrentUICulture, "CurrentUICulture");
  1134. }
  1135. [Test]
  1136. public void ThreadStartSimple ()
  1137. {
  1138. int i = 0;
  1139. Thread t = new Thread (delegate () {
  1140. // ensure the NSAutoreleasePool works
  1141. i++;
  1142. });
  1143. t.Start ();
  1144. t.Join ();
  1145. Assert.AreEqual (1, i, "ThreadStart");
  1146. }
  1147. [Test]
  1148. public void ParametrizedThreadStart ()
  1149. {
  1150. int i = 0;
  1151. object arg = null;
  1152. Thread t = new Thread (delegate (object obj) {
  1153. // ensure the NSAutoreleasePool works
  1154. i++;
  1155. arg = obj;
  1156. });
  1157. t.Start (this);
  1158. t.Join ();
  1159. Assert.AreEqual (1, i, "ParametrizedThreadStart");
  1160. Assert.AreEqual (this, arg, "obj");
  1161. }
  1162. [Test]
  1163. public void SetNameTpThread () {
  1164. ThreadPool.QueueUserWorkItem(new WaitCallback(ThreadProc));
  1165. }
  1166. static void ThreadProc(Object stateInfo) {
  1167. Thread.CurrentThread.Name = "My Worker";
  1168. }
  1169. [Test]
  1170. public void GetStackTraces () {
  1171. var m = typeof (Thread).GetMethod ("Mono_GetStackTraces", BindingFlags.NonPublic|BindingFlags.Static);
  1172. if (m != null) {
  1173. var res = (Dictionary<Thread,SD.StackTrace>)typeof (Thread).GetMethod ("Mono_GetStackTraces", BindingFlags.NonPublic|BindingFlags.Static).Invoke (null, null);
  1174. foreach (var t in res.Keys) {
  1175. var st = res [t].ToString ();
  1176. }
  1177. }
  1178. }
  1179. }
  1180. public class TestUtil
  1181. {
  1182. public static void WaitForNotAlive (Thread t, string s)
  1183. {
  1184. WhileAlive (t, true, s);
  1185. }
  1186. public static void WaitForAlive (Thread t, string s)
  1187. {
  1188. WhileAlive (t, false, s);
  1189. }
  1190. public static bool WaitForAliveOrStop (Thread t, string s)
  1191. {
  1192. return WhileAliveOrStop (t, false, s);
  1193. }
  1194. public static void WhileAlive (Thread t, bool alive, string s)
  1195. {
  1196. var sw = SD.Stopwatch.StartNew ();
  1197. while (t.IsAlive == alive) {
  1198. if (sw.Elapsed.TotalSeconds > 10) {
  1199. if (alive) Assert.Fail ("Timeout while waiting for not alive state. " + s);
  1200. else Assert.Fail ("Timeout while waiting for alive state. " + s);
  1201. }
  1202. }
  1203. }
  1204. public static bool WhileAliveOrStop (Thread t, bool alive, string s)
  1205. {
  1206. var sw = SD.Stopwatch.StartNew ();
  1207. while (t.IsAlive == alive) {
  1208. if (t.ThreadState == ThreadState.Stopped)
  1209. return false;
  1210. if (sw.Elapsed.TotalSeconds > 10) {
  1211. if (alive) Assert.Fail ("Timeout while waiting for not alive state. " + s);
  1212. else Assert.Fail ("Timeout while waiting for alive state. " + s);
  1213. }
  1214. }
  1215. return true;
  1216. }
  1217. }
  1218. }