Merge pull request #1949 from lewurm/fixtype
[mono.git] / mcs / class / corlib / Test / System.Threading / ThreadTest.cs
index 35ec599d7d01dde2924c4acb78c8a43c3577ba33..53921d2e13f30f80360c47ddd904bd3b26d20f8b 100644 (file)
@@ -25,9 +25,7 @@ namespace MonoTests.System.Threading
        {
                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");
 
@@ -39,7 +37,6 @@ namespace MonoTests.System.Threading
                        // 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);
@@ -76,7 +73,6 @@ namespace MonoTests.System.Threading
                        Assert.IsNotNull (Thread.CurrentPrincipal, "#7");
                        // in this case we can't return to null
                }
-#endif // TARGET_JVM
 
                public static void CopyOnNewThread ()
                {
@@ -89,10 +85,10 @@ namespace MonoTests.System.Threading
        [Category("MobileNotWorking")] // Abort #10240
        public class ThreadTest
        {
-               TimeSpan Infinite = new TimeSpan (-10000);      // -10000 ticks == -1 ms
+               //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 MaxValue = TimeSpan.FromMilliseconds ((long) Int32.MaxValue);
                TimeSpan TooLarge = TimeSpan.FromMilliseconds ((long) Int32.MaxValue + 1);
 
                static bool is_win32;
@@ -264,38 +260,29 @@ namespace MonoTests.System.Threading
                        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 ()
                {
@@ -320,7 +307,6 @@ namespace MonoTests.System.Threading
                        Assert.AreEqual (mtB2, mtB3, "#B2");
                        Assert.IsFalse (mtB1 == mtA1, "#B3");
                }
-#endif
 
                [Test]
                [Category ("NotDotNet")] // it hangs.
@@ -369,11 +355,7 @@ namespace MonoTests.System.Threading
                        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();
                }
 
@@ -530,13 +512,44 @@ namespace MonoTests.System.Threading
 
                [Test]
                [ExpectedException (typeof (InvalidOperationException))]
-               public void ReName ()
+               public void Rename ()
                {
-                       Thread t = new Thread (new ThreadStart (ReName));
+                       Thread t = new Thread (new ThreadStart (Rename));
                        t.Name = "a";
                        t.Name = "b";
                }
 
+               bool rename_finished;
+               bool rename_failed;
+
+               [Test]
+               public void RenameTpThread ()
+               {
+                       object monitor = new object ();
+                       ThreadPool.QueueUserWorkItem (new WaitCallback (Rename_callback), monitor);
+                       lock (monitor) {
+                               if (!rename_finished)
+                                       Monitor.Wait (monitor);
+                       }
+                       Assert.IsTrue (!rename_failed);
+               }
+
+               void Rename_callback (object o) {
+                       Thread.CurrentThread.Name = "a";
+                       try {
+                               Thread.CurrentThread.Name = "b";
+                               //Console.WriteLine ("Thread name is: {0}", Thread.CurrentThread.Name);
+                       } catch (Exception e) {
+                               //Console.Error.WriteLine (e);
+                               rename_failed = true;
+                       }
+                       object monitor = o;
+                       lock (monitor) {
+                               rename_finished = true;
+                               Monitor.Pulse (monitor);
+                       }
+               }
+
                [Test]
                public void TestNestedThreads1()
                {
@@ -684,7 +697,6 @@ namespace MonoTests.System.Threading
                        }
                }
 
-#if !TARGET_JVM // AppDomain.SetPrincipalPolicy not supported for TARGET_JVM
                [Test]
                [Ignore ("see comment below.")]
                public void CurrentPrincipal_PrincipalPolicy_UnauthenticatedPrincipal () 
@@ -713,7 +725,6 @@ namespace MonoTests.System.Threading
                                t.Abort ();
                        }
                }
-#endif // TARGET_JVM
                
                [Test]
                public void IPrincipal_CopyOnNewThread () 
@@ -789,15 +800,17 @@ namespace MonoTests.System.Threading
                [Test]
                public void Test_Interrupt ()
                {
+                       ManualResetEvent mre = new ManualResetEvent (false);
                        bool interruptedExceptionThrown = false;
+
                        ThreadPool.QueueUserWorkItem (Test_Interrupt_Worker, Thread.CurrentThread);
 
                        try {
                                try {
-                                       Thread.Sleep (3000);
+                                       mre.WaitOne (3000);
                                } finally {
                                        try {
-                                               Thread.Sleep (0);
+                                               mre.WaitOne (0);
                                        } catch (ThreadInterruptedException) {
                                                Assert.Fail ("ThreadInterruptedException thrown twice");
                                        }
@@ -827,16 +840,24 @@ namespace MonoTests.System.Threading
                [Category ("NotDotNet")] // it crashes nunit.
                public void Test_InterruptCurrentThread ()
                {
+                       ManualResetEvent mre = new ManualResetEvent (false);
                        bool interruptedExceptionThrown = false;
 
                        Thread.CurrentThread.Interrupt ();
                        try {
-                               Thread.Sleep (0);
+                               mre.WaitOne (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;
@@ -1105,11 +1126,7 @@ namespace MonoTests.System.Threading
                                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;
@@ -1141,8 +1158,7 @@ namespace MonoTests.System.Threading
                        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");
+                       t1.SetApartmentState (ApartmentState.MTA);
                }
 
                [Test]
@@ -1153,10 +1169,10 @@ namespace MonoTests.System.Threading
                        Assert.AreEqual (ApartmentState.STA, t1.ApartmentState, "#1");
 
                        bool result = t1.TrySetApartmentState (ApartmentState.MTA);
-                       Assert.IsTrue (result, "#2");
+                       Assert.IsFalse (result, "#2");
 
                        result = t1.TrySetApartmentState (ApartmentState.STA);
-                       Assert.IsFalse (result, "#3");
+                       Assert.IsTrue (result, "#3");
                }
 
                [Test]
@@ -1168,13 +1184,11 @@ namespace MonoTests.System.Threading
 
                        t1.Start ();
 
-                       bool exception_occured = false;
                        try {
                                t1.TrySetApartmentState (ApartmentState.STA);
-                       } catch (Exception) {
-                               exception_occured = true;
+                               Assert.Fail ("#2");
+                       } catch (ThreadStateException) {
                        }
-                       Assert.IsFalse (exception_occured, "Invalid Exception Occured");
 
                        t1.Join ();
                }