Skip to content

Commit

Permalink
tmp implement async callbacks
Browse files Browse the repository at this point in the history
Signed-off-by: jparisu <javierparis@eprosima.com>
  • Loading branch information
jparisu committed Sep 28, 2022
1 parent 588ce38 commit 7a3702b
Show file tree
Hide file tree
Showing 5 changed files with 187 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class IWatchDataBase : public IDataBase<Key, Value>
virtual void register_modification_callback(
const std::function<void(Key, Value)>& callback) = 0;

virtual void register_deletion_callback(
virtual void register_remove_callback(
const std::function<void(Key, Value)>& callback) = 0;

};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,18 +66,27 @@ class StdWatchDataBase : public IWatchDataBase<Key, Value>
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_call);

virtual void async_modify(Key key, Value value);
virtual void modify(Key key, Value value, bool sync_insertion, bool sync_call);

virtual void async_remove(Key key);
virtual void remove(Key key, bool sync_insertion, bool sync_call);

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);
Expand All @@ -99,6 +108,7 @@ class StdWatchDataBase : public IWatchDataBase<Key, Value>
thread::SlotConnector<Key, Value> add_slot_connector_;
thread::SlotConnector<Key, Value> modify_slot_connector_;
thread::SlotConnector<Key> remove_slot_connector_;
thread::SlotConnector<Key, Value, DataBaseActionKind> call_callbacks_slot_connector_;

};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,10 +157,11 @@ void StdWatchDataBase<Key, Value>::async_remove(Key key)
}

template <typename Key, typename Value>
void StdWatchDataBase<Key, Value>::call_callback_common_(
void StdWatchDataBase<Key, Value>::call_callbacks_common_(
const Key& key,
const Value& value,
DataBaseActionKind action_kind)
DataBaseActionKind action_kind,
bool sync)
{
std::shared_lock<std::shared_timed_mutex> lock_db(callbacks_[action_kind]);
for (auto& slot : callbacks_[action_kind])
Expand Down
21 changes: 12 additions & 9 deletions ddsrouter_utils/test/unittest/collection/database/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include <gtest_aux.hpp>
#include <gtest/gtest.h>

#include <ddsrouter_utils/wait/IntWaitHandler.hpp>
#include <ddsrouter_utils/collection/database/StdWatchDataBase.hpp>
#include <ddsrouter_utils/thread/manager/StdThreadPool.hpp>

Expand All @@ -32,10 +33,164 @@ std::shared_ptr<thread::IManager> create_thread_manager()

} /* namespace test */

TEST(StdWatchDataBaseTest, add)
TEST(StdWatchDataBaseTest, basic_functionality_test)
{
// Create Database
StdWatchDataBase<int, std::string> 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<int, std::string> addition_callback_result;
std::pair<int, std::string> modification_callback_result;
std::pair<int, std::string> remove_callback_result;

// Create Database
StdWatchDataBase<int, std::string> 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<int, std::string> addition_callback_result;
std::pair<int, std::string> modification_callback_result;
std::pair<int, std::string> remove_callback_result;

// Create Database
StdWatchDataBase<int, std::string> 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(
Expand Down

0 comments on commit 7a3702b

Please sign in to comment.