5 #include <gtest/gtest.h> 9 #include <uavcan/protocol/RestartNode.hpp> 10 #include <uavcan/protocol/GetDataTypeInfo.hpp> 11 #include <root_ns_a/StringService.hpp> 12 #include <root_ns_a/EmptyService.hpp> 18 template <
typename DataType>
29 std::cout << result << std::endl;
32 last_server_node_id = result.
getCallID().server_node_id;
36 bool match(StatusType status,
uavcan::NodeID server_node_id,
const typename DataType::Response& response)
const 38 if (status == last_status &&
39 server_node_id == last_server_node_id &&
40 response == last_response)
46 std::cout <<
"MISMATCH: status=" <<
int(last_status) <<
", last_server_node_id=" 47 <<
int(last_server_node_id.
get()) <<
", last response:\n" << last_response << std::endl;
62 rsp.string_response =
"Request string: ";
63 rsp.string_response += req.string_request;
70 rsp.string_response =
"Request string: ";
71 rsp.string_response += req.string_request;
103 ClientType client1(nodes.
b);
104 ClientType client2(nodes.
b);
105 ClientType client3(nodes.
b);
107 ASSERT_EQ(0, client1.getNumPendingCalls());
108 ASSERT_EQ(0, client2.getNumPendingCalls());
109 ASSERT_EQ(0, client3.getNumPendingCalls());
111 ASSERT_FALSE(client1.hasPendingCallToServer(1));
113 client1.setCallback(handler.
bind());
114 client2.setCallback(client1.getCallback());
115 client3.setCallback(client1.getCallback());
121 root_ns_a::StringService::Request request;
122 request.string_request =
"Hello world";
124 std::cout <<
"!!! Calling!" << std::endl;
126 ASSERT_LT(0, client1.call(1, request));
127 ASSERT_LT(0, client1.call(1, request));
128 ASSERT_LT(0, client2.call(1, request));
129 ASSERT_LT(0, client3.call(99, request));
130 ASSERT_LT(0, client3.call(1, request));
132 ASSERT_TRUE(client1.hasPendingCallToServer(1));
133 ASSERT_TRUE(client2.hasPendingCallToServer(1));
134 ASSERT_TRUE(client3.hasPendingCallToServer(99));
135 ASSERT_TRUE(client3.hasPendingCallToServer(1));
137 std::cout <<
"!!! Spinning!" << std::endl;
141 ASSERT_TRUE(client1.hasPendingCalls());
142 ASSERT_TRUE(client2.hasPendingCalls());
143 ASSERT_TRUE(client3.hasPendingCalls());
145 ASSERT_EQ(2, client1.getNumPendingCalls());
146 ASSERT_EQ(1, client2.getNumPendingCalls());
147 ASSERT_EQ(2, client3.getNumPendingCalls());
149 ASSERT_EQ(
uavcan::NodeID(1), client2.getCallIDByIndex(0).server_node_id);
150 ASSERT_EQ(
uavcan::NodeID(), client2.getCallIDByIndex(1).server_node_id);
154 std::cout <<
"!!! Spin finished!" << std::endl;
158 ASSERT_FALSE(client1.hasPendingCalls());
159 ASSERT_FALSE(client2.hasPendingCalls());
160 ASSERT_TRUE(client3.hasPendingCalls());
162 ASSERT_EQ(0, client1.getNumPendingCalls());
163 ASSERT_EQ(0, client2.getNumPendingCalls());
164 ASSERT_EQ(1, client3.getNumPendingCalls());
167 root_ns_a::StringService::Response expected_response;
168 expected_response.string_response =
"Request string: Hello world";
169 ASSERT_TRUE(handler.
match(ResultType::Success, 1, expected_response));
173 ASSERT_FALSE(client1.hasPendingCalls());
174 ASSERT_FALSE(client2.hasPendingCalls());
175 ASSERT_FALSE(client3.hasPendingCalls());
180 ASSERT_TRUE(handler.
match(ResultType::ErrorTimeout, 99, root_ns_a::StringService::Response()));
183 ASSERT_LT(0, client3.call(99, request));
184 ASSERT_TRUE(client3.hasPendingCalls());
211 ClientType client1(nodes.
b);
213 client1.setCallback(handler.
bind());
215 root_ns_a::StringService::Request request;
216 request.string_request =
"Hello world";
218 ASSERT_LT(0, client1.call(1, request));
220 ASSERT_EQ(
uavcan::NodeID(1), client1.getCallIDByIndex(0).server_node_id);
221 ASSERT_EQ(
uavcan::NodeID(), client1.getCallIDByIndex(1).server_node_id);
224 ASSERT_TRUE(client1.hasPendingCalls());
225 ASSERT_TRUE(client1.hasPendingCallToServer(1));
228 ASSERT_FALSE(client1.hasPendingCalls());
229 ASSERT_FALSE(client1.hasPendingCallToServer(1));
233 ASSERT_TRUE(handler.
match(ResultType::ErrorTimeout, 1, root_ns_a::StringService::Response()));
237 TEST(ServiceClient, ConcurrentCalls)
258 ClientType client(nodes.
b);
260 ASSERT_EQ(0, client.getNumPendingCalls());
262 client.setCallback(handler.
bind());
267 ASSERT_FALSE(client.hasPendingCalls());
268 ASSERT_EQ(0, client.getNumPendingCalls());
275 for (
int i = 0; i < 10; i++)
277 std::ostringstream os;
279 root_ns_a::StringService::Request request;
280 request.string_request = os.str().c_str();
281 ASSERT_LT(0, client.call(1, request, last_call_id));
284 ASSERT_LT(0, client.call(99, root_ns_a::StringService::Request()));
288 ASSERT_LT(0, client.call(88, root_ns_a::StringService::Request()));
290 ASSERT_TRUE(client.hasPendingCalls());
291 ASSERT_EQ(12, client.getNumPendingCalls());
298 client.cancelCall(last_call_id);
300 ASSERT_TRUE(client.hasPendingCalls());
301 ASSERT_EQ(11, client.getNumPendingCalls());
310 ASSERT_TRUE(client.hasPendingCalls());
311 ASSERT_EQ(2, client.getNumPendingCalls());
318 for (
int i = 0; i < 9; i++)
320 std::ostringstream os;
321 os <<
"Request string: " << i;
322 last_response.string_response = os.str().c_str();
326 ASSERT_STREQ(last_response.string_response.c_str(), handler.
responses.front().string_response.c_str());
338 ASSERT_TRUE(client.hasPendingCalls());
339 ASSERT_EQ(1, client.getNumPendingCalls());
342 ASSERT_TRUE(handler.
match(ResultType::ErrorTimeout, 88, root_ns_a::StringService::Response()));
349 ASSERT_FALSE(client.hasPendingCalls());
350 ASSERT_EQ(0, client.getNumPendingCalls());
353 ASSERT_TRUE(handler.
match(ResultType::ErrorTimeout, 99, root_ns_a::StringService::Response()));
375 ClientType client(nodes.
b);
377 client.setCallback(handler.
bind());
379 root_ns_a::EmptyService::Request request;
381 ASSERT_LT(0, client.call(1, request));
401 ClientType client(nodes.
b);
402 client.setCallback(handler.
bind());
405 root_ns_a::EmptyService::Request request;
407 client.setPriority(0);
408 ASSERT_LT(0, client.call(1, request));
410 client.setPriority(10);
411 ASSERT_LT(0, client.call(1, request));
413 client.setPriority(20);
414 ASSERT_LT(0, client.call(1, request));
416 client.setPriority(31);
417 ASSERT_LT(0, client.call(1, request));
426 ASSERT_EQ(0, frame.getPriority().get());
430 ASSERT_EQ(10, frame.getPriority().get());
434 ASSERT_EQ(20, frame.getPriority().get());
438 ASSERT_EQ(31, frame.getPriority().get());
446 std::cout <<
"GetDataTypeInfo server: " <<
449 std::cout <<
"RestartNode server: " <<
452 std::cout <<
"GetDataTypeInfo client: " <<
TEST(ServiceClient, Basic)
ServiceCallID getCallID() const
Dispatcher & getDispatcher()
DataType::Response last_response
bool match(StatusType status, uavcan::NodeID server_node_id, const typename DataType::Response &response) const
unsigned getNumServiceResponseListeners() const
uavcan::ServiceCallResult< DataType >::Status StatusType
void setResponseEnabled(bool x)
uavcan::MethodBinder< ServiceCallResultHandler *, void(ServiceCallResultHandler::*)(const uavcan::ServiceCallResult< DataType > &)> Binder
bool isResponseEnabled() const
unsigned getNumServiceRequestListeners() const
static void rejectingStringServiceServerCallback(const uavcan::ReceivedDataStructure< root_ns_a::StringService::Request > &req, uavcan::ServiceResponseDataStructure< root_ns_a::StringService::Response > &rsp)
static void emptyServiceServerCallback(const uavcan::ReceivedDataStructure< root_ns_a::EmptyService::Request > &, uavcan::ServiceResponseDataStructure< root_ns_a::EmptyService::Response > &)
std::queue< typename DataType::Response > responses
uavcan::NodeID last_server_node_id
const ResponseFieldType & getResponse() const
static GlobalDataTypeRegistry & instance()
int start(const Callback &callback)
void handleResponse(const uavcan::ServiceCallResult< DataType > &result)
static MonotonicDuration fromMSec(int64_t ms)
int spinBoth(uavcan::MonotonicDuration duration)
std::queue< uavcan::CanFrame > read_queue
static void stringServiceServerCallback(const uavcan::ReceivedDataStructure< root_ns_a::StringService::Request > &req, uavcan::ServiceResponseDataStructure< root_ns_a::StringService::Response > &rsp)