264 lines
8.6 KiB
C++
264 lines
8.6 KiB
C++
#include "catch/catch.hpp"
|
|
#include "lib/config/config.h"
|
|
#include "lib/os/os.h"
|
|
#include "lib/quickbus/quickbus.h"
|
|
#include "lib/utils/bts_chrono.h"
|
|
|
|
#include <chrono>
|
|
#include <future>
|
|
|
|
namespace bts {
|
|
int qb_pubsub_tests;
|
|
namespace qbus {
|
|
|
|
TEST_CASE("Quickbus Pubsub Tests", "[quickbus]") {
|
|
auto parent_port = os::get_random_port();
|
|
|
|
SECTION("smoke") {
|
|
auto pcfg = config::get<config::QuickbusCfg>().node();
|
|
pcfg.set_my_port(parent_port);
|
|
Quickbus parent(pcfg);
|
|
|
|
auto ccfg = config::get<config::QuickbusCfg>().node();
|
|
ccfg.set_parent_host("127.0.0.1");
|
|
ccfg.set_reconnect_period_secs(1);
|
|
ccfg.set_parent_port(parent_port);
|
|
Quickbus child(ccfg);
|
|
|
|
TablePublisher pub(parent, Topic("A"));
|
|
std::promise<void> p;
|
|
auto f = p.get_future();
|
|
TableSubscriber sub(
|
|
child, equal_topic(Topic("A")), [&p](TableEvent const &ev) {
|
|
if (ev.event_type == TableEvent::TABLE_UPDATED) {
|
|
REQUIRE(ev.update);
|
|
REQUIRE(ev.update->size() == 1);
|
|
auto change = *ev.update->begin();
|
|
REQUIRE(change.type == Change::CREATE);
|
|
REQUIRE(change.key == RecordKey(1));
|
|
REQUIRE(*change.value == Record("B"));
|
|
p.set_value();
|
|
}
|
|
});
|
|
|
|
pub.publish(
|
|
{{Change::CREATE, RecordKey(1), std::make_shared<Record>("B")}});
|
|
REQUIRE(
|
|
f.wait_for(std::chrono::seconds(15)) == std::future_status::ready);
|
|
}
|
|
|
|
SECTION("quickbus_late_join_works") {
|
|
auto pcfg = config::get<config::QuickbusCfg>().node();
|
|
pcfg.set_my_port(parent_port);
|
|
Quickbus parent(pcfg);
|
|
|
|
auto ccfg = config::get<config::QuickbusCfg>().node();
|
|
ccfg.set_parent_host("127.0.0.1");
|
|
ccfg.set_reconnect_period_secs(1);
|
|
ccfg.set_parent_port(parent_port);
|
|
Quickbus child(ccfg);
|
|
|
|
TablePublisher pub(parent, Topic("A"));
|
|
std::promise<void> p;
|
|
auto f = p.get_future();
|
|
|
|
TableSubscriber sub(
|
|
child, equal_topic(Topic("A")), [&p](TableEvent const &ev) {
|
|
if (ev.event_type == TableEvent::TABLE_UPDATED) {
|
|
p.set_value();
|
|
}
|
|
});
|
|
|
|
pub.publish(
|
|
{{Change::CREATE, RecordKey(1), std::make_shared<Record>("B")}});
|
|
REQUIRE(
|
|
f.wait_for(std::chrono::seconds(15)) == std::future_status::ready);
|
|
|
|
Quickbus child2(ccfg);
|
|
std::promise<void> p2;
|
|
auto f2 = p2.get_future();
|
|
TableSubscriber sub2(
|
|
child2, equal_topic(Topic("A")), [&p2](TableEvent const &ev) {
|
|
if (ev.event_type == TableEvent::TABLE_UPDATED) {
|
|
p2.set_value();
|
|
}
|
|
});
|
|
REQUIRE(
|
|
f2.wait_for(std::chrono::seconds(15)) == std::future_status::ready);
|
|
}
|
|
|
|
SECTION("subscriber_late_join_work") {
|
|
auto pcfg = config::get<config::QuickbusCfg>().node();
|
|
pcfg.set_my_port(parent_port);
|
|
Quickbus parent(pcfg);
|
|
|
|
auto ccfg = config::get<config::QuickbusCfg>().node();
|
|
ccfg.set_parent_host("127.0.0.1");
|
|
ccfg.set_reconnect_period_secs(1);
|
|
ccfg.set_parent_port(parent_port);
|
|
Quickbus child(ccfg);
|
|
|
|
TablePublisher pub(parent, Topic("A"));
|
|
std::promise<void> p;
|
|
auto f = p.get_future();
|
|
TableSubscriber sub(
|
|
child, equal_topic(Topic("A")), [&p](TableEvent const &ev) {
|
|
if (ev.event_type == TableEvent::TABLE_UPDATED) {
|
|
p.set_value();
|
|
}
|
|
});
|
|
|
|
pub.publish(
|
|
{{Change::CREATE, RecordKey(1), std::make_shared<Record>("B")}});
|
|
REQUIRE(
|
|
f.wait_for(std::chrono::seconds(15)) == std::future_status::ready);
|
|
|
|
std::promise<void> p2;
|
|
auto f2 = p2.get_future();
|
|
TableSubscriber sub2(
|
|
child, equal_topic(Topic("A")), [&p2](TableEvent const &ev) {
|
|
if (ev.event_type == TableEvent::TABLE_UPDATED) {
|
|
p2.set_value();
|
|
}
|
|
});
|
|
REQUIRE(
|
|
f2.wait_for(std::chrono::seconds(15)) == std::future_status::ready);
|
|
}
|
|
|
|
SECTION("filtering_work") {
|
|
auto pcfg = config::get<config::QuickbusCfg>().node();
|
|
pcfg.set_my_port(parent_port);
|
|
Quickbus parent(pcfg);
|
|
|
|
auto ccfg = config::get<config::QuickbusCfg>().node();
|
|
ccfg.set_parent_host("127.0.0.1");
|
|
ccfg.set_reconnect_period_secs(1);
|
|
ccfg.set_parent_port(parent_port);
|
|
Quickbus child(ccfg);
|
|
|
|
TablePublisher pub(parent, Topic("A"));
|
|
TableSubscriber sub1(
|
|
child, equal_topic(Topic("B")),
|
|
[](TableEvent const &) { REQUIRE(false); });
|
|
|
|
pub.publish(
|
|
{{Change::CREATE, RecordKey(1), std::make_shared<Record>("B")}});
|
|
std::promise<void> p;
|
|
auto f = p.get_future();
|
|
TableSubscriber sub2(
|
|
child, equal_topic(Topic("A")), [&p](TableEvent const &ev) {
|
|
if (ev.event_type == TableEvent::TABLE_UPDATED)
|
|
p.set_value();
|
|
});
|
|
|
|
std::this_thread::sleep_for(2s);
|
|
REQUIRE(
|
|
f.wait_for(std::chrono::seconds(15)) == std::future_status::ready);
|
|
}
|
|
|
|
SECTION("unsubscribe_work") {
|
|
auto pcfg = config::get<config::QuickbusCfg>().node();
|
|
pcfg.set_my_port(parent_port);
|
|
Quickbus parent(pcfg);
|
|
|
|
auto ccfg = config::get<config::QuickbusCfg>().node();
|
|
ccfg.set_parent_host("127.0.0.1");
|
|
ccfg.set_reconnect_period_secs(1);
|
|
ccfg.set_parent_port(parent_port);
|
|
Quickbus child(ccfg);
|
|
|
|
TablePublisher pub(parent, Topic("A"));
|
|
std::promise<void> p;
|
|
auto f = p.get_future();
|
|
TableSubscriber sub(
|
|
child, equal_topic(Topic("A")), [&p](TableEvent const &ev) {
|
|
if (ev.event_type == TableEvent::TABLE_UPDATED) {
|
|
REQUIRE(ev.update);
|
|
REQUIRE(ev.update->size() == 1);
|
|
auto change = *ev.update->begin();
|
|
REQUIRE(change.type == Change::CREATE);
|
|
REQUIRE(change.key == RecordKey(1));
|
|
REQUIRE(*change.value == Record("B"));
|
|
p.set_value();
|
|
}
|
|
});
|
|
|
|
pub.publish(
|
|
{{Change::CREATE, RecordKey(1), std::make_shared<Record>("B")}});
|
|
|
|
REQUIRE(
|
|
f.wait_for(std::chrono::seconds(15)) == std::future_status::ready);
|
|
sub.disconnect();
|
|
|
|
pub.publish(
|
|
{{Change::REPLACE, RecordKey(1), std::make_shared<Record>("C")}});
|
|
std::this_thread::sleep_for(2s);
|
|
}
|
|
|
|
SECTION("force_singleton_topics_work") {
|
|
Quickbus qb{config::get<config::QuickbusCfg>().node()};
|
|
|
|
TablePublisher pub1(qb, Topic("A"));
|
|
TablePublisher pub2(qb, Topic("A"));
|
|
|
|
std::promise<void> p1;
|
|
bool sub1_got_a = false;
|
|
bool sub1_got_b = false;
|
|
auto f1 = p1.get_future();
|
|
TableSubscriber sub1(
|
|
qb, equal_topic(Topic("A")),
|
|
[&p1, &sub1_got_b, &sub1_got_a](TableEvent const &ev) {
|
|
if (ev.event_type == TableEvent::TABLE_UPDATED) {
|
|
REQUIRE(ev.update);
|
|
REQUIRE(ev.update->size() == 1);
|
|
auto change = *ev.update->begin();
|
|
REQUIRE(change.type == Change::CREATE);
|
|
REQUIRE(change.key == RecordKey(1));
|
|
if (*change.value == Record("B"))
|
|
sub1_got_b = true;
|
|
if (*change.value == Record("A"))
|
|
sub1_got_a = true;
|
|
if (sub1_got_a && sub1_got_b)
|
|
p1.set_value();
|
|
}
|
|
});
|
|
|
|
std::promise<void> p2;
|
|
bool sub2_got_a = false;
|
|
bool sub2_got_b = false;
|
|
auto f2 = p2.get_future();
|
|
TableSubscriber sub2(
|
|
qb, equal_topic(Topic("A")),
|
|
[&sub2_got_b, &sub2_got_a](TableEvent const &ev) {
|
|
if (ev.event_type == TableEvent::TABLE_UPDATED) {
|
|
REQUIRE(ev.update);
|
|
REQUIRE(ev.update->size() == 1);
|
|
auto change = *ev.update->begin();
|
|
REQUIRE(change.type == Change::CREATE);
|
|
REQUIRE(change.key == RecordKey(1));
|
|
if (*change.value == Record("B"))
|
|
sub2_got_b = true;
|
|
if (*change.value == Record("A"))
|
|
sub2_got_a = true;
|
|
}
|
|
},
|
|
true);
|
|
|
|
pub1.publish(
|
|
{{Change::CREATE, RecordKey(1), std::make_shared<Record>("B")}});
|
|
pub2.publish(
|
|
{{Change::CREATE, RecordKey(1), std::make_shared<Record>("A")}});
|
|
|
|
REQUIRE(
|
|
f1.wait_for(std::chrono::seconds(15)) == std::future_status::ready);
|
|
std::this_thread::sleep_for(2s);
|
|
|
|
REQUIRE(sub1_got_a);
|
|
REQUIRE(sub1_got_b);
|
|
REQUIRE(sub2_got_a != sub2_got_b);
|
|
}
|
|
}
|
|
|
|
} // namespace qbus
|
|
} // namespace bts
|