68 lines
2.0 KiB
C++
68 lines
2.0 KiB
C++
#include <gtest/gtest.h>
|
|
#include <gmock/gmock.h>
|
|
#include <thread>
|
|
#include <chrono>
|
|
#include <sstream>
|
|
#include <iostream>
|
|
#include <future>
|
|
|
|
#include "cloud_point_rpc/tcp_server.hpp"
|
|
#include "cloud_point_rpc/rpc_server.hpp"
|
|
#include "cloud_point_rpc/cli.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& params) {
|
|
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>{1.0, 2.0, 3.0};
|
|
});
|
|
|
|
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("\"result\": ["));
|
|
EXPECT_THAT(response, ::testing::HasSubstr("1.0"));
|
|
EXPECT_THAT(response, ::testing::HasSubstr("2.0"));
|
|
EXPECT_THAT(response, ::testing::HasSubstr("3.0"));
|
|
}
|