diff --git a/ddsrouter_utils/include/ddsrouter_utils/collection/database/IWatchDataBase.hpp b/ddsrouter_utils/include/ddsrouter_utils/collection/database/IWatchDataBase.hpp index 025ea82f0..ddd0abc9e 100644 --- a/ddsrouter_utils/include/ddsrouter_utils/collection/database/IWatchDataBase.hpp +++ b/ddsrouter_utils/include/ddsrouter_utils/collection/database/IWatchDataBase.hpp @@ -37,7 +37,7 @@ class IWatchDataBase : public IDataBase virtual void register_modification_callback( const std::function& callback) = 0; - virtual void register_deletion_callback( + virtual void register_remove_callback( const std::function& callback) = 0; }; diff --git a/ddsrouter_utils/include/ddsrouter_utils/collection/database/StdWatchDataBase.hpp b/ddsrouter_utils/include/ddsrouter_utils/collection/database/StdWatchDataBase.hpp index 9af091b2b..f314ff81d 100644 --- a/ddsrouter_utils/include/ddsrouter_utils/collection/database/StdWatchDataBase.hpp +++ b/ddsrouter_utils/include/ddsrouter_utils/collection/database/StdWatchDataBase.hpp @@ -66,18 +66,27 @@ class StdWatchDataBase : public IWatchDataBase virtual void register_modification_callback( const CallbackType& callback) override; - virtual void register_deletion_callback( + virtual void register_remove_callback( const CallbackType& callback) override; - virtual void async_add(Key key, Value value); + virtual void add(Key key, Value value, bool sync_insertion, bool sync_callback_call, bool sync_all_callbacks); - virtual void async_modify(Key key, Value value); + virtual void modify(Key key, Value value, bool sync_insertion, bool sync_callback_call, bool sync_all_callbacks); - virtual void async_remove(Key key); + virtual void remove(Key key, bool sync_insertion, bool sync_callback_call, bool sync_all_callbacks); protected: - void call_callback_common_( + void sync_add_(const Key& key, const Value& value); + void sync_modify_(const Key& key, const Value& value); + void sync_remove_(const Key& key, const Value& value); + + void sync_call_callbacks_common_( + const Key& key, + const Value& value, + DataBaseActionKind action_kind); + + void call_callbacks_common_sync_( const Key& key, const Value& value, DataBaseActionKind action_kind); @@ -99,6 +108,7 @@ class StdWatchDataBase : public IWatchDataBase thread::SlotConnector add_slot_connector_; thread::SlotConnector modify_slot_connector_; thread::SlotConnector remove_slot_connector_; + thread::SlotConnector call_callbacks_slot_connector_; }; diff --git a/ddsrouter_utils/include/ddsrouter_utils/collection/database/impl/StdWatchDataBase.ipp b/ddsrouter_utils/include/ddsrouter_utils/collection/database/impl/StdWatchDataBase.ipp index d38bc17dd..1328a666a 100644 --- a/ddsrouter_utils/include/ddsrouter_utils/collection/database/impl/StdWatchDataBase.ipp +++ b/ddsrouter_utils/include/ddsrouter_utils/collection/database/impl/StdWatchDataBase.ipp @@ -157,10 +157,11 @@ void StdWatchDataBase::async_remove(Key key) } template -void StdWatchDataBase::call_callback_common_( +void StdWatchDataBase::call_callbacks_common_( const Key& key, const Value& value, - DataBaseActionKind action_kind) + DataBaseActionKind action_kind, + bool sync) { std::shared_lock lock_db(callbacks_[action_kind]); for (auto& slot : callbacks_[action_kind]) diff --git a/ddsrouter_utils/test/unittest/collection/database/CMakeLists.txt b/ddsrouter_utils/test/unittest/collection/database/CMakeLists.txt index 96e446195..ecfa7f529 100644 --- a/ddsrouter_utils/test/unittest/collection/database/CMakeLists.txt +++ b/ddsrouter_utils/test/unittest/collection/database/CMakeLists.txt @@ -28,15 +28,18 @@ set(TEST_SOURCES ) set(TEST_LIST - parametrize - add - modify - delete - exist - get - addition_callback - modification_callback - deletion_callback + basic_functionality_test + sync_callback_functionality_test + semisync_callback_functionality_test + # parametrize + # add + # modify + # delete + # exist + # get + # addition_callback + # modification_callback + # deletion_callback ) set(TEST_EXTRA_LIBRARIES diff --git a/ddsrouter_utils/test/unittest/collection/database/std_watch_data_base_tests.cpp b/ddsrouter_utils/test/unittest/collection/database/std_watch_data_base_tests.cpp index bfc4c961d..8021db86d 100644 --- a/ddsrouter_utils/test/unittest/collection/database/std_watch_data_base_tests.cpp +++ b/ddsrouter_utils/test/unittest/collection/database/std_watch_data_base_tests.cpp @@ -15,6 +15,7 @@ #include #include +#include #include #include @@ -32,10 +33,164 @@ std::shared_ptr create_thread_manager() } /* namespace test */ -TEST(StdWatchDataBaseTest, add) +TEST(StdWatchDataBaseTest, basic_functionality_test) { // Create Database StdWatchDataBase database(test::create_thread_manager()); + + // Add value + database.add(1, "number 1"); + + // Check that value exist + ASSERT_TRUE(database.exist(1)); + + // Get value + ASSERT_EQ(database.get(1), "number 1"); + + // Modify value + database.modify(1, "number 1 modified"); + + // Get value + ASSERT_EQ(database.get(1), "number 1 modified"); + + // Remove value + database.remove(1); + + // Check that value does not exist anymore + ASSERT_FALSE(database.exist(1)); +} + +TEST(StdWatchDataBaseTest, sync_callback_functionality_test) +{ + // Use "global" variables to know the arguments to the callback + std::pair addition_callback_result; + std::pair modification_callback_result; + std::pair remove_callback_result; + + // Create Database + StdWatchDataBase database(test::create_thread_manager()); + + // Add addition callback + database.register_addition_callback( + [&addition_callback_result] + (int key, std::string value) + { + addition_callback_result.first = key; + addition_callback_result.second = value; + } + ); + + // Add value + database.sync_add(1, "number 1"); + + // Wait for counter to be raised once and check the arguments + ASSERT_EQ(addition_callback_result.first, 1); + ASSERT_EQ(addition_callback_result.second, "number 1"); + + // Add modification callback + database.register_modification_callback( + [&modification_callback_result] + (int key, std::string value) + { + modification_callback_result.first = key; + modification_callback_result.second = value; + } + ); + + // Add value + database.sync_modify(1, "number 1 modified"); + + // Wait for counter to be raised once and check the arguments + ASSERT_EQ(modification_callback_result.first, 1); + ASSERT_EQ(modification_callback_result.second, "number 1 modified"); + + // Add remove callback + database.register_deletion_callback( + [&remove_callback_result] + (int key, std::string value) + { + remove_callback_result.first = key; + remove_callback_result.second = value; + } + ); + + // Add value + database.sync_remove(1); + + // Wait for counter to be raised once and check the arguments + ASSERT_EQ(remove_callback_result.first, 1); + ASSERT_EQ(remove_callback_result.second, "number 1 modified"); +} + +TEST(StdWatchDataBaseTest, semisync_callback_functionality_test) +{ + // Use "global" variables and a counter to know when the callback has been called and its arguments + eprosima::ddsrouter::event::IntWaitHandler counter(0); + std::pair addition_callback_result; + std::pair modification_callback_result; + std::pair remove_callback_result; + + // Create Database + StdWatchDataBase database(test::create_thread_manager()); + + // Add addition callback + database.register_addition_callback( + [&addition_callback_result, &counter] + (int key, std::string value) + { + addition_callback_result.first = key; + addition_callback_result.second = value; + ++counter; + } + ); + + // Add value + database.add(1, "number 1"); + + // Wait for counter to be raised once and check the arguments + counter.wait_equal(1); + ASSERT_EQ(addition_callback_result.first, 1); + ASSERT_EQ(addition_callback_result.second, "number 1"); + counter.set_value(0); + + // Add modification callback + database.register_modification_callback( + [&modification_callback_result, &counter] + (int key, std::string value) + { + modification_callback_result.first = key; + modification_callback_result.second = value; + ++counter; + } + ); + + // Add value + database.modify(1, "number 1 modified"); + + // Wait for counter to be raised once and check the arguments + counter.wait_equal(1); + ASSERT_EQ(modification_callback_result.first, 1); + ASSERT_EQ(modification_callback_result.second, "number 1 modified"); + counter.set_value(0); + + // Add remove callback + database.register_deletion_callback( + [&remove_callback_result, &counter] + (int key, std::string value) + { + remove_callback_result.first = key; + remove_callback_result.second = value; + ++counter; + } + ); + + // Add value + database.remove(1); + + // Wait for counter to be raised once and check the arguments + counter.wait_equal(1); + ASSERT_EQ(remove_callback_result.first, 1); + ASSERT_EQ(remove_callback_result.second, "number 1 modified"); } int main(