Skip to content

Commit 4b0f198

Browse files
committed
Refactor tests to use common TestableTcpServer
1 parent c505f3f commit 4b0f198

8 files changed

Lines changed: 228 additions & 393 deletions

src/comm/tcp_server.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ TCPServer::TCPServer(const int port, const size_t max_num_tries, const std::chro
5656

5757
TCPServer::~TCPServer()
5858
{
59-
URCL_LOG_DEBUG("Destroying TCPServer object.");
59+
URCL_LOG_INFO("Destroying TCPServer object.");
6060
shutdown();
6161
}
6262

@@ -257,9 +257,9 @@ void TCPServer::spin()
257257

258258
void TCPServer::handleDisconnect(const socket_t fd)
259259
{
260-
URCL_LOG_DEBUG("%d disconnected.", fd);
260+
URCL_LOG_INFO("%d disconnected.", fd);
261261
ur_close(fd);
262-
if (disconnect_callback_)
262+
if (disconnect_callback_ && keep_running_)
263263
{
264264
disconnect_callback_(fd);
265265
}

tests/test_external_control_program.cpp

Lines changed: 4 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232

3333
#include "test_utils.h"
3434
#include "ur_client_library/example_robot_wrapper.h"
35-
#include "ur_client_library/log.h"
3635

3736
using namespace urcl;
3837

@@ -46,16 +45,6 @@ std::unique_ptr<ExampleRobotWrapper> g_my_robot;
4645

4746
class ExternalControlProgramTest : public ::testing::Test
4847
{
49-
public:
50-
// callback functions
51-
void connectionCallback(const socket_t filedescriptor)
52-
{
53-
std::lock_guard<std::mutex> lk(connect_mutex_);
54-
client_fd_ = filedescriptor;
55-
connect_cv_.notify_one();
56-
connection_callback_ = true;
57-
}
58-
5948
protected:
6049
static void SetUpTestSuite()
6150
{
@@ -75,9 +64,7 @@ class ExternalControlProgramTest : public ::testing::Test
7564
ASSERT_TRUE(g_my_robot->resendRobotProgram());
7665
ASSERT_TRUE(g_my_robot->waitForProgramRunning(500));
7766
}
78-
server_.reset(new comm::TCPServer(60005));
79-
server_->setConnectCallback(
80-
std::bind(&ExternalControlProgramTest::connectionCallback, this, std::placeholders::_1));
67+
server_.reset(new TestableTcpServer(60005));
8168
server_->start();
8269
}
8370

@@ -113,34 +100,14 @@ class ExternalControlProgramTest : public ::testing::Test
113100
return modified_script_path;
114101
}
115102

116-
bool waitForConnectionCallback(int milliseconds = 100)
117-
{
118-
std::unique_lock<std::mutex> lk(connect_mutex_);
119-
if (connect_cv_.wait_for(lk, std::chrono::milliseconds(milliseconds),
120-
[this]() { return connection_callback_ == true; }))
121-
{
122-
connection_callback_ = false;
123-
return true;
124-
}
125-
else
126-
{
127-
return false;
128-
}
129-
}
130-
std::unique_ptr<comm::TCPServer> server_;
131-
132-
private:
133-
std::condition_variable connect_cv_;
134-
std::mutex connect_mutex_;
135-
socket_t client_fd_ = INVALID_SOCKET;
136-
bool connection_callback_ = false;
103+
std::unique_ptr<TestableTcpServer> server_;
137104
};
138105

139106
TEST_F(ExternalControlProgramTest, program_halts_on_timeout)
140107
{
141108
vector6d_t zeros = { 0, 0, 0, 0, 0, 0 };
142109
g_my_robot->getUrDriver()->writeJointCommand(zeros, comm::ControlMode::MODE_IDLE, RobotReceiveTimeout::millisec(200));
143-
EXPECT_FALSE(waitForConnectionCallback(1000));
110+
EXPECT_FALSE(server_->waitForConnectionCallback(1000));
144111
}
145112

146113
TEST_F(ExternalControlProgramTest, stop_control_does_not_halt_program)
@@ -150,7 +117,7 @@ TEST_F(ExternalControlProgramTest, stop_control_does_not_halt_program)
150117

151118
// Make sure that we can stop the robot control, when robot receive timeout has been set off
152119
g_my_robot->getUrDriver()->stopControl();
153-
EXPECT_TRUE(waitForConnectionCallback(1000));
120+
EXPECT_TRUE(server_->waitForConnectionCallback(1000));
154121
}
155122

156123
int main(int argc, char* argv[])

tests/test_pipeline.cpp

Lines changed: 11 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
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
4547
protected:
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_;
@@ -151,13 +131,13 @@ TEST_F(PipelineTest, get_product_from_stopped_pipeline)
151131

152132
TEST_F(PipelineTest, get_product_from_running_pipeline)
153133
{
154-
waitForConnectionCallback();
134+
server_->waitForConnectionCallback();
155135
pipeline_->run();
156136

157137
// RTDE package with timestamp
158138
uint8_t data_package[] = { 0x00, 0x0c, 0x55, 0x01, 0x40, 0xbb, 0xbf, 0xdb, 0xa5, 0xe3, 0x53, 0xf7 };
159139
size_t written;
160-
server_->write(client_fd_, data_package, sizeof(data_package), written);
140+
server_->write(data_package, sizeof(data_package), written);
161141

162142
std::unique_ptr<rtde_interface::RTDEPackage> urpackage;
163143
std::chrono::milliseconds timeout{ 500 };
@@ -178,13 +158,13 @@ TEST_F(PipelineTest, get_product_from_running_pipeline)
178158

179159
TEST_F(PipelineTest, stop_pipeline)
180160
{
181-
waitForConnectionCallback();
161+
server_->waitForConnectionCallback();
182162
pipeline_->run();
183163

184164
// RTDE package with timestamp
185165
uint8_t data_package[] = { 0x00, 0x0c, 0x55, 0x01, 0x40, 0xbb, 0xbf, 0xdb, 0xa5, 0xe3, 0x53, 0xf7 };
186166
size_t written;
187-
server_->write(client_fd_, data_package, sizeof(data_package), written);
167+
server_->write(data_package, sizeof(data_package), written);
188168

189169
std::unique_ptr<rtde_interface::RTDEPackage> urpackage;
190170
std::chrono::milliseconds timeout{ 500 };
@@ -206,13 +186,13 @@ TEST_F(PipelineTest, consumer_pipeline)
206186
pipeline_.reset(
207187
new comm::Pipeline<rtde_interface::RTDEPackage>(*producer_.get(), &consumer, "RTDE_PIPELINE", notifier_));
208188
pipeline_->init();
209-
waitForConnectionCallback();
189+
server_->waitForConnectionCallback();
210190
pipeline_->run();
211191

212192
// RTDE package with timestamp
213193
uint8_t data_package[] = { 0x00, 0x0c, 0x55, 0x01, 0x40, 0xbb, 0xbf, 0xdb, 0xa5, 0xe3, 0x53, 0xf7 };
214194
size_t written;
215-
server_->write(client_fd_, data_package, sizeof(data_package), written);
195+
server_->write(data_package, sizeof(data_package), written);
216196

217197
// Wait for data to be consumed
218198
int max_retries = 3;
@@ -223,7 +203,7 @@ TEST_F(PipelineTest, consumer_pipeline)
223203
{
224204
break;
225205
}
226-
server_->write(client_fd_, data_package, sizeof(data_package), written);
206+
server_->write(data_package, sizeof(data_package), written);
227207
count++;
228208
}
229209
EXPECT_LT(count, max_retries);

tests/test_producer.cpp

Lines changed: 5 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#include <gtest/gtest.h>
3232
#include <chrono>
3333
#include <condition_variable>
34+
#include "test_utils.h"
3435

3536
#include <ur_client_library/comm/producer.h>
3637
#include <ur_client_library/comm/stream.h>
@@ -44,8 +45,7 @@ class ProducerTest : public ::testing::Test
4445
protected:
4546
void SetUp()
4647
{
47-
server_.reset(new comm::TCPServer(60002));
48-
server_->setConnectCallback(std::bind(&ProducerTest::connectionCallback, this, std::placeholders::_1));
48+
server_.reset(new TestableTcpServer(60002));
4949
server_->start();
5050
}
5151

@@ -55,34 +55,7 @@ class ProducerTest : public ::testing::Test
5555
server_.reset();
5656
}
5757

58-
void connectionCallback(const socket_t filedescriptor)
59-
{
60-
std::lock_guard<std::mutex> lk(connect_mutex_);
61-
client_fd_ = filedescriptor;
62-
connect_cv_.notify_one();
63-
connection_callback_ = true;
64-
}
65-
66-
bool waitForConnectionCallback(int milliseconds = 100)
67-
{
68-
std::unique_lock<std::mutex> lk(connect_mutex_);
69-
if (connect_cv_.wait_for(lk, std::chrono::milliseconds(milliseconds)) == std::cv_status::no_timeout ||
70-
connection_callback_ == true)
71-
{
72-
connection_callback_ = false;
73-
return true;
74-
}
75-
return false;
76-
}
77-
78-
std::unique_ptr<comm::TCPServer> server_;
79-
socket_t client_fd_;
80-
81-
private:
82-
std::condition_variable connect_cv_;
83-
std::mutex connect_mutex_;
84-
85-
bool connection_callback_ = false;
58+
std::unique_ptr<TestableTcpServer> server_;
8659
};
8760

8861
TEST_F(ProducerTest, get_data_package)
@@ -94,13 +67,13 @@ TEST_F(ProducerTest, get_data_package)
9467
comm::URProducer<rtde_interface::RTDEPackage> producer(stream, parser);
9568

9669
producer.setupProducer();
97-
waitForConnectionCallback();
70+
server_->waitForConnectionCallback();
9871
producer.startProducer();
9972

10073
// RTDE package with timestamp
10174
uint8_t data_package[] = { 0x00, 0x0c, 0x55, 0x01, 0x40, 0xbb, 0xbf, 0xdb, 0xa5, 0xe3, 0x53, 0xf7 };
10275
size_t written;
103-
server_->write(client_fd_, data_package, sizeof(data_package), written);
76+
server_->write(data_package, sizeof(data_package), written);
10477

10578
std::vector<std::unique_ptr<rtde_interface::RTDEPackage>> products;
10679
EXPECT_EQ(producer.tryGet(products), true);

0 commit comments

Comments
 (0)