dotgnu-pnet-commits
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Dotgnu-pnet-commits] CVS: pnetlib/tests/runtime/System/Threading TestM


From: Thong Nguyen <address@hidden>
Subject: [Dotgnu-pnet-commits] CVS: pnetlib/tests/runtime/System/Threading TestMonitor.cs,NONE,1.1 TestThread.cs,NONE,1.1 SuiteThreading.cs,1.2,1.3 TestAutoResetEvent.cs,1.3,1.4 TestManualResetEvent.cs,1.3,1.4
Date: Thu, 03 Jul 2003 18:23:25 -0400

Update of /cvsroot/dotgnu-pnet/pnetlib/tests/runtime/System/Threading
In directory subversions:/tmp/cvs-serv30581

Modified Files:
        SuiteThreading.cs TestAutoResetEvent.cs 
        TestManualResetEvent.cs 
Added Files:
        TestMonitor.cs TestThread.cs 
Log Message:
New threading tests.


--- NEW FILE ---
/*
 * TestBoolean.cs - Tests for the "Boolean" class.
 *
 * Copyright (C) 2002  Free Software Foundation.
 *
 * Authors: Thong Nguyen (address@hidden)
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

using CSUnit;
using System;
using System.Threading;

public class TestMonitor
        : TestCase
{
        public TestMonitor(String name)
                : base(name)
        {
        }

        protected override void Setup()
        {
        }

        protected override void Cleanup()
        {
        }

        public void TestMonitorSingleThreaded()
        {
                object o = new object();
                
                Monitor.Enter(o);
                Monitor.Enter(o);
                Monitor.Exit(o);
                Monitor.Exit(o);
        }       

        public void TestMonitorExitNoEnter()
        {
                object o = new object();
                
                try
                {
                        Monitor.Exit(o);
                        
                        Assert("Expected SynchronizationLockException", false);
                }
                catch (SynchronizationLockException)
                {
                }
        }

        public void TestMonitorEnterExitMismatch()
        {
                object o = new object();
                
                try
                {
                        Monitor.Enter(o);
                        Monitor.Exit(o);
                        Monitor.Exit(o);

                        Assert("Expected SynchronizationLockException", false);
                }
                catch (SynchronizationLockException)
                {
                }
        }

        public void TestMonitorEnterExitMultiple()
        {
                object o1 = new object();
                object o2 = new object();
                
                Monitor.Enter(o1);
                Monitor.Enter(o2);
                Monitor.Exit(o1);
                Monitor.Exit(o2);
        }


        /*
         * Variables used for monitor thrashing..
         */

        private int state = 0;
        private bool failed = false;
        private bool stop = false;
        
        private void Run1()
        {
                while (!stop)
                {
                        lock (typeof(TestMonitor))
                        {
                                if (state == 0 || state == 1)
                                {
                                        // OK!                          
                                }
                                else
                                {
                                        failed = true;
                                        stop = true;

                                        break;
                                }
                        }
                }
        }
        
        private void Run2()
        {
                while (!stop)
                {
                        lock (typeof(TestMonitor))
                        {
                                if (state == 1)
                                {
                                        state = 0;
                                }
                                else
                                {
                                        state = 1;
                                }
                        }                       
                }
        }
        
        public void TestMonitorMultiThreadedThrash()
        {
                Thread thread1, thread2;
                
                if (!TestThread.IsThreadingSupported)
                {
                        return;
                }
                
                thread1 = new Thread(new ThreadStart(Run1));
                thread2 = new Thread(new ThreadStart(Run2));
                
                thread1.Start();
                thread2.Start();
                
                Console.Write("Thrashing will take 4 seconds ... ");

                /* Testing shows 4 seconds is *usually* enough to cause
                        a failure if monitors aren't working */
                                        
                Thread.Sleep(4000);
                
                stop = true;

                thread1.Join();
                thread2.Join();
                
                AssertEquals("Monitor locking", failed, false);
        }

        bool flag;
        object monitor = new object();
        
        private void ExclusionRun1()
        {
                lock (monitor)
                {               
                        Thread.Sleep(1000);
                        
                        flag = true;
                }               
        }
                
        private void ExclusionRun2()
        {
                /* Wait for thread1 to obtain lock */
                
                Thread.Sleep(100);
                
                lock (monitor)
                {
                        /* Fails if lock didn't wait for thread1 */
                        
                        failed = !flag; 
                }
        }
        
        public void TestMonitorExclusion()
        {
                if (!TestThread.IsThreadingSupported)
                {
                        return;
                }
                
                Thread thread1, thread2;
                
                flag = false;
                failed = true;
                
                thread1 = new Thread(new ThreadStart(ExclusionRun1));
                thread2 = new Thread(new ThreadStart(ExclusionRun2));   
                
                thread1.Start();
                thread2.Start();

                thread1.Join();
                thread2.Join();
                
                Assert("Exclusion failed", !failed);
        }
}

--- NEW FILE ---
/*
 * TestBoolean.cs - Tests for the "Boolean" class.
 *
 * Copyright (C) 2002  Free Software Foundation.
 *
 * Authors: Thong Nguyen (address@hidden)
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

using CSUnit;
using System;
using System.Threading;

#if !ECMA_COMPAT

public class TestThread
        : TestCase
{
        internal static bool isThreadingSupported;
        
        static TestThread()
        {
                try
                {
                        Thread thread;
                        
                        thread = new Thread(new ThreadStart(NullMethod));
                        
                        thread.Start();
                        
                        isThreadingSupported = true;
                        
                }
                catch (NotSupportedException)
                {
                        isThreadingSupported = false;
                }
        }
        
        private static void NullMethod()
        {
        }
        
        internal static bool IsThreadingSupported
        {
                get
                {
                        return isThreadingSupported;
                }
        }
        
        public TestThread(String name)
                : base(name)
        {
        }

        protected override void Setup()
        {
        }

        protected override void Cleanup()
        {
        }

        int flag = 0;
        bool failed = false;
        
        private void StartJoinRun()
        {
                flag += Convert.ToInt32(Thread.CurrentThread.Name);
                
                Thread.Sleep(200);
        }
        
        public void TestStartJoin()
        {
                if (!IsThreadingSupported)
                {
                        return;
                }
                
                int expected;
                Thread thread;
                Thread[] threads = new Thread[10];
        
                flag = 0;
                expected = 0;
                        
                for (int i = 0; i < 10; i++)
                {
                        threads[i] = new Thread(new ThreadStart(StartJoinRun));

                        threads[i].Name = i.ToString();
                                                
                        threads[i].Start();
                        
                        expected += i;
                }
                
                for (int i = 0; i < 10; i++)
                {
                        threads[i].Join();
                }
                
                AssertEquals("ThreadStartJoin failed", flag, expected);
        }

        private void StartRestartRun()
        {
        }
                
        public void TestStartRestart()
        {
                Thread thread;
                
                if (!IsThreadingSupported)
                {
                        return;
                }
                
                thread = new Thread(new ThreadStart(StartRestartRun));
                
                thread.Start();
                
                thread.Join();

                try
                {               
                        thread.Start();
                        
                        Assert("Thread illegally restarted", false);
                }
                catch (ThreadStateException)
                {
                }
        }       
        
        private void ThreadStateRun()
        {
                if (Thread.CurrentThread.ThreadState == ThreadState.Running)
                {
                        failed = false;
                }
        }
        
        public void TestThreadState()
        {
                Thread thread;
                
                if (!IsThreadingSupported)
                {
                        return;
                }
                
                failed = true;
                
                thread = new Thread(new ThreadStart(ThreadStateRun));
                
                thread.Start();
                thread.Join();
                
                Assert("ThreadState==ThreadState.Running", !failed);
                Assert("ThreadState==ThreadState.Stopped",
                         thread.ThreadState == ThreadState.Stopped);    
                
        }
}

#endif

Index: SuiteThreading.cs
===================================================================
RCS file: 
/cvsroot/dotgnu-pnet/pnetlib/tests/runtime/System/Threading/SuiteThreading.cs,v
retrieving revision 1.2
retrieving revision 1.3
diff -C2 -r1.2 -r1.3
*** SuiteThreading.cs   1 Apr 2003 23:54:05 -0000       1.2
--- SuiteThreading.cs   3 Jul 2003 22:23:22 -0000       1.3
***************
*** 35,38 ****
--- 35,41 ----
                suite.AddTests(typeof(TestAutoResetEvent));
        #endif
+       
+               suite.AddTests(typeof(TestThread));
+               suite.AddTests(typeof(TestMonitor));
                
                return suite;

Index: TestAutoResetEvent.cs
===================================================================
RCS file: 
/cvsroot/dotgnu-pnet/pnetlib/tests/runtime/System/Threading/TestAutoResetEvent.cs,v
retrieving revision 1.3
retrieving revision 1.4
diff -C2 -r1.3 -r1.4
*** TestAutoResetEvent.cs       1 Apr 2003 23:54:05 -0000       1.3
--- TestAutoResetEvent.cs       3 Jul 2003 22:23:22 -0000       1.4
***************
*** 45,49 ****
        }
  
!       public void TestWaitOne()
        {
                bool x;
--- 45,49 ----
        }
  
!       public void TestWaitOneSingleThreaded()
        {
                bool x;
***************
*** 64,67 ****
--- 64,103 ----
  
                x = e1.WaitOne(10,false);
+ 
+               AssertEquals("WaitOne(set)", x, false);
+       }
+ 
+       private void SetE1()
+       {
+               e1.Set();
+       }
+       
+       public void TestWaitOneMultiThreaded()
+       {
+               bool x;
+               Thread thread1;
+ 
+               if (!TestThread.IsThreadingSupported)
+               {
+                       return;
+               }
+ 
+               e1 = new AutoResetEvent(false);
+ 
+               x = e1.WaitOne(10,false);
+ 
+               AssertEquals("WaitOne(unset)", x, false);
+ 
+               thread1 = new Thread(new ThreadStart(SetE1));
+               
+               thread1.Start();
+ 
+               x = e1.WaitOne(4000, false);
+ 
+               AssertEquals("WaitOne(set)", x, true);
+ 
+               // It should be reset now.
+ 
+               x = e1.WaitOne(10, false);
  
                AssertEquals("WaitOne(set)", x, false);

Index: TestManualResetEvent.cs
===================================================================
RCS file: 
/cvsroot/dotgnu-pnet/pnetlib/tests/runtime/System/Threading/TestManualResetEvent.cs,v
retrieving revision 1.3
retrieving revision 1.4
diff -C2 -r1.3 -r1.4
*** TestManualResetEvent.cs     1 Apr 2003 23:54:05 -0000       1.3
--- TestManualResetEvent.cs     3 Jul 2003 22:23:22 -0000       1.4
***************
*** 45,49 ****
        }
  
!       public void TestWaitOne()
        {
                bool x;
--- 45,49 ----
        }
  
!       public void TestWaitOneSingleThreaded()
        {
                bool x;
***************
*** 58,61 ****
--- 58,97 ----
  
                x = e1.WaitOne(10,false);
+ 
+               AssertEquals("WaitOne(set)", x, true);
+ 
+               // It should still be set.
+ 
+               x = e1.WaitOne(10,false);
+ 
+               AssertEquals("WaitOne(set)", x, true);
+       }
+ 
+       private void SetE1()
+       {
+               e1.Set();
+       }
+       
+       public void TestWaitOneMultiThreaded()
+       {
+               bool x;
+               Thread thread1;
+ 
+               if (!TestThread.IsThreadingSupported)
+               {
+                       return;
+               }
+               
+               e1 = new ManualResetEvent(false);
+ 
+               x = e1.WaitOne(10,false);
+ 
+               AssertEquals("WaitOne(unset)", x, false);
+ 
+               thread1 = new Thread(new ThreadStart(SetE1));
+               
+               thread1.Start();
+ 
+               x = e1.WaitOne(4000,false);
  
                AssertEquals("WaitOne(set)", x, true);





reply via email to

[Prev in Thread] Current Thread [Next in Thread]