2020-02-12 00:08:19 +01:00
|
|
|
#include "common.h"
|
2020-02-11 07:30:07 +01:00
|
|
|
#include <future>
|
2020-02-28 05:16:43 +01:00
|
|
|
#include <lokimq/hex.h>
|
2020-02-11 07:30:07 +01:00
|
|
|
|
|
|
|
using namespace lokimq;
|
|
|
|
|
2020-02-12 00:08:19 +01:00
|
|
|
TEST_CASE("basic requests", "[requests]") {
|
2020-02-11 07:30:07 +01:00
|
|
|
std::string listen = "tcp://127.0.0.1:5678";
|
|
|
|
LokiMQ server{
|
|
|
|
"", "", // generate ephemeral keys
|
|
|
|
false, // not a service node
|
2020-02-28 05:16:43 +01:00
|
|
|
[](auto) { return ""; },
|
2020-02-11 07:30:07 +01:00
|
|
|
};
|
2020-02-28 05:16:43 +01:00
|
|
|
server.listen_curve(listen);
|
2020-02-11 07:30:07 +01:00
|
|
|
|
|
|
|
std::atomic<int> hellos{0}, his{0};
|
|
|
|
|
|
|
|
server.add_category("public", Access{AuthLevel::none});
|
|
|
|
server.add_request_command("public", "hello", [&](Message& m) {
|
|
|
|
m.send_reply("123");
|
|
|
|
});
|
|
|
|
server.start();
|
|
|
|
|
|
|
|
LokiMQ client(
|
|
|
|
[](LogLevel, const char* file, int line, std::string msg) { std::cerr << file << ":" << line << " --C-- " << msg << "\n"; }
|
|
|
|
);
|
|
|
|
//client.log_level(LogLevel::trace);
|
|
|
|
|
|
|
|
client.start();
|
|
|
|
|
|
|
|
std::atomic<bool> connected{false}, failed{false};
|
|
|
|
std::string pubkey;
|
|
|
|
|
2020-02-28 05:16:43 +01:00
|
|
|
auto c = client.connect_remote(listen,
|
|
|
|
[&](auto conn) { pubkey = conn.pubkey(); connected = true; },
|
|
|
|
[&](auto, auto) { failed = true; },
|
2020-02-11 07:30:07 +01:00
|
|
|
server.get_pubkey());
|
|
|
|
|
2020-02-12 00:08:19 +01:00
|
|
|
int i;
|
|
|
|
for (i = 0; i < 5; i++) {
|
2020-02-11 07:30:07 +01:00
|
|
|
if (connected.load())
|
|
|
|
break;
|
2020-02-12 00:08:19 +01:00
|
|
|
std::this_thread::sleep_for(50ms);
|
2020-02-11 07:30:07 +01:00
|
|
|
}
|
2020-03-21 15:27:01 +01:00
|
|
|
{
|
|
|
|
auto lock = catch_lock();
|
|
|
|
REQUIRE( connected.load() );
|
|
|
|
REQUIRE( !failed.load() );
|
|
|
|
REQUIRE( i <= 1 );
|
|
|
|
REQUIRE( to_hex(pubkey) == to_hex(server.get_pubkey()) );
|
|
|
|
}
|
2020-02-11 07:30:07 +01:00
|
|
|
|
|
|
|
std::atomic<bool> got_reply{false};
|
|
|
|
bool success;
|
|
|
|
std::vector<std::string> data;
|
2020-02-28 05:16:43 +01:00
|
|
|
client.request(c, "public.hello", [&](bool ok, std::vector<std::string> data_) {
|
|
|
|
got_reply = true;
|
|
|
|
success = ok;
|
|
|
|
data = std::move(data_);
|
|
|
|
});
|
|
|
|
|
|
|
|
std::this_thread::sleep_for(50ms);
|
2020-03-21 15:27:01 +01:00
|
|
|
auto lock = catch_lock();
|
2020-02-28 05:16:43 +01:00
|
|
|
REQUIRE( got_reply.load() );
|
|
|
|
REQUIRE( success );
|
|
|
|
REQUIRE( data == std::vector<std::string>{{"123"}} );
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE("request from server to client", "[requests]") {
|
|
|
|
std::string listen = "tcp://127.0.0.1:5678";
|
|
|
|
LokiMQ server{
|
|
|
|
"", "", // generate ephemeral keys
|
|
|
|
false, // not a service node
|
|
|
|
[](auto) { return ""; },
|
|
|
|
};
|
|
|
|
server.listen_curve(listen);
|
|
|
|
|
|
|
|
std::atomic<int> hellos{0}, his{0};
|
|
|
|
|
|
|
|
server.add_category("public", Access{AuthLevel::none});
|
|
|
|
server.add_request_command("public", "hello", [&](Message& m) {
|
|
|
|
m.send_reply("123");
|
|
|
|
});
|
|
|
|
server.start();
|
|
|
|
|
|
|
|
LokiMQ client(
|
|
|
|
[](LogLevel, const char* file, int line, std::string msg) { std::cerr << file << ":" << line << " --C-- " << msg << "\n"; }
|
|
|
|
);
|
|
|
|
//client.log_level(LogLevel::trace);
|
|
|
|
|
|
|
|
client.start();
|
|
|
|
|
|
|
|
std::atomic<bool> connected{false}, failed{false};
|
|
|
|
std::string pubkey;
|
|
|
|
|
|
|
|
auto c = client.connect_remote(listen,
|
|
|
|
[&](auto conn) { pubkey = conn.pubkey(); connected = true; },
|
|
|
|
[&](auto, auto) { failed = true; },
|
|
|
|
server.get_pubkey());
|
|
|
|
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < 5; i++) {
|
|
|
|
if (connected.load())
|
|
|
|
break;
|
|
|
|
std::this_thread::sleep_for(50ms);
|
|
|
|
}
|
2020-03-21 15:27:01 +01:00
|
|
|
{
|
|
|
|
auto lock = catch_lock();
|
|
|
|
REQUIRE( connected.load() );
|
|
|
|
REQUIRE( !failed.load() );
|
|
|
|
REQUIRE( i <= 1 );
|
|
|
|
REQUIRE( to_hex(pubkey) == to_hex(server.get_pubkey()) );
|
|
|
|
}
|
2020-02-28 05:16:43 +01:00
|
|
|
|
|
|
|
std::atomic<bool> got_reply{false};
|
|
|
|
bool success;
|
|
|
|
std::vector<std::string> data;
|
|
|
|
client.request(c, "public.hello", [&](bool ok, std::vector<std::string> data_) {
|
2020-02-11 07:30:07 +01:00
|
|
|
got_reply = true;
|
|
|
|
success = ok;
|
|
|
|
data = std::move(data_);
|
|
|
|
});
|
|
|
|
|
2020-02-12 00:08:19 +01:00
|
|
|
std::this_thread::sleep_for(50ms);
|
2020-03-21 15:27:01 +01:00
|
|
|
{
|
|
|
|
auto lock = catch_lock();
|
|
|
|
REQUIRE( got_reply.load() );
|
|
|
|
REQUIRE( success );
|
|
|
|
REQUIRE( data == std::vector<std::string>{{"123"}} );
|
|
|
|
}
|
2020-02-11 07:30:07 +01:00
|
|
|
}
|