[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
Wed Feb 1 18:38:41 CST 2012


branch "retry_deux" has been updated
       via  a2f6d2e757244acc9ca6c0583bc68fb4d52d14f6 (commit)
      from  f8321e105ea059622ad3f1ba1690e9f7f581a8f8 (commit)

Summary of changes:
 .../AsteriskSCF/Helpers/OperationContextCache.h    |   13 ++-
 src/Helpers/OperationContextCache.cpp              |   14 +++-
 test/CMakeLists.txt                                |    3 +
 .../LocatorRegistrationTest.cpp                    |   15 +---
 .../OperationContextCacheTest.cpp                  |   83 ++++++++++++++++++++
 .../OperationContextCacheTest.h}                   |   13 ++--
 test/UtilityTests.cpp                              |    4 +-
 7 files changed, 119 insertions(+), 26 deletions(-)
 create mode 100644 test/OperationContextCache/OperationContextCacheTest.cpp
 copy test/{PropertyHelper/PropertyHelperTest.h => OperationContextCache/OperationContextCacheTest.h} (67%)


- Log -----------------------------------------------------------------
commit a2f6d2e757244acc9ca6c0583bc68fb4d52d14f6
Author: Ken Hunt <ken.hunt at digium.com>
Date:   Wed Feb 1 18:38:10 2012 -0600

    Added unit test for OperationContextCache.

diff --git a/include/AsteriskSCF/Helpers/OperationContextCache.h b/include/AsteriskSCF/Helpers/OperationContextCache.h
index 2470981..77207c2 100644
--- a/include/AsteriskSCF/Helpers/OperationContextCache.h
+++ b/include/AsteriskSCF/Helpers/OperationContextCache.h
@@ -27,21 +27,21 @@ namespace Helpers
 {
 
 class OperationContextCachEntry;
-typedef boost::shared_ptr<OperationContextCachEntry> OperationContextCachEntryPtr;
+typedef ASTSCF_DLL_EXPORT boost::shared_ptr<OperationContextCachEntry> OperationContextCachEntryPtr;
 
 /**
  * Utiltity class that provides a queryable cache of OperationContext objects.
  */
-class OperationContextCache : public IceUtil::Shared
+class ASTSCF_DLL_EXPORT OperationContextCache : public IceUtil::Shared
 {
 public:
     /**
      * ctor
-     * @param ttlMinutes  The time-to-live for the OperationContexts being cached.
+     * @param ttlSeconds  The time-to-live for the OperationContexts being cached.
      * Entries will remain in the cache for at least the provided value, but can 
      * remain in cache longer. 
      */
-    OperationContextCache(int ttlMinutes);
+    OperationContextCache(int ttlSeconds);
     ~OperationContextCache();
 
     /**
@@ -64,6 +64,11 @@ public:
      */
     void prune(); 
 
+    /**
+     * Retrieve the number of entries currently in the cache. 
+     */
+    std::size_t size();
+
 private:
     boost::shared_mutex mLock;
     IceUtil::TimerTaskPtr mTimerTask;
diff --git a/src/Helpers/OperationContextCache.cpp b/src/Helpers/OperationContextCache.cpp
index 3c6085f..b71ba1d 100644
--- a/src/Helpers/OperationContextCache.cpp
+++ b/src/Helpers/OperationContextCache.cpp
@@ -92,12 +92,12 @@ typedef IceUtil::Handle<OperationContextPruner> OperationContextPrunerPtr;
 
 /**
  * Constructor.
- * @param ttlMinutes The time to live for the cache, specified in minutes. 
+ * @param ttlSeconds The time to live for the cache, specified in seconds. 
  *  This is a minimum time for an OperationContext to be cached. They
  *  may be cached longer. 
  */
-OperationContextCache::OperationContextCache(int ttlMinutes) 
-    : mTTL(IceUtil::Time::seconds(ttlMinutes*60)),
+OperationContextCache::OperationContextCache(int ttlSeconds) 
+    : mTTL(IceUtil::Time::seconds(ttlSeconds)),
       mTimer(new IceUtil::Timer),
       mTimerTask(new OperationContextPruner(this))
 {
@@ -139,7 +139,7 @@ bool OperationContextCache::contains(const AsteriskSCF::System::V1::OperationCon
     boost::unique_lock<boost::shared_mutex> lock(mLock);
 
     std::map<std::string, OperationContextCachEntryPtr>::iterator entry = mCache.find(operationContext->id);
-    if (entry != mCache.end())
+    if (entry == mCache.end())
     {
         return false;
     }
@@ -175,5 +175,11 @@ void OperationContextCache::prune()
     }
 }
 
+std::size_t OperationContextCache::size()
+{
+    boost::unique_lock<boost::shared_mutex> lock(mLock);
+    return mCache.size();
+}
+
 } // namespace Helpers 
 } // namespace AsteriskSCF 
diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt
index 1e0cd0a..6c3e4fe 100644
--- a/test/CMakeLists.txt
+++ b/test/CMakeLists.txt
@@ -7,11 +7,14 @@ astscf_component_add_files(ASTSCFIceUtilCppTest PropertyHelper/PropertyHelperTes
 astscf_component_add_files(ASTSCFIceUtilCppTest PropertyHelper/PropertyHelperTest.h)
 astscf_component_add_files(ASTSCFIceUtilCppTest ProxyHelper/ProxyHelperTests.cpp)
 astscf_component_add_files(ASTSCFIceUtilCppTest ProxyHelper/ProxyHelperTests.h)
+astscf_component_add_files(ASTSCFIceUtilCppTest OperationContextCache/OperationContextCacheTest.cpp)
+astscf_component_add_files(ASTSCFIceUtilCppTest OperationContextCache/OperationContextCacheTest.h)
 astscf_component_add_files(ASTSCFIceUtilCppTest UtilityTests.cpp)
 astscf_component_add_ice_libraries(ASTSCFIceUtilCppTest IceBox)
 astscf_component_add_boost_libraries(ASTSCFIceUtilCppTest unit_test_framework date_time thread)
 astscf_component_add_slice_collection_libraries(ASTSCFIceUtilCppTest ASTSCF)
 astscf_component_build_icebox(ASTSCFIceUtilCppTest)
+target_link_libraries(ASTSCFIceUtilCppTest ASTSCFIceUtilCpp)
 astscf_test_icebox(ASTSCFIceUtilCppTest config/IceUtilCppTests.conf)
 
 add_subdirectory(Async)
diff --git a/test/LocatorRegistration/LocatorRegistrationTest.cpp b/test/LocatorRegistration/LocatorRegistrationTest.cpp
index 7f84c58..d100fab 100644
--- a/test/LocatorRegistration/LocatorRegistrationTest.cpp
+++ b/test/LocatorRegistration/LocatorRegistrationTest.cpp
@@ -39,6 +39,7 @@
 using namespace std;
 using namespace boost::unit_test;
 using namespace AsteriskSCF::Discovery;
+using namespace AsteriskSCF::System::V1;
 
 namespace AsteriskSCF
 {
@@ -139,7 +140,8 @@ public:
     {
     }
 
-    void addLocatorParams(const AsteriskSCF::Core::Discovery::V1::ServiceLocatorParamsPtr& params,
+    void addLocatorParams(const AsteriskSCF::System::V1::OperationContextPtr& operationContext,
+            const AsteriskSCF::Core::Discovery::V1::ServiceLocatorParamsPtr& params,
             const std::string& compareGUID,
             const Ice::Current&)
     {
@@ -181,15 +183,6 @@ public:
         }
     }
 
-    AsteriskSCF::Core::Discovery::V1::ServiceLocatorParamsSeq getLocatorParams(const Ice::Current&)
-    {
-        if (!mData->destroy())
-        {
-            throw Ice::ObjectNotExistException(__FILE__, __LINE__);
-        }
-        return AsteriskSCF::Core::Discovery::V1::ServiceLocatorParamsSeq();
-    }
-
 private:
     IceUtil::Handle<RegistrationData> mData;
 };
@@ -482,7 +475,7 @@ public:
             mMgrToken = mManager->addService(mManager, mMgrTokenUUID);
             AsteriskSCF::Core::Discovery::V1::ServiceLocatorParamsPtr params(new AsteriskSCF::Core::Discovery::V1::ServiceLocatorParams);
             params->category = "AsteriskSCF.LocatorManager";
-            mMgrToken->addLocatorParams(params, "");
+            mMgrToken->addLocatorParams(new OperationContext(IceUtil::generateUUID()), params, "");
         }
     }
 
diff --git a/test/OperationContextCache/OperationContextCacheTest.cpp b/test/OperationContextCache/OperationContextCacheTest.cpp
new file mode 100644
index 0000000..266590d
--- /dev/null
+++ b/test/OperationContextCache/OperationContextCacheTest.cpp
@@ -0,0 +1,83 @@
+/*
+ * Asterisk SCF -- An open-source communications framework.
+ *
+ * Copyright (C) 2010-2011, Digium, Inc.
+ *
+ * See http://www.asterisk.org for more information about
+ * the Asterisk SCF project. Please do not directly contact
+ * any of the maintainers of this project for assistance;
+ * the project provides a web site, mailing lists and IRC
+ * channels for your use.
+ *
+ * This program is free software, distributed under the terms of
+ * the GNU General Public License Version 2. See the LICENSE.txt file
+ * at the top of the source tree.
+ */
+
+#include <Ice/Properties.h>
+#include <Ice/Initialize.h>
+#include <AsteriskSCF/Helpers/OperationContextCache.h>
+#include <boost/lexical_cast.hpp>
+#include <boost/test/unit_test.hpp>
+#include <string>
+#include <vector>
+
+#include "OperationContextCacheTest.h"
+
+using namespace AsteriskSCF;
+using namespace AsteriskSCF::System::V1;
+using namespace AsteriskSCF::Helpers;
+
+static void testCache(const OperationContextCachePtr& cache, std::vector<OperationContextPtr>& expected)
+{
+    BOOST_CHECK(cache->size() == expected.size());
+
+    for(int i=0; i < expected.size(); ++i)
+    {
+        BOOST_CHECK(cache->contains(expected[i]));
+    }
+}
+
+static void runOperationContextCacheTest()
+{
+    const int TTL_SECONDS(5);
+
+    OperationContextCachePtr cache = new OperationContextCache(TTL_SECONDS);
+    std::vector<OperationContextPtr> testOperations;
+
+    // Verify empty
+    testCache(cache, testOperations);
+
+    testOperations.push_back(new OperationContext("ID1"));
+    testOperations.push_back(new OperationContext("ID2"));
+    testOperations.push_back(new OperationContext("ID3"));
+
+    BOOST_CHECK(cache->addOperationContext(testOperations[0]) == true);
+    BOOST_CHECK(cache->addOperationContext(testOperations[1]) == true);
+    BOOST_CHECK(cache->addOperationContext(testOperations[2]) == true);
+
+    // Verify the cache contains our 3 test operations.
+    testCache(cache, testOperations);
+
+    // Make sure it rejets duplicates. 
+    BOOST_CHECK(cache->addOperationContext(testOperations[0]) == false);
+
+    // Make sure the cache purges as intended. Twice the TTL is worst case. 
+    IceUtil::ThreadControl::sleep(IceUtil::Time::seconds(2*TTL_SECONDS));
+
+    testOperations.clear();
+
+    // The cache should be as empty as our test operations vector. 
+    testCache(cache, testOperations);
+
+    testOperations.clear();
+
+}
+
+using namespace boost::unit_test;
+
+AsteriskSCF::OperationContextCacheTests::OperationContextCacheTestSuite::OperationContextCacheTestSuite()
+{
+    framework::master_test_suite().
+        add(BOOST_TEST_CASE(runOperationContextCacheTest));
+}
diff --git a/test/OperationContextCache/OperationContextCacheTest.h b/test/OperationContextCache/OperationContextCacheTest.h
new file mode 100644
index 0000000..fc71ae3
--- /dev/null
+++ b/test/OperationContextCache/OperationContextCacheTest.h
@@ -0,0 +1,37 @@
+/*
+ * Asterisk SCF -- An open-source communications framework.
+ *
+ * Copyright (C) 2010-2011, Digium, Inc.
+ *
+ * See http://www.asterisk.org for more information about
+ * the Asterisk SCF project. Please do not directly contact
+ * any of the maintainers of this project for assistance;
+ * the project provides a web site, mailing lists and IRC
+ * channels for your use.
+ *
+ * This program is free software, distributed under the terms of
+ * the GNU General Public License Version 2. See the LICENSE.txt file
+ * at the top of the source tree.
+ */
+
+#pragma once
+
+#include <Ice/Ice.h>
+
+namespace AsteriskSCF
+{
+namespace OperationContextCacheTests
+{
+
+class OperationContextCacheTestSuite
+{
+public:
+    /**
+     * Registers the test suite with the global test suite. 
+     * There isn't anything else that needs to be done.
+     **/
+    OperationContextCacheTestSuite();
+};
+
+} // namespace OperationContextCacheTests
+} // namespace AsteriskSCF 
diff --git a/test/UtilityTests.cpp b/test/UtilityTests.cpp
index 5243613..f0e3d79 100644
--- a/test/UtilityTests.cpp
+++ b/test/UtilityTests.cpp
@@ -24,6 +24,7 @@
 #include "PropertyHelper/PropertyHelperTest.h"
 #include "ProxyHelper/ProxyHelperTests.h"
 #include "LocatorRegistration/LocatorRegistrationTest.h"
+#include "OperationContextCache/OperationContextCacheTest.h"
 
 #include <boost/test/unit_test.hpp>
 #include <boost/bind.hpp>
@@ -61,10 +62,11 @@ public:
         int argc = mArgs.argc();
         char** argv = mArgs.argv();
 
+        OperationContextCacheTests::OperationContextCacheTestSuite operationContextTest;
         PropertyHelperTests::PropertyHelperTestSuite propertyTests(mCommunicator);
         ProxyHelperTests::ProxyHelperTestSuite proxyHelperTests(mCommunicator);
         LocatorRegistrationTests::LocatorRegistrationTestSuite locatorWrapperTest(mCommunicator);
-        
+
         ::boost::unit_test::unit_test_main(&init, argc, argv);
 
         IceBox::ServiceManagerPrx mgr; 

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


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



More information about the asterisk-scf-commits mailing list