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 2008/10/25 20:49:47 UTC
svn commit: r707889 - in
/activemq/activemq-cpp/trunk/src/test/decaf/util/concurrent: MutexTest.cpp
MutexTest.h
Author: tabish
Date: Sat Oct 25 11:49:47 2008
New Revision: 707889
URL: http://svn.apache.org/viewvc?rev=707889&view=rev
Log:
Test Code Cleanup
Modified:
activemq/activemq-cpp/trunk/src/test/decaf/util/concurrent/MutexTest.cpp
activemq/activemq-cpp/trunk/src/test/decaf/util/concurrent/MutexTest.h
Modified: activemq/activemq-cpp/trunk/src/test/decaf/util/concurrent/MutexTest.cpp
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/src/test/decaf/util/concurrent/MutexTest.cpp?rev=707889&r1=707888&r2=707889&view=diff
==============================================================================
--- activemq/activemq-cpp/trunk/src/test/decaf/util/concurrent/MutexTest.cpp (original)
+++ activemq/activemq-cpp/trunk/src/test/decaf/util/concurrent/MutexTest.cpp Sat Oct 25 11:49:47 2008
@@ -24,26 +24,111 @@
using namespace decaf::util::concurrent;
///////////////////////////////////////////////////////////////////////////////
-void MutexTest::testTimedWait(){
+class MyThread : public lang::Thread, public Synchronizable{
+private:
- try
- {
- MyTimedWaitingThread test;
- time_t startTime = time( NULL );
- test.start();
- test.join();
- time_t endTime = time( NULL );
+ Mutex mutex;
- time_t delta = endTime - startTime;
+public:
- CPPUNIT_ASSERT( delta >= 1 && delta <= 3 );
+ int value;
+ MyThread(){ value = 0;}
+ virtual ~MyThread(){}
+
+ virtual void lock() throw(lang::Exception){
+ mutex.lock();
}
- catch(lang::Exception& ex)
- {
- std::cout << ex.getMessage() << std::endl;
+ virtual void unlock() throw(lang::Exception){
+ mutex.unlock();
+ }
+ virtual void wait() throw(lang::Exception){
+ mutex.wait();
+ }
+ virtual void wait(unsigned long millisecs) throw(lang::Exception){
+ mutex.wait( millisecs );
}
+ virtual void notify() throw(lang::Exception){
+ mutex.notify();
+ }
+ virtual void notifyAll() throw(lang::Exception){
+ mutex.notifyAll();
+ }
+
+ virtual void run(){
+ {
+ Lock lock (this);
+
+ value = value * 25;
+ }
+ }
+
+};
+
+///////////////////////////////////////////////////////////////////////////////
+void MutexTest::test() {
+ MyThread test;
+
+ synchronized(&test){
+
+ test.start();
+
+ for( int ix=0; ix<100; ix++ ){
+ test.value += 1;
+ }
+ }
+
+ test.join();
+
+ CPPUNIT_ASSERT( test.value == 2500 );
}
+///////////////////////////////////////////////////////////////////////////////
+class MyWaitingThread : public lang::Thread, public Synchronizable{
+private:
+
+ Mutex mutex;
+
+public:
+
+ int value;
+ MyWaitingThread(){ value = 0;}
+ virtual ~MyWaitingThread(){}
+ virtual void lock() throw(lang::Exception){
+ mutex.lock();
+ }
+ virtual void unlock() throw(lang::Exception){
+ mutex.unlock();
+ }
+ virtual void wait() throw(lang::Exception){
+ mutex.wait();
+ }
+ virtual void wait(unsigned long millisecs) throw(lang::Exception){
+ mutex.wait( millisecs );
+ }
+ virtual void notify() throw(lang::Exception){
+ mutex.notify();
+ }
+ virtual void notifyAll() throw(lang::Exception){
+ mutex.notifyAll();
+ }
+
+ virtual void run(){
+
+ try {
+
+ synchronized(this) {
+ this->wait();
+ std::cout.flush();
+ value = value * 25;
+ }
+
+ } catch( lang::Exception& ex ) {
+ ex.setMark( __FILE__, __LINE__ );
+ }
+ }
+};
+
+///////////////////////////////////////////////////////////////////////////////
void MutexTest::testWait(){
try
@@ -70,27 +155,135 @@
}
}
-void MutexTest::test()
-{
- MyThread test;
+///////////////////////////////////////////////////////////////////////////////
+class MyTimedWaitingThread : public lang::Thread, public Synchronizable{
+private:
- synchronized(&test){
+ Mutex mutex;
- test.start();
+public:
- for( int ix=0; ix<100; ix++ ){
- test.value += 1;
+ int value;
+ MyTimedWaitingThread(){ value = 0;}
+ virtual ~MyTimedWaitingThread(){}
+ virtual void lock() throw(lang::Exception){
+ mutex.lock();
+ }
+ virtual void unlock() throw(lang::Exception){
+ mutex.unlock();
+ }
+ virtual void wait() throw(lang::Exception){
+ mutex.wait();
+ }
+ virtual void wait(unsigned long millisecs) throw(lang::Exception){
+ mutex.wait( millisecs );
+ }
+ virtual void notify() throw(lang::Exception){
+ mutex.notify();
+ }
+ virtual void notifyAll() throw(lang::Exception){
+ mutex.notifyAll();
+ }
+
+ virtual void run(){
+
+ try {
+ synchronized( this ) {
+
+ this->wait(2000);
+ value = 666;
+ }
+ } catch( lang::Exception& ex ) {
+ ex.setMark( __FILE__, __LINE__ );
}
}
+};
- test.join();
+///////////////////////////////////////////////////////////////////////////////
+void MutexTest::testTimedWait(){
- CPPUNIT_ASSERT( test.value == 2500 );
+ try {
+
+ MyTimedWaitingThread test;
+ time_t startTime = time( NULL );
+ test.start();
+ test.join();
+ time_t endTime = time( NULL );
+
+ time_t delta = endTime - startTime;
+
+ CPPUNIT_ASSERT( delta >= 1 && delta <= 3 );
+
+ } catch(lang::Exception& ex) {
+ std::cout << ex.getMessage() << std::endl;
+ }
}
-void MutexTest::testNotify()
-{
+///////////////////////////////////////////////////////////////////////////////
+class MyNotifiedThread : public lang::Thread, public Synchronizable{
+public:
+
+ bool done;
+ Mutex* mutex;
+ Mutex* started;
+ Mutex* completed;
+
+public:
+
+ int value;
+ MyNotifiedThread(Mutex* mutex, Mutex* started, Mutex* completed ){
+ this->mutex = mutex;
+ this->started = started;
+ this->completed = completed;
+ this->done = false;
+ }
+ virtual ~MyNotifiedThread(){}
+ virtual void lock() throw(lang::Exception){
+ mutex->lock();
+ }
+ virtual void unlock() throw(lang::Exception){
+ mutex->unlock();
+ }
+ virtual void wait() throw(lang::Exception){
+ mutex->wait();
+ }
+ virtual void wait(unsigned long millisecs) throw(lang::Exception){
+ mutex->wait( millisecs );
+ }
+ virtual void notify() throw(lang::Exception){
+ mutex->notify();
+ }
+ virtual void notifyAll() throw(lang::Exception){
+ mutex->notifyAll();
+ }
+
+ virtual void run(){
+
+ try {
+ done = false;
+ synchronized( this ) {
+ synchronized( started ) {
+ started->notify();
+ }
+
+ this->wait();
+ done = true;
+
+ synchronized( completed ) {
+ completed->notify();
+ }
+ }
+ } catch( lang::Exception& ex ) {
+ ex.setMark( __FILE__, __LINE__ );
+ }
+ }
+};
+
+///////////////////////////////////////////////////////////////////////////////
+void MutexTest::testNotify() {
+
try{
+
Mutex mutex;
Mutex started;
Mutex completed;
@@ -104,19 +297,16 @@
threads[ix]->start();
}
- synchronized( &started )
- {
+ synchronized( &started ) {
int count = 0;
- while( count < ( numThreads ) )
- {
+ while( count < ( numThreads ) ) {
started.wait( 30 );
count++;
}
}
- synchronized(&mutex)
- {
+ synchronized(&mutex ) {
mutex.notify();
}
@@ -132,20 +322,17 @@
// Make sure only 1 thread was notified.
CPPUNIT_ASSERT( counter == 1 );
- synchronized(&mutex)
- {
+ synchronized( &mutex ) {
// Notify all threads.
for( int ix=0; ix<numThreads-1; ++ix ){
mutex.notify();
}
}
- synchronized( &started )
- {
+ synchronized( &started ){
int count = 0;
- while( count < ( numThreads ) )
- {
+ while( count < ( numThreads ) ) {
started.wait( 30 );
count++;
}
@@ -159,13 +346,11 @@
}
CPPUNIT_ASSERT( numComplete == numThreads );
- synchronized( &mutex )
- {
+ synchronized( &mutex ) {
mutex.wait( 5 );
}
- synchronized( &mutex )
- {
+ synchronized( &mutex ) {
mutex.notifyAll();
}
@@ -176,9 +361,11 @@
}catch( lang::Exception& ex ){
ex.setMark( __FILE__, __LINE__ );
+ CPPUNIT_ASSERT( false );
}
}
+///////////////////////////////////////////////////////////////////////////////
void MutexTest::testNotifyAll()
{
try{
@@ -250,6 +437,63 @@
}
}
+///////////////////////////////////////////////////////////////////////////////
+class MyRecursiveLockThread
+:
+ public lang::Thread,
+ public Synchronizable{
+
+public:
+
+ bool done;
+ Mutex* mutex;
+
+public:
+
+ int value;
+ MyRecursiveLockThread(Mutex* mutex){ this->mutex = mutex; done = false; }
+ virtual ~MyRecursiveLockThread(){}
+ virtual void lock() throw(lang::Exception){
+ mutex->lock();
+ }
+ virtual void unlock() throw(lang::Exception){
+ mutex->unlock();
+ }
+ virtual void wait() throw(lang::Exception){
+ mutex->wait();
+ }
+ virtual void wait(unsigned long millisecs) throw(lang::Exception){
+ mutex->wait( millisecs );
+ }
+ virtual void notify() throw(lang::Exception){
+ mutex->notify();
+ }
+ virtual void notifyAll() throw(lang::Exception){
+ mutex->notifyAll();
+ }
+
+ virtual void run(){
+
+ try
+ {
+ done = false;
+ synchronized(this)
+ {
+ synchronized(this)
+ {
+ this->wait();
+ done = true;
+ }
+ }
+ }
+ catch(lang::Exception& ex)
+ {
+ ex.setMark( __FILE__, __LINE__ );
+ }
+ }
+};
+
+////////////////////////////////////////////////////////////////////////////////
void MutexTest::testRecursiveLock()
{
try{
@@ -304,6 +548,52 @@
}
}
+///////////////////////////////////////////////////////////////////////////////
+class MyDoubleLockThread
+:
+ public lang::Thread
+{
+
+public:
+
+ bool done;
+ Mutex* mutex1;
+ Mutex* mutex2;
+
+public:
+
+ int value;
+ MyDoubleLockThread(Mutex* mutex1, Mutex* mutex2)
+ {
+ this->mutex1 = mutex1;
+ this->mutex2 = mutex2;
+ done = false;
+ }
+
+ virtual ~MyDoubleLockThread(){}
+
+ virtual void run(){
+
+ try
+ {
+ done = false;
+ synchronized(mutex1)
+ {
+ synchronized(mutex2)
+ {
+ mutex2->wait();
+ done = true;
+ }
+ }
+ }
+ catch(lang::Exception& ex)
+ {
+ ex.setMark( __FILE__, __LINE__ );
+ }
+ }
+};
+
+////////////////////////////////////////////////////////////////////////////////
void MutexTest::testDoubleLock()
{
try{
@@ -333,6 +623,114 @@
}
///////////////////////////////////////////////////////////////////////////////
+class MyStoppableThread : public lang::Runnable
+{
+public:
+
+ bool started;
+ bool closed;
+ Mutex mutex;
+ lang::Thread* thread;
+ util::Random rand;
+
+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 lang::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 ) {
+
+ lang::Thread::sleep( rand.nextInt( 100 ) );
+
+ 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 );
+ }
+ }
+};
+
+////////////////////////////////////////////////////////////////////////////////
void MutexTest::testStressMutex(){
MyStoppableThread tester;
Modified: activemq/activemq-cpp/trunk/src/test/decaf/util/concurrent/MutexTest.h
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/src/test/decaf/util/concurrent/MutexTest.h?rev=707889&r1=707888&r2=707889&view=diff
==============================================================================
--- activemq/activemq-cpp/trunk/src/test/decaf/util/concurrent/MutexTest.h (original)
+++ activemq/activemq-cpp/trunk/src/test/decaf/util/concurrent/MutexTest.h Sat Oct 25 11:49:47 2008
@@ -47,432 +47,6 @@
public:
- class MyThread
- :
- public lang::Thread,
- public Synchronizable{
-
- private:
-
- Mutex mutex;
-
- public:
-
- int value;
- MyThread(){ value = 0;}
- virtual ~MyThread(){}
-
- virtual void lock() throw(lang::Exception){
- mutex.lock();
- }
- virtual void unlock() throw(lang::Exception){
- mutex.unlock();
- }
- virtual void wait() throw(lang::Exception){
- mutex.wait();
- }
- virtual void wait(unsigned long millisecs) throw(lang::Exception){
- mutex.wait( millisecs );
- }
- virtual void notify() throw(lang::Exception){
- mutex.notify();
- }
- virtual void notifyAll() throw(lang::Exception){
- mutex.notifyAll();
- }
-
- virtual void run(){
-
- {
- Lock lock (this);
-
- value = value * 25;
- }
- }
-
- };
-
- class MyWaitingThread
- :
- public lang::Thread,
- public Synchronizable{
-
- private:
-
- Mutex mutex;
-
- public:
-
- int value;
- MyWaitingThread(){ value = 0;}
- virtual ~MyWaitingThread(){}
- virtual void lock() throw(lang::Exception){
- mutex.lock();
- }
- virtual void unlock() throw(lang::Exception){
- mutex.unlock();
- }
- virtual void wait() throw(lang::Exception){
- mutex.wait();
- }
- virtual void wait(unsigned long millisecs) throw(lang::Exception){
- mutex.wait( millisecs );
- }
- virtual void notify() throw(lang::Exception){
- mutex.notify();
- }
- virtual void notifyAll() throw(lang::Exception){
- mutex.notifyAll();
- }
-
- virtual void run(){
-
- try
- {
- synchronized(this)
- {
- this->wait();
-
- std::cout.flush();
-
- value = value * 25;
- }
- }
- catch(lang::Exception& ex)
- {
- ex.setMark( __FILE__, __LINE__ );
- }
- }
- };
-
- class MyTimedWaitingThread
- :
- public lang::Thread,
- public Synchronizable{
-
- private:
-
- Mutex mutex;
-
- public:
-
- int value;
- MyTimedWaitingThread(){ value = 0;}
- virtual ~MyTimedWaitingThread(){}
- virtual void lock() throw(lang::Exception){
- mutex.lock();
- }
- virtual void unlock() throw(lang::Exception){
- mutex.unlock();
- }
- virtual void wait() throw(lang::Exception){
- mutex.wait();
- }
- virtual void wait(unsigned long millisecs) throw(lang::Exception){
- mutex.wait( millisecs );
- }
- virtual void notify() throw(lang::Exception){
- mutex.notify();
- }
- virtual void notifyAll() throw(lang::Exception){
- mutex.notifyAll();
- }
-
- virtual void run(){
-
- try
- {
- synchronized(this)
- {
- this->wait(2000);
-
- value = 666;
- }
- }
- catch(lang::Exception& ex)
- {
- ex.setMark( __FILE__, __LINE__ );
- }
- }
- };
-
- class MyNotifiedThread
- :
- public lang::Thread,
- public Synchronizable{
-
- public:
-
- bool done;
- Mutex* mutex;
- Mutex* started;
- Mutex* completed;
-
- public:
-
- int value;
- MyNotifiedThread(Mutex* mutex, Mutex* started, Mutex* completed ){
- this->mutex = mutex;
- this->started = started;
- this->completed = completed;
- this->done = false;
- }
- virtual ~MyNotifiedThread(){}
- virtual void lock() throw(lang::Exception){
- mutex->lock();
- }
- virtual void unlock() throw(lang::Exception){
- mutex->unlock();
- }
- virtual void wait() throw(lang::Exception){
- mutex->wait();
- }
- virtual void wait(unsigned long millisecs) throw(lang::Exception){
- mutex->wait( millisecs );
- }
- virtual void notify() throw(lang::Exception){
- mutex->notify();
- }
- virtual void notifyAll() throw(lang::Exception){
- mutex->notifyAll();
- }
-
- virtual void run(){
-
- try
- {
- done = false;
- synchronized(this)
- {
- synchronized( started )
- {
- started->notify();
- }
-
- this->wait();
- done = true;
-
- synchronized( completed )
- {
- completed->notify();
- }
- }
- }
- catch(lang::Exception& ex)
- {
- ex.setMark( __FILE__, __LINE__ );
- }
- }
- };
-
- class MyRecursiveLockThread
- :
- public lang::Thread,
- public Synchronizable{
-
- public:
-
- bool done;
- Mutex* mutex;
-
- public:
-
- int value;
- MyRecursiveLockThread(Mutex* mutex){ this->mutex = mutex; done = false; }
- virtual ~MyRecursiveLockThread(){}
- virtual void lock() throw(lang::Exception){
- mutex->lock();
- }
- virtual void unlock() throw(lang::Exception){
- mutex->unlock();
- }
- virtual void wait() throw(lang::Exception){
- mutex->wait();
- }
- virtual void wait(unsigned long millisecs) throw(lang::Exception){
- mutex->wait( millisecs );
- }
- virtual void notify() throw(lang::Exception){
- mutex->notify();
- }
- virtual void notifyAll() throw(lang::Exception){
- mutex->notifyAll();
- }
-
- virtual void run(){
-
- try
- {
- done = false;
- synchronized(this)
- {
- synchronized(this)
- {
- this->wait();
- done = true;
- }
- }
- }
- catch(lang::Exception& ex)
- {
- ex.setMark( __FILE__, __LINE__ );
- }
- }
- };
-
- class MyDoubleLockThread
- :
- public lang::Thread
- {
-
- public:
-
- bool done;
- Mutex* mutex1;
- Mutex* mutex2;
-
- public:
-
- int value;
- MyDoubleLockThread(Mutex* mutex1, Mutex* mutex2)
- {
- this->mutex1 = mutex1;
- this->mutex2 = mutex2;
- done = false;
- }
-
- virtual ~MyDoubleLockThread(){}
-
- virtual void run(){
-
- try
- {
- done = false;
- synchronized(mutex1)
- {
- synchronized(mutex2)
- {
- mutex2->wait();
- done = true;
- }
- }
- }
- catch(lang::Exception& ex)
- {
- ex.setMark( __FILE__, __LINE__ );
- }
- }
- };
-
- class MyStoppableThread : public lang::Runnable
- {
- public:
-
- bool started;
- bool closed;
- Mutex mutex;
- lang::Thread* thread;
- util::Random rand;
-
- 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 lang::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 ) {
-
- lang::Thread::sleep( rand.nextInt( 100 ) );
-
- 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(){}
virtual void setUp(){}
virtual void tearDown(){}