From: Francis Dupont Date: Sat, 31 Dec 2016 20:11:29 +0000 (+0100) Subject: [5095] Added some shared manager unit tests showing use constraints X-Git-Tag: trac3590_base~15^2~5 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=fd36cd2766b0cecaf480e4d09e75e7105187357e;p=thirdparty%2Fkea.git [5095] Added some shared manager unit tests showing use constraints --- diff --git a/src/lib/hooks/tests/hooks_manager_unittest.cc b/src/lib/hooks/tests/hooks_manager_unittest.cc index 9d783d75e0..54aaf86cca 100644 --- a/src/lib/hooks/tests/hooks_manager_unittest.cc +++ b/src/lib/hooks/tests/hooks_manager_unittest.cc @@ -40,9 +40,10 @@ public: /// @brief Destructor /// - /// Unload all libraries. + /// Unload all libraries and reset the shared manager. ~HooksManagerTest() { HooksManager::unloadLibraries(); + CalloutManager::getSharedManager().reset(); } @@ -391,6 +392,144 @@ TEST_F(HooksManagerTest, PrePostCalloutTest) { EXPECT_EQ(-15, result); } +// Test with a shared manager the pre- and post- callout functions survive +// a reload + +TEST_F(HooksManagerTest, PrePostCalloutShared) { + + HookLibsCollection library_names; + + // Initialize the shared manager. + CalloutManager::getSharedManager().reset(new CalloutManager(0)); + + // Load the pre- and post- callouts. + HooksManager::preCalloutsLibraryHandle().registerCallout("hookpt_two", + testPreCallout); + HooksManager::postCalloutsLibraryHandle().registerCallout("hookpt_two", + testPostCallout); + + // With the pre- and post- callouts above, the result expected is + // + // 1027 * 2 + CalloutHandlePtr handle = HooksManager::createCalloutHandle(); + handle->setArgument("result", static_cast(0)); + handle->setArgument("data_2", static_cast(15)); + + HooksManager::callCallouts(hookpt_two_index_, *handle); + + int result = 0; + handle->getArgument("result", result); + EXPECT_EQ(2054, result); + + // ... and check that the pre- and post- callout functions survive a + // reload. + EXPECT_TRUE(HooksManager::loadLibraries(library_names)); + handle = HooksManager::createCalloutHandle(); + + handle->setArgument("result", static_cast(0)); + handle->setArgument("data_2", static_cast(15)); + + HooksManager::callCallouts(hookpt_two_index_, *handle); + + // Expect same value i.e. 1027 * 2 + result = 0; + handle->getArgument("result", result); + EXPECT_EQ(2054, result); +} + +// Test with a shared manager the pre- and post- callout functions survive +// a reload but not with a not empty list of libraries + +TEST_F(HooksManagerTest, PrePostCalloutSharedNotEmpty) { + + HookLibsCollection library_names; + library_names.push_back(make_pair(std::string(FULL_CALLOUT_LIBRARY), + data::ConstElementPtr())); + + // Initialize the shared manager. + CalloutManager::getSharedManager().reset(new CalloutManager(0)); + + // Load the pre- and post- callouts. + HooksManager::preCalloutsLibraryHandle().registerCallout("hookpt_two", + testPreCallout); + HooksManager::postCalloutsLibraryHandle().registerCallout("hookpt_two", + testPostCallout); + + // With the pre- and post- callouts above, the result expected is + // + // 1027 * 2 + CalloutHandlePtr handle = HooksManager::createCalloutHandle(); + handle->setArgument("result", static_cast(0)); + handle->setArgument("data_2", static_cast(15)); + + HooksManager::callCallouts(hookpt_two_index_, *handle); + + int result = 0; + handle->getArgument("result", result); + EXPECT_EQ(2054, result); + + // ... and check that the pre- and post- callout functions don't survive a + // reload with a not empty list of libraries. + EXPECT_TRUE(HooksManager::loadLibraries(library_names)); + handle = HooksManager::createCalloutHandle(); + + handle->setArgument("result", static_cast(0)); + handle->setArgument("data_2", static_cast(15)); + + HooksManager::callCallouts(hookpt_two_index_, *handle); + + // Expect result - data_2 + result = 0; + handle->getArgument("result", result); + EXPECT_EQ(-15, result); +} + +// Test with a shared manager the pre- and post- callout functions don't +// survive a reload if the shared manager is initialized too late. + +TEST_F(HooksManagerTest, PrePostCalloutSharedTooLate) { + + HookLibsCollection library_names; + EXPECT_TRUE(HooksManager::loadLibraries(library_names)); + + // Initialize the shared manager (after loadLibraries so too late) + CalloutManager::getSharedManager().reset(new CalloutManager(0)); + + // Load the pre- and post- callouts. + HooksManager::preCalloutsLibraryHandle().registerCallout("hookpt_two", + testPreCallout); + HooksManager::postCalloutsLibraryHandle().registerCallout("hookpt_two", + testPostCallout); + + // With the pre- and post- callouts above, the result expected is + // + // 1027 * 2 + CalloutHandlePtr handle = HooksManager::createCalloutHandle(); + handle->setArgument("result", static_cast(0)); + handle->setArgument("data_2", static_cast(15)); + + HooksManager::callCallouts(hookpt_two_index_, *handle); + + int result = 0; + handle->getArgument("result", result); + EXPECT_EQ(2054, result); + + // ... and check that the pre- and post- callout functions don't survive a + // reload. + EXPECT_TRUE(HooksManager::loadLibraries(library_names)); + handle = HooksManager::createCalloutHandle(); + + handle->setArgument("result", static_cast(0)); + handle->setArgument("data_2", static_cast(15)); + + HooksManager::callCallouts(hookpt_two_index_, *handle); + + // Expect no change so result = 0 + result = 0; + handle->getArgument("result", result); + EXPECT_EQ(0, result); +} + // Check that everything works even with no libraries loaded. First that // calloutsPresent() always returns false.