292 lines
8.5 KiB
C++
292 lines
8.5 KiB
C++
#include "spark/config/config.h"
|
|
#include "spark/config/protos/quickbus_cfg.pb.h"
|
|
#include "spark/quickbus/quickbus.h"
|
|
#include "spark/utils/TimeStamp.h"
|
|
#include "spark/utils/benchmarking_utils.h"
|
|
#include "system/os.h"
|
|
|
|
#include <atomic>
|
|
#include <catch2/catch_all.hpp>
|
|
#include <chrono>
|
|
#include <future>
|
|
|
|
namespace bts {
|
|
int qb_rpc_tests;
|
|
namespace qbus {
|
|
|
|
double measure_latency(RPCClient &cli) {
|
|
REQUIRE(cli.wait_until_connected(25));
|
|
|
|
std::promise<void> p;
|
|
auto f = p.get_future();
|
|
|
|
std::vector<double> times;
|
|
times.reserve(10000);
|
|
utils::TimeStamp rpc_begin;
|
|
auto msg = std::make_shared<Record>("hello");
|
|
|
|
OnRPCReply on_reply;
|
|
|
|
on_reply = [&](RPCError er, RPCReplyPtr const &) {
|
|
REQUIRE(er == RPCError::RPC_OK);
|
|
times.push_back((utils::TimeStamp::now() - rpc_begin).nanoseconds());
|
|
if (times.size() == 1000)
|
|
p.set_value();
|
|
else {
|
|
rpc_begin = utils::TimeStamp::now();
|
|
cli.send(msg, on_reply);
|
|
}
|
|
};
|
|
rpc_begin = utils::TimeStamp::now();
|
|
cli.send(msg, on_reply);
|
|
REQUIRE(f.wait_for(std::chrono::seconds(5)) == std::future_status::ready);
|
|
return utils::median(times) / 1000;
|
|
}
|
|
|
|
TEST_CASE("Quickbus RPC Tests", "[quickbus]") {
|
|
auto qb_port = os::get_random_port();
|
|
|
|
SECTION("smoke") {
|
|
auto cfg = config::get<config::QuickbusCfg>().node();
|
|
cfg.set_my_port(qb_port);
|
|
Quickbus qb(cfg);
|
|
auto sn = RPCServiceName("test");
|
|
RPCClient cli(qb, sn);
|
|
|
|
RPCServer server(
|
|
qb, sn, [](RPCRequestPtr const &req, const ReplyHandler &on_reply) {
|
|
on_reply(req);
|
|
});
|
|
|
|
REQUIRE(cli.wait_until_connected(15));
|
|
|
|
std::promise<void> p;
|
|
auto f = p.get_future();
|
|
cli.send(
|
|
std::make_shared<Record>("hello"),
|
|
[&p](RPCError er, RPCReplyPtr const &ptr) {
|
|
REQUIRE(er == RPCError::RPC_OK);
|
|
REQUIRE(ptr);
|
|
REQUIRE(ptr->value() == "hello");
|
|
p.set_value();
|
|
});
|
|
REQUIRE(f.wait_for(std::chrono::seconds(5)) == std::future_status::ready);
|
|
}
|
|
|
|
SECTION("no_server_is_reported") {
|
|
auto cfg = config::get<config::QuickbusCfg>().node();
|
|
cfg.set_my_port(qb_port);
|
|
Quickbus qb(cfg);
|
|
auto sn = RPCServiceName("test");
|
|
RPCClient cli(qb, sn);
|
|
|
|
std::promise<void> p;
|
|
auto f = p.get_future();
|
|
|
|
cli.send(
|
|
std::make_shared<Record>("hello"),
|
|
[&p](RPCError er, RPCReplyPtr const &ptr) {
|
|
REQUIRE(er == RPCError::NO_SERVER);
|
|
REQUIRE(!ptr);
|
|
p.set_value();
|
|
});
|
|
REQUIRE(
|
|
f.wait_for(std::chrono::seconds(15)) == std::future_status::ready);
|
|
}
|
|
|
|
SECTION("failure_with_in_flight_is_reported") {
|
|
auto cfg = config::get<config::QuickbusCfg>().node();
|
|
cfg.set_my_port(qb_port);
|
|
Quickbus qb(cfg);
|
|
auto sn = RPCServiceName("test");
|
|
RPCClient cli(qb, sn);
|
|
|
|
RPCServer server(
|
|
qb, sn, [](RPCRequestPtr const &, const ReplyHandler &) {});
|
|
|
|
REQUIRE(cli.wait_until_connected(15));
|
|
|
|
std::promise<void> p;
|
|
auto f = p.get_future();
|
|
cli.send(
|
|
std::make_shared<Record>("hello"),
|
|
[&p](RPCError er, RPCReplyPtr const &ptr) {
|
|
REQUIRE(er == RPCError::SERVER_DISCONNECT);
|
|
REQUIRE(!ptr);
|
|
p.set_value();
|
|
});
|
|
server.disconnect();
|
|
REQUIRE(f.wait_for(std::chrono::seconds(5)) == std::future_status::ready);
|
|
}
|
|
|
|
SECTION("failure_with_no_in_flight_is_hidden") {
|
|
auto cfg = config::get<config::QuickbusCfg>().node();
|
|
cfg.set_my_port(qb_port);
|
|
Quickbus qb(cfg);
|
|
auto sn = RPCServiceName("test");
|
|
RPCClient cli(qb, sn);
|
|
|
|
RPCServer server(
|
|
qb, sn, [](RPCRequestPtr const &req, const ReplyHandler &on_reply) {
|
|
on_reply(req);
|
|
});
|
|
|
|
REQUIRE(cli.wait_until_connected(15));
|
|
|
|
std::promise<void> p;
|
|
auto f = p.get_future();
|
|
cli.send(
|
|
std::make_shared<Record>("hello"),
|
|
[&p](RPCError er, RPCReplyPtr const &ptr) {
|
|
REQUIRE(er == RPCError::RPC_OK);
|
|
REQUIRE(ptr);
|
|
REQUIRE(ptr->value() == "hello");
|
|
p.set_value();
|
|
});
|
|
REQUIRE(f.wait_for(std::chrono::seconds(5)) == std::future_status::ready);
|
|
|
|
// got first reply, now kill the server and replace it
|
|
server.disconnect();
|
|
REQUIRE(cli.wait_until_disconnected(5));
|
|
server = RPCServer(
|
|
qb, sn, [](RPCRequestPtr const &req, const ReplyHandler &on_reply) {
|
|
on_reply(std::make_shared<Record>(req->value() + "_2"));
|
|
});
|
|
|
|
// second call should be replied by the second server
|
|
REQUIRE(cli.wait_until_connected(15));
|
|
std::promise<void> p2;
|
|
auto f2 = p2.get_future();
|
|
cli.send(
|
|
std::make_shared<Record>("hello"),
|
|
[&p2](RPCError er, RPCReplyPtr const &ptr) {
|
|
REQUIRE(er == RPCError::RPC_OK);
|
|
REQUIRE(ptr);
|
|
REQUIRE(ptr->value() == "hello_2");
|
|
p2.set_value();
|
|
});
|
|
REQUIRE(
|
|
f2.wait_for(std::chrono::seconds(5)) == std::future_status::ready);
|
|
}
|
|
|
|
SECTION("replies_are_matched_to_requests") {
|
|
auto cfg = config::get<config::QuickbusCfg>().node();
|
|
cfg.set_my_port(qb_port);
|
|
Quickbus qb(cfg);
|
|
auto sn = RPCServiceName("test");
|
|
|
|
RPCClient cli(qb, sn);
|
|
|
|
RPCServer server(
|
|
qb, sn, [](RPCRequestPtr const &req, const ReplyHandler &on_reply) {
|
|
on_reply(req);
|
|
});
|
|
|
|
REQUIRE(cli.wait_until_connected(15));
|
|
std::vector<std::promise<void>> pis(100);
|
|
|
|
for (size_t i = 0; i < pis.size(); i++) {
|
|
auto &p = pis[i];
|
|
auto token = "hello_" + std::to_string(i);
|
|
|
|
cli.send(
|
|
std::make_shared<Record>(token),
|
|
[&p, token](RPCError er, RPCReplyPtr const &ptr) {
|
|
REQUIRE(er == RPCError::RPC_OK);
|
|
REQUIRE(ptr);
|
|
REQUIRE(ptr->value() == token);
|
|
p.set_value();
|
|
});
|
|
}
|
|
|
|
for (auto &p : pis) {
|
|
auto f = p.get_future();
|
|
REQUIRE(
|
|
f.wait_for(std::chrono::seconds(5)) == std::future_status::ready);
|
|
}
|
|
}
|
|
|
|
SECTION("replies_are_matched_to_clients") {
|
|
auto cfg = config::get<config::QuickbusCfg>().node();
|
|
cfg.set_my_port(qb_port);
|
|
Quickbus qb(cfg);
|
|
auto sn = RPCServiceName("test");
|
|
|
|
std::vector<RPCClient> clis(100);
|
|
for (auto &cli : clis)
|
|
cli = RPCClient(qb, sn);
|
|
|
|
RPCServer server(
|
|
qb, sn, [](RPCRequestPtr const &req, const ReplyHandler &on_reply) {
|
|
on_reply(req);
|
|
});
|
|
|
|
for (auto &cli : clis)
|
|
REQUIRE(cli.wait_until_connected(15));
|
|
|
|
std::vector<std::promise<void>> pis(clis.size());
|
|
|
|
for (size_t i = 0; i < clis.size(); i++) {
|
|
auto &p = pis[i];
|
|
auto &cli = clis[i];
|
|
auto token = "hello_" + std::to_string(i);
|
|
|
|
cli.send(
|
|
std::make_shared<Record>(token),
|
|
[&p, token](RPCError er, RPCReplyPtr const &ptr) {
|
|
REQUIRE(er == RPCError::RPC_OK);
|
|
REQUIRE(ptr);
|
|
REQUIRE(ptr->value() == token);
|
|
p.set_value();
|
|
});
|
|
}
|
|
|
|
for (auto &p : pis) {
|
|
auto f = p.get_future();
|
|
REQUIRE(
|
|
f.wait_for(std::chrono::seconds(5)) == std::future_status::ready);
|
|
}
|
|
}
|
|
|
|
SECTION("rpc_latency_same_qb") {
|
|
auto cfg = config::get<config::QuickbusCfg>().node();
|
|
cfg.set_my_port(qb_port);
|
|
Quickbus qb(cfg);
|
|
auto sn = RPCServiceName("test");
|
|
|
|
RPCClient cli(qb, sn);
|
|
RPCServer server(
|
|
qb, sn, [](RPCRequestPtr const &req, const ReplyHandler &on_reply) {
|
|
on_reply(req);
|
|
});
|
|
|
|
LOG(Testing, Info, "Quickbus RPC intra bus one way time: {}us",
|
|
measure_latency(cli));
|
|
}
|
|
|
|
// too flaky
|
|
// SECTION("rpc_latency_between_qb")
|
|
// {
|
|
// config::QuickbusNodeCfg cfg;
|
|
// cfg.set_my_port (qb_port);
|
|
// Quickbus parent_qb(cfg);
|
|
|
|
// cfg.set_my_port (qb_port+1);
|
|
// cfg.set_parent_port (qb_port);
|
|
// cfg.set_parent_host ("127.0.0.1");
|
|
// Quickbus child_qb(cfg);
|
|
|
|
// auto sn = RPCServiceName("test");
|
|
|
|
// RPCClient cli(child_qb,sn);
|
|
// RPCServer server(parent_qb,sn,[] (RPCRequestPtr const&req,const
|
|
// ReplyHandler &on_reply) {on_reply(req);});
|
|
|
|
// LOG(Testing,Info,"Quickbus RPC inter bus one way time: {}us",
|
|
// measure_latency(cli));
|
|
// }
|
|
}
|
|
|
|
} // namespace qbus
|
|
} // namespace bts
|