21 #include <boost/asio.hpp> 23 #include <gtest/gtest.h> 24 #include <gmock/gmock.h> 26 #include <pilz_testutils/async_test.h> 34 using namespace ::testing;
35 using boost::asio::ip::udp;
43 static constexpr std::chrono::seconds
READ_TIMEOUT{ std::chrono::seconds(1) };
50 void SetUp()
override;
53 std::future<std::size_t> startAsyncReadOperation(boost::asio::mutable_buffers_1& read_buf);
60 void ScannerCommunicationInterfaceTests::SetUp()
62 scanner_interface_.open();
65 std::future<std::size_t>
66 ScannerCommunicationInterfaceTests::startAsyncReadOperation(boost::asio::mutable_buffers_1& read_buf)
68 return std::async(std::launch::async,
69 [
this, &read_buf]() {
return scanner_interface_.read(read_buf,
READ_TIMEOUT); });
74 EXPECT_CALL(mock_udp_server_, receivedUdpMsg()).WillOnce(ACTION_OPEN_BARRIER_VOID(
MSG_RECEIVED));
75 mock_udp_server_.startIOService();
77 mock_udp_server_.asyncReceive();
78 boost::array<char, 10> write_buf = {
"Hello!" };
79 scanner_interface_.write(boost::asio::buffer(write_buf));
86 constexpr std::size_t expected_length{ 15 };
87 boost::array<char, expected_length> read_array;
88 auto read_buffer{ boost::asio::buffer(read_array) };
90 std::future<std::size_t> read_future{ startAsyncReadOperation(read_buffer) };
92 const udp::endpoint send_endpoint(boost::asio::ip::address_v4::from_string(
IP_ADDRESS),
UDP_PORT);
93 boost::array<char, expected_length> send_array = {
"Hello answer" };
95 mock_udp_server_.asyncSend<expected_length>(send_endpoint, send_array);
96 ASSERT_EQ(std::future_status::ready, read_future.wait_for(
READ_TIMEOUT)) <<
"Timeout while waiting for read()";
97 ASSERT_EQ(expected_length, read_future.get()) <<
"Received message length incorrect";
98 ASSERT_EQ(send_array, read_array) <<
"Send and received message are not equal";
static const std::string MSG_RECEIVED
TEST_F(ScannerCommunicationInterfaceTests, testScannerReadOperation)
static constexpr std::chrono::seconds READ_TIMEOUT
static const std::string IP_ADDRESS
MockUDPServer mock_udp_server_
Class for the UDP communication with the scanner.
Class for the UDP communication with the scanner.
constexpr uint32_t UDP_PORT