activemq-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From tab...@apache.org
Subject svn commit: r1336440 - in /activemq/activemq-cpp/trunk/activemq-cpp/src/test/decaf/util/concurrent: CopyOnWriteArrayListTest.cpp CopyOnWriteArrayListTest.h
Date Wed, 09 May 2012 22:48:20 GMT
Author: tabish
Date: Wed May  9 22:48:20 2012
New Revision: 1336440

URL: http://svn.apache.org/viewvc?rev=1336440&view=rev
Log:
Adds a unit test that exposes a race condition in this class.

Modified:
    activemq/activemq-cpp/trunk/activemq-cpp/src/test/decaf/util/concurrent/CopyOnWriteArrayListTest.cpp
    activemq/activemq-cpp/trunk/activemq-cpp/src/test/decaf/util/concurrent/CopyOnWriteArrayListTest.h

Modified: activemq/activemq-cpp/trunk/activemq-cpp/src/test/decaf/util/concurrent/CopyOnWriteArrayListTest.cpp
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/activemq-cpp/src/test/decaf/util/concurrent/CopyOnWriteArrayListTest.cpp?rev=1336440&r1=1336439&r2=1336440&view=diff
==============================================================================
--- activemq/activemq-cpp/trunk/activemq-cpp/src/test/decaf/util/concurrent/CopyOnWriteArrayListTest.cpp
(original)
+++ activemq/activemq-cpp/trunk/activemq-cpp/src/test/decaf/util/concurrent/CopyOnWriteArrayListTest.cpp
Wed May  9 22:48:20 2012
@@ -18,7 +18,10 @@
 #include "CopyOnWriteArrayListTest.h"
 
 #include <decaf/util/concurrent/CopyOnWriteArrayList.h>
+#include <decaf/util/concurrent/ThreadPoolExecutor.h>
+#include <decaf/util/concurrent/LinkedBlockingQueue.h>
 #include <decaf/util/StlList.h>
+#include <decaf/util/Random.h>
 #include <decaf/lang/Integer.h>
 
 using namespace decaf;
@@ -957,3 +960,87 @@ void CopyOnWriteArrayListTest::testRetai
     list3.retainAll( collection );
     CPPUNIT_ASSERT_EQUAL( 0, list3.size() );
 }
+
+////////////////////////////////////////////////////////////////////////////////
+namespace {
+
+    class Target {
+    private:
+
+        int counter;
+
+    public:
+
+        Target() : counter(0) {
+        }
+
+        void increment() {
+            this->counter++;
+        }
+    };
+
+    class AddRemoveItemRunnable : public Runnable {
+    private:
+
+        Random rand;
+        CopyOnWriteArrayList<Target*>* list;
+
+    public:
+
+        AddRemoveItemRunnable(CopyOnWriteArrayList<Target*>* list) :
+            Runnable(), rand(), list(list) {
+        }
+
+        virtual ~AddRemoveItemRunnable() {}
+
+        virtual void run() {
+            TimeUnit::MILLISECONDS.sleep(rand.nextInt(10));
+            Target* target = new Target();
+            list->add(target);
+            TimeUnit::MILLISECONDS.sleep(rand.nextInt(10));
+            list->remove(target);
+            delete target;
+        }
+    };
+
+    class IterateAndExecuteMethodRunnable : public Runnable {
+    private:
+
+        Random rand;
+        CopyOnWriteArrayList<Target*>* list;
+
+    public:
+
+        IterateAndExecuteMethodRunnable(CopyOnWriteArrayList<Target*>* list) :
+            Runnable(), rand(), list(list) {
+        }
+
+        virtual ~IterateAndExecuteMethodRunnable() {}
+
+        virtual void run() {
+            TimeUnit::MILLISECONDS.sleep(rand.nextInt(15));
+            Pointer< Iterator<Target*> > iter(list->iterator());
+            while(iter->hasNext()) {
+                iter->next()->increment();
+            }
+        }
+    };
+
+}
+
+////////////////////////////////////////////////////////////////////////////////
+void CopyOnWriteArrayListTest::testConcurrentRandomAddRemoveAndIterate() {
+
+    ThreadPoolExecutor executor(50, Integer::MAX_VALUE, 60LL, TimeUnit::SECONDS, new LinkedBlockingQueue<Runnable*>());
+    CopyOnWriteArrayList<Target*> list;
+
+    Random rand;
+
+    for (int i = 0; i < 3000; i++) {
+        executor.execute(new AddRemoveItemRunnable(&list));
+        executor.execute(new IterateAndExecuteMethodRunnable(&list));
+    }
+
+    executor.shutdown();
+    CPPUNIT_ASSERT_MESSAGE("executor terminated", executor.awaitTermination(45, TimeUnit::SECONDS));
+}

Modified: activemq/activemq-cpp/trunk/activemq-cpp/src/test/decaf/util/concurrent/CopyOnWriteArrayListTest.h
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/activemq-cpp/src/test/decaf/util/concurrent/CopyOnWriteArrayListTest.h?rev=1336440&r1=1336439&r2=1336440&view=diff
==============================================================================
--- activemq/activemq-cpp/trunk/activemq-cpp/src/test/decaf/util/concurrent/CopyOnWriteArrayListTest.h
(original)
+++ activemq/activemq-cpp/trunk/activemq-cpp/src/test/decaf/util/concurrent/CopyOnWriteArrayListTest.h
Wed May  9 22:48:20 2012
@@ -79,6 +79,7 @@ namespace concurrent {
         CPPUNIT_TEST( testGet2IndexOutOfBoundsException );
         CPPUNIT_TEST( testSet1IndexOutOfBoundsException );
         CPPUNIT_TEST( testSet2IndexOutOfBoundsException );
+        CPPUNIT_TEST( testConcurrentRandomAddRemoveAndIterate );
         CPPUNIT_TEST_SUITE_END();
 
     private:
@@ -141,6 +142,7 @@ namespace concurrent {
         void testGet2IndexOutOfBoundsException();
         void testSet1IndexOutOfBoundsException();
         void testSet2IndexOutOfBoundsException();
+        void testConcurrentRandomAddRemoveAndIterate();
 
     };
 



Mime
View raw message