integrationtest_brake_test_required.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2019 Pilz GmbH & Co. KG
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8 
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU Lesser General Public License for more details.
13 
14  * You should have received a copy of the GNU Lesser General Public License
15  * along with this program. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #include <gtest/gtest.h>
19 #include <gmock/gmock.h>
20 
21 #include <thread>
22 #include <memory>
23 #include <chrono>
24 #include <vector>
25 #include <functional>
26 #include <condition_variable>
27 #include <atomic>
28 #include <string>
29 
30 #include <ros/ros.h>
31 #include <std_srvs/SetBool.h>
32 #include <std_srvs/Trigger.h>
33 #include <std_msgs/Bool.h>
34 
35 #include <modbus/modbus.h>
36 
37 #include <pilz_msgs/IsBrakeTestRequired.h>
38 #include <pilz_msgs/IsBrakeTestRequiredResult.h>
39 
40 #include <prbt_hardware_support/ModbusMsgInStamped.h>
44 #include <prbt_hardware_support/WriteModbusRegister.h>
45 
49 
50 #include <pilz_testutils/async_test.h>
51 
52 namespace prbt_hardware_support
53 {
54 using namespace modbus_api::v3;
55 
56 using ::testing::_;
57 using ::testing::InSequence;
58 using ::testing::Invoke;
59 using ::testing::InvokeWithoutArgs;
60 
61 static const std::string SERVICE_BRAKETEST_REQUIRED = "/prbt/brake_test_required";
62 static const std::string MODBUS_SERVICE_NAME{ "/pilz_modbus_client_node/modbus_write" };
63 
64 static constexpr unsigned int DEFAULT_RETRIES{ 10 };
65 
72 class BrakeTestRequiredIntegrationTest : public testing::Test, public testing::AsyncTest
73 {
74 public:
75  MOCK_METHOD2(modbusWrite, bool(WriteModbusRegister::Request&, WriteModbusRegister::Response&));
76 
77 protected:
79  ros::NodeHandle nh_priv_{ "~" };
80 
82  &BrakeTestRequiredIntegrationTest::modbusWrite, this) };
83 };
84 
85 ::testing::AssertionResult
87  pilz_msgs::IsBrakeTestRequiredResult::_value_type expectation,
88  uint16_t retries = DEFAULT_RETRIES)
89 {
90  pilz_msgs::IsBrakeTestRequired srv;
91  for (int i = 0; i <= retries; i++)
92  {
93  auto res = brake_test_required_client.call(srv);
94  if (!res)
95  {
96  return ::testing::AssertionFailure() << "Could not call service";
97  }
98  if (srv.response.result.value == expectation)
99  {
100  return ::testing::AssertionSuccess() << "It took " << i + 1 << " tries for the service call.";
101  }
102  sleep(1); // This then may take {retries*1}seconds.
103  }
104  return ::testing::AssertionFailure() << "Did not get expected brake test result via service";
105 }
106 
132 TEST_F(BrakeTestRequiredIntegrationTest, testBrakeTestAnnouncement)
133 {
134  /**********
135  * Setup *
136  **********/
137  std::string ip;
138  int port;
139  ASSERT_TRUE(nh_priv_.getParam("modbus_server_ip", ip));
140  ASSERT_TRUE(nh_priv_.getParam("modbus_server_port", port));
141 
142  ModbusApiSpec api_spec{ nh_ };
143 
144  unsigned int const modbus_register_size{ api_spec.getMaxRegisterDefinition() + 1U };
145 
146  /**********
147  * Step 1 *
148  **********/
149  prbt_hardware_support::PilzModbusServerMock modbus_server(modbus_register_size);
150 
151  std::thread modbus_server_thread(&initalizeAndRun<prbt_hardware_support::PilzModbusServerMock>,
152  std::ref(modbus_server), ip.c_str(), static_cast<unsigned int>(port));
153 
154  waitForNode("/pilz_modbus_client_node");
155  waitForNode("/modbus_adapter_brake_test_node");
156 
157  /**********
158  * Step 2 *
159  **********/
160  ASSERT_TRUE(api_spec.hasRegisterDefinition(modbus_api_spec::VERSION));
161  unsigned int const version_register = api_spec.getRegisterDefinition(modbus_api_spec::VERSION);
162 
163  ASSERT_TRUE(api_spec.hasRegisterDefinition(modbus_api_spec::BRAKETEST_REQUEST));
164  unsigned int const braketest_register = api_spec.getRegisterDefinition(modbus_api_spec::BRAKETEST_REQUEST);
165 
166  modbus_server.setHoldingRegister({ { braketest_register, 1 }, { version_register, MODBUS_API_VERSION_REQUIRED } });
167 
168  ros::ServiceClient is_brake_test_required_client =
169  nh_.serviceClient<pilz_msgs::IsBrakeTestRequired>(SERVICE_BRAKETEST_REQUIRED);
170  ASSERT_TRUE(is_brake_test_required_client.waitForExistence(ros::Duration(10)));
171 
172  EXPECT_TRUE(expectBrakeTestRequiredServiceCallResult(is_brake_test_required_client,
173  pilz_msgs::IsBrakeTestRequiredResult::REQUIRED));
174 
175  /**********
176  * Step 3 *
177  **********/
178  ASSERT_TRUE(api_spec.hasRegisterDefinition(modbus_api_spec::RUN_PERMITTED));
179  unsigned int const run_permitted_register = api_spec.getRegisterDefinition(modbus_api_spec::RUN_PERMITTED);
180 
181  modbus_server.setHoldingRegister({ { run_permitted_register, 1 } });
182 
183  EXPECT_TRUE(expectBrakeTestRequiredServiceCallResult(is_brake_test_required_client,
184  pilz_msgs::IsBrakeTestRequiredResult::REQUIRED));
185 
186  /**********
187  * Step 4 *
188  **********/
189  modbus_server.setHoldingRegister({ { braketest_register, 0 } });
190 
191  EXPECT_TRUE(expectBrakeTestRequiredServiceCallResult(is_brake_test_required_client,
192  pilz_msgs::IsBrakeTestRequiredResult::NOT_REQUIRED));
193 
194  /**********
195  * Step 5 *
196  **********/
197  modbus_server.terminate();
198  modbus_server_thread.join();
199 }
200 
201 } // namespace prbt_hardware_support
202 
203 int main(int argc, char* argv[])
204 {
205  ros::init(argc, argv, "integrationtest_brake_test_required");
206  ros::NodeHandle nh;
207 
208  testing::InitGoogleTest(&argc, argv);
209  return RUN_ALL_TESTS();
210 }
::testing::AssertionResult expectBrakeTestRequiredServiceCallResult(ros::ServiceClient &brake_test_required_client, pilz_msgs::IsBrakeTestRequiredResult::_value_type expectation, uint16_t retries=DEFAULT_RETRIES)
static const std::string BRAKETEST_REQUEST
Offers a modbus server and read/write functionality via subscription/publication. ...
TEST_F(BrakeTestRequiredIntegrationTest, testBrakeTestAnnouncement)
int main(int argc, char *argv[])
void setHoldingRegister(std::initializer_list< std::pair< unsigned int, uint16_t > > reg_list)
ROSCPP_DECL void init(int &argc, char **argv, const std::string &name, uint32_t options=0)
bool call(MReq &req, MRes &res)
static const std::string MODBUS_SERVICE_NAME
ServiceServer advertiseService(const std::string &service, bool(T::*srv_func)(MReq &, MRes &), T *obj)
static constexpr uint16_t MODBUS_API_VERSION_REQUIRED
static const std::string SERVICE_BRAKETEST_REQUIRED
static const std::string RUN_PERMITTED
bool waitForExistence(ros::Duration timeout=ros::Duration(-1))
BrakeTestRequiredIntegrationTest checks if the chain ModbusServerMock -> ModbusReadClient -> ModbusAd...
void terminate()
Terminate the Server. Reading or connecting to it will fail.
inline ::testing::AssertionResult waitForNode(const std::string node_name, const double loop_frequency=10.0, const ros::Duration timeout=ros::Duration(10.0))
Blocks until a node defined by node_name comes up.
static constexpr unsigned int DEFAULT_RETRIES
Specifies the meaning of the holding registers.


prbt_hardware_support
Author(s):
autogenerated on Sun Oct 25 2020 03:13:00