#include "cloud_point_rpc/rpc_server.hpp" #include "server_api.h" #include "test_api.h" #include #include #include #include class TestCApi : public ::testing::Test { protected: void SetUp() override { FLAGS_logtostderr = true; if (!google::IsGoogleLoggingInitialized()) google::InitGoogleLogging("TestRPC"); EXPECT_NO_THROW(crpc_test_init()); } void TearDown() override { crpc_test_deinit(); } }; TEST_F(TestCApi, Base) { rpc_string name; name.s = "test"; static std::promise task; std::future called = task.get_future(); DLOG(INFO) << "Test"; crpc_test_add_method( +[](rpc_string *) -> rpc_string * { static bool installed = false; DLOG(INFO) << "Trying to do something"; if (!installed) { DLOG(INFO) << "Trying to install"; installed = true; task.set_value(installed); } DLOG(INFO) << "Go out"; return crpc_str_create("res", sizeof("res")); }, &name); using namespace std::chrono_literals; const std::future_status res = called.wait_for(500ms); EXPECT_NE(res, std::future_status::timeout); EXPECT_EQ(called.get(), true); DLOG(INFO) << "DONE"; } TEST_F(TestCApi, AddedMultiple) { constexpr int N = 4; std::array, N> tasks; std::array, rpc_string>, N> called; // The Bridge: A static pointer local to this test function static std::array, N>* bridge; bridge = &tasks; for (int i = 0; i < N; i++) { called[i].first = tasks[i].get_future(); std::string n = "test" + std::to_string(i); called[i].second = rpc_string{n.c_str(), n.size()}; } auto register_idx = [&]() { crpc_test_add_method(+[](rpc_string*) -> rpc_string* { static bool installed = false; if (!installed) { installed = true; (*bridge)[I].set_value(true); } return crpc_str_create("res", sizeof("res")); }, &called[I].second); }; register_idx.template operator()<0>(); register_idx.template operator()<1>(); register_idx.template operator()<2>(); register_idx.template operator()<3>(); auto test_idx = [&]() { using namespace std::chrono_literals; const std::future_status res = called[I].first.wait_for(1000ms); EXPECT_NE(res, std::future_status::timeout); EXPECT_EQ(called[I].first.get(), true); LOG(INFO) << "Done" << "; task=" << I; }; test_idx.template operator()<0>(); test_idx.template operator()<1>(); test_idx.template operator()<2>(); test_idx.template operator()<3>(); } TEST_F(TestCApi, RemoveMethod) { rpc_string name{"test", sizeof("test") - 1}; EXPECT_EQ(crpc_test_remove_method(&name), -1); } TEST_F(TestCApi, ChangeDuration) { constexpr int dur = 1e3; EXPECT_NO_THROW(crpc_test_change_duration(dur)); EXPECT_EQ(crpc_test_duration(), dur); LOG(INFO) << "Test"; } TEST_F(TestCApi, ScheduleCall) { EXPECT_NO_THROW(crpc_test_auto_call(0)); constexpr int N = 4; std::array, N> tasks; std::array, rpc_string>, N> called; // The Bridge: A static pointer local to this test function static std::array, N>* bridge; bridge = &tasks; LOG(INFO) << "Started Schedule Call"; for (int i = 0; i < N; i++) { called[i].first = tasks[i].get_future(); std::string n = "test" + std::to_string(i); called[i].second = rpc_string{n.c_str(), n.size()}; } auto register_idx = [&]() { crpc_test_add_method(+[](rpc_string*) -> rpc_string* { static bool installed = false; if (!installed) { installed = true; (*bridge)[I].set_value(true); } return crpc_str_create("res", sizeof("res")); }, &called[I].second); }; auto test_idx = [&]() { using namespace std::chrono_literals; const std::future_status res = called[I].first.wait_for(500ms); EXPECT_NE(res, std::future_status::timeout); EXPECT_EQ(called[I].first.get(), true); }; auto schedule_call = []() { rpc_string test{"test", sizeof("test") - 1}; test.s += std::to_string(I); LOG(INFO) << "Trying to add :" << test.s; crpc_test_schedule_call(&test); }; register_idx.template operator()<0>(); register_idx.template operator()<1>(); register_idx.template operator()<2>(); register_idx.template operator()<3>(); schedule_call.template operator()<0>(); schedule_call.template operator()<1>(); schedule_call.template operator()<2>(); schedule_call.template operator()<3>(); test_idx.template operator()<0>(); test_idx.template operator()<1>(); test_idx.template operator()<2>(); test_idx.template operator()<3>(); } TEST_F(TestCApi, String) { rpc_string name; name.s = "test"; EXPECT_EQ(name.s.c_str(), crpc_str_get_data(&name)); EXPECT_EQ(name.s.size(), crpc_str_get_size(&name)); auto creation = crpc_str_create("test 2222", sizeof("test 2222")); EXPECT_NO_THROW(crpc_str_destroy(creation)); }