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

Commits to the Asterisk SCF project code repositories asterisk-scf-commits at lists.digium.com
Thu Mar 22 15:30:02 CDT 2012


branch "retry_deux" has been updated
       via  080fec7062496aa20104d2218773b63af11bd65f (commit)
       via  5a207a78dee911fce308a3e329dc550d387b534e (commit)
      from  30866a79a0c267f42b5bb055362661c23c13d674 (commit)

Summary of changes:
 .../AsteriskSCF/Operations/OperationContextCache.h |   66 ++++++++++++--
 .../OperationContextCacheTest.cpp                  |   92 +++++++++++++++++++-
 test/ThreadPool/TestThreadPool.cpp                 |    2 +-
 3 files changed, 148 insertions(+), 12 deletions(-)


- Log -----------------------------------------------------------------
commit 080fec7062496aa20104d2218773b63af11bd65f
Author: David M. Lee <dlee at digium.com>
Date:   Thu Mar 22 15:29:52 2012 -0500

    ValueOperationContextCookie can now handle non-Ice exceptions

diff --git a/include/AsteriskSCF/Operations/OperationContextCache.h b/include/AsteriskSCF/Operations/OperationContextCache.h
index 5e607c5..d4678a3 100644
--- a/include/AsteriskSCF/Operations/OperationContextCache.h
+++ b/include/AsteriskSCF/Operations/OperationContextCache.h
@@ -61,14 +61,39 @@ public:
     void set(const T& v)
     {
         boost::lock_guard<boost::mutex> lock(mMutex);
-        val = v;
+        mVal = v;
     }
 
     /** Set the exception value of this cookie. */
-    void setException(const IceUtil::Exception& e)
+    void setException(const std::exception& e)
     {
         boost::lock_guard<boost::mutex> lock(mMutex);
-        ex.reset(e.ice_clone());
+        const IceUtil::Exception *iceEx = dynamic_cast<const IceUtil::Exception *>(&e);
+        if (iceEx)
+        {
+            // Ice exceptions are clonable; copy it so we can rethrow the original
+            // exception
+            mEx.reset(iceEx->ice_clone());
+        }
+        else
+        {
+            // std::exception can't be copied. Fortunately, Ice doesn't really
+            // care what it is if it's not an Ice exception. Just keep the
+            // what() message for later.
+            mStdExceptionWhat = e.what();
+        }
+    }
+
+    /**
+     * Set the cookie to throw an exception, but we don't know what that
+     * exception is. Ideally, this code is never called at runtime, because
+     * we never throw any type that's not derived from std::exception. But
+     * we have to cover our bases.
+     */
+    void setException()
+    {
+        boost::lock_guard<boost::mutex> lock(mMutex);
+        mPoorlyTypedException = true;
     }
 
     /**
@@ -78,11 +103,22 @@ public:
     T get() const
     {
         boost::lock_guard<boost::mutex> lock(mMutex);
-        if (ex)
+        if (mEx)
+        {
+            mEx->ice_throw();
+        }
+        else if (!mStdExceptionWhat.empty())
+        {
+            // throw an exception with the same what() message as the original
+            // exception
+            throw StdException(mStdExceptionWhat);
+        }
+        else if (mPoorlyTypedException)
         {
-            ex->ice_throw();
+            // someone throw a non-standard exception; we should, too.
+            throw "Okay; who's throwing non-exception types?";
         }
-        return val;
+        return mVal;
     }
 
 private:
@@ -90,8 +126,22 @@ private:
     // reads happening concurrently are small. There would be no benefit from
     // a shared_mutex, so just using a plain mutex instead.
     mutable boost::mutex mMutex;
-    boost::shared_ptr<IceUtil::Exception> ex;
-    T val;
+    T mVal;
+
+    boost::shared_ptr<IceUtil::Exception> mEx;
+    std::string mStdExceptionWhat;
+    bool mPoorlyTypedException;
+
+    class StdException : public std::exception
+    {
+    public:
+        StdException(const std::string& wat): mWhat(wat) {}
+        ~StdException() throw() {}
+
+        const char* what() throw() { return mWhat.c_str(); }
+    private:
+        std::string mWhat;
+    };
 };
 
 class OperationContextCache;
diff --git a/test/OperationContextCache/OperationContextCacheTest.cpp b/test/OperationContextCache/OperationContextCacheTest.cpp
index a9ff493..e3eaf0c 100644
--- a/test/OperationContextCache/OperationContextCacheTest.cpp
+++ b/test/OperationContextCache/OperationContextCacheTest.cpp
@@ -35,9 +35,14 @@ using namespace AsteriskSCF::System::Logging;
 namespace
 {
 Logger lg = AsteriskSCF::System::Logging::getLoggerFactory().getLogger("TestMyLogOutput");
-}
 
-static void testCache(const OperationContextCachePtr& cache, std::vector<OperationContextPtr>& expected)
+class PhonyException : public std::exception
+{
+public:
+    const char* what() throw() { return "phony"; }
+};
+
+void testCache(const OperationContextCachePtr& cache, std::vector<OperationContextPtr>& expected)
 {
     BOOST_CHECK_MESSAGE(cache->size() == expected.size(), "testCache failed: Cache size=" << cache->size() << ", expected " << expected.size());
 
@@ -47,7 +52,7 @@ static void testCache(const OperationContextCachePtr& cache, std::vector<Operati
     }
 }
 
-static void runOperationContextCacheTest()
+void runOperationContextCacheTest()
 {
     lg.setLevel(Trace);
 
@@ -86,10 +91,91 @@ static void runOperationContextCacheTest()
     testOperations.clear();
 }
 
+void ValueOperationContextCookie_valueTest()
+{
+    ValueOperationContextCookie<int> uut;
+
+    uut.set(5);
+
+    BOOST_REQUIRE_EQUAL(5, uut.get());
+}
+
+void ValueOperationContextCookie_iceExceptionTest()
+{
+    ValueOperationContextCookie<int> uut;
+    IceUtil::NullHandleException e("not-a-file.cpp", 314159);
+
+    uut.setException(e);
+
+    try
+    {
+        uut.get();
+    }
+    catch (const IceUtil::NullHandleException&)
+    {
+        // expected
+    }
+}
+
+void ValueOperationContextCookie_stdExceptionTest()
+{
+    ValueOperationContextCookie<int> uut;
+    PhonyException e;
+
+    uut.setException(e);
+
+    try
+    {
+        uut.get();
+    }
+    catch (const Ice::Exception&)
+    {
+        BOOST_FAIL("Unexpected Ice exception");
+    }
+    catch (const std::exception&)
+    {
+        // expected
+    }
+}
+
+void ValueOperationContextCookie_untypedExceptionTest()
+{
+    ValueOperationContextCookie<int> uut;
+
+    uut.setException();
+
+    try
+    {
+        uut.get();
+    }
+    catch (const Ice::Exception&)
+    {
+        BOOST_FAIL("Unexpected Ice exception");
+    }
+    catch (const std::exception&)
+    {
+        BOOST_FAIL("Unexpected std::exception");
+    }
+    catch (...)
+    {
+        // expected
+    }
+}
+
+} // anonymous namespace
+
 using namespace boost::unit_test;
 
 AsteriskSCF::OperationContextCacheTests::OperationContextCacheTestSuite::OperationContextCacheTestSuite()
 {
     framework::master_test_suite().
         add(BOOST_TEST_CASE(runOperationContextCacheTest));
+    framework::master_test_suite().
+        add(BOOST_TEST_CASE(ValueOperationContextCookie_valueTest));
+    framework::master_test_suite().
+        add(BOOST_TEST_CASE(ValueOperationContextCookie_iceExceptionTest));
+    framework::master_test_suite().
+        add(BOOST_TEST_CASE(ValueOperationContextCookie_stdExceptionTest));
+    framework::master_test_suite().
+        add(BOOST_TEST_CASE(ValueOperationContextCookie_untypedExceptionTest));
 }

commit 5a207a78dee911fce308a3e329dc550d387b534e
Author: David M. Lee <dlee at digium.com>
Date:   Thu Mar 22 15:21:07 2012 -0500

    Removed extra parens; caused warning with Clang

diff --git a/test/ThreadPool/TestThreadPool.cpp b/test/ThreadPool/TestThreadPool.cpp
index af00f92..5f77851 100644
--- a/test/ThreadPool/TestThreadPool.cpp
+++ b/test/ThreadPool/TestThreadPool.cpp
@@ -130,7 +130,7 @@ typedef IceUtil::Handle<ComplexTask> ComplexTaskPtr;
 #define WAIT_WHILE(condition) \
 {\
     boost::unique_lock<boost::mutex> lock(listener->mLock);\
-    while ((condition))\
+    while (condition)\
     {\
         listener->mDone.wait(lock);\
     }\

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


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



More information about the asterisk-scf-commits mailing list