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