From: Stephen Morris Date: Mon, 10 Jun 2013 12:01:17 +0000 (+0100) Subject: [2974] Add getNumLibraries() method to CalloutManager. X-Git-Tag: bind10-1.2.0beta1-release~402^2~5 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=347a2846eaacb84791fedcda6537d2fd8be6160f;p=thirdparty%2Fkea.git [2974] Add getNumLibraries() method to CalloutManager. --- diff --git a/src/lib/util/hooks/callout_manager.h b/src/lib/util/hooks/callout_manager.h index bc3e58043b..c21bfe2a26 100644 --- a/src/lib/util/hooks/callout_manager.h +++ b/src/lib/util/hooks/callout_manager.h @@ -86,11 +86,22 @@ public: /// /// @param hooks Collection of known hook names. /// @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& hooks, int num_libraries) : current_library_(-1), hooks_(hooks), hook_vector_(hooks->getCount()), 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 /// @@ -161,6 +172,16 @@ public: /// @return Status return. 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 /// /// Returns the index of the "current" library. This the index associated diff --git a/src/lib/util/tests/callout_manager_unittest.cc b/src/lib/util/tests/callout_manager_unittest.cc index d2375060ae..630b18bd23 100644 --- a/src/lib/util/tests/callout_manager_unittest.cc +++ b/src/lib/util/tests/callout_manager_unittest.cc @@ -18,6 +18,7 @@ #include #include +#include #include #include @@ -69,6 +70,10 @@ public: return (callout_manager_); } + boost::shared_ptr getServerHooks() { + return (hooks_); + } + /// Static variable used for accumulating information static int callout_value_; @@ -80,16 +85,14 @@ public: int delta_index_; private: + /// Callout handle used in calls + boost::shared_ptr callout_handle_; + /// Callout manager used for the test boost::shared_ptr callout_manager_; /// Server hooks boost::shared_ptr hooks_; - - /// Callout handle used in calls - boost::shared_ptr callout_handle_; - - }; // Definition of the static variable. @@ -167,6 +170,34 @@ int manager_four_error(CalloutHandle& handle) { }; // extern "C" +// Constructor - check that we trap bad parameters. + +TEST_F(CalloutManagerTest, BadConstructorParameters) { + boost::scoped_ptr 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 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 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. TEST_F(CalloutManagerTest, CheckLibraryIndex) {