Skip to content

Commit 2e69bc1

Browse files
urmahpfmauch
andauthored
Added tests for the comm classes (UniversalRobots#129)
* Added tests for the comm classes This add tests for * bin parser * package serializer * TCP socket * stream * producer * pipeline Fixed check size in bin parser, so that it works with starndard data types Added recconection time variable to TCP socket, this will control the time in between connection attempts if the server is unavailable. Co-authored-by: Felix Exner <[email protected]>
1 parent 1853580 commit 2e69bc1

File tree

10 files changed

+1497
-4
lines changed

10 files changed

+1497
-4
lines changed

include/ur_client_library/comm/bin_parser.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -350,7 +350,8 @@ class BinParser
350350
template <typename T>
351351
bool checkSize(void)
352352
{
353-
return checkSize(T::SIZE);
353+
size_t size = sizeof(T);
354+
return checkSize(size);
354355
}
355356

356357
/*!

include/ur_client_library/comm/tcp_socket.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ class TCPSocket
5050
private:
5151
std::atomic<int> socket_fd_;
5252
std::atomic<SocketState> state_;
53+
std::chrono::seconds reconnection_time_;
5354

5455
protected:
5556
virtual bool open(int socket_fd, struct sockaddr* address, size_t address_len)
@@ -138,6 +139,17 @@ class TCPSocket
138139
* \param timeout Timeout used for setting things up
139140
*/
140141
void setReceiveTimeout(const timeval& timeout);
142+
143+
/*!
144+
* \brief Set reconnection time, if the server is unavailable during connection this will set the time before
145+
* trying connect to the server again.
146+
*
147+
* \param reconnection_time time in between connection attempts to the server
148+
*/
149+
void setReconnectionTime(std::chrono::seconds reconnection_time)
150+
{
151+
reconnection_time_ = reconnection_time;
152+
}
141153
};
142154
} // namespace comm
143155
} // namespace urcl

src/comm/tcp_socket.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ namespace urcl
3535
{
3636
namespace comm
3737
{
38-
TCPSocket::TCPSocket() : socket_fd_(-1), state_(SocketState::Invalid)
38+
TCPSocket::TCPSocket() : socket_fd_(-1), state_(SocketState::Invalid), reconnection_time_(std::chrono::seconds(10))
3939
{
4040
}
4141
TCPSocket::~TCPSocket()
@@ -101,9 +101,10 @@ bool TCPSocket::setup(std::string& host, int port)
101101
state_ = SocketState::Invalid;
102102
std::stringstream ss;
103103
ss << "Failed to connect to robot on IP " << host_name
104-
<< ". Please check that the robot is booted and reachable on " << host_name << ". Retrying in 10 seconds";
104+
<< ". Please check that the robot is booted and reachable on " << host_name << ". Retrying in "
105+
<< reconnection_time_.count() << " seconds";
105106
URCL_LOG_ERROR("%s", ss.str().c_str());
106-
std::this_thread::sleep_for(std::chrono::seconds(10));
107+
std::this_thread::sleep_for(reconnection_time_);
107108
}
108109
}
109110
setOptions(socket_fd_);

tests/CMakeLists.txt

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,3 +153,45 @@ target_include_directories(version_information_tests PRIVATE ${GTEST_INCLUDE_DIR
153153
target_link_libraries(version_information_tests PRIVATE ur_client_library::urcl ${GTEST_LIBRARIES})
154154
gtest_add_tests(TARGET version_information_tests
155155
)
156+
157+
add_executable(bin_parser_tests test_bin_parser.cpp)
158+
target_compile_options(bin_parser_tests PRIVATE ${CXX17_FLAG})
159+
target_include_directories(bin_parser_tests PRIVATE ${GTEST_INCLUDE_DIRS})
160+
target_link_libraries(bin_parser_tests PRIVATE ur_client_library::urcl ${GTEST_LIBRARIES})
161+
gtest_add_tests(TARGET bin_parser_tests
162+
)
163+
164+
add_executable(package_serializer_tests test_package_serializer.cpp)
165+
target_compile_options(package_serializer_tests PRIVATE ${CXX17_FLAG})
166+
target_include_directories(package_serializer_tests PRIVATE ${GTEST_INCLUDE_DIRS})
167+
target_link_libraries(package_serializer_tests PRIVATE ur_client_library::urcl ${GTEST_LIBRARIES})
168+
gtest_add_tests(TARGET package_serializer_tests
169+
)
170+
171+
add_executable(tcp_socket_tests test_tcp_socket.cpp)
172+
target_compile_options(tcp_socket_tests PRIVATE ${CXX17_FLAG})
173+
target_include_directories(tcp_socket_tests PRIVATE ${GTEST_INCLUDE_DIRS})
174+
target_link_libraries(tcp_socket_tests PRIVATE ur_client_library::urcl ${GTEST_LIBRARIES})
175+
gtest_add_tests(TARGET tcp_socket_tests
176+
)
177+
178+
add_executable(stream_tests test_stream.cpp)
179+
target_compile_options(stream_tests PRIVATE ${CXX17_FLAG})
180+
target_include_directories(stream_tests PRIVATE ${GTEST_INCLUDE_DIRS})
181+
target_link_libraries(stream_tests PRIVATE ur_client_library::urcl ${GTEST_LIBRARIES})
182+
gtest_add_tests(TARGET stream_tests
183+
)
184+
185+
add_executable(producer_tests test_producer.cpp)
186+
target_compile_options(producer_tests PRIVATE ${CXX17_FLAG})
187+
target_include_directories(producer_tests PRIVATE ${GTEST_INCLUDE_DIRS})
188+
target_link_libraries(producer_tests PRIVATE ur_client_library::urcl ${GTEST_LIBRARIES})
189+
gtest_add_tests(TARGET producer_tests
190+
)
191+
192+
add_executable(pipeline_tests test_pipeline.cpp)
193+
target_compile_options(pipeline_tests PRIVATE ${CXX17_FLAG})
194+
target_include_directories(pipeline_tests PRIVATE ${GTEST_INCLUDE_DIRS})
195+
target_link_libraries(pipeline_tests PRIVATE ur_client_library::urcl ${GTEST_LIBRARIES})
196+
gtest_add_tests(TARGET pipeline_tests
197+
)

0 commit comments

Comments
 (0)