Ejemplo n.º 1
0
        public override void PerformTest()
        {
            Log("Creating " + _numberOfThreads + " threads");
            Thread[] threads = new Thread[_numberOfThreads];
            for (int i = 0; i < _numberOfThreads; i++)
            {
                threads[i] = new Thread(Work, ThreadPriority.Normal);
            }

            _startedThreads = 0;
            _startedThreadsLock = new Lock();
            _allThreadsStartedLock = new Lock();
            _allThreadsStartedCondition = new Condition(_allThreadsStartedLock);
            _allThreadsStartedLock.Acquire();

            Log("Starting " + _numberOfThreads + " threads"); // TODO: ParametizedThreadStart doesn't work properly
            for (int i = 0; i < _numberOfThreads; i++)
            {
                threads[i].Start();
            }

            // wait for all threads to be running
            _allThreadsStartedCondition.Await();
            _allThreadsStartedLock.Release();

            Log("Waiting for all threads to finish");

            _semaphore.Acquire(_numberOfThreads); // wait for all threads to finish

            Assert(_failedThreads + " threads failed the calculation", _failedThreads == 0);
            Log("All " + _numberOfThreads + " threads finished");

        }
Ejemplo n.º 2
0
 public void TestAbortThread()
 {
     Lock sync = new Lock();
     Condition aborted = new Condition(sync);
     Thread testThread = new Thread(delegate()
     {
         try
         {
             Log("Test thread sleeping for 8 seconds..");
             Thread.Sleep(8000);
             Fail("This line should never be executed");
         }
         catch (ThreadAbortException)
         {
             Log("Test thread aborted..");
             aborted.Signal();
         }
     });
     testThread.Start();
     sync.Acquire();
     Log("Aborting test thread in 2 seconds.  Sleeping...");
     Thread.Sleep(2000);
     Log("...awake!  Going to abort the test thread.");
     testThread.Abort();
     Log("Called testThread.Abort()");
     Assert("Thread was not aborted as expected!", aborted.Await(12000));
 }
Ejemplo n.º 3
0
        private static XmlHttpRequest _doSimpleXmlHttpRequest(string url, RequestMethod method, string postData, string username, string password, int timeoutMs)
        {
            XmlHttpRequest xhr = GetXmlHttpRequest();
            Lock l = new Lock();
            l.Acquire();
            Condition condition = new Condition(l);
            if (_driverProcess == null)
            {
                throw new SystemException("XmlHttpRequestManager.Driver was not started.");
            }
            OnReadyStateChangeSignalHandler handler = new OnReadyStateChangeSignalHandler
            {
                XmlHttpRequest = xhr,
                Condition = condition
            };

            // TODO: Use resource manager to ensure these signal senders and handlers get cleaned up
            OnReadyStateChangeSignalSender signalSender = CreateOnReadyStateChangeSignalSender(handler);
            try
            {
                xhr.OnReadyStateChange = (NativeFunction)new NativeVoidDelegate(signalSender.SendSignal);
                string methodString;
                if (method == RequestMethod.Get)
                {
                    methodString = "GET";
                }
                else
                {
                    methodString = "POST";
                }
                xhr.Open(methodString, url, true, username, password);
                xhr.Send(postData);
                if (timeoutMs > 0)
                {
                    if (!condition.Await(timeoutMs))
                    {
                        xhr.OnReadyStateChange = null;
                        throw new XmlHttpRequestTimeoutException("Timed out waiting for " + url, xhr);
                    }
                }
                else
                {
                    condition.Await();
                }
                // TODO: finally blocks are not executed if there is a return statement in the try block.  fix that
            }
            finally
            {
                DestroyOnReadyStateChangeSignalSender(signalSender);
            }
            return xhr;
        }
Ejemplo n.º 4
0
        public void TestLockIdiomUnlocksWhenExceptionsAreThrown()
        {
            object sync = new object();
            try
            {
                lock (sync)
                {
                    Log("In locked section");
                    throw new Exception("Purposefully thrown");
                }
            }
            catch (Exception)
            { }

            bool secondThreadAcquiredLock = false;
            Lock secondThreadDoneLock = new Lock();
            Condition secondThreadDone = new Condition(secondThreadDoneLock);
            Thread secondThread = new Thread(delegate()
            {
                secondThreadAcquiredLock = Monitor.TryEnter(sync);
            });
            secondThread.Start();
            secondThreadDone.Await();
            Assert("Finally block of lock idiom should have unlocked sync obj", secondThreadAcquiredLock);
        }
Ejemplo n.º 5
0
 public void TestAbortThreadWithState()
 {
     Lock sync = new Lock();
     Condition aborted = new Condition(sync);
     Thread testThread = new Thread(delegate()
     {
         try
         {
             Log("Test thread sleeping for 8 seconds..");
             Thread.Sleep(8000);
             Fail("This line should never be executed");
         }
         catch (ThreadAbortException e)
         {
             if (e.ExceptionState != null && e.ExceptionState.ToString() == "Exception state")
             {
                 Log("Test thread aborted with state: " + e.ExceptionState);
                 aborted.Signal();
             }
             else
             {
                 Log("Test thread aborted with unknown state: " + e.ExceptionState);
             }
         }
     });
     testThread.Start();
     sync.Acquire();
     Log("Aborting test thread in 2 seconds");
     Thread.Sleep(2000);
     testThread.Abort("Exception state");
     Assert("Thread was not aborted as expected!", aborted.Await(12000));
 }