ThreadTest.cs 37 KB

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