3131#include < gtest/gtest.h>
3232#include < condition_variable>
3333
34+ #include " test_utils.h"
35+
3436#include < ur_client_library/comm/pipeline.h>
3537#include < ur_client_library/comm/tcp_server.h>
3638#include < ur_client_library/comm/stream.h>
@@ -45,8 +47,7 @@ class PipelineTest : public ::testing::Test
4547protected:
4648 void SetUp ()
4749 {
48- server_.reset (new comm::TCPServer (60002 ));
49- server_->setConnectCallback (std::bind (&PipelineTest::connectionCallback, this , std::placeholders::_1));
50+ server_.reset (new TestableTcpServer (60002 ));
5051 server_->start ();
5152
5253 // Setup pipeline
@@ -68,28 +69,7 @@ class PipelineTest : public ::testing::Test
6869 server_.reset ();
6970 }
7071
71- void connectionCallback (const socket_t filedescriptor)
72- {
73- std::lock_guard<std::mutex> lk (connect_mutex_);
74- client_fd_ = filedescriptor;
75- connect_cv_.notify_one ();
76- connection_callback_ = true ;
77- }
78-
79- bool waitForConnectionCallback (int milliseconds = 100 )
80- {
81- std::unique_lock<std::mutex> lk (connect_mutex_);
82- if (connect_cv_.wait_for (lk, std::chrono::milliseconds (milliseconds)) == std::cv_status::no_timeout ||
83- connection_callback_ == true )
84- {
85- connection_callback_ = false ;
86- return true ;
87- }
88- return false ;
89- }
90-
91- std::unique_ptr<comm::TCPServer> server_;
92- socket_t client_fd_;
72+ std::unique_ptr<TestableTcpServer> server_;
9373
9474 std::unique_ptr<comm::URStream<rtde_interface::RTDEPackage>> stream_;
9575 std::unique_ptr<rtde_interface::RTDEParser> parser_;
@@ -138,8 +118,6 @@ class PipelineTest : public ::testing::Test
138118private:
139119 std::condition_variable connect_cv_;
140120 std::mutex connect_mutex_;
141-
142- bool connection_callback_ = false ;
143121};
144122
145123TEST_F (PipelineTest, get_product_from_stopped_pipeline)
@@ -151,13 +129,13 @@ TEST_F(PipelineTest, get_product_from_stopped_pipeline)
151129
152130TEST_F (PipelineTest, get_product_from_running_pipeline)
153131{
154- waitForConnectionCallback ();
132+ server_-> waitForConnectionCallback ();
155133 pipeline_->run ();
156134
157135 // RTDE package with timestamp
158136 uint8_t data_package[] = { 0x00 , 0x0c , 0x55 , 0x01 , 0x40 , 0xbb , 0xbf , 0xdb , 0xa5 , 0xe3 , 0x53 , 0xf7 };
159137 size_t written;
160- server_->write (client_fd_, data_package, sizeof (data_package), written);
138+ server_->write (data_package, sizeof (data_package), written);
161139
162140 std::unique_ptr<rtde_interface::RTDEPackage> urpackage;
163141 std::chrono::milliseconds timeout{ 500 };
@@ -178,13 +156,13 @@ TEST_F(PipelineTest, get_product_from_running_pipeline)
178156
179157TEST_F (PipelineTest, stop_pipeline)
180158{
181- waitForConnectionCallback ();
159+ server_-> waitForConnectionCallback ();
182160 pipeline_->run ();
183161
184162 // RTDE package with timestamp
185163 uint8_t data_package[] = { 0x00 , 0x0c , 0x55 , 0x01 , 0x40 , 0xbb , 0xbf , 0xdb , 0xa5 , 0xe3 , 0x53 , 0xf7 };
186164 size_t written;
187- server_->write (client_fd_, data_package, sizeof (data_package), written);
165+ server_->write (data_package, sizeof (data_package), written);
188166
189167 std::unique_ptr<rtde_interface::RTDEPackage> urpackage;
190168 std::chrono::milliseconds timeout{ 500 };
@@ -206,13 +184,13 @@ TEST_F(PipelineTest, consumer_pipeline)
206184 pipeline_.reset (
207185 new comm::Pipeline<rtde_interface::RTDEPackage>(*producer_.get (), &consumer, " RTDE_PIPELINE" , notifier_));
208186 pipeline_->init ();
209- waitForConnectionCallback ();
187+ server_-> waitForConnectionCallback ();
210188 pipeline_->run ();
211189
212190 // RTDE package with timestamp
213191 uint8_t data_package[] = { 0x00 , 0x0c , 0x55 , 0x01 , 0x40 , 0xbb , 0xbf , 0xdb , 0xa5 , 0xe3 , 0x53 , 0xf7 };
214192 size_t written;
215- server_->write (client_fd_, data_package, sizeof (data_package), written);
193+ server_->write (data_package, sizeof (data_package), written);
216194
217195 // Wait for data to be consumed
218196 int max_retries = 3 ;
@@ -223,7 +201,7 @@ TEST_F(PipelineTest, consumer_pipeline)
223201 {
224202 break ;
225203 }
226- server_->write (client_fd_, data_package, sizeof (data_package), written);
204+ server_->write (data_package, sizeof (data_package), written);
227205 count++;
228206 }
229207 EXPECT_LT (count, max_retries);
0 commit comments