Skip to content

Commit

Permalink
test: refine rpc
Browse files Browse the repository at this point in the history
  • Loading branch information
shuai132 committed Sep 18, 2023
1 parent 40bdcd0 commit 54d2654
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 8 deletions.
2 changes: 1 addition & 1 deletion include/asio_net/rpc_core
20 changes: 13 additions & 7 deletions test/rpc_2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,14 @@ int main(int argc, char** argv) {

// server
std::thread([] {
asio::io_context context;
auto rpc = rpc_core::rpc::create();
rpc->subscribe("cmd", [](const std::string& data) -> std::string {
LOG("session on cmd: %s", data.c_str());
ASSERT(data == "hello");
return "world";
});

asio::io_context context;
static rpc_server server(context, PORT, rpc_config{.rpc = rpc}); // static for test session lifecycle
server.on_session = [&](const std::weak_ptr<rpc_session>& rs) {
LOG("on_session:");
Expand All @@ -31,19 +37,16 @@ int main(int argc, char** argv) {
LOG("session on_close:");
pass_flag_session_close = true;
};
session->rpc->subscribe("cmd", [](const std::string& data) -> std::string {
LOG("session on cmd: %s", data.c_str());
ASSERT(data == "hello");
return "world";
});
};
server.start(true);
}).detach();

// client
std::thread([] {
asio::io_context context;
auto rpc = rpc_core::rpc::create();
rpc->cmd("cmd")->msg(std::string("hello"))->call(); // no effect

asio::io_context context;
static rpc_client client(context, rpc_config{.rpc = rpc}); // static for test session lifecycle
client.on_open = [&](const std::shared_ptr<rpc_core::rpc>& rpc_) {
LOG("client on_open:");
Expand All @@ -66,6 +69,9 @@ int main(int argc, char** argv) {
};
client.open("localhost", PORT);
client.run();

LOG("client exited");
rpc->cmd("cmd")->msg(std::string("hello"))->call(); // no effect
}).join();

ASSERT(pass_flag_rpc_pass);
Expand Down

0 comments on commit 54d2654

Please sign in to comment.