integrationtest_psen_scan_udp_interface.cpp
Go to the documentation of this file.
1 // Copyright (c) 2019-2020 Pilz GmbH & Co. KG
2 //
3 // This program is free software: you can redistribute it and/or modify
4 // it under the terms of the GNU Lesser General Public License as published by
5 // the Free Software Foundation, either version 3 of the License, or
6 // (at your option) any later version.
7 //
8 // This program is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 // GNU Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public License
14 // along with this program. If not, see <https://www.gnu.org/licenses/>.
15 
16 #include <chrono>
17 #include <string>
18 #include <thread>
19 #include <future>
20 
21 #include <boost/asio.hpp>
22 
23 #include <gtest/gtest.h>
24 #include <gmock/gmock.h>
25 
26 #include <pilz_testutils/async_test.h>
27 
32 
33 using namespace psen_scan;
34 using namespace ::testing;
35 using boost::asio::ip::udp;
36 
37 namespace psen_scan_test
38 {
39 static const std::string MSG_RECEIVED{ "msg_received" };
40 
41 static const std::string IP_ADDRESS{ "127.0.0.1" };
42 
43 static constexpr std::chrono::seconds READ_TIMEOUT{ std::chrono::seconds(1) };
44 
45 static constexpr uint32_t UDP_PORT{ 45000 };
46 
47 class ScannerCommunicationInterfaceTests : public testing::Test, public testing::AsyncTest
48 {
49 public:
50  void SetUp() override;
51 
52 protected:
53  std::future<std::size_t> startAsyncReadOperation(boost::asio::mutable_buffers_1& read_buf);
54 
55 protected:
57  PSENscanUDPInterface scanner_interface_{ IP_ADDRESS, UDP_PORT };
58 };
59 
60 void ScannerCommunicationInterfaceTests::SetUp()
61 {
62  scanner_interface_.open();
63 }
64 
65 std::future<std::size_t>
66 ScannerCommunicationInterfaceTests::startAsyncReadOperation(boost::asio::mutable_buffers_1& read_buf)
67 {
68  return std::async(std::launch::async,
69  [this, &read_buf]() { return scanner_interface_.read(read_buf, READ_TIMEOUT); });
70 }
71 
72 TEST_F(ScannerCommunicationInterfaceTests, testScannerWriteOperation)
73 {
74  EXPECT_CALL(mock_udp_server_, receivedUdpMsg()).WillOnce(ACTION_OPEN_BARRIER_VOID(MSG_RECEIVED));
75  mock_udp_server_.startIOService();
76 
77  mock_udp_server_.asyncReceive();
78  boost::array<char, 10> write_buf = { "Hello!" };
79  scanner_interface_.write(boost::asio::buffer(write_buf));
80 
81  BARRIER(MSG_RECEIVED);
82 }
83 
84 TEST_F(ScannerCommunicationInterfaceTests, testScannerReadOperation)
85 {
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) };
89 
90  std::future<std::size_t> read_future{ startAsyncReadOperation(read_buffer) };
91 
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" };
94 
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";
99 }
100 
101 } // namespace psen_scan_test
TEST_F(ScannerCommunicationInterfaceTests, testScannerReadOperation)
static constexpr std::chrono::seconds READ_TIMEOUT
Class for the UDP communication with the scanner.
Class for the UDP communication with the scanner.
constexpr uint32_t UDP_PORT


psen_scan
Author(s):
autogenerated on Mon Feb 28 2022 23:16:20