score-back/tests/test_cli.cpp
Artur Mukhamadiev f94a23b723 [test-c-api] appended api with helpful methods
valgrind doesn't reports any error
thread sanitizer reports one issue on test tear down (need to
investigate)
2026-03-01 23:54:06 +03:00

67 lines
1.9 KiB
C++

#include <chrono>
#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include <iostream>
#include <sstream>
#include <thread>
#include "cloud_point_rpc/cli.hpp"
#include "cloud_point_rpc/rpc_server.hpp"
#include "cloud_point_rpc/tcp_server.hpp"
using namespace cloud_point_rpc;
class CliTest : public ::testing::Test {
public:
void start() {
tcp_server->start();
}
protected:
void SetUp() override {
server_ip = "127.0.0.1";
server_port = 9096;
rpc_server = std::make_unique<RpcServer>();
tcp_server = std::make_unique<TcpServer>(
server_ip, server_port, [this](const std::string &req) {
return rpc_server->process(req);
});
std::this_thread::sleep_for(std::chrono::milliseconds(100));
}
void TearDown() override { tcp_server->stop(); }
std::string server_ip;
int server_port;
std::unique_ptr<RpcServer> rpc_server;
std::unique_ptr<TcpServer> tcp_server;
};
TEST_F(CliTest, SendsInputToServerAndReceivesResponse) {
std::stringstream input;
std::stringstream output;
// Select option 1 (get-intrinsic-params) then 0 (exit)
// First we need to make sure the rpc_server has the method registered.
// Our SetUp registers "hello", let's register "get-intrinsic-params" too.
rpc_server->register_method(
"get-intrinsic-params", [](const nlohmann::json &) {
return std::vector<double>{14589.0, 22489.0, 3123124.555};
});
this->start();
input << "1" << std::endl;
input << "0" << std::endl;
int result = run_cli(input, output, server_ip, server_port);
EXPECT_EQ(result, 0);
std::string response = output.str();
// Use more flexible check because of pretty printing
EXPECT_THAT(response, ::testing::HasSubstr("14589.0"));
EXPECT_THAT(response, ::testing::HasSubstr("22489.0"));
EXPECT_THAT(response, ::testing::HasSubstr("3123124.555"));
}