summaryrefslogtreecommitdiff
path: root/mcs/class/corlib/Test/System.Threading/ThreadTest.cs
blob: ca5489ef0d876de3cb5437c79c6bdd184f807e71 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
// ThreadTest.cs - NUnit Test Cases for the System.Threading.Thread class
//
// Authors
//	Eduardo Garcia Cebollero (kiwnix@yahoo.es)
//	Sebastien Pouliot  <sebastien@ximian.com>
//
// (C) Eduardo Garcia Cebollero.
// (C) Ximian, Inc.  http://www.ximian.com
// (C) 2004 Novell (http://www.novell.com)
//

using System;
using System.Globalization;
using System.Security.Principal;
using System.Threading;

using NUnit.Framework;

namespace MonoTests.System.Threading
{
	// These tests seem to hang the 2.0 framework. So they are disabled for now
	// Don't reenable them until you can run a few thousand times on an SMP box.
	[Category ("NotWorking")]
	public class ThreadedPrincipalTest
	{
		public static void NoPrincipal () 
		{
#if !TARGET_JVM // AppDomain.SetPrincipalPolicy not supported for TARGET_JVM
			AppDomain.CurrentDomain.SetPrincipalPolicy (PrincipalPolicy.NoPrincipal);
#endif
			IPrincipal p = Thread.CurrentPrincipal;
			Assert.IsNull (p, "#1");

			Thread.CurrentPrincipal = new GenericPrincipal (new GenericIdentity ("mono"), null);
			Assert.IsNotNull (Thread.CurrentPrincipal, "#2");

			Thread.CurrentPrincipal = null;
			Assert.IsNull (Thread.CurrentPrincipal, "#3");
			// in this case we can return to null
		}

#if !TARGET_JVM // AppDomain.SetPrincipalPolicy not supported for TARGET_JVM
		public static void UnauthenticatedPrincipal () 
		{
			AppDomain.CurrentDomain.SetPrincipalPolicy (PrincipalPolicy.UnauthenticatedPrincipal);
			IPrincipal p = Thread.CurrentPrincipal;
			Assert.IsNotNull (p, "#1");
			Assert.IsTrue ((p is GenericPrincipal), "#2");
			Assert.AreEqual (String.Empty, p.Identity.Name, "#3");
			Assert.AreEqual (String.Empty, p.Identity.AuthenticationType, "#4");
			Assert.IsFalse (p.Identity.IsAuthenticated, "#5");

			Thread.CurrentPrincipal = new GenericPrincipal (new GenericIdentity ("mono"), null);
			Assert.IsNotNull (Thread.CurrentPrincipal, "#6");

			Thread.CurrentPrincipal = null;
			Assert.IsNotNull (Thread.CurrentPrincipal, "#7");
			// in this case we can't return to null
		}

		public static void WindowsPrincipal () 
		{
			AppDomain.CurrentDomain.SetPrincipalPolicy (PrincipalPolicy.WindowsPrincipal);
			IPrincipal p = Thread.CurrentPrincipal;
			Assert.IsNotNull (p, "#1");
			Assert.IsTrue ((p is WindowsPrincipal), "#2");
			Assert.IsNotNull (p.Identity.Name, "#3");
			Assert.IsNotNull (p.Identity.AuthenticationType, "#4");
			Assert.IsTrue (p.Identity.IsAuthenticated, "#5");

			// note: we can switch from a WindowsPrincipal to a GenericPrincipal
			Thread.CurrentPrincipal = new GenericPrincipal (new GenericIdentity ("mono"), null);
			Assert.IsNotNull (Thread.CurrentPrincipal, "#6");

			Thread.CurrentPrincipal = null;
			Assert.IsNotNull (Thread.CurrentPrincipal, "#7");
			// in this case we can't return to null
		}
#endif // TARGET_JVM

		public static void CopyOnNewThread ()
		{
			Assert.IsNotNull (Thread.CurrentPrincipal, "#1");
			Assert.AreEqual ("good", Thread.CurrentPrincipal.Identity.Name, "#2");
		}
	}

	[TestFixture]
	[Category("MobileNotWorking")] // Abort #10240
	public class ThreadTest
	{
		TimeSpan Infinite = new TimeSpan (-10000);	// -10000 ticks == -1 ms
		TimeSpan SmallNegative = new TimeSpan (-2);	// between 0 and -1.0 (infinite) ms
		TimeSpan Negative = new TimeSpan (-20000);	// really negative
		TimeSpan MaxValue = TimeSpan.FromMilliseconds ((long) Int32.MaxValue);
		TimeSpan TooLarge = TimeSpan.FromMilliseconds ((long) Int32.MaxValue + 1);

		static bool is_win32;
		static bool is_mono;

		static ThreadTest ()
		{
			switch (Environment.OSVersion.Platform) {
			case PlatformID.Win32NT:
			case PlatformID.Win32S:
			case PlatformID.Win32Windows:
			case PlatformID.WinCE:
				is_win32 = true;
				break;
			}

			// check a class in mscorlib to determine if we're running on Mono
			if (Type.GetType ("System.MonoType", false) != null)
				is_mono = true;
		}

		//Some Classes to test as threads
		private class C1Test
		{
			public int cnt;
			public Thread thread1;
			public bool endm1;
			public bool endm2;

			public C1Test()
			{
				thread1 = (Thread)null;
				this.cnt = 0;
				endm1 = endm2 = false;
			}
			
			public void TestMethod()
			{
				while (cnt < 10)
				{
					cnt++;
				}
				endm1 = true;
			}
			public void TestMethod2()
			{
				if (!(thread1==(Thread)null) )
				{
					thread1.Join();
				}
				endm2 = true;
			}
		}

		private class C2Test
		{
			public int cnt;
			public bool run = false;
			
			public C2Test()
			{
				this.cnt = 0;
			}

			public void TestMethod()
			{
				run = true;
				while (true)
				{
					if (cnt < 1000)
						cnt++;
					else
						cnt = 0;
				}
			}
		}
		
		private class C3Test
		{
			public C1Test sub_class;
			public Thread sub_thread;

			public C3Test()
			{
				sub_class = new C1Test();
				sub_thread = new Thread(new ThreadStart(sub_class.TestMethod));
			}

			public void TestMethod1()
			{
				sub_thread.Start();
				Thread.Sleep (100);
				sub_thread.Abort();
			}
		}
		
		private class C4Test
		{
			public C1Test class1;
			public C1Test class2;
			public Thread thread1;
			public Thread thread2;
			public bool T1ON ;
			public bool T2ON ;

			public C4Test()
			{
				T1ON = false;
				T2ON = false;
				class1 = new C1Test();
				class2 = new C1Test();
				thread1 = new Thread(new ThreadStart(class1.TestMethod));
				thread2 = new Thread(new ThreadStart(class2.TestMethod));
			}

			public void TestMethod1()
			{
				thread1.Start();
				TestUtil.WaitForAlive (thread1, "wait1");
				T1ON = true;
				thread2.Start();
				TestUtil.WaitForAlive (thread2, "wait2");
				T2ON = true;
				thread1.Abort();
				TestUtil.WaitForNotAlive (thread1, "wait3");
				T1ON = false;
				thread2.Abort();
				TestUtil.WaitForNotAlive (thread2, "wait4");
				T2ON = false;
			}
			
			public void TestMethod2()
			{
				thread1.Start();
				thread1.Join();
			}
		}

		[Test]
		public void TestCtor1()
		{
			C1Test test1 = new C1Test();
			Thread t = new Thread (new ThreadStart (test1.TestMethod));

			Assert.IsTrue (t.CurrentCulture.IsReadOnly, "CurrentCulture.IsReadOnly");
			Assert.IsFalse (t.IsAlive, "IsAlive");
			Assert.IsFalse (t.IsBackground, "IsBackground");
			Assert.IsNull (t.Name, "Name");
			Assert.AreEqual (ThreadState.Unstarted, t.ThreadState, "ThreadState");
		}

		[Test]
		[Category ("NotWorking")] // we're not sharing (read-only) CultureInfo
		public void CultureInfo_Shared_Across_Threads ()
		{
			Thread t = new Thread (TestCtor1);
			Assert.AreSame (t.CurrentCulture, t.CurrentUICulture, "Culture");

			Assert.AreSame (t.CurrentCulture, CultureInfo.CurrentCulture, "CultureInfo.CurrentCulture");
			Assert.AreSame (t.CurrentUICulture, CultureInfo.CurrentUICulture, "CultureInfo.CurrentUICulture");

			Assert.AreSame (t.CurrentCulture, Thread.CurrentThread.CurrentCulture, "Thread.CurrentThread.CurrentCulture");
			Assert.AreSame (t.CurrentUICulture, Thread.CurrentThread.CurrentUICulture, "Thread.CurrentThread.CurrentUICulture");
		}

		[Test] // bug #325566
		public void GetHashCodeTest ()
		{
			C1Test test1 = new C1Test ();
			Thread tA = new Thread (new ThreadStart (test1.TestMethod));
			int hA1 = tA.GetHashCode ();
#if NET_2_0
			Assert.IsTrue (hA1 > 0, "#A1");
#endif
			tA.Start ();
			int hA2 = tA.GetHashCode ();
			Assert.AreEqual (hA1, hA2, "#A2");
			tA.Join ();
			int hA3 = tA.GetHashCode ();
			Assert.AreEqual (hA1, hA3, "#A3");
#if NET_2_0
			Assert.AreEqual (hA1, tA.ManagedThreadId, "#A4");
#endif

			test1 = new C1Test ();
			Thread tB = new Thread (new ThreadStart (test1.TestMethod));
			int hB1 = tB.GetHashCode ();
#if NET_2_0
			Assert.IsTrue (hB1 > 0, "#B1");
#endif
			tB.Start ();
			int hB2 = tB.GetHashCode ();
			Assert.AreEqual (hB1, hB2, "#B2");
			tB.Join ();
			int hB3 = tB.GetHashCode ();
			Assert.AreEqual (hB1, hB3, "#B3");
#if NET_2_0
			Assert.AreEqual (hB1, tB.ManagedThreadId, "#B4");
#endif
			Assert.IsFalse (hA2 == hB2, "#B5");
		}

#if NET_2_0
		[Test] // bug #82700
		public void ManagedThreadId ()
		{
			C1Test test1 = new C1Test ();
			Thread t1 = new Thread (new ThreadStart (test1.TestMethod));
			int mtA1 = t1.ManagedThreadId;
			t1.Start ();
			int mtA2 = t1.ManagedThreadId;
			t1.Join ();
			int mtA3 = t1.ManagedThreadId;
			Assert.AreEqual (mtA1, mtA2, "#A1");
			Assert.AreEqual (mtA2, mtA3, "#A2");

			test1 = new C1Test ();
			Thread t2 = new Thread (new ThreadStart (test1.TestMethod));
			int mtB1 = t2.ManagedThreadId;
			t2.Start ();
			int mtB2 = t2.ManagedThreadId;
			t2.Join ();
			int mtB3 = t2.ManagedThreadId;
			Assert.AreEqual (mtB1, mtB2, "#B1");
			Assert.AreEqual (mtB2, mtB3, "#B2");
			Assert.IsFalse (mtB1 == mtA1, "#B3");
		}
#endif

		[Test]
		[Category ("NotDotNet")] // it hangs.
		public void TestStart()
		{
			if (is_win32 && is_mono)
				Assert.Fail ("This test fails on Win32. The test should be fixed.");
		{
			C1Test test1 = new C1Test();
			Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
			TestThread.Start();
			TestThread.Join();
			Assert.AreEqual (10, test1.cnt, "#1");
		}
		{
			C2Test test1 = new C2Test();
			Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
			TestThread.Start();
			TestThread.Abort();
			try {
				TestThread.Start();
				Assert.Fail ("#2");
			} catch (ThreadStateException) {
			}
		}
		{
			C2Test test1 = new C2Test();
			Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
			TestThread.Start();
			while (!test1.run) {
			}
			bool started = (TestThread.ThreadState == ThreadState.Running);
			Assert.AreEqual (started, test1.run, "#15 Thread Is not in the correct state: ");
			TestThread.Abort();
		}
		}

		[Test]
		public void TestApartmentState ()
		{
			if (is_win32 && is_mono)
				Assert.Fail ("This test fails on mono on win32. Our runtime should be fixed.");

			C2Test test1 = new C2Test();
			Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
			Assert.AreEqual (ApartmentState.Unknown, TestThread.ApartmentState, "#1");
			TestThread.Start();
			TestUtil.WaitForAlive (TestThread, "wait5");
#if NET_2_0
			Assert.AreEqual (ApartmentState.MTA, TestThread.ApartmentState, "#2");
#else
			Assert.AreEqual (ApartmentState.Unknown, TestThread.ApartmentState, "#3");
#endif
			TestThread.Abort();
		}

		[Test]
		public void TestPriority1()
		{
			if (is_win32 && is_mono)
				Assert.Fail ("This test fails on mono on Win32. Our runtime should be fixed.");

			C2Test test1 = new C2Test();
			Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
			try {
				TestThread.Priority=ThreadPriority.BelowNormal;
				ThreadPriority after = TestThread.Priority;
				TestThread.Start();
				TestUtil.WaitForAlive (TestThread, "wait7");
				ThreadPriority before = TestThread.Priority;
				Assert.AreEqual (before, after, "#41 Unexpected Priority Change: ");
			} finally {
				TestThread.Abort();
			}
		}

		[Test]
		[Category ("NotDotNet")] // on MS, Thread is still in AbortRequested state when Start is invoked
		public void AbortUnstarted ()
		{
			C2Test test1 = new C2Test();
			Thread th = new Thread (new ThreadStart (test1.TestMethod));
			th.Abort ();
			th.Start ();
		}

		[Test]
		[Category ("NotDotNet")] // on MS, ThreadState is immediately Stopped after Abort
		[Category ("NotWorking")] // this is a MonoTODO -> no support for Priority
		public void TestPriority2()
		{
			C2Test test1 = new C2Test();
			Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
			try {
				Assert.AreEqual (ThreadPriority.Normal, TestThread.Priority, "#42 Incorrect Priority in New thread: ");
				TestThread.Start();
				TestUtil.WaitForAliveOrStop (TestThread, "wait8");
				Assert.AreEqual (ThreadPriority.Normal, TestThread.Priority, "#43 Incorrect Priority in Started thread: ");
			} finally {
				TestThread.Abort();
			}
			Assert.AreEqual (ThreadPriority.Normal, TestThread.Priority, "#44 Incorrect Priority in Aborted thread: ");
		}

		[Test]
		[Category ("NotWorking")] // this is a MonoTODO -> no support for Priority
		public void TestPriority3()
		{
			C2Test test1 = new C2Test();
			Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
			try {
				TestThread.Start();
				TestThread.Priority = ThreadPriority.Lowest;
				Assert.AreEqual (ThreadPriority.Lowest, TestThread.Priority, "#45A Incorrect Priority:");
				TestThread.Priority = ThreadPriority.BelowNormal;
				Assert.AreEqual (ThreadPriority.BelowNormal, TestThread.Priority, "#45B Incorrect Priority:");
				TestThread.Priority = ThreadPriority.Normal;
				Assert.AreEqual (ThreadPriority.Normal, TestThread.Priority, "#45C Incorrect Priority:");
				TestThread.Priority = ThreadPriority.AboveNormal;
				Assert.AreEqual (ThreadPriority.AboveNormal, TestThread.Priority, "#45D Incorrect Priority:");
				TestThread.Priority = ThreadPriority.Highest;
				Assert.AreEqual (ThreadPriority.Highest, TestThread.Priority, "#45E Incorrect Priority:");
			}
			finally {
				TestThread.Abort();
			}
		}

		[Test]
		public void TestUndivisibleByPageSizeMaxStackSize ()
		{
			const int undivisible_stacksize = 1048573;

			var thread = new Thread (new ThreadStart (delegate {}), undivisible_stacksize);
			thread.Start ();
			thread.Join ();
		}

		[Test]
		public void TestIsBackground1 ()
		{
			if (is_win32 && is_mono)
				Assert.Fail ("This test fails on mono on Win32. Our runtime should be fixed.");

			C2Test test1 = new C2Test();
			Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
			try {
				TestThread.Start();
				TestUtil.WaitForAlive (TestThread, "wait9");
				bool state = TestThread.IsBackground;
				Assert.IsFalse (state, "#51 IsBackground not set at the default state: ");
			} finally {
				TestThread.Abort();
			}
		}

		[Test]
		public void TestIsBackground2 ()
		{
			C2Test test1 = new C2Test();
			Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
			TestThread.IsBackground = true;
			try {
				TestThread.Start();
			} finally {
				TestThread.Abort();
			}
			
			if (TestThread.IsAlive) {
				try {
					Assert.IsTrue (TestThread.IsBackground, "#52 Is Background Changed to Start ");
				} catch (ThreadStateException) {
					// Ignore if thread died meantime
				}
			}
		}

		[Test]
		public void TestName()
		{
			if (is_win32 && is_mono)
				Assert.Fail ("This test fails on mono on Win32. Our runtime should be fixed.");

			C2Test test1 = new C2Test();
			Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
			try {
				TestThread.Start();
				TestUtil.WaitForAlive (TestThread, "wait10");
				string name = TestThread.Name;
				Assert.IsNull (name, "#61 Name set when mustn't be set: ");
				string newname = "Testing....";
				TestThread.Name = newname;
				Assert.AreEqual (newname, TestThread.Name, "#62 Name not set when must be set: ");
			} finally {
				TestThread.Abort();
			}
		}

		[Test]
		public void Name ()
		{
			Thread t = new Thread (new ThreadStart (Name));
			Assert.IsNull (t.Name, "Name-1");
			t.Name = null;
			Assert.IsNull (t.Name, "Name-2");
		}

		[Test]
		[ExpectedException (typeof (InvalidOperationException))]
		public void ReName ()
		{
			Thread t = new Thread (new ThreadStart (ReName));
			t.Name = "a";
			t.Name = "b";
		}

		[Test]
		public void TestNestedThreads1()
		{
			C3Test test1 = new C3Test();
			Thread TestThread = new Thread(new ThreadStart(test1.TestMethod1));
			try {
				TestThread.Start();
				TestUtil.WaitForAlive (TestThread, "wait11");
			} finally {
				TestThread.Abort();
			}
		}

		[Test]
		public void TestNestedThreads2()
		{
			C4Test test1 = new C4Test();
			Thread TestThread = new Thread(new ThreadStart(test1.TestMethod1));
			try {
				TestThread.Start();
			} finally {
				TestThread.Abort();
			}
		}

		[Test]
		public void TestJoin1()
		{
			C1Test test1 = new C1Test();
			C1Test test2 = new C1Test();
			Thread thread1 = new Thread(new ThreadStart(test1.TestMethod));
			Thread thread2 = new Thread(new ThreadStart(test1.TestMethod2));
			try {
				thread1.Start();
				thread2.Start();
				thread2.Join();
			} finally {
				thread1.Abort();
				thread2.Abort();
			}
		}

		[Test]
		[ExpectedException (typeof (ArgumentOutOfRangeException))]
		public void Join_Int32_Negative ()
		{
			// -1 is Timeout.Infinite
			Thread.CurrentThread.Join (-2);
		}

		[Test]
		[ExpectedException (typeof (ArgumentOutOfRangeException))]
		public void Join_TimeSpan_Negative ()
		{
			Thread.CurrentThread.Join (Negative);
		}

		[Test]
		[ExpectedException (typeof (ArgumentOutOfRangeException))]
		public void Join_TimeSpan_TooLarge ()
		{
			Thread.CurrentThread.Join (TooLarge);
		}

		[Test]
		public void Join_TimeSpan_SmallNegative ()
		{
			Thread.CurrentThread.Join (SmallNegative);
		}

		[Test]
		[ExpectedException (typeof (ArgumentOutOfRangeException))]
		public void Sleep_Int32_Negative ()
		{
			// -1 is Timeout.Infinite
			Thread.Sleep (-2);
		}

		[Test]
		public void Sleep_TimeSpan_SmallNegative ()
		{
			Thread.Sleep (SmallNegative);
		}

		[Test]
		[ExpectedException (typeof (ArgumentOutOfRangeException))]
		public void Sleep_TimeSpan_Negative ()
		{
			Thread.Sleep (Negative);
		}

		[Test]
		[ExpectedException (typeof (ArgumentOutOfRangeException))]
		public void Sleep_TimeSpan_TooLarge ()
		{
			Thread.Sleep (TooLarge);
		}

		[Test]
		public void SpinWait ()
		{
			// no exception for negative numbers
			Thread.SpinWait (Int32.MinValue);
			Thread.SpinWait (0);
		}

		[Test]
		public void TestThreadState ()
		{
			if (is_win32 && is_mono)
				Assert.Fail ("This test fails on mono on Win32. Our runtime should be fixed.");

			//TODO: Test The rest of the possible transitions
			C2Test test1 = new C2Test();
			Thread TestThread = new Thread(new ThreadStart(test1.TestMethod));
			Assert.AreEqual (ThreadState.Unstarted, TestThread.ThreadState, "#101 Wrong Thread State");
			try {
				TestThread.Start();
				//while(!TestThread.IsAlive); //In the MS Documentation this is not necessary
											  //but in the MS SDK it is
				Assert.IsTrue (TestThread.ThreadState == ThreadState.Running || (TestThread.ThreadState & ThreadState.Unstarted) != 0,
					"#102 Wrong Thread State: " + TestThread.ThreadState.ToString ());
			} finally {
				TestThread.Abort();
			}
			
			TestUtil.WaitForNotAlive (TestThread, "wait12");
			// Docs say state will be Stopped, but Aborted happens sometimes (?)
			Assert.IsTrue ((ThreadState.Stopped & TestThread.ThreadState) != 0 || (ThreadState.Aborted & TestThread.ThreadState) != 0,
				"#103 Wrong Thread State: " + TestThread.ThreadState.ToString ());
		}

		[Test]
		[Ignore ("see comment below.")]
		public void CurrentPrincipal_PrincipalPolicy_NoPrincipal () 
		{
			// note: switching from PrincipalPolicy won't work inside the same thread
			// because as soon as a Principal object is created the Policy doesn't matter anymore
			Thread t = new Thread (new ThreadStart (ThreadedPrincipalTest.NoPrincipal));
			try {
				t.Start ();
				t.Join ();
			} catch {
				t.Abort ();
			}
		}

#if !TARGET_JVM // AppDomain.SetPrincipalPolicy not supported for TARGET_JVM
		[Test]
		[Ignore ("see comment below.")]
		public void CurrentPrincipal_PrincipalPolicy_UnauthenticatedPrincipal () 
		{
			// note: switching from PrincipalPolicy won't work inside the same thread
			// because as soon as a Principal object is created the Policy doesn't matter anymore
			Thread t = new Thread (new ThreadStart (ThreadedPrincipalTest.UnauthenticatedPrincipal));
			try {
				t.Start ();
				t.Join ();
			} catch {
				t.Abort ();
			}
		}

		[Test]
		public void CurrentPrincipal_PrincipalPolicy_WindowsPrincipal () 
		{
			// note: switching from PrincipalPolicy won't work inside the same thread
			// because as soon as a Principal object is created the Policy doesn't matter anymore
			Thread t = new Thread (new ThreadStart (ThreadedPrincipalTest.WindowsPrincipal));
			try {
				t.Start ();
				t.Join ();
			} catch {
				t.Abort ();
			}
		}
#endif // TARGET_JVM
		
		[Test]
		public void IPrincipal_CopyOnNewThread () 
		{
			Thread.CurrentPrincipal = new GenericPrincipal (new GenericIdentity ("bad"), null);
			Thread t = new Thread (new ThreadStart (ThreadedPrincipalTest.CopyOnNewThread));
			try {
				Thread.CurrentPrincipal = new GenericPrincipal (new GenericIdentity ("good"), null);
				t.Start ();
				t.Join ();
			} catch {
				t.Abort ();
			}
		}

		int counter = 0;

		[Test]
		public void TestSuspend ()
		{
			Thread t = new Thread (new ThreadStart (DoCount));
			t.IsBackground = true;
			t.Start ();
			
			CheckIsRunning ("t1", t);
			
			t.Suspend ();
			WaitSuspended ("t2", t);
			
			CheckIsNotRunning ("t3", t);
			
			t.Resume ();
			WaitResumed ("t4", t);
			
			CheckIsRunning ("t5", t);
			
			t.Abort ();
			TestUtil.WaitForNotAlive (t, "wait13");
			CheckIsNotRunning ("t6", t);
		}

		[Test]
		[Category("NotDotNet")] // On MS, ThreadStateException is thrown on Abort: "Thread is suspended; attempting to abort"
		public void TestSuspendAbort ()
		{
			if (is_win32 && is_mono)
				Assert.Fail ("This test fails on Win32. The test should be fixed.");

			Thread t = new Thread (new ThreadStart (DoCount));
			t.IsBackground = true;
			t.Start ();
			
			CheckIsRunning ("t1", t);
			
			t.Suspend ();
			WaitSuspended ("t2", t);
			
			CheckIsNotRunning ("t3", t);
			
			t.Abort ();
			
			int n=0;
			while (t.IsAlive && n < 200) {
				Thread.Sleep (10);
				n++;
			}

			Assert.IsTrue (n < 200, "Timeout while waiting for abort");
			
			CheckIsNotRunning ("t6", t);
		}

		[Test]
		public void Test_Interrupt ()
		{
			bool interruptedExceptionThrown = false;
			ThreadPool.QueueUserWorkItem (Test_Interrupt_Worker, Thread.CurrentThread);

			try {
				try {
					Thread.Sleep (3000);
				} finally {
					try {
						Thread.Sleep (0);
					} catch (ThreadInterruptedException) {
						Assert.Fail ("ThreadInterruptedException thrown twice");
					}
				}
			} catch (ThreadInterruptedException) {
				interruptedExceptionThrown = true;
			}

			Assert.IsTrue (interruptedExceptionThrown, "ThreadInterruptedException expected.");
		}

		[Test]
		[ExpectedException (typeof (ArgumentNullException))]
		public void TestQueueUserWorkItemNullCallback ()
		{
			ThreadPool.QueueUserWorkItem (null, null);
		}

		private void Test_Interrupt_Worker (object o)
		{
			Thread t = o as Thread;
			Thread.Sleep (100);
			t.Interrupt ();
		}
		
		[Test]
		[Category ("NotDotNet")] // it crashes nunit.
		public void Test_InterruptCurrentThread ()
		{
			bool interruptedExceptionThrown = false;

			Thread.CurrentThread.Interrupt ();
			try {
				Thread.Sleep (0);
				Assert.Fail ();
			} catch (ThreadInterruptedException) {
			}
		}

		[Test]
		public void GetNamedDataSlotTest ()
		{
			Assert.IsNotNull (Thread.GetNamedDataSlot ("te#st"), "#1");
			Assert.AreSame (Thread.GetNamedDataSlot ("te#st"), Thread.GetNamedDataSlot ("te#st"), "#2");
		}

		void CheckIsRunning (string s, Thread t)
		{
			int c = counter;
			Thread.Sleep (100);
			Assert.IsTrue (counter > c, s);
		}
		
		void CheckIsNotRunning (string s, Thread t)
		{
			int c = counter;
			Thread.Sleep (100);
			Assert.AreEqual (counter, c, s);
		}
		
		void WaitSuspended (string s, Thread t)
		{
			int n=0;
			ThreadState state = t.ThreadState;
			while ((state & ThreadState.Suspended) == 0) {
				Assert.IsTrue ((state & ThreadState.SuspendRequested) != 0, s + ": expected SuspendRequested state");
				Thread.Sleep (10);
				n++;
				Assert.IsTrue (n < 100, s + ": failed to suspend");
				state = t.ThreadState;
			}
			Assert.IsTrue ((state & ThreadState.SuspendRequested) == 0, s + ": SuspendRequested state not expected");
		}
		
		void WaitResumed (string s, Thread t)
		{
			int n=0;
			while ((t.ThreadState & ThreadState.Suspended) != 0) {
				Thread.Sleep (10);
				n++;
				Assert.IsTrue (n < 100, s + ": failed to resume");
			}
		}
		
		public void DoCount ()
		{
			while (true) {
				counter++;
				Thread.Sleep (1);
			}
		}
	}

	[TestFixture]
	public class ThreadStateTest {
		void Start ()
		{
		}

		[Test] // bug #81720
		public void IsBackGround ()
		{
			Thread t1 = new Thread (new ThreadStart (Start));
			Assert.AreEqual (ThreadState.Unstarted, t1.ThreadState, "#A1");
			Assert.IsFalse (t1.IsBackground, "#A2");
			t1.Start ();
			t1.Join ();
			Assert.AreEqual (ThreadState.Stopped, t1.ThreadState, "#A3");

			try {
				bool isBackGround = t1.IsBackground;
				Assert.Fail ("#A4: " + isBackGround.ToString ());
			} catch (ThreadStateException ex) {
				Assert.AreEqual (typeof (ThreadStateException), ex.GetType (), "#A5");
				Assert.IsNull (ex.InnerException, "#A6");
				Assert.IsNotNull (ex.Message, "#A7");
			}

			Thread t2 = new Thread (new ThreadStart (Start));
			Assert.AreEqual (ThreadState.Unstarted, t2.ThreadState, "#B1");
			t2.IsBackground = true;
			Assert.AreEqual (ThreadState.Unstarted | ThreadState.Background, t2.ThreadState, "#B2");
			Assert.IsTrue (t2.IsBackground, "#B3");
			t2.Start ();
			t2.Join ();
			Assert.AreEqual (ThreadState.Stopped, t2.ThreadState, "#B4");

			try {
				bool isBackGround = t2.IsBackground;
				Assert.Fail ("#B5: " + isBackGround.ToString ());
			} catch (ThreadStateException ex) {
				Assert.AreEqual (typeof (ThreadStateException), ex.GetType (), "#B6");
				Assert.IsNull (ex.InnerException, "#B7");
				Assert.IsNotNull (ex.Message, "#B8");
			}
		}
	}

	[TestFixture]
	[Serializable]
	public class ThreadTest_ManagedThreadId
	{
		AppDomain ad1;
		AppDomain ad2;
		MBRO mbro = new MBRO ();

		class MBRO : MarshalByRefObject {
			public int id_a1;
			public int id_b1;
			public int id_b2;
			public string ad_a1;
			public string ad_b1;
			public string ad_b2;
			public string message;
		}
#if !MOBILE
		[Test]
		public void ManagedThreadId_AppDomains ()
		{
			AppDomain currentDomain = AppDomain.CurrentDomain;
			ad1 = AppDomain.CreateDomain ("AppDomain 1", currentDomain.Evidence, currentDomain.SetupInformation);
			ad2 = AppDomain.CreateDomain ("AppDomain 2", currentDomain.Evidence, currentDomain.SetupInformation);

			Thread a = new Thread (ThreadA);
			Thread b = new Thread (ThreadB);
			// execute on AppDomain 1 thread A
			// execute on AppDomain 2 thread B
			// execute on AppDomain 1 thread B - must have same ManagedThreadId as Ad 2 on thread B
			a.Start ();
			a.Join ();
			b.Start ();
			b.Join ();

			AppDomain.Unload (ad1);
			AppDomain.Unload (ad2);

			if (mbro.message != null)
				Assert.Fail (mbro.message);

			// 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);

			Assert.AreEqual ("AppDomain 1", mbro.ad_a1, "Name #1");
			Assert.AreEqual ("AppDomain 1", mbro.ad_b1, "Name #2");
			Assert.AreEqual ("AppDomain 2", mbro.ad_b2, "Name #3");

			Assert.AreNotEqual (mbro.id_a1, mbro.id_b1, "Id #1");
			Assert.AreNotEqual (mbro.id_a1, mbro.id_b2, "Id #2");
			Assert.AreEqual (mbro.id_b1, mbro.id_b2, "Id #3");

			Assert.AreNotEqual (mbro.id_a1, Thread.CurrentThread.ManagedThreadId, "Id #4");
			Assert.AreNotEqual (mbro.id_b1, Thread.CurrentThread.ManagedThreadId, "Id #5");
			Assert.AreNotEqual (mbro.id_b2, Thread.CurrentThread.ManagedThreadId, "Id #6");
			Assert.AreNotEqual (mbro.ad_a1, AppDomain.CurrentDomain.FriendlyName, "Name #4");
			Assert.AreNotEqual (mbro.ad_b1, AppDomain.CurrentDomain.FriendlyName, "Name #5");
			Assert.AreNotEqual (mbro.ad_b2, AppDomain.CurrentDomain.FriendlyName, "Name #6");
		}
#endif
		void A1 ()
		{
			mbro.id_a1 = Thread.CurrentThread.ManagedThreadId;
			mbro.ad_a1 = AppDomain.CurrentDomain.FriendlyName;
		}
		
		void B2 ()
		{
			mbro.id_b2 = Thread.CurrentThread.ManagedThreadId;
			mbro.ad_b2 = AppDomain.CurrentDomain.FriendlyName;
		}

		void B1 ()
		{
			mbro.id_b1 = Thread.CurrentThread.ManagedThreadId;
			mbro.ad_b1 = AppDomain.CurrentDomain.FriendlyName;
		}

		void ThreadA (object obj)
		{
			// Console.WriteLine ("ThreadA");
			try {
				ad1.DoCallBack (A1);
			} catch (Exception ex) {
				mbro.message = string.Format ("ThreadA exception: {0}", ex);
			}
			// Console.WriteLine ("ThreadA Done");
		}

		void ThreadB (object obj)
		{
			// Console.WriteLine ("ThreadB");
			try {
				ad2.DoCallBack (B2);
				ad1.DoCallBack (B1);
			} catch (Exception ex) {
				mbro.message = string.Format ("ThreadB exception: {0}", ex);
			}
			// Console.WriteLine ("ThreadB Done");
		}
	}

	[TestFixture]
	public class ThreadApartmentTest
	{
		void Start ()
		{
		}

		[Test] // bug #81658
		public void ApartmentState_StoppedThread ()
		{
			Thread t1 = new Thread (new ThreadStart (Start));
			t1.Start ();
			t1.Join ();
			try {
				ApartmentState state = t1.ApartmentState;
				Assert.Fail ("#A1: " + state.ToString ());
			} catch (ThreadStateException ex) {
				Assert.AreEqual (typeof (ThreadStateException), ex.GetType (), "#A2");
				Assert.IsNull (ex.InnerException, "#A3");
				Assert.IsNotNull (ex.Message, "#A4");
			}

			Thread t2 = new Thread (new ThreadStart (Start));
			t2.IsBackground = true;
			t2.Start ();
			t2.Join ();
			try {
				ApartmentState state = t2.ApartmentState;
				Assert.Fail ("#B1: " + state.ToString ());
			} catch (ThreadStateException ex) {
				Assert.AreEqual (typeof (ThreadStateException), ex.GetType (), "#B2");
				Assert.IsNull (ex.InnerException, "#B3");
				Assert.IsNotNull (ex.Message, "#B4");
			}
		}

		[Test]
		public void ApartmentState_BackGround ()
		{
			Thread t1 = new Thread (new ThreadStart (Start));
			t1.IsBackground = true;
			Assert.AreEqual (ApartmentState.Unknown, t1.ApartmentState, "#1");
			t1.ApartmentState = ApartmentState.STA;
			Assert.AreEqual (ApartmentState.STA, t1.ApartmentState, "#2");
		}

		[Test]
		public void TestApartmentState ()
		{
			Thread t1 = new Thread (new ThreadStart (Start));
			Thread t2 = new Thread (new ThreadStart (Start));
			Thread t3 = new Thread (new ThreadStart (Start));

			Assert.AreEqual (ApartmentState.Unknown, t1.ApartmentState, "Thread1 Default");
			Assert.AreEqual (ApartmentState.Unknown, t2.ApartmentState, "Thread2 Default");
			Assert.AreEqual (ApartmentState.Unknown, t3.ApartmentState, "Thread3 Default");

			t1.ApartmentState = ApartmentState.STA;
			Assert.AreEqual (ApartmentState.STA, t1.ApartmentState, "Thread1 Set Once");
			t1.ApartmentState = ApartmentState.MTA;
			Assert.AreEqual (ApartmentState.STA, t1.ApartmentState, "Thread1 Set Twice");

			t2.ApartmentState = ApartmentState.MTA;
			Assert.AreEqual (ApartmentState.MTA, t2.ApartmentState, "Thread2 Set Once");
			t2.ApartmentState = ApartmentState.STA;
			Assert.AreEqual (ApartmentState.MTA, t2.ApartmentState, "Thread2 Set Twice");

			bool exception_occured = false;
			try {
				t3.ApartmentState = ApartmentState.Unknown;
			}
			catch (Exception) {
				exception_occured = true;
			}
			Assert.AreEqual (ApartmentState.Unknown, t3.ApartmentState, "Thread3 Set Invalid");
#if NET_2_0
			Assert.IsFalse (exception_occured, "Thread3 Set Invalid Exception Occured");
#else
			Assert.IsTrue (exception_occured, "Thread3 Set Invalid Exception Occured");
#endif

			t1.Start ();
			exception_occured = false;
			try {
				t1.ApartmentState = ApartmentState.STA;
			}
			catch (Exception) {
				exception_occured = true;
			}
			Assert.IsTrue (exception_occured, "Thread1 Started Invalid Exception Occured");
		}

		[Test]
		public void TestSetApartmentStateSameState ()
		{
			Thread t1 = new Thread (new ThreadStart (Start));
			t1.SetApartmentState (ApartmentState.STA);
			Assert.AreEqual (ApartmentState.STA, t1.ApartmentState, "Thread1 Set Once");

			t1.SetApartmentState (ApartmentState.STA);
			Assert.AreEqual (ApartmentState.STA, t1.ApartmentState, "Thread1 Set twice");
		}

		[Test]
		[ExpectedException(typeof(InvalidOperationException))]
		public void TestSetApartmentStateDiffState ()
		{
			Thread t1 = new Thread (new ThreadStart (Start));
			t1.SetApartmentState (ApartmentState.STA);
			Assert.AreEqual (ApartmentState.STA, t1.ApartmentState, "Thread1 Set Once");

			t1.SetApartmentState (ApartmentState.MTA);
		}

		[Test]
		public void TestTrySetApartmentState ()
		{
			Thread t1 = new Thread (new ThreadStart (Start));
			t1.SetApartmentState (ApartmentState.STA);
			Assert.AreEqual (ApartmentState.STA, t1.ApartmentState, "#1");

			bool result = t1.TrySetApartmentState (ApartmentState.MTA);
			Assert.IsFalse (result, "#2");

			result = t1.TrySetApartmentState (ApartmentState.STA);
			Assert.IsTrue (result, "#3");
		}

		[Test]
		public void TestTrySetApartmentStateRunning ()
		{
			Thread t1 = new Thread (new ThreadStart (Start));
			t1.SetApartmentState (ApartmentState.STA);
			Assert.AreEqual (ApartmentState.STA, t1.ApartmentState, "#1");

			t1.Start ();

			try {
				t1.TrySetApartmentState (ApartmentState.STA);
				Assert.Fail ("#2");
			} catch (ThreadStateException) {
			}

			t1.Join ();
		}

		[Test]
		public void Volatile () {
			double v3 = 55667;
			Thread.VolatileWrite (ref v3, double.MaxValue);
			Assert.AreEqual (v3, double.MaxValue);

			float v4 = 1;
			Thread.VolatileWrite (ref v4, float.MaxValue);
			Assert.AreEqual (v4, float.MaxValue);
		}

		[Test]
		public void Culture ()
		{
			Assert.IsNotNull (Thread.CurrentThread.CurrentCulture, "CurrentCulture");
			Assert.IsNotNull (Thread.CurrentThread.CurrentUICulture, "CurrentUICulture");
		}

		[Test]
		public void ThreadStartSimple ()
		{
			int i = 0;
			Thread t = new Thread (delegate () {
				// ensure the NSAutoreleasePool works
				i++;
			});
			t.Start ();
			t.Join ();
			Assert.AreEqual (1, i, "ThreadStart");
		}

		[Test]
		public void ParametrizedThreadStart ()
		{
			int i = 0;
			object arg = null;
			Thread t = new Thread (delegate (object obj) {
				// ensure the NSAutoreleasePool works
				i++;
				arg = obj;
			});
			t.Start (this);
			t.Join ();

			Assert.AreEqual (1, i, "ParametrizedThreadStart");
			Assert.AreEqual (this, arg, "obj");	
		}		

		[Test]
		public void SetNameTpThread () {
			ThreadPool.QueueUserWorkItem(new WaitCallback(ThreadProc));
		}

		static void ThreadProc(Object stateInfo) {
			Thread.CurrentThread.Name = "My Worker";
		}
	}

	public class TestUtil
	{
		public static void WaitForNotAlive (Thread t, string s)
		{
			WhileAlive (t, true, s);
		}
		
		public static void WaitForAlive (Thread t, string s)
		{
			WhileAlive (t, false, s);
		}
		
		public static bool WaitForAliveOrStop (Thread t, string s)
		{
			return WhileAliveOrStop (t, false, s);
		}
		
		public static void WhileAlive (Thread t, bool alive, string s)
		{
			DateTime ti = DateTime.Now;
			while (t.IsAlive == alive) {
				if ((DateTime.Now - ti).TotalSeconds > 10) {
					if (alive) Assert.Fail ("Timeout while waiting for not alive state. " + s);
					else Assert.Fail ("Timeout while waiting for alive state. " + s);
				}
			}
		}

		public static bool WhileAliveOrStop (Thread t, bool alive, string s)
		{
			DateTime ti = DateTime.Now;
			while (t.IsAlive == alive) {
				if (t.ThreadState == ThreadState.Stopped)
					return false;

				if ((DateTime.Now - ti).TotalSeconds > 10) {
					if (alive) Assert.Fail ("Timeout while waiting for not alive state. " + s);
					else Assert.Fail ("Timeout while waiting for alive state. " + s);
				}
			}

			return true;
		}
	}
}