Detailed: As base used jsonrpccxx implementation paired with TCP socket TCP socket updated to handle dynamic sized buffers TCP communication protocol changed to serialized packet size after which json string is presented
65 lines
2.0 KiB
C++
65 lines
2.0 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 {
|
|
protected:
|
|
void SetUp() override {
|
|
server_ip = "127.0.0.1";
|
|
server_port = 9096;
|
|
|
|
rpc_server = std::make_unique<RpcServer>();
|
|
rpc_server->register_method(
|
|
"hello", [](const nlohmann::json &) { return "world"; });
|
|
|
|
tcp_server = std::make_unique<TcpServer>(
|
|
server_ip, server_port, [this](const std::string &req) {
|
|
return rpc_server->process(req);
|
|
});
|
|
|
|
tcp_server->start();
|
|
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};
|
|
});
|
|
|
|
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"));
|
|
}
|