ThreadTest.cs 37 KB

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