1 #include "../../src/socket/udp_socket.h"
3 #include <fruit/fruit.h>
4 #include <gtest/gtest.h>
7 #include <boost/array.hpp>
8 #include <boost/asio.hpp>
13 #include <unordered_map>
15 #include "std_msgs/UInt8MultiArray.h"
17 using boost::asio::ip::address;
18 using boost::asio::ip::udp;
19 using fruit::Component;
20 using fruit::createComponent;
21 using fruit::Injector;
24 TEST(TestSuite, socket_callback) {
28 bool is_received =
false;
30 std::vector<uint8_t> received_vec;
33 auto func_ptr = std::make_shared<std::function<void(std::vector<uint8_t>)>>(
34 [&](std::vector<uint8_t> vec) {
36 copy(vec.begin(), vec.end(), back_inserter(received_vec));
44 auto source = socketFactory(udp::endpoint(udp::v4(), 0), func_ptr);
48 unsigned short port = source->Port();
49 std::thread sending_thread([&] {
52 boost::asio::io_service io_service;
53 udp::resolver resolver(io_service);
54 udp::endpoint receiver_endpoint(udp::v4(), port);
56 udp::socket socket(io_service);
57 socket.open(udp::v4());
59 std::vector<uint8_t> cvec = {0, 1, 2, 3, 4};
60 auto buf = boost::asio::buffer(cvec);
61 socket.send_to(buf, receiver_endpoint);
74 sending_thread.join();
77 for (
int i = 0; i < received_vec.size(); i++) {
78 ASSERT_EQ(received_vec[i], i);
84 TEST(TestSuite, socket_callback_twice) {
88 bool is_received =
false;
90 std::vector<uint8_t> received_vec;
95 auto func_ptr = std::make_shared<std::function<void(std::vector<uint8_t>)>>(
96 [&](std::vector<uint8_t> vec) {
100 copy(vec.begin(), vec.end(), back_inserter(received_vec));
109 auto source = socketFactory(udp::endpoint(udp::v4(), 0), func_ptr);
113 unsigned short port = source->Port();
114 std::thread sending_thread([&] {
117 boost::asio::io_service io_service;
118 udp::resolver resolver(io_service);
119 udp::endpoint receiver_endpoint(udp::v4(), port);
121 udp::socket socket(io_service);
122 socket.open(udp::v4());
124 std::vector<uint8_t> cvec = {0, 1, 2, 3, 4};
125 auto buf = boost::asio::buffer(cvec);
127 socket.send_to(buf, receiver_endpoint);
128 socket.send_to(buf, receiver_endpoint);
141 sending_thread.join();
144 for (
int i = 0; i < received_vec.size(); i++) {
145 ASSERT_EQ(received_vec[i], i);
151 TEST(TestSuite, socket_callback_in_map) {
155 bool is_received =
false;
157 std::vector<uint8_t> received_vec;
160 auto func_ptr = std::make_shared<std::function<void(std::vector<uint8_t>)>>(
161 [&](std::vector<uint8_t> vec) {
163 copy(vec.begin(), vec.end(), back_inserter(received_vec));
172 auto source = socketFactory(udp::endpoint(udp::v4(), 0), func_ptr);
173 std::unordered_map<std::string, std::unique_ptr<Socket>> map;
174 map.emplace(
"socket", std::move(source));
175 map.at(
"socket")->Start();
178 unsigned short port = map.at(
"socket")->Port();
179 std::thread sending_thread([&] {
182 boost::asio::io_service io_service;
183 udp::resolver resolver(io_service);
184 udp::endpoint receiver_endpoint(udp::v4(), port);
186 udp::socket socket(io_service);
187 socket.open(udp::v4());
189 std::vector<uint8_t> cvec = {0, 1, 2, 3, 4};
190 auto buf = boost::asio::buffer(cvec);
191 socket.send_to(buf, receiver_endpoint);
202 map.at(
"socket")->Stop();
204 sending_thread.join();
207 for (
int i = 0; i < received_vec.size(); i++) {
208 ASSERT_EQ(received_vec[i], i);
213 TEST(TestSuite, socket_send_data) {
217 bool is_received =
false;
219 std::vector<uint8_t> received_vec;
222 boost::asio::io_service io_service;
223 udp::socket socket(io_service, udp::endpoint(udp::v4(), 0));
225 auto endpoint = socket.local_endpoint();
227 std::thread receiving_thread([&] {
228 boost::array<uint8_t, 2048> recv_buf;
229 udp::endpoint remote_endpoint;
230 boost::system::error_code error;
231 size_t len = socket.receive_from(boost::asio::buffer(recv_buf),
232 remote_endpoint, 0, error);
233 received_vec.clear();
234 received_vec.insert(received_vec.end(), &recv_buf[0], &recv_buf[len]);
246 auto func_ptr = std::make_shared<std::function<void(std::vector<uint8_t>)>>(
247 [&](std::vector<uint8_t> vec) { ASSERT_TRUE(
false); });
253 auto source = socketFactory(endpoint, func_ptr);
255 std::vector<uint8_t> data{0, 1, 2, 3};
256 source->SendData(data);
262 receiving_thread.join();
265 for (
int i = 0; i < received_vec.size(); i++) {
266 ASSERT_EQ(received_vec[i], i);
269 ASSERT_TRUE(is_received);
274 TEST(TestSuite, socket_send_data_in_map) {
278 bool is_received =
false;
280 std::vector<uint8_t> received_vec;
283 boost::asio::io_service io_service;
284 udp::socket socket(io_service, udp::endpoint(udp::v4(), 0));
286 auto endpoint = socket.local_endpoint();
288 std::thread receiving_thread([&] {
289 boost::array<uint8_t, 2048> recv_buf;
290 udp::endpoint remote_endpoint;
291 boost::system::error_code error;
292 size_t len = socket.receive_from(boost::asio::buffer(recv_buf),
293 remote_endpoint, 0, error);
294 received_vec.clear();
295 received_vec.insert(received_vec.end(), &recv_buf[0], &recv_buf[len]);
307 auto func_ptr = std::make_shared<std::function<void(std::vector<uint8_t>)>>(
308 [&](std::vector<uint8_t> vec) { ASSERT_TRUE(
false); });
314 auto source = socketFactory(endpoint, func_ptr);
315 std::unordered_map<std::string, std::unique_ptr<Socket>> map;
316 map.emplace(
"socket", std::move(source));
317 map.at(
"socket")->Start();
318 std::vector<uint8_t> data{0, 1, 2, 3};
319 map.at(
"socket")->SendData(data);
323 map.at(
"socket")->Stop();
325 receiving_thread.join();
328 for (
int i = 0; i < received_vec.size(); i++) {
329 ASSERT_EQ(received_vec[i], i);
332 ASSERT_TRUE(is_received);