Skip to content

Commit eefdde8

Browse files
authored
Merge pull request eclipse-uprotocol#89 from debruce/feature/rpc-client-server-test
This implements a test for RPC on Zenoh transport.
2 parents 3377910 + 4092d4a commit eefdde8

File tree

1 file changed

+98
-7
lines changed

1 file changed

+98
-7
lines changed

test/extra/RpcClientServerTest.cpp

Lines changed: 98 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,28 +10,119 @@
1010
// SPDX-License-Identifier: Apache-2.0
1111

1212
#include <gtest/gtest.h>
13+
#include <up-cpp/communication/RpcClient.h>
14+
#include <up-cpp/communication/RpcServer.h>
15+
#include <up-cpp/datamodel/builder/Payload.h>
16+
#include <up-cpp/datamodel/builder/Uuid.h>
17+
#include <up-cpp/datamodel/serializer/UUri.h>
18+
#include <up-cpp/datamodel/serializer/Uuid.h>
19+
#include <up-transport-zenoh-cpp/ZenohUTransport.h>
20+
21+
#include <iostream>
22+
23+
using namespace std::chrono_literals;
1324

1425
namespace {
1526

16-
class TestFixture : public testing::Test {
27+
using namespace uprotocol::v1;
28+
using uprotocol::communication::RpcClient;
29+
using uprotocol::communication::RpcServer;
30+
31+
constexpr std::string_view ZENOH_CONFIG_FILE = BUILD_REALPATH_ZENOH_CONF;
32+
33+
struct MyUUri {
34+
std::string auth = "";
35+
uint32_t ue_id = 0x8000;
36+
uint32_t ue_version_major = 1;
37+
uint32_t resource_id = 1;
38+
39+
operator uprotocol::v1::UUri() const {
40+
UUri ret;
41+
ret.set_authority_name(auth);
42+
ret.set_ue_id(ue_id);
43+
ret.set_ue_version_major(ue_version_major);
44+
ret.set_resource_id(resource_id);
45+
return ret;
46+
}
47+
48+
std::string to_string() const {
49+
return std::string("<< ") + UUri(*this).ShortDebugString() + " >>";
50+
}
51+
};
52+
53+
class RpcClientServerTest : public testing::Test {
1754
protected:
55+
MyUUri rpc_service_uuri_{"me_authority", 65538, 1, 32600};
56+
MyUUri ident_{"me_authority", 65538, 1, 0};
57+
58+
using Transport = uprotocol::transport::ZenohUTransport;
59+
std::shared_ptr<Transport> transport_;
60+
1861
// Run once per TEST_F.
1962
// Used to set up clean environments per test.
20-
void SetUp() override {}
21-
void TearDown() override {}
63+
void SetUp() override {
64+
transport_ = std::make_shared<Transport>(ident_, ZENOH_CONFIG_FILE);
65+
EXPECT_NE(nullptr, transport_);
66+
}
67+
68+
void TearDown() override { transport_ = nullptr; }
2269

2370
// Run once per execution of the test application.
2471
// Used for setup of all tests. Has access to this instance.
25-
TestFixture() = default;
26-
~TestFixture() = default;
72+
RpcClientServerTest() = default;
73+
~RpcClientServerTest() = default;
2774

2875
// Run once per execution of the test application.
2976
// Used only for global setup outside of tests.
3077
static void SetUpTestSuite() {}
3178
static void TearDownTestSuite() {}
3279
};
3380

34-
// TODO replace
35-
TEST_F(TestFixture, SomeTestName) {}
81+
TEST_F(RpcClientServerTest, SimpleRoundTrip) {
82+
using namespace std;
83+
84+
string client_request{"RPC Request"};
85+
uprotocol::datamodel::builder::Payload client_request_payload(
86+
client_request, UPayloadFormat::UPAYLOAD_FORMAT_TEXT);
87+
bool client_called = false;
88+
UMessage client_capture;
89+
90+
bool server_called = false;
91+
UMessage server_capture;
92+
string server_response{"RPC Response"};
93+
uprotocol::datamodel::builder::Payload server_response_payload(
94+
server_response, UPayloadFormat::UPAYLOAD_FORMAT_TEXT);
95+
96+
auto serverOrStatus = RpcServer::create(
97+
transport_, rpc_service_uuri_,
98+
[this, &server_called, &server_capture,
99+
&server_response_payload](const UMessage& message) {
100+
server_called = true;
101+
server_capture = message;
102+
return server_response_payload;
103+
},
104+
UPayloadFormat::UPAYLOAD_FORMAT_TEXT);
105+
ASSERT_TRUE(serverOrStatus.has_value());
106+
ASSERT_NE(serverOrStatus.value(), nullptr);
107+
108+
auto client = RpcClient(transport_, rpc_service_uuri_,
109+
UPriority::UPRIORITY_CS4, 1000ms);
110+
111+
uprotocol::communication::RpcClient::InvokeHandle client_handle;
112+
EXPECT_NO_THROW(
113+
client_handle = client.invokeMethod(
114+
std::move(client_request_payload),
115+
[this, &client_called, &client_capture](auto maybe_response) {
116+
client_called = true;
117+
if (maybe_response.has_value()) {
118+
client_capture = maybe_response.value();
119+
}
120+
}));
121+
122+
EXPECT_TRUE(server_called);
123+
EXPECT_EQ(client_request, server_capture.payload());
124+
EXPECT_TRUE(client_called);
125+
EXPECT_EQ(server_response, client_capture.payload());
126+
}
36127

37128
} // namespace

0 commit comments

Comments
 (0)