[asterisk-scf-commits] asterisk-scf/integration/ice-util-cpp.git branch "workqueue" updated.

Commits to the Asterisk SCF project code repositories asterisk-scf-commits at lists.digium.com
Thu Mar 17 10:44:34 CDT 2011


branch "workqueue" has been updated
       via  f91844c5783119bb2e40bbe0eb2e7c4639915e5d (commit)
       via  0c8ffa41b30e8e5ccbf65c7a0a231ef052d76447 (commit)
       via  c675dd5854605af4857149254935f09abdf1862c (commit)
       via  178c58cbfe0880cdd81f5afbd2c42408a5e25cc5 (commit)
      from  777ec8309a11a35b8637e1ca464d9038af7134fe (commit)

Summary of changes:
 WorkQueue/src/WorkQueue.cpp      |   37 +++++++--
 WorkQueue/test/TestWorkQueue.cpp |  173 ++++++++++++++++++++++++++++++++++----
 2 files changed, 187 insertions(+), 23 deletions(-)


- Log -----------------------------------------------------------------
commit f91844c5783119bb2e40bbe0eb2e7c4639915e5d
Author: Mark Michelson <mmichelson at digium.com>
Date:   Thu Mar 17 10:37:54 2011 -0500

    Throw WorkExists exception when attempting to enqueue an item already in the queue.
    
    Also added new test cases. All tests pass.

diff --git a/WorkQueue/src/WorkQueue.cpp b/WorkQueue/src/WorkQueue.cpp
index d0328ca..df045e7 100644
--- a/WorkQueue/src/WorkQueue.cpp
+++ b/WorkQueue/src/WorkQueue.cpp
@@ -48,6 +48,15 @@ WorkQueue::WorkQueue(const QueueListenerPtr &listener)
 void WorkQueue::enqueueWork(const WorkPtr &work)
 {
     boost::unique_lock<boost::mutex> lock(mImpl->mLock);
+    
+    if (std::find(mImpl->q.begin(), mImpl->q.end(), work) != mImpl->q.end())
+    {
+        //XXX When slice2cpp bug is cleared, we'll want
+        //to actually include the duplicated work item in
+        //the exception
+        throw WorkExists();
+    }
+
     bool wasEmpty = mImpl->q.empty();
     mImpl->q.push_back(work);
     lock.unlock();
@@ -57,6 +66,19 @@ void WorkQueue::enqueueWork(const WorkPtr &work)
 void WorkQueue::enqueueWorkSeq(const WorkSeq &works)
 {
     boost::unique_lock<boost::mutex> lock(mImpl->mLock);
+    
+    for (WorkSeq::const_iterator iter = works.begin(); iter != works.end(); ++iter)
+    {
+        if (std::find(mImpl->q.begin(), mImpl->q.end(), *iter) != mImpl->q.end())
+        {
+            //XXX When slice2cpp bug is cleared, we'll need
+            //not to throw the exception immediately. Instead,
+            //we'll keep a running list of duplicated items so
+            //we can throw an exception with all duplicated items.
+            throw WorkExists();
+        }
+    }
+
     bool wasEmpty = mImpl->q.empty();
     mImpl->q.insert(mImpl->q.end(), works.begin(), works.end());
     lock.unlock();
diff --git a/WorkQueue/test/TestWorkQueue.cpp b/WorkQueue/test/TestWorkQueue.cpp
index b3e392d..0434214 100644
--- a/WorkQueue/test/TestWorkQueue.cpp
+++ b/WorkQueue/test/TestWorkQueue.cpp
@@ -73,7 +73,17 @@ BOOST_AUTO_TEST_CASE(addWork)
     QueuePtr queue(new WorkQueue(listener));
     TaskPtr work(new Task);
 
-    queue->enqueueWork(work);
+    bool excepted = false;
+    try
+    {
+        queue->enqueueWork(work);
+    }
+    catch (const WorkExists &)
+    {
+        excepted = true;
+    }
+
+    BOOST_CHECK(excepted == false);
     BOOST_CHECK(listener->addedNotice == true);
     BOOST_CHECK(listener->addedEmptyNotice == true);
     BOOST_CHECK(queue->workCount() == 1);
@@ -89,14 +99,24 @@ BOOST_AUTO_TEST_CASE(addWorkSeq)
     works.push_back(work1);
     works.push_back(work2);
 
-    queue->enqueueWorkSeq(works);
+    bool excepted = false;
+    try
+    {
+        queue->enqueueWorkSeq(works);
+    }
+    catch (const WorkExists &)
+    {
+        excepted = true;
+    }
+
+    BOOST_CHECK(excepted == false);
     BOOST_CHECK(listener->addedNotice == true);
     BOOST_CHECK(listener->addedEmptyNotice == true);
     BOOST_CHECK(listener->emptyNotice == false);
     BOOST_CHECK(queue->workCount() == 2);
 }
 
-BOOST_AUTO_TEST_CASE(addExistingWork)
+BOOST_AUTO_TEST_CASE(appendWork)
 {
     TestListenerPtr listener(new TestListener);
     QueuePtr queue(new WorkQueue(listener));
@@ -106,13 +126,78 @@ BOOST_AUTO_TEST_CASE(addExistingWork)
     queue->enqueueWork(work1);
     listener->addedNotice = false;
 
-    queue->enqueueWork(work2);
+    bool excepted = false;
+    try
+    {
+        queue->enqueueWork(work2);
+    }
+    catch (const WorkExists &)
+    {
+        excepted = true;
+    }
+
+    BOOST_CHECK(excepted == false);
     BOOST_CHECK(listener->addedNotice == true);
     BOOST_CHECK(listener->addedEmptyNotice == false);
     BOOST_CHECK(listener->emptyNotice == false);
     BOOST_CHECK(queue->workCount() == 2);
 }
 
+BOOST_AUTO_TEST_CASE(addExistentWork)
+{
+    TestListenerPtr listener(new TestListener);
+    QueuePtr queue(new WorkQueue(listener));
+    TaskPtr work(new Task);
+
+    queue->enqueueWork(work);
+
+    bool excepted = false;
+    try
+    {
+        queue->enqueueWork(work);
+    }
+    catch(const WorkExists&)
+    {
+        excepted = true;
+    }
+
+    BOOST_CHECK(excepted == true);
+    BOOST_CHECK(queue->workCount() == 1);
+}
+
+BOOST_AUTO_TEST_CASE(addExistentWorkSeq)
+{
+    TestListenerPtr listener(new TestListener);
+    QueuePtr queue(new WorkQueue(listener));
+    TaskPtr work1(new Task);
+    TaskPtr work2(new Task);
+    TaskPtr work3(new Task);
+    WorkSeq works1;
+    WorkSeq works2;
+    works1.push_back(work1);
+    works1.push_back(work2);
+    works2.push_back(work3);
+    works2.push_back(work2);
+    
+    queue->enqueueWorkSeq(works1);
+
+    bool excepted = false;
+    try
+    {
+        queue->enqueueWorkSeq(works2);
+    }
+    catch (const WorkExists &)
+    {
+        excepted = true;
+    }
+
+    BOOST_CHECK(excepted == true);
+    BOOST_CHECK(listener->addedNotice == true);
+    BOOST_CHECK(listener->addedEmptyNotice == true);
+    BOOST_CHECK(listener->emptyNotice == false);
+    BOOST_CHECK(queue->workCount() == 2);
+}
+
 BOOST_AUTO_TEST_CASE(cancelWork)
 {
     TestListenerPtr listener(new TestListener);

commit 0c8ffa41b30e8e5ccbf65c7a0a231ef052d76447
Author: Mark Michelson <mmichelson at digium.com>
Date:   Thu Mar 17 10:14:12 2011 -0500

    Use std::find when canceling work. Tests still pass!

diff --git a/WorkQueue/src/WorkQueue.cpp b/WorkQueue/src/WorkQueue.cpp
index c70d947..d0328ca 100644
--- a/WorkQueue/src/WorkQueue.cpp
+++ b/WorkQueue/src/WorkQueue.cpp
@@ -71,23 +71,14 @@ void WorkQueue::cancelWork(const WorkPtr &work)
         throw WorkNotFound();
     }
 
-    bool workCanceled = false;
-    for (std::deque<WorkPtr>::iterator i = mImpl->q.begin();
-            i != mImpl->q.end(); ++i)
-    {
-        if (*i == work)
-        {
-            mImpl->q.erase(i);
-            workCanceled = true;
-            break;
-        }
-    }
-
-    if (!workCanceled)
+    std::deque<WorkPtr>::iterator i = std::find(mImpl->q.begin(), mImpl->q.end(), work);
+    if (i == mImpl->q.end())
     {
         throw WorkNotFound();
     }
 
+    mImpl->q.erase(i);
+
     if (mImpl->q.empty())
     {
         lock.unlock();

commit c675dd5854605af4857149254935f09abdf1862c
Author: Mark Michelson <mmichelson at digium.com>
Date:   Thu Mar 17 09:52:32 2011 -0500

    Throw exception when trying to cancel nonexistent work.

diff --git a/WorkQueue/src/WorkQueue.cpp b/WorkQueue/src/WorkQueue.cpp
index bf906e5..c70d947 100644
--- a/WorkQueue/src/WorkQueue.cpp
+++ b/WorkQueue/src/WorkQueue.cpp
@@ -68,18 +68,26 @@ void WorkQueue::cancelWork(const WorkPtr &work)
     boost::unique_lock<boost::mutex> lock(mImpl->mLock);
     if (mImpl->q.empty())
     {
-        return;
+        throw WorkNotFound();
     }
 
+    bool workCanceled = false;
     for (std::deque<WorkPtr>::iterator i = mImpl->q.begin();
             i != mImpl->q.end(); ++i)
     {
         if (*i == work)
         {
             mImpl->q.erase(i);
+            workCanceled = true;
             break;
         }
     }
+
+    if (!workCanceled)
+    {
+        throw WorkNotFound();
+    }
+
     if (mImpl->q.empty())
     {
         lock.unlock();
diff --git a/WorkQueue/test/TestWorkQueue.cpp b/WorkQueue/test/TestWorkQueue.cpp
index 4e04c47..b3e392d 100644
--- a/WorkQueue/test/TestWorkQueue.cpp
+++ b/WorkQueue/test/TestWorkQueue.cpp
@@ -120,25 +120,68 @@ BOOST_AUTO_TEST_CASE(cancelWork)
     TaskPtr work1(new Task);
 
     queue->enqueueWork(work1);
-    queue->cancelWork(work1);
 
+    bool excepted = false;
+    try
+    {
+        queue->cancelWork(work1);
+    }
+    catch (const WorkNotFound &)
+    {
+        excepted = true;
+    }
+
+    BOOST_CHECK(excepted == false);
     BOOST_CHECK(listener->emptyNotice == true);
     BOOST_CHECK(queue->workCount() == 0);
 }
 
-BOOST_AUTO_TEST_CASE(cancelNonExistent)
+BOOST_AUTO_TEST_CASE(cancelNonExistent1)
 {
     TestListenerPtr listener(new TestListener);
     QueuePtr queue(new WorkQueue(listener));
     TaskPtr work(new Task);
 
-    queue->cancelWork(work);
+    bool excepted = false;
+    try
+    {
+        queue->cancelWork(work);
+    }
+    catch (const WorkNotFound &)
+    {
+        excepted = true;
+    }
+
+    BOOST_CHECK(excepted == true);
     BOOST_CHECK(listener->addedNotice == false);
     BOOST_CHECK(listener->addedEmptyNotice == false);
     BOOST_CHECK(listener->emptyNotice == false);
     BOOST_CHECK(queue->workCount() == 0);
 }
 
+BOOST_AUTO_TEST_CASE(cancelNonExistent2)
+{
+    TestListenerPtr listener(new TestListener);
+    QueuePtr queue(new WorkQueue(listener));
+    TaskPtr work1(new Task);
+    TaskPtr work2(new Task);
+
+    queue->enqueueWork(work1);
+
+    bool excepted = false;
+    try
+    {
+        queue->cancelWork(work2);
+    }
+    catch (const WorkNotFound &wnf)
+    {
+        excepted = true;
+    }
+
+    BOOST_CHECK(excepted == true);
+    BOOST_CHECK(queue->workCount() == 1);
+}
+
 BOOST_AUTO_TEST_CASE(workExecution)
 {
     TestListenerPtr listener(new TestListener);

commit 178c58cbfe0880cdd81f5afbd2c42408a5e25cc5
Author: Mark Michelson <mmichelson at digium.com>
Date:   Thu Mar 17 09:42:09 2011 -0500

    Test return value of executeWork() calls.

diff --git a/WorkQueue/test/TestWorkQueue.cpp b/WorkQueue/test/TestWorkQueue.cpp
index 8da6074..4e04c47 100644
--- a/WorkQueue/test/TestWorkQueue.cpp
+++ b/WorkQueue/test/TestWorkQueue.cpp
@@ -146,8 +146,9 @@ BOOST_AUTO_TEST_CASE(workExecution)
     TaskPtr work(new Task);
 
     queue->enqueueWork(work);
-    queue->executeWork();
+    bool moreWork = queue->executeWork();
 
+    BOOST_CHECK(moreWork == false);
     BOOST_CHECK(work->taskExecuted == true);
     BOOST_CHECK(listener->emptyNotice == true);
     BOOST_CHECK(queue->workCount() == 0);
@@ -158,7 +159,9 @@ BOOST_AUTO_TEST_CASE(executeNonExistent)
     TestListenerPtr listener(new TestListener);
     QueuePtr queue(new WorkQueue(listener));
 
-    queue->executeWork();
+    bool moreWork = queue->executeWork();
+
+    BOOST_CHECK(moreWork == false);
     BOOST_CHECK(queue->workCount() == 0);
     BOOST_CHECK(listener->emptyNotice == false);
 }
@@ -173,11 +176,15 @@ BOOST_AUTO_TEST_CASE(executionOrder1)
     queue->enqueueWork(work1);
     queue->enqueueWork(work2);
     
-    queue->executeWork();
+    bool moreWork = queue->executeWork();
+
+    BOOST_CHECK(moreWork == true);
     BOOST_CHECK(work1->taskExecuted == true);
     BOOST_CHECK(work2->taskExecuted == false);
     
-    queue->executeWork();
+    moreWork = queue->executeWork();
+
+    BOOST_CHECK(moreWork == false);
     BOOST_CHECK(work2->taskExecuted == true);
     BOOST_CHECK(listener->emptyNotice == true);
     BOOST_CHECK(queue->workCount() == 0);
@@ -202,22 +209,30 @@ BOOST_AUTO_TEST_CASE(executionOrder2)
     queue->enqueueWorkSeq(works1);
     queue->enqueueWorkSeq(works2);
 
-    queue->executeWork();
+    bool moreWork = queue->executeWork();
+
+    BOOST_CHECK(moreWork == true);
     BOOST_CHECK(work1->taskExecuted == true);
     BOOST_CHECK(work2->taskExecuted == false);
     BOOST_CHECK(work3->taskExecuted == false);
     BOOST_CHECK(work4->taskExecuted == false);
 
-    queue->executeWork();
+    moreWork = queue->executeWork();
+
+    BOOST_CHECK(moreWork == true);
     BOOST_CHECK(work2->taskExecuted == true);
     BOOST_CHECK(work3->taskExecuted == false);
     BOOST_CHECK(work4->taskExecuted == false);
 
-    queue->executeWork();
+    moreWork = queue->executeWork();
+
+    BOOST_CHECK(moreWork == true);
     BOOST_CHECK(work3->taskExecuted == true);
     BOOST_CHECK(work4->taskExecuted == false);
 
-    queue->executeWork();
+    moreWork = queue->executeWork();
+
+    BOOST_CHECK(moreWork == false);
     BOOST_CHECK(work4->taskExecuted == true);
     BOOST_CHECK(listener->emptyNotice == true);
     BOOST_CHECK(queue->workCount() == 0);

-----------------------------------------------------------------------


-- 
asterisk-scf/integration/ice-util-cpp.git



More information about the asterisk-scf-commits mailing list