ThreadTest.cs 36 KB

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