Browse Source

[2974] Add getNumLibraries() method to CalloutManager.

Stephen Morris 12 years ago
parent
commit
347a2846ea

+ 22 - 1
src/lib/util/hooks/callout_manager.h

@@ -86,11 +86,22 @@ public:
     ///
     ///
     /// @param hooks Collection of known hook names.
     /// @param hooks Collection of known hook names.
     /// @param num_libraries Number of loaded libraries.
     /// @param num_libraries Number of loaded libraries.
+    ///
+    /// @throw isc::BadValue if the number of libraries is less than or equal
+    ///        to 0, or if the pointer to the server hooks object is empty.
     CalloutManager(const boost::shared_ptr<ServerHooks>& hooks,
     CalloutManager(const boost::shared_ptr<ServerHooks>& hooks,
                    int num_libraries)
                    int num_libraries)
         : current_library_(-1), hooks_(hooks), hook_vector_(hooks->getCount()),
         : current_library_(-1), hooks_(hooks), hook_vector_(hooks->getCount()),
           library_handle_(this), num_libraries_(num_libraries)
           library_handle_(this), num_libraries_(num_libraries)
-    {}
+    {
+        if (!hooks) {
+            isc_throw(isc::BadValue, "must pass a pointer to a valid server "
+                      "hooks object to the CalloutManager");
+        } else if (num_libraries <= 0) {
+            isc_throw(isc::BadValue, "number of libraries passed to the "
+                      "CalloutManager must be >= 0");
+        }
+    }
 
 
     /// @brief Register a callout on a hook
     /// @brief Register a callout on a hook
     ///
     ///
@@ -161,6 +172,16 @@ public:
     /// @return Status return.
     /// @return Status return.
     int callCallouts(int hook_index, CalloutHandle& callout_handle);
     int callCallouts(int hook_index, CalloutHandle& callout_handle);
 
 
+    /// @brief Get number of libraries
+    ///
+    /// Returns the number of libraries that this CalloutManager is expected
+    /// to serve.  This is the number passed to its constructor.
+    ///
+    /// @return Number of libraries server by this CalloutManager.
+    int getNumLibraries() const {
+        return (num_libraries_);
+    }
+
     /// @brief Get current library index
     /// @brief Get current library index
     ///
     ///
     /// Returns the index of the "current" library.  This the index associated
     /// Returns the index of the "current" library.  This the index associated

+ 36 - 5
src/lib/util/tests/callout_manager_unittest.cc

@@ -18,6 +18,7 @@
 #include <util/hooks/library_handle.h>
 #include <util/hooks/library_handle.h>
 #include <util/hooks/server_hooks.h>
 #include <util/hooks/server_hooks.h>
 
 
+#include <boost/scoped_ptr.hpp>
 #include <gtest/gtest.h>
 #include <gtest/gtest.h>
 
 
 #include <algorithm>
 #include <algorithm>
@@ -69,6 +70,10 @@ public:
         return (callout_manager_);
         return (callout_manager_);
     }
     }
 
 
+    boost::shared_ptr<ServerHooks> getServerHooks() {
+        return (hooks_);
+    }
+
     /// Static variable used for accumulating information
     /// Static variable used for accumulating information
     static int callout_value_;
     static int callout_value_;
 
 
@@ -80,16 +85,14 @@ public:
     int delta_index_;
     int delta_index_;
 
 
 private:
 private:
+    /// Callout handle used in calls
+    boost::shared_ptr<CalloutHandle> callout_handle_;
+
     /// Callout manager used for the test
     /// Callout manager used for the test
     boost::shared_ptr<CalloutManager> callout_manager_;
     boost::shared_ptr<CalloutManager> callout_manager_;
 
 
     /// Server hooks
     /// Server hooks
     boost::shared_ptr<ServerHooks> hooks_;
     boost::shared_ptr<ServerHooks> hooks_;
-
-    /// Callout handle used in calls
-    boost::shared_ptr<CalloutHandle> callout_handle_;
-
-
 };
 };
 
 
 // Definition of the static variable.
 // Definition of the static variable.
@@ -167,6 +170,34 @@ int manager_four_error(CalloutHandle& handle) {
 
 
 };  // extern "C"
 };  // extern "C"
 
 
+// Constructor - check that we trap bad parameters.
+
+TEST_F(CalloutManagerTest, BadConstructorParameters) {
+    boost::scoped_ptr<CalloutManager> cm;
+
+    // Invalid number of libraries
+    EXPECT_THROW(cm.reset(new CalloutManager(getServerHooks(), 0)), BadValue);
+    EXPECT_THROW(cm.reset(new CalloutManager(getServerHooks(), -1)), BadValue);
+
+    // Invalid server hooks pointer.
+    boost::shared_ptr<ServerHooks> sh;
+    EXPECT_THROW(cm.reset(new CalloutManager(sh, 4)), BadValue);
+}
+
+// Check the number of libraries is reported successfully.
+
+TEST_F(CalloutManagerTest, GetNumLibraries) {
+    boost::scoped_ptr<CalloutManager> cm;
+
+    // Check two valid values of number of libraries to ensure that the
+    // GetNumLibraries() returns the value set.
+    EXPECT_NO_THROW(cm.reset(new CalloutManager(getServerHooks(), 4)));
+    EXPECT_EQ(4, cm->getNumLibraries());
+
+    EXPECT_NO_THROW(cm.reset(new CalloutManager(getServerHooks(), 42)));
+    EXPECT_EQ(42, cm->getNumLibraries());
+}
+
 // Check that we can only set the current library index to the correct values.
 // Check that we can only set the current library index to the correct values.
 
 
 TEST_F(CalloutManagerTest, CheckLibraryIndex) {
 TEST_F(CalloutManagerTest, CheckLibraryIndex) {