ThreadTest.cs 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429
  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. bool rename_finished;
  521. bool rename_failed;
  522. [Test]
  523. public void RenameTpThread ()
  524. {
  525. object monitor = new object ();
  526. ThreadPool.QueueUserWorkItem (new WaitCallback (Rename_callback), monitor);
  527. lock (monitor) {
  528. if (!rename_finished)
  529. Monitor.Wait (monitor);
  530. }
  531. Assert.IsTrue (!rename_failed);
  532. }
  533. void Rename_callback (object o) {
  534. Thread.CurrentThread.Name = "a";
  535. try {
  536. Thread.CurrentThread.Name = "b";
  537. //Console.WriteLine ("Thread name is: {0}", Thread.CurrentThread.Name);
  538. } catch (Exception e) {
  539. //Console.Error.WriteLine (e);
  540. rename_failed = true;
  541. }
  542. object monitor = o;
  543. lock (monitor) {
  544. rename_finished = true;
  545. Monitor.Pulse (monitor);
  546. }
  547. }
  548. [Test]
  549. public void TestNestedThreads1()
  550. {
  551. C3Test test1 = new C3Test();
  552. Thread TestThread = new Thread(new ThreadStart(test1.TestMethod1));
  553. try {
  554. TestThread.Start();
  555. TestUtil.WaitForAlive (TestThread, "wait11");
  556. } finally {
  557. #if MONO_FEATURE_THREAD_ABORT
  558. TestThread.Abort();
  559. #else
  560. TestThread.Interrupt ();
  561. #endif
  562. }
  563. }
  564. [Test]
  565. public void TestNestedThreads2()
  566. {
  567. C4Test test1 = new C4Test();
  568. Thread TestThread = new Thread(new ThreadStart(test1.TestMethod1));
  569. try {
  570. TestThread.Start();
  571. } finally {
  572. #if MONO_FEATURE_THREAD_ABORT
  573. TestThread.Abort();
  574. #else
  575. TestThread.Interrupt ();
  576. #endif
  577. }
  578. }
  579. [Test]
  580. public void TestJoin1()
  581. {
  582. C1Test test1 = new C1Test();
  583. C1Test test2 = new C1Test();
  584. Thread thread1 = new Thread(new ThreadStart(test1.TestMethod));
  585. Thread thread2 = new Thread(new ThreadStart(test1.TestMethod2));
  586. try {
  587. thread1.Start();
  588. thread2.Start();
  589. thread2.Join();
  590. } finally {
  591. #if MONO_FEATURE_THREAD_ABORT
  592. thread1.Abort();
  593. thread2.Abort();
  594. #else
  595. thread1.Interrupt ();
  596. thread2.Interrupt ();
  597. #endif
  598. }
  599. }
  600. [Test]
  601. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  602. public void Join_Int32_Negative ()
  603. {
  604. // -1 is Timeout.Infinite
  605. Thread.CurrentThread.Join (-2);
  606. }
  607. [Test]
  608. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  609. public void Join_TimeSpan_Negative ()
  610. {
  611. Thread.CurrentThread.Join (Negative);
  612. }
  613. [Test]
  614. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  615. public void Join_TimeSpan_TooLarge ()
  616. {
  617. Thread.CurrentThread.Join (TooLarge);
  618. }
  619. [Test]
  620. public void Join_TimeSpan_SmallNegative ()
  621. {
  622. Thread.CurrentThread.Join (SmallNegative);
  623. }
  624. [Test]
  625. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  626. public void Sleep_Int32_Negative ()
  627. {
  628. // -1 is Timeout.Infinite
  629. Thread.Sleep (-2);
  630. }
  631. [Test]
  632. public void Sleep_TimeSpan_SmallNegative ()
  633. {
  634. Thread.Sleep (SmallNegative);
  635. }
  636. [Test]
  637. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  638. public void Sleep_TimeSpan_Negative ()
  639. {
  640. Thread.Sleep (Negative);
  641. }
  642. [Test]
  643. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  644. public void Sleep_TimeSpan_TooLarge ()
  645. {
  646. Thread.Sleep (TooLarge);
  647. }
  648. [Test]
  649. public void SpinWait ()
  650. {
  651. // no exception for negative numbers
  652. Thread.SpinWait (Int32.MinValue);
  653. Thread.SpinWait (0);
  654. }
  655. [Test]
  656. public void TestThreadState ()
  657. {
  658. if (is_win32 && is_mono)
  659. Assert.Fail ("This test fails on mono on Win32. Our runtime should be fixed.");
  660. //TODO: Test The rest of the possible transitions
  661. C2Test test1 = new C2Test();
  662. Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
  663. Assert.AreEqual (ThreadState.Unstarted, TestThread.ThreadState, "#101 Wrong Thread State");
  664. try {
  665. TestThread.Start();
  666. //while(!TestThread.IsAlive); //In the MS Documentation this is not necessary
  667. //but in the MS SDK it is
  668. Assert.IsTrue (TestThread.ThreadState == ThreadState.Running || (TestThread.ThreadState & ThreadState.Unstarted) != 0,
  669. "#102 Wrong Thread State: " + TestThread.ThreadState.ToString ());
  670. } finally {
  671. #if MONO_FEATURE_THREAD_ABORT
  672. TestThread.Abort();
  673. #else
  674. TestThread.Interrupt ();
  675. #endif
  676. }
  677. TestUtil.WaitForNotAlive (TestThread, "wait12");
  678. // Docs say state will be Stopped, but Aborted happens sometimes (?)
  679. Assert.IsTrue ((ThreadState.Stopped & TestThread.ThreadState) != 0 || (ThreadState.Aborted & TestThread.ThreadState) != 0,
  680. "#103 Wrong Thread State: " + TestThread.ThreadState.ToString ());
  681. }
  682. [Test]
  683. [Ignore ("see comment below.")]
  684. public void CurrentPrincipal_PrincipalPolicy_NoPrincipal ()
  685. {
  686. // note: switching from PrincipalPolicy won't work inside the same thread
  687. // because as soon as a Principal object is created the Policy doesn't matter anymore
  688. Thread t = new Thread (new ThreadStart (ThreadedPrincipalTest.NoPrincipal));
  689. try {
  690. t.Start ();
  691. t.Join ();
  692. } catch {
  693. #if MONO_FEATURE_THREAD_ABORT
  694. t.Abort ();
  695. #else
  696. t.Interrupt ();
  697. #endif
  698. }
  699. }
  700. [Test]
  701. [Ignore ("see comment below.")]
  702. public void CurrentPrincipal_PrincipalPolicy_UnauthenticatedPrincipal ()
  703. {
  704. // note: switching from PrincipalPolicy won't work inside the same thread
  705. // because as soon as a Principal object is created the Policy doesn't matter anymore
  706. Thread t = new Thread (new ThreadStart (ThreadedPrincipalTest.UnauthenticatedPrincipal));
  707. try {
  708. t.Start ();
  709. t.Join ();
  710. } catch {
  711. #if MONO_FEATURE_THREAD_ABORT
  712. t.Abort ();
  713. #else
  714. t.Interrupt ();
  715. #endif
  716. }
  717. }
  718. [Test]
  719. public void CurrentPrincipal_PrincipalPolicy_WindowsPrincipal ()
  720. {
  721. // note: switching from PrincipalPolicy won't work inside the same thread
  722. // because as soon as a Principal object is created the Policy doesn't matter anymore
  723. Thread t = new Thread (new ThreadStart (ThreadedPrincipalTest.WindowsPrincipal));
  724. try {
  725. t.Start ();
  726. t.Join ();
  727. } catch {
  728. #if MONO_FEATURE_THREAD_ABORT
  729. t.Abort ();
  730. #else
  731. t.Interrupt ();
  732. #endif
  733. }
  734. }
  735. [Test]
  736. public void IPrincipal_CopyOnNewThread ()
  737. {
  738. Thread.CurrentPrincipal = new GenericPrincipal (new GenericIdentity ("bad"), null);
  739. Thread t = new Thread (new ThreadStart (ThreadedPrincipalTest.CopyOnNewThread));
  740. try {
  741. Thread.CurrentPrincipal = new GenericPrincipal (new GenericIdentity ("good"), null);
  742. t.Start ();
  743. t.Join ();
  744. } catch {
  745. #if MONO_FEATURE_THREAD_ABORT
  746. t.Abort ();
  747. #else
  748. t.Interrupt ();
  749. #endif
  750. }
  751. }
  752. int counter = 0;
  753. #if MONO_FEATURE_THREAD_SUSPEND_RESUME
  754. [Test]
  755. public void TestSuspend ()
  756. {
  757. Thread t = new Thread (new ThreadStart (DoCount));
  758. t.IsBackground = true;
  759. t.Start ();
  760. CheckIsRunning ("t1", t);
  761. t.Suspend ();
  762. WaitSuspended ("t2", t);
  763. CheckIsNotRunning ("t3", t);
  764. t.Resume ();
  765. WaitResumed ("t4", t);
  766. CheckIsRunning ("t5", t);
  767. t.Abort ();
  768. TestUtil.WaitForNotAlive (t, "wait13");
  769. CheckIsNotRunning ("t6", t);
  770. }
  771. #endif
  772. #if MONO_FEATURE_THREAD_SUSPEND_RESUME && MONO_FEATURE_THREAD_ABORT
  773. [Test]
  774. [Category("NotDotNet")] // On MS, ThreadStateException is thrown on Abort: "Thread is suspended; attempting to abort"
  775. public void TestSuspendAbort ()
  776. {
  777. if (is_win32 && is_mono)
  778. Assert.Fail ("This test fails on Win32. The test should be fixed.");
  779. Thread t = new Thread (new ThreadStart (DoCount));
  780. t.IsBackground = true;
  781. t.Start ();
  782. CheckIsRunning ("t1", t);
  783. t.Suspend ();
  784. WaitSuspended ("t2", t);
  785. CheckIsNotRunning ("t3", t);
  786. t.Abort ();
  787. int n=0;
  788. while (t.IsAlive && n < 200) {
  789. Thread.Sleep (10);
  790. n++;
  791. }
  792. Assert.IsTrue (n < 200, "Timeout while waiting for abort");
  793. CheckIsNotRunning ("t6", t);
  794. }
  795. #endif
  796. [Test]
  797. public void Test_Interrupt ()
  798. {
  799. ManualResetEvent mre = new ManualResetEvent (false);
  800. bool interruptedExceptionThrown = false;
  801. ThreadPool.QueueUserWorkItem (Test_Interrupt_Worker, Thread.CurrentThread);
  802. try {
  803. try {
  804. mre.WaitOne (3000);
  805. } finally {
  806. try {
  807. mre.WaitOne (0);
  808. } catch (ThreadInterruptedException) {
  809. Assert.Fail ("ThreadInterruptedException thrown twice");
  810. }
  811. }
  812. } catch (ThreadInterruptedException) {
  813. interruptedExceptionThrown = true;
  814. }
  815. Assert.IsTrue (interruptedExceptionThrown, "ThreadInterruptedException expected.");
  816. }
  817. [Test]
  818. [ExpectedException (typeof (ArgumentNullException))]
  819. public void TestQueueUserWorkItemNullCallback ()
  820. {
  821. ThreadPool.QueueUserWorkItem (null, null);
  822. }
  823. private void Test_Interrupt_Worker (object o)
  824. {
  825. Thread t = o as Thread;
  826. Thread.Sleep (100);
  827. t.Interrupt ();
  828. }
  829. [Test]
  830. [Category ("NotDotNet")] // it crashes nunit.
  831. public void Test_InterruptCurrentThread ()
  832. {
  833. ManualResetEvent mre = new ManualResetEvent (false);
  834. bool interruptedExceptionThrown = false;
  835. Thread.CurrentThread.Interrupt ();
  836. try {
  837. mre.WaitOne (0);
  838. Assert.Fail ();
  839. } catch (ThreadInterruptedException) {
  840. }
  841. }
  842. [Test]
  843. public void GetNamedDataSlotTest ()
  844. {
  845. Assert.IsNotNull (Thread.GetNamedDataSlot ("te#st"), "#1");
  846. Assert.AreSame (Thread.GetNamedDataSlot ("te#st"), Thread.GetNamedDataSlot ("te#st"), "#2");
  847. }
  848. class DomainClass : MarshalByRefObject {
  849. Thread m_thread;
  850. bool success;
  851. public bool Run () {
  852. m_thread = new Thread(ThreadProc);
  853. m_thread.Start(Thread.CurrentThread);
  854. m_thread.Join();
  855. return success;
  856. }
  857. public void ThreadProc (object arg) {
  858. success = m_thread == Thread.CurrentThread;
  859. }
  860. }
  861. #if MONO_FEATURE_MULTIPLE_APPDOMAINS
  862. [Test]
  863. public void CurrentThread_Domains ()
  864. {
  865. AppDomain ad = AppDomain.CreateDomain ("foo");
  866. ad.Load (typeof (DomainClass).Assembly.GetName ());
  867. var o = (DomainClass)ad.CreateInstanceAndUnwrap (typeof (DomainClass).Assembly.FullName, typeof (DomainClass).FullName);
  868. Assert.IsTrue (o.Run ());
  869. AppDomain.Unload (ad);
  870. }
  871. #endif // MONO_FEATURE_MULTIPLE_APPDOMAINS
  872. void CheckIsRunning (string s, Thread t)
  873. {
  874. int c = counter;
  875. Thread.Sleep (100);
  876. Assert.IsTrue (counter > c, s);
  877. }
  878. void CheckIsNotRunning (string s, Thread t)
  879. {
  880. int c = counter;
  881. Thread.Sleep (100);
  882. Assert.AreEqual (counter, c, s);
  883. }
  884. void WaitSuspended (string s, Thread t)
  885. {
  886. int n=0;
  887. ThreadState state = t.ThreadState;
  888. while ((state & ThreadState.Suspended) == 0) {
  889. Assert.IsTrue ((state & ThreadState.SuspendRequested) != 0, s + ": expected SuspendRequested state");
  890. Thread.Sleep (10);
  891. n++;
  892. Assert.IsTrue (n < 100, s + ": failed to suspend");
  893. state = t.ThreadState;
  894. }
  895. Assert.IsTrue ((state & ThreadState.SuspendRequested) == 0, s + ": SuspendRequested state not expected");
  896. }
  897. void WaitResumed (string s, Thread t)
  898. {
  899. int n=0;
  900. while ((t.ThreadState & ThreadState.Suspended) != 0) {
  901. Thread.Sleep (10);
  902. n++;
  903. Assert.IsTrue (n < 100, s + ": failed to resume");
  904. }
  905. }
  906. public void DoCount ()
  907. {
  908. while (true) {
  909. counter++;
  910. Thread.Sleep (1);
  911. }
  912. }
  913. }
  914. [TestFixture]
  915. public class ThreadStateTest {
  916. void Start ()
  917. {
  918. }
  919. [Test] // bug #81720
  920. public void IsBackGround ()
  921. {
  922. Thread t1 = new Thread (new ThreadStart (Start));
  923. Assert.AreEqual (ThreadState.Unstarted, t1.ThreadState, "#A1");
  924. Assert.IsFalse (t1.IsBackground, "#A2");
  925. t1.Start ();
  926. t1.Join ();
  927. Assert.AreEqual (ThreadState.Stopped, t1.ThreadState, "#A3");
  928. try {
  929. bool isBackGround = t1.IsBackground;
  930. Assert.Fail ("#A4: " + isBackGround.ToString ());
  931. } catch (ThreadStateException ex) {
  932. Assert.AreEqual (typeof (ThreadStateException), ex.GetType (), "#A5");
  933. Assert.IsNull (ex.InnerException, "#A6");
  934. Assert.IsNotNull (ex.Message, "#A7");
  935. }
  936. Thread t2 = new Thread (new ThreadStart (Start));
  937. Assert.AreEqual (ThreadState.Unstarted, t2.ThreadState, "#B1");
  938. t2.IsBackground = true;
  939. Assert.AreEqual (ThreadState.Unstarted | ThreadState.Background, t2.ThreadState, "#B2");
  940. Assert.IsTrue (t2.IsBackground, "#B3");
  941. t2.Start ();
  942. t2.Join ();
  943. Assert.AreEqual (ThreadState.Stopped, t2.ThreadState, "#B4");
  944. try {
  945. bool isBackGround = t2.IsBackground;
  946. Assert.Fail ("#B5: " + isBackGround.ToString ());
  947. } catch (ThreadStateException ex) {
  948. Assert.AreEqual (typeof (ThreadStateException), ex.GetType (), "#B6");
  949. Assert.IsNull (ex.InnerException, "#B7");
  950. Assert.IsNotNull (ex.Message, "#B8");
  951. }
  952. }
  953. }
  954. [TestFixture]
  955. [Serializable]
  956. public class ThreadTest_ManagedThreadId
  957. {
  958. AppDomain ad1;
  959. AppDomain ad2;
  960. MBRO mbro = new MBRO ();
  961. class MBRO : MarshalByRefObject {
  962. public int id_a1;
  963. public int id_b1;
  964. public int id_b2;
  965. public string ad_a1;
  966. public string ad_b1;
  967. public string ad_b2;
  968. public string message;
  969. }
  970. #if !MOBILE
  971. [Test]
  972. public void ManagedThreadId_AppDomains ()
  973. {
  974. AppDomain currentDomain = AppDomain.CurrentDomain;
  975. ad1 = AppDomain.CreateDomain ("AppDomain 1", currentDomain.Evidence, currentDomain.SetupInformation);
  976. ad2 = AppDomain.CreateDomain ("AppDomain 2", currentDomain.Evidence, currentDomain.SetupInformation);
  977. Thread a = new Thread (ThreadA);
  978. Thread b = new Thread (ThreadB);
  979. // execute on AppDomain 1 thread A
  980. // execute on AppDomain 2 thread B
  981. // execute on AppDomain 1 thread B - must have same ManagedThreadId as Ad 2 on thread B
  982. a.Start ();
  983. a.Join ();
  984. b.Start ();
  985. b.Join ();
  986. AppDomain.Unload (ad1);
  987. AppDomain.Unload (ad2);
  988. if (mbro.message != null)
  989. Assert.Fail (mbro.message);
  990. // 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);
  991. Assert.AreEqual ("AppDomain 1", mbro.ad_a1, "Name #1");
  992. Assert.AreEqual ("AppDomain 1", mbro.ad_b1, "Name #2");
  993. Assert.AreEqual ("AppDomain 2", mbro.ad_b2, "Name #3");
  994. Assert.AreNotEqual (mbro.id_a1, mbro.id_b1, "Id #1");
  995. Assert.AreNotEqual (mbro.id_a1, mbro.id_b2, "Id #2");
  996. Assert.AreEqual (mbro.id_b1, mbro.id_b2, "Id #3");
  997. Assert.AreNotEqual (mbro.id_a1, Thread.CurrentThread.ManagedThreadId, "Id #4");
  998. Assert.AreNotEqual (mbro.id_b1, Thread.CurrentThread.ManagedThreadId, "Id #5");
  999. Assert.AreNotEqual (mbro.id_b2, Thread.CurrentThread.ManagedThreadId, "Id #6");
  1000. Assert.AreNotEqual (mbro.ad_a1, AppDomain.CurrentDomain.FriendlyName, "Name #4");
  1001. Assert.AreNotEqual (mbro.ad_b1, AppDomain.CurrentDomain.FriendlyName, "Name #5");
  1002. Assert.AreNotEqual (mbro.ad_b2, AppDomain.CurrentDomain.FriendlyName, "Name #6");
  1003. }
  1004. #endif
  1005. void A1 ()
  1006. {
  1007. mbro.id_a1 = Thread.CurrentThread.ManagedThreadId;
  1008. mbro.ad_a1 = AppDomain.CurrentDomain.FriendlyName;
  1009. }
  1010. void B2 ()
  1011. {
  1012. mbro.id_b2 = Thread.CurrentThread.ManagedThreadId;
  1013. mbro.ad_b2 = AppDomain.CurrentDomain.FriendlyName;
  1014. }
  1015. void B1 ()
  1016. {
  1017. mbro.id_b1 = Thread.CurrentThread.ManagedThreadId;
  1018. mbro.ad_b1 = AppDomain.CurrentDomain.FriendlyName;
  1019. }
  1020. void ThreadA (object obj)
  1021. {
  1022. // Console.WriteLine ("ThreadA");
  1023. try {
  1024. ad1.DoCallBack (A1);
  1025. } catch (Exception ex) {
  1026. mbro.message = string.Format ("ThreadA exception: {0}", ex);
  1027. }
  1028. // Console.WriteLine ("ThreadA Done");
  1029. }
  1030. void ThreadB (object obj)
  1031. {
  1032. // Console.WriteLine ("ThreadB");
  1033. try {
  1034. ad2.DoCallBack (B2);
  1035. ad1.DoCallBack (B1);
  1036. } catch (Exception ex) {
  1037. mbro.message = string.Format ("ThreadB exception: {0}", ex);
  1038. }
  1039. // Console.WriteLine ("ThreadB Done");
  1040. }
  1041. }
  1042. [TestFixture]
  1043. public class ThreadApartmentTest
  1044. {
  1045. void Start ()
  1046. {
  1047. }
  1048. [Test] // bug #81658
  1049. public void ApartmentState_StoppedThread ()
  1050. {
  1051. Thread t1 = new Thread (new ThreadStart (Start));
  1052. t1.Start ();
  1053. t1.Join ();
  1054. try {
  1055. ApartmentState state = t1.ApartmentState;
  1056. Assert.Fail ("#A1: " + state.ToString ());
  1057. } catch (ThreadStateException ex) {
  1058. Assert.AreEqual (typeof (ThreadStateException), ex.GetType (), "#A2");
  1059. Assert.IsNull (ex.InnerException, "#A3");
  1060. Assert.IsNotNull (ex.Message, "#A4");
  1061. }
  1062. Thread t2 = new Thread (new ThreadStart (Start));
  1063. t2.IsBackground = true;
  1064. t2.Start ();
  1065. t2.Join ();
  1066. try {
  1067. ApartmentState state = t2.ApartmentState;
  1068. Assert.Fail ("#B1: " + state.ToString ());
  1069. } catch (ThreadStateException ex) {
  1070. Assert.AreEqual (typeof (ThreadStateException), ex.GetType (), "#B2");
  1071. Assert.IsNull (ex.InnerException, "#B3");
  1072. Assert.IsNotNull (ex.Message, "#B4");
  1073. }
  1074. }
  1075. [Test]
  1076. public void ApartmentState_BackGround ()
  1077. {
  1078. Thread t1 = new Thread (new ThreadStart (Start));
  1079. t1.IsBackground = true;
  1080. Assert.AreEqual (ApartmentState.Unknown, t1.ApartmentState, "#1");
  1081. t1.ApartmentState = ApartmentState.STA;
  1082. Assert.AreEqual (ApartmentState.STA, t1.ApartmentState, "#2");
  1083. }
  1084. [Test]
  1085. public void TestApartmentState ()
  1086. {
  1087. Thread t1 = new Thread (new ThreadStart (Start));
  1088. Thread t2 = new Thread (new ThreadStart (Start));
  1089. Thread t3 = new Thread (new ThreadStart (Start));
  1090. Assert.AreEqual (ApartmentState.Unknown, t1.ApartmentState, "Thread1 Default");
  1091. Assert.AreEqual (ApartmentState.Unknown, t2.ApartmentState, "Thread2 Default");
  1092. Assert.AreEqual (ApartmentState.Unknown, t3.ApartmentState, "Thread3 Default");
  1093. t1.ApartmentState = ApartmentState.STA;
  1094. Assert.AreEqual (ApartmentState.STA, t1.ApartmentState, "Thread1 Set Once");
  1095. t1.ApartmentState = ApartmentState.MTA;
  1096. Assert.AreEqual (ApartmentState.STA, t1.ApartmentState, "Thread1 Set Twice");
  1097. t2.ApartmentState = ApartmentState.MTA;
  1098. Assert.AreEqual (ApartmentState.MTA, t2.ApartmentState, "Thread2 Set Once");
  1099. t2.ApartmentState = ApartmentState.STA;
  1100. Assert.AreEqual (ApartmentState.MTA, t2.ApartmentState, "Thread2 Set Twice");
  1101. bool exception_occured = false;
  1102. try {
  1103. t3.ApartmentState = ApartmentState.Unknown;
  1104. }
  1105. catch (Exception) {
  1106. exception_occured = true;
  1107. }
  1108. Assert.AreEqual (ApartmentState.Unknown, t3.ApartmentState, "Thread3 Set Invalid");
  1109. Assert.IsFalse (exception_occured, "Thread3 Set Invalid Exception Occured");
  1110. t1.Start ();
  1111. exception_occured = false;
  1112. try {
  1113. t1.ApartmentState = ApartmentState.STA;
  1114. }
  1115. catch (Exception) {
  1116. exception_occured = true;
  1117. }
  1118. Assert.IsTrue (exception_occured, "Thread1 Started Invalid Exception Occured");
  1119. }
  1120. [Test]
  1121. public void TestSetApartmentStateSameState ()
  1122. {
  1123. Thread t1 = new Thread (new ThreadStart (Start));
  1124. t1.SetApartmentState (ApartmentState.STA);
  1125. Assert.AreEqual (ApartmentState.STA, t1.ApartmentState, "Thread1 Set Once");
  1126. t1.SetApartmentState (ApartmentState.STA);
  1127. Assert.AreEqual (ApartmentState.STA, t1.ApartmentState, "Thread1 Set twice");
  1128. }
  1129. [Test]
  1130. [ExpectedException(typeof(InvalidOperationException))]
  1131. public void TestSetApartmentStateDiffState ()
  1132. {
  1133. Thread t1 = new Thread (new ThreadStart (Start));
  1134. t1.SetApartmentState (ApartmentState.STA);
  1135. Assert.AreEqual (ApartmentState.STA, t1.ApartmentState, "Thread1 Set Once");
  1136. t1.SetApartmentState (ApartmentState.MTA);
  1137. }
  1138. [Test]
  1139. public void TestTrySetApartmentState ()
  1140. {
  1141. Thread t1 = new Thread (new ThreadStart (Start));
  1142. t1.SetApartmentState (ApartmentState.STA);
  1143. Assert.AreEqual (ApartmentState.STA, t1.ApartmentState, "#1");
  1144. bool result = t1.TrySetApartmentState (ApartmentState.MTA);
  1145. Assert.IsFalse (result, "#2");
  1146. result = t1.TrySetApartmentState (ApartmentState.STA);
  1147. Assert.IsTrue (result, "#3");
  1148. }
  1149. [Test]
  1150. public void TestTrySetApartmentStateRunning ()
  1151. {
  1152. Thread t1 = new Thread (new ThreadStart (Start));
  1153. t1.SetApartmentState (ApartmentState.STA);
  1154. Assert.AreEqual (ApartmentState.STA, t1.ApartmentState, "#1");
  1155. t1.Start ();
  1156. try {
  1157. t1.TrySetApartmentState (ApartmentState.STA);
  1158. Assert.Fail ("#2");
  1159. } catch (ThreadStateException) {
  1160. }
  1161. t1.Join ();
  1162. }
  1163. [Test]
  1164. public void Volatile () {
  1165. double v3 = 55667;
  1166. Thread.VolatileWrite (ref v3, double.MaxValue);
  1167. Assert.AreEqual (v3, double.MaxValue);
  1168. float v4 = 1;
  1169. Thread.VolatileWrite (ref v4, float.MaxValue);
  1170. Assert.AreEqual (v4, float.MaxValue);
  1171. }
  1172. [Test]
  1173. public void Culture ()
  1174. {
  1175. Assert.IsNotNull (Thread.CurrentThread.CurrentCulture, "CurrentCulture");
  1176. Assert.IsNotNull (Thread.CurrentThread.CurrentUICulture, "CurrentUICulture");
  1177. }
  1178. [Test]
  1179. public void ThreadStartSimple ()
  1180. {
  1181. int i = 0;
  1182. Thread t = new Thread (delegate () {
  1183. // ensure the NSAutoreleasePool works
  1184. i++;
  1185. });
  1186. t.Start ();
  1187. t.Join ();
  1188. Assert.AreEqual (1, i, "ThreadStart");
  1189. }
  1190. [Test]
  1191. public void ParametrizedThreadStart ()
  1192. {
  1193. int i = 0;
  1194. object arg = null;
  1195. Thread t = new Thread (delegate (object obj) {
  1196. // ensure the NSAutoreleasePool works
  1197. i++;
  1198. arg = obj;
  1199. });
  1200. t.Start (this);
  1201. t.Join ();
  1202. Assert.AreEqual (1, i, "ParametrizedThreadStart");
  1203. Assert.AreEqual (this, arg, "obj");
  1204. }
  1205. [Test]
  1206. public void SetNameTpThread () {
  1207. ThreadPool.QueueUserWorkItem(new WaitCallback(ThreadProc));
  1208. }
  1209. static void ThreadProc(Object stateInfo) {
  1210. Thread.CurrentThread.Name = "My Worker";
  1211. }
  1212. [Test]
  1213. public void GetStackTraces () {
  1214. var m = typeof (Thread).GetMethod ("Mono_GetStackTraces", BindingFlags.NonPublic|BindingFlags.Static);
  1215. if (m != null) {
  1216. var res = (Dictionary<Thread,SD.StackTrace>)typeof (Thread).GetMethod ("Mono_GetStackTraces", BindingFlags.NonPublic|BindingFlags.Static).Invoke (null, null);
  1217. foreach (var t in res.Keys) {
  1218. var st = res [t].ToString ();
  1219. }
  1220. }
  1221. }
  1222. }
  1223. public class TestUtil
  1224. {
  1225. public static void WaitForNotAlive (Thread t, string s)
  1226. {
  1227. WhileAlive (t, true, s);
  1228. }
  1229. public static void WaitForAlive (Thread t, string s)
  1230. {
  1231. WhileAlive (t, false, s);
  1232. }
  1233. public static bool WaitForAliveOrStop (Thread t, string s)
  1234. {
  1235. return WhileAliveOrStop (t, false, s);
  1236. }
  1237. public static void WhileAlive (Thread t, bool alive, string s)
  1238. {
  1239. DateTime ti = DateTime.Now;
  1240. while (t.IsAlive == alive) {
  1241. if ((DateTime.Now - ti).TotalSeconds > 10) {
  1242. if (alive) Assert.Fail ("Timeout while waiting for not alive state. " + s);
  1243. else Assert.Fail ("Timeout while waiting for alive state. " + s);
  1244. }
  1245. }
  1246. }
  1247. public static bool WhileAliveOrStop (Thread t, bool alive, string s)
  1248. {
  1249. DateTime ti = DateTime.Now;
  1250. while (t.IsAlive == alive) {
  1251. if (t.ThreadState == ThreadState.Stopped)
  1252. return false;
  1253. if ((DateTime.Now - ti).TotalSeconds > 10) {
  1254. if (alive) Assert.Fail ("Timeout while waiting for not alive state. " + s);
  1255. else Assert.Fail ("Timeout while waiting for alive state. " + s);
  1256. }
  1257. }
  1258. return true;
  1259. }
  1260. }
  1261. }