ThreadTest.cs 38 KB

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