246 lines
7.4 KiB
C++
246 lines
7.4 KiB
C++
#include "catch/catch.hpp"
|
|
|
|
#include "lib/quickbus/quickbus.h"
|
|
#include "lib/quickbus/routing/Router.h"
|
|
|
|
#include <boost/signals2.hpp>
|
|
|
|
namespace bts {
|
|
int quickbus_tests = 0;
|
|
namespace qbus {
|
|
|
|
struct FakeNode : public INode {
|
|
|
|
FakeNode()
|
|
: on_advertise(),
|
|
on_unadvertise(),
|
|
on_subscribe (),
|
|
on_notify(),
|
|
on_unsubscribe ()
|
|
{}
|
|
|
|
void advertise(NodeID const& sender, TableID const&id, Advertisement const &ad) override
|
|
{on_advertise(sender,id,ad);}
|
|
|
|
void unadvertise(NodeID const&sender,TableID const&cid) override
|
|
{on_unadvertise(sender,cid);}
|
|
|
|
void subscribe (NodeID const&sender, TableID const&tid,SubscriptionID const&sid) override
|
|
{on_subscribe(sender,tid,sid);}
|
|
|
|
void notify(NodeID const&sender,TableID const&tid,SubscriptionID const&sid,Update const&u) override
|
|
{on_notify(sender,tid,sid,u);}
|
|
|
|
void unsubscribe (NodeID const&sender, TableID const&tid) override
|
|
{on_unsubscribe(sender, tid);}
|
|
|
|
boost::signals2::signal<void (NodeID const&,TableID const&id, Advertisement const &)> on_advertise;
|
|
boost::signals2::signal<void (NodeID const&,TableID const&ci)> on_unadvertise;
|
|
boost::signals2::signal<void (NodeID const&,TableID const&ti,SubscriptionID const&)> on_subscribe ;
|
|
boost::signals2::signal<void (NodeID const&,TableID const&,SubscriptionID const&sid,Update const&)> on_notify;
|
|
boost::signals2::signal<void (NodeID const&,TableID const&ti)> on_unsubscribe ;
|
|
};
|
|
|
|
TEST_CASE ("Quickbus QBRouter_Tests" , "[quickbus]")
|
|
{
|
|
SECTION("advertisments_are_forwarded")
|
|
{
|
|
NodeID r_id("src");
|
|
Router r(r_id);
|
|
|
|
NodeID adv_source_id ("adv_source");
|
|
auto adv_source = std::make_shared<FakeNode>();
|
|
adv_source->on_advertise.connect([] (NodeID const&, TableID const&, Advertisement const &)
|
|
{REQUIRE(false);});
|
|
|
|
NodeID adv_sink1_id ("adv_sink1");
|
|
auto adv_sink1 = std::make_shared<FakeNode>();
|
|
bool s1_got_it = false;
|
|
|
|
adv_sink1->on_advertise.connect(
|
|
[r_id,&s1_got_it] (NodeID const&sender, TableID const&,Advertisement const&) {
|
|
REQUIRE(sender == r_id);
|
|
s1_got_it = true;
|
|
});
|
|
|
|
NodeID adv_sink2_id ("adv_sink2");
|
|
auto adv_sink2 = std::make_shared<FakeNode>();
|
|
bool s2_got_it = false;
|
|
|
|
adv_sink2->on_advertise.connect(
|
|
[r_id,&s2_got_it] (NodeID const&sender, TableID const&, const Advertisement &) {
|
|
REQUIRE(sender == r_id);
|
|
s2_got_it = true;
|
|
});
|
|
|
|
|
|
r.add_neighbour(adv_source_id,adv_source);
|
|
r.add_neighbour(adv_sink1_id,adv_sink1);
|
|
r.add_neighbour(adv_sink2_id,adv_sink2);
|
|
|
|
auto tid = make_table_id();
|
|
Advertisement ad(Topic("hello"));
|
|
r.advertise(adv_source_id,tid, ad);
|
|
REQUIRE(s1_got_it);
|
|
REQUIRE(s2_got_it);
|
|
}
|
|
|
|
SECTION("late_joiners_receive_advertisments")
|
|
{
|
|
NodeID r_id("src");
|
|
Router r(r_id);
|
|
|
|
NodeID adv_source_id ("adv_source");
|
|
auto adv_source=std::make_shared<FakeNode>();
|
|
adv_source->on_advertise.connect(
|
|
[] (NodeID const&, TableID const&, Advertisement const &)
|
|
{REQUIRE(false);});
|
|
|
|
NodeID adv_sink1_id ("adv_sink1");
|
|
auto adv_sink1 = std::make_shared<FakeNode>();
|
|
bool s1_got_it = false;
|
|
adv_sink1->on_advertise.connect(
|
|
[r_id,&s1_got_it] (NodeID const&sender, TableID const&, Advertisement const &) {
|
|
REQUIRE(sender == r_id);
|
|
s1_got_it = true;
|
|
});
|
|
|
|
auto tid = make_table_id();
|
|
Advertisement ad(Topic("hello"));
|
|
|
|
r.add_neighbour(adv_source_id,adv_source);
|
|
r.advertise(adv_source_id,tid, ad);
|
|
r.add_neighbour(adv_sink1_id,adv_sink1);
|
|
REQUIRE(s1_got_it);
|
|
}
|
|
|
|
SECTION("removed_nodes_clean_advertisdements")
|
|
{
|
|
NodeID r_id("src");
|
|
Router r(r_id);
|
|
|
|
NodeID adv_source_id ("adv_source");
|
|
auto adv_source = std::make_shared<FakeNode>();
|
|
adv_source->on_advertise.connect(
|
|
[] (NodeID const&, TableID const&, Advertisement const &)
|
|
{REQUIRE(false);});
|
|
|
|
NodeID adv_sink1_id ("adv_sink1");
|
|
auto adv_sink1 = std::make_shared<FakeNode>();
|
|
bool s1_got_it = false;
|
|
adv_sink1->on_advertise.connect(
|
|
[r_id,&s1_got_it] (NodeID const&sender, TableID const&, Advertisement const &) {
|
|
REQUIRE(sender == r_id);
|
|
s1_got_it = true;
|
|
});
|
|
|
|
adv_sink1->on_unadvertise.connect(
|
|
[r_id,&s1_got_it] (NodeID const&sender, TableID const&) {
|
|
REQUIRE(sender == r_id);
|
|
s1_got_it = false;
|
|
});
|
|
|
|
auto tid = make_table_id();
|
|
Advertisement ad(Topic("hello"));
|
|
|
|
r.add_neighbour(adv_source_id,adv_source);
|
|
r.advertise(adv_source_id,tid, ad);
|
|
r.add_neighbour(adv_sink1_id,adv_sink1);
|
|
REQUIRE(s1_got_it);
|
|
|
|
r.remove_neighbour(adv_source_id);
|
|
REQUIRE(!s1_got_it);
|
|
}
|
|
|
|
SECTION("subs_are_forwarded")
|
|
{
|
|
NodeID r_id("src");
|
|
Router r(r_id);
|
|
|
|
auto pid = make_node_id();
|
|
Advertisement ad(Topic("hello"));
|
|
auto tid = make_table_id();
|
|
auto producer = std::make_shared<FakeNode>();
|
|
bool p1_got_sub=false;
|
|
producer->on_subscribe.connect([tid,&p1_got_sub] (NodeID const&,TableID const&stid,SubscriptionID const&sid) {
|
|
REQUIRE(tid == stid);
|
|
REQUIRE(sid.value() == 1);
|
|
p1_got_sub =true;
|
|
});
|
|
|
|
auto pid2 = make_node_id();
|
|
auto tid2 = make_table_id();
|
|
auto producer2 = std::make_shared<FakeNode>();
|
|
producer2->on_subscribe.connect([] (NodeID const&, TableID const&,SubscriptionID const&) {
|
|
REQUIRE(false);
|
|
});
|
|
|
|
auto sid = make_node_id();
|
|
auto consumer = std::make_shared<FakeNode>();
|
|
|
|
|
|
r.add_neighbour(pid,producer);
|
|
r.advertise(pid,tid, ad);
|
|
|
|
r.add_neighbour(pid2,producer2);
|
|
r.advertise(pid2,tid2,ad);
|
|
|
|
r.add_neighbour(sid,consumer);
|
|
r.subscribe(sid,tid,SubscriptionID(1));
|
|
REQUIRE(p1_got_sub);
|
|
}
|
|
|
|
SECTION("unsubs_are_forwarded")
|
|
{
|
|
NodeID r_id("src");
|
|
Router r(r_id);
|
|
|
|
auto pid = make_node_id();
|
|
auto tid = make_table_id();
|
|
auto producer = std::make_shared<FakeNode>();
|
|
bool p1_got_sub=false;
|
|
|
|
producer->on_subscribe.connect([tid,&p1_got_sub] (NodeID const&,TableID const&stid,const SubscriptionID&sid) {
|
|
REQUIRE(tid == stid);
|
|
REQUIRE(sid.value() == 1);
|
|
p1_got_sub =true;
|
|
});
|
|
|
|
producer->on_unsubscribe.connect([tid,&p1_got_sub] (NodeID const&,TableID const&stid) {
|
|
REQUIRE(tid == stid);
|
|
p1_got_sub =false;
|
|
});
|
|
|
|
auto pid2 = make_node_id();
|
|
auto tid2 = make_table_id();
|
|
auto producer2 = std::make_shared<FakeNode>();
|
|
producer2->on_subscribe.connect([] (NodeID const&, TableID const&,const SubscriptionID&) {
|
|
REQUIRE(false);
|
|
});
|
|
|
|
producer2->on_unsubscribe.connect([] (NodeID const&, TableID const&) {
|
|
REQUIRE(false);
|
|
});
|
|
|
|
auto sid = make_node_id();
|
|
auto consumer = std::make_shared<FakeNode>();
|
|
|
|
|
|
r.add_neighbour(pid,producer);
|
|
Advertisement ad(Topic("hello"));
|
|
r.advertise(pid,tid, ad);
|
|
|
|
r.add_neighbour(pid2,producer2);
|
|
r.advertise(pid2,tid2,ad);
|
|
|
|
r.add_neighbour(sid,consumer);
|
|
r.subscribe(sid,tid,SubscriptionID(1));
|
|
REQUIRE(p1_got_sub);
|
|
r.unsubscribe(sid,tid);
|
|
REQUIRE(!p1_got_sub);
|
|
}
|
|
}
|
|
|
|
|
|
}}
|