Skip to content

Commit

Permalink
[feat|test|style] add construct test, fix spell error
Browse files Browse the repository at this point in the history
Signed-off-by: FeiW <[email protected]>
  • Loading branch information
wf0312 committed Jan 27, 2024
1 parent 0445168 commit ee3e1c3
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 31 deletions.
2 changes: 1 addition & 1 deletion include/rest_rpc/rpc_client.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ class rpc_client : private asio::noncopyable {
err_cb_ = std::move(f);
}

uint64_t reqest_id() { return temp_req_id_; }
uint64_t request_id() { return temp_req_id_; }

bool has_connected() const { return has_connected_; }

Expand Down
66 changes: 36 additions & 30 deletions tests/test_rest_rpc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,41 @@ void hello(rpc_conn conn, const std::string &str) {
std::cout << "hello " << str << std::endl;
}

TEST_CASE("test_client_constructor") {
constexpr unsigned short port = 9000;
rpc_server server(port, std::thread::hardware_concurrency());
dummy d;
server.register_handler("add", &dummy::add, &d);
server.async_run();
std::this_thread::sleep_for(std::chrono::milliseconds(200));
SUBCASE("default_constructor") {
rpc_client client;
client.update_addr("127.0.0.1", port);
bool r = client.connect("127.0.0.1", port);
CHECK(r);
auto result = client.call<int>("add", 1, 2);
CHECK_EQ(result, 3);
}

SUBCASE("with_language_constructor") {
rpc_client client(client_language_t::CPP, nullptr);
bool r = client.connect("127.0.0.1", port);
CHECK(r);
auto result = client.call<int>("add", 1, 2);
CHECK_EQ(result, 3);
}

SUBCASE("with_host_constructor") {
const std::string host{"127.0.0.1"};
rpc_client client(host, port);

auto r = client.connect();
CHECK(r);

auto result = client.call<int>("add", 1, 2);
}
}

TEST_CASE("test_client_reconnect") {
rpc_client client;
client.enable_auto_reconnect(); // automatic reconnect
Expand Down Expand Up @@ -68,35 +103,6 @@ TEST_CASE("test_client_reconnect") {
}
}

TEST_CASE("test_client_default_constructor") {
rpc_server server(9000, std::thread::hardware_concurrency());
dummy d;
server.register_handler("add", &dummy::add, &d);
server.async_run();
std::this_thread::sleep_for(std::chrono::milliseconds(200));

rpc_client client;
client.update_addr("127.0.0.1", 9000);
bool r = client.connect("127.0.0.1", 9000);
CHECK(r);
auto result = client.call<int>("add", 1, 2);
CHECK_EQ(result, 3);
}

TEST_CASE("test_constructor_with_language") {
rpc_server server(9000, std::thread::hardware_concurrency());
dummy d;
server.register_handler("add", &dummy::add, &d);
server.async_run();
std::this_thread::sleep_for(std::chrono::milliseconds(200));

rpc_client client(client_language_t::CPP, nullptr);
bool r = client.connect("127.0.0.1", 9000);
CHECK(r);
auto result = client.call<int>("add", 1, 2);
CHECK_EQ(result, 3);
}

TEST_CASE("test_client_async_connect") {
rpc_server server(9000, std::thread::hardware_concurrency());
dummy d;
Expand Down Expand Up @@ -322,4 +328,4 @@ TEST_CASE("test_server_user_data") {
bool r = client.connect();
CHECK(r);
client.call<>("server_user_data");
}
}

0 comments on commit ee3e1c3

Please sign in to comment.