FairMQ: Rewrite event manager to support multiple subscribers

This commit is contained in:
Dennis Klein
2017-09-20 21:23:21 +02:00
committed by Mohammad Al-Turany
parent 88b3b8ef18
commit 6ecd0e9085
6 changed files with 233 additions and 1 deletions

View File

@@ -142,6 +142,15 @@ add_testsuite(FairMQ.PluginServices
TIMEOUT 10
)
add_testsuite(FairMQ.EventManager
SOURCES
event_manager/runner.cxx
event_manager/_event_manager.cxx
LINKS FairMQ
TIMEOUT 10
)
##############################
# Aggregate all test targets #
##############################

View File

@@ -0,0 +1,64 @@
/********************************************************************************
* Copyright (C) 2017 GSI Helmholtzzentrum fuer Schwerionenforschung GmbH *
* *
* This software is distributed under the terms of the *
* GNU Lesser General Public Licence (LGPL) version 3, *
* copied verbatim in the file "LICENSE" *
********************************************************************************/
#include <gtest/gtest.h>
#include <fairmq/EventManager.h>
#include <string>
namespace
{
using namespace std;
using namespace fair::mq;
struct TestEvent : fair::mq::Event<std::string> {};
TEST(EventManager, Basics)
{
EventManager mgr{};
int call_counter = 0;
int call_counter2 = 0;
int value = 0;
string value2;
EventManager::Callback<TestEvent, int> callback{
[&](TestEvent::KeyType& key, int newValue){
++call_counter;
if (key == "test") value = newValue;
}
};
EventManager::Callback<TestEvent, string> callback2{
[&](TestEvent::KeyType& key, string newValue){
++call_counter2;
if (key == "test") value2 = newValue;
}
};
mgr.Subscribe<TestEvent, int>("foo_subscriber", callback);
// double subscription will automatically unsubscribe first
mgr.Subscribe<TestEvent, int>("foo_subscriber", callback);
mgr.Emit<TestEvent>(TestEvent::KeyType{"test"}, 42);
ASSERT_EQ(call_counter, 1);
ASSERT_EQ(value, 42);
mgr.Unsubscribe<TestEvent, int>("foo_subscriber");
mgr.Emit<TestEvent>(TestEvent::KeyType{"test"}, 13);
ASSERT_EQ(call_counter, 1);
mgr.Subscribe<TestEvent, int>("foo_subscriber", callback);
mgr.Subscribe<TestEvent, string>("foo_subscriber", callback2);
// two different callbacks:
mgr.Emit<TestEvent>(TestEvent::KeyType{"test"}, 9000);
mgr.Emit<TestEvent>(TestEvent::KeyType{"test"}, string{"over 9000"});
ASSERT_EQ(call_counter, 2);
ASSERT_EQ(value, 9000);
ASSERT_EQ(call_counter2, 1);
ASSERT_EQ(value2, "over 9000");
}
} // namespace

View File

@@ -0,0 +1,16 @@
/********************************************************************************
* Copyright (C) 2017 GSI Helmholtzzentrum fuer Schwerionenforschung GmbH *
* *
* This software is distributed under the terms of the *
* GNU Lesser General Public Licence (LGPL) version 3, *
* copied verbatim in the file "LICENSE" *
********************************************************************************/
#include <gtest/gtest.h>
auto main(int argc, char** argv) -> int
{
::testing::InitGoogleTest(&argc, argv);
::testing::FLAGS_gtest_death_test_style = "threadsafe";
return RUN_ALL_TESTS();
}