You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@activemq.apache.org by ta...@apache.org on 2007/07/05 14:52:55 UTC

svn commit: r553485 - in /activemq/activemq-cpp/trunk/src/test/activemq/concurrent: MutexTest.cpp MutexTest.h

Author: tabish
Date: Thu Jul  5 05:52:54 2007
New Revision: 553485

URL: http://svn.apache.org/viewvc?view=rev&rev=553485
Log:
http://issues.apache.org/activemq/browse/AMQCPP-128

Modified:
    activemq/activemq-cpp/trunk/src/test/activemq/concurrent/MutexTest.cpp
    activemq/activemq-cpp/trunk/src/test/activemq/concurrent/MutexTest.h

Modified: activemq/activemq-cpp/trunk/src/test/activemq/concurrent/MutexTest.cpp
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/src/test/activemq/concurrent/MutexTest.cpp?view=diff&rev=553485&r1=553484&r2=553485
==============================================================================
--- activemq/activemq-cpp/trunk/src/test/activemq/concurrent/MutexTest.cpp (original)
+++ activemq/activemq-cpp/trunk/src/test/activemq/concurrent/MutexTest.cpp Thu Jul  5 05:52:54 2007
@@ -337,3 +337,19 @@
         ex.setMark( __FILE__, __LINE__ );
     }
 }
+
+///////////////////////////////////////////////////////////////////////////////
+void MutexTest::testStressMutex(){
+    MyStoppableThread tester;
+
+    tester.start();
+
+    CPPUNIT_ASSERT( tester.isStarted() );
+
+    for( int i = 0; i < 100; ++i ) {
+        tester.stop();
+        tester.start();
+    }
+
+    CPPUNIT_ASSERT( true );
+}

Modified: activemq/activemq-cpp/trunk/src/test/activemq/concurrent/MutexTest.h
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/src/test/activemq/concurrent/MutexTest.h?view=diff&rev=553485&r1=553484&r2=553485
==============================================================================
--- activemq/activemq-cpp/trunk/src/test/activemq/concurrent/MutexTest.h (original)
+++ activemq/activemq-cpp/trunk/src/test/activemq/concurrent/MutexTest.h Thu Jul  5 05:52:54 2007
@@ -39,6 +39,7 @@
         CPPUNIT_TEST( testNotifyAll );
         CPPUNIT_TEST( testRecursiveLock );
         CPPUNIT_TEST( testDoubleLock );
+        CPPUNIT_TEST( testStressMutex );
         CPPUNIT_TEST_SUITE_END();
 
     public:
@@ -359,6 +360,112 @@
             }
         };
 
+        class MyStoppableThread : public Runnable
+        {
+        public:
+
+            bool started;
+            bool closed;
+            Mutex mutex;
+            Thread* thread;
+
+        public:
+
+            MyStoppableThread() {
+                this->started = false;
+                this->closed = false;
+                this->thread = NULL;
+            }
+
+            virtual ~MyStoppableThread(){ close(); }
+
+            virtual void start(){
+                synchronized( &mutex ) {
+
+                    if( closed || started ) {
+                        return;
+                    }
+
+                    started = true;
+
+                    // Don't create the thread unless we need to.
+                    if( thread == NULL ) {
+                        thread = new Thread( this );
+                        thread->start();
+                    }
+
+                    mutex.notifyAll();
+                }
+            }
+
+            virtual void stop() {
+                synchronized( &mutex ) {
+
+                    if( closed || !started ) {
+                        return;
+                    }
+
+                    // Set the state to stopped.
+                    started = false;
+
+                    // Wakeup the thread so that it can acknowledge the stop request.
+                    mutex.notifyAll();
+
+                    // Wait for the thread to notify us that it has acknowledged
+                    // the stop request.
+                    mutex.wait();
+                }
+            }
+
+            virtual void close() {
+                synchronized( &mutex ) {
+
+                    closed = true;
+                    mutex.notifyAll();
+                }
+
+                if( thread != NULL ) {
+                    thread->join();
+                    delete thread;
+                    thread = NULL;
+                }
+            }
+
+            virtual bool isStarted() const {
+                return started;
+            }
+
+            virtual void run(){
+                try {
+
+                    while( true ) {
+
+                        Thread::sleep( 30 );
+
+                        synchronized( &mutex ) {
+
+                            // If we're closing down, exit the thread.
+                            if( closed ) {
+                                return;
+                            }
+
+                            // When told to stop, the calling thread will wait for a
+                            // responding notification, indicating that we have acknowledged
+                            // the stop command.
+                            if( !isStarted() ) {
+                                mutex.notifyAll();
+
+                                // Wait for more data or to be woken up.
+                                mutex.wait();
+                            }
+                        }
+                    }
+                } catch(...) {
+                    CPPUNIT_ASSERT( false );
+                }
+            }
+        };
+
     public:
 
         virtual ~MutexTest(){}
@@ -372,6 +479,7 @@
         void testNotifyAll();
         void testRecursiveLock();
         void testDoubleLock();
+        void testStressMutex();
     };
 
 }}