Skip to content

Commit 56154c3

Browse files
committed
Use a condition variable instead of a sleep
1 parent cfa38ca commit 56154c3

File tree

2 files changed

+89
-13
lines changed

2 files changed

+89
-13
lines changed

tests/test_reverse_interface.cpp

Lines changed: 41 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,38 @@
3232
#include <ur_client_library/control/reverse_interface.h>
3333
#include <ur_client_library/comm/tcp_socket.h>
3434
#include <ur_client_library/exceptions.h>
35+
#include "ur_client_library/log.h"
3536

3637
using namespace urcl;
38+
std::mutex g_connection_mutex;
39+
std::condition_variable g_connection_condition;
40+
41+
class TestableReverseInterface : public control::ReverseInterface
42+
{
43+
public:
44+
TestableReverseInterface(const control::ReverseInterfaceConfig& config) : control::ReverseInterface(config)
45+
{
46+
}
47+
48+
virtual void connectionCallback(const socket_t filedescriptor)
49+
{
50+
control::ReverseInterface::connectionCallback(filedescriptor);
51+
connected = true;
52+
std::lock_guard<std::mutex> lk(g_connection_mutex);
53+
g_connection_condition.notify_one();
54+
}
55+
56+
virtual void disconnectionCallback(const socket_t filedescriptor)
57+
{
58+
URCL_LOG_DEBUG("There are %zu disconnection callbacks registered.", disconnect_callbacks_.size());
59+
control::ReverseInterface::disconnectionCallback(filedescriptor);
60+
connected = false;
61+
std::lock_guard<std::mutex> lk(g_connection_mutex);
62+
g_connection_condition.notify_one();
63+
}
64+
65+
std::atomic<bool> connected = false;
66+
};
3767

3868
class ReverseIntefaceTest : public ::testing::Test
3969
{
@@ -153,8 +183,11 @@ class ReverseIntefaceTest : public ::testing::Test
153183
control::ReverseInterfaceConfig config;
154184
config.port = 50001;
155185
config.handle_program_state = std::bind(&ReverseIntefaceTest::handleProgramState, this, std::placeholders::_1);
156-
reverse_interface_.reset(new control::ReverseInterface(config));
186+
reverse_interface_.reset(new TestableReverseInterface(config));
157187
client_.reset(new Client(50001));
188+
std::unique_lock<std::mutex> lk(g_connection_mutex);
189+
g_connection_condition.wait_for(lk, std::chrono::seconds(1),
190+
[&]() { return reverse_interface_->connected.load(); });
158191
}
159192

160193
void TearDown()
@@ -187,7 +220,7 @@ class ReverseIntefaceTest : public ::testing::Test
187220
return false;
188221
}
189222

190-
std::unique_ptr<control::ReverseInterface> reverse_interface_;
223+
std::unique_ptr<TestableReverseInterface> reverse_interface_;
191224
std::unique_ptr<Client> client_;
192225

193226
private:
@@ -473,7 +506,8 @@ TEST_F(ReverseIntefaceTest, disconnected_callbacks_are_called)
473506
// Close the client connection
474507
client_->close();
475508
EXPECT_TRUE(waitForProgramState(1000, false));
476-
sleep(1);
509+
std::unique_lock<std::mutex> lk(g_connection_mutex);
510+
g_connection_condition.wait_for(lk, std::chrono::seconds(1), [&]() { return !reverse_interface_->connected.load(); });
477511
EXPECT_TRUE(disconnect_called_1);
478512
EXPECT_TRUE(disconnect_called_2);
479513

@@ -484,7 +518,7 @@ TEST_F(ReverseIntefaceTest, disconnected_callbacks_are_called)
484518
EXPECT_TRUE(waitForProgramState(1000, true));
485519
reverse_interface_->unregisterDisconnectionCallback(disconnection_callback_id_1);
486520
client_->close();
487-
sleep(1);
521+
g_connection_condition.wait_for(lk, std::chrono::seconds(1), [&]() { return !reverse_interface_->connected.load(); });
488522
EXPECT_TRUE(waitForProgramState(1000, false));
489523
EXPECT_FALSE(disconnect_called_1);
490524
EXPECT_TRUE(disconnect_called_2);
@@ -496,7 +530,7 @@ TEST_F(ReverseIntefaceTest, disconnected_callbacks_are_called)
496530
EXPECT_TRUE(waitForProgramState(1000, true));
497531
reverse_interface_->unregisterDisconnectionCallback(disconnection_callback_id_2);
498532
client_->close();
499-
sleep(1);
533+
g_connection_condition.wait_for(lk, std::chrono::seconds(1), [&]() { return !reverse_interface_->connected.load(); });
500534
EXPECT_TRUE(waitForProgramState(1000, false));
501535
EXPECT_FALSE(disconnect_called_1);
502536
EXPECT_FALSE(disconnect_called_2);
@@ -505,6 +539,7 @@ TEST_F(ReverseIntefaceTest, disconnected_callbacks_are_called)
505539
int main(int argc, char* argv[])
506540
{
507541
::testing::InitGoogleTest(&argc, argv);
542+
urcl::setLogLevel(LogLevel::INFO);
508543

509544
return RUN_ALL_TESTS();
510-
}
545+
}

tests/test_trajectory_point_interface.cpp

Lines changed: 48 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,36 @@
3737

3838
using namespace urcl;
3939

40+
std::mutex g_connection_mutex;
41+
std::condition_variable g_connection_condition;
42+
43+
class TestableTrajectoryPointInterface : public control::TrajectoryPointInterface
44+
{
45+
public:
46+
TestableTrajectoryPointInterface(uint32_t port) : control::TrajectoryPointInterface(port)
47+
{
48+
}
49+
50+
virtual void connectionCallback(const socket_t filedescriptor) override
51+
{
52+
control::TrajectoryPointInterface::connectionCallback(filedescriptor);
53+
connected = true;
54+
std::lock_guard<std::mutex> lk(g_connection_mutex);
55+
g_connection_condition.notify_one();
56+
}
57+
58+
virtual void disconnectionCallback(const socket_t filedescriptor) override
59+
{
60+
URCL_LOG_DEBUG("There are %zu disconnection callbacks registered.", disconnect_callbacks_.size());
61+
control::TrajectoryPointInterface::disconnectionCallback(filedescriptor);
62+
connected = false;
63+
std::lock_guard<std::mutex> lk(g_connection_mutex);
64+
g_connection_condition.notify_one();
65+
}
66+
67+
std::atomic<bool> connected = false; //!< True, if the interface is connected to the robot.
68+
};
69+
4070
class TrajectoryPointInterfaceTest : public ::testing::Test
4171
{
4272
protected:
@@ -257,10 +287,12 @@ class TrajectoryPointInterfaceTest : public ::testing::Test
257287

258288
void SetUp()
259289
{
260-
traj_point_interface_.reset(new control::TrajectoryPointInterface(50003));
290+
traj_point_interface_.reset(new TestableTrajectoryPointInterface(50003));
261291
client_.reset(new Client(50003));
262292
// Need to be sure that the client has connected to the server
263-
std::this_thread::sleep_for(std::chrono::seconds(1));
293+
std::unique_lock<std::mutex> lk(g_connection_mutex);
294+
g_connection_condition.wait_for(lk, std::chrono::seconds(1),
295+
[&]() { return traj_point_interface_->connected.load(); });
264296
}
265297

266298
void TearDown()
@@ -271,7 +303,7 @@ class TrajectoryPointInterfaceTest : public ::testing::Test
271303
}
272304
}
273305

274-
std::unique_ptr<control::TrajectoryPointInterface> traj_point_interface_;
306+
std::unique_ptr<TestableTrajectoryPointInterface> traj_point_interface_;
275307
std::unique_ptr<Client> client_;
276308

277309
public:
@@ -618,27 +650,36 @@ TEST_F(TrajectoryPointInterfaceTest, disconnected_callbacks_are_called_correctly
618650

619651
// Close the client connection
620652
client_->close();
621-
sleep(1);
653+
std::unique_lock<std::mutex> lk(g_connection_mutex);
654+
g_connection_condition.wait_for(lk, std::chrono::seconds(1),
655+
[&]() { return !traj_point_interface_->connected.load(); });
622656
EXPECT_TRUE(disconnect_called_1);
623657
EXPECT_TRUE(disconnect_called_2);
624658

625659
// Unregister 1. 2 should still be called
626660
disconnect_called_1 = false;
627661
disconnect_called_2 = false;
628662
client_.reset(new Client(50003));
663+
g_connection_condition.wait_for(lk, std::chrono::seconds(1),
664+
[&]() { return traj_point_interface_->connected.load(); });
665+
629666
traj_point_interface_->unregisterDisconnectionCallback(disconnection_callback_id_1);
630667
client_->close();
631-
sleep(1);
668+
g_connection_condition.wait_for(lk, std::chrono::seconds(1),
669+
[&]() { return !traj_point_interface_->connected.load(); });
632670
EXPECT_FALSE(disconnect_called_1);
633671
EXPECT_TRUE(disconnect_called_2);
634672

635673
// Unregister both. None should be called
636674
disconnect_called_1 = false;
637675
disconnect_called_2 = false;
638676
client_.reset(new Client(50003));
677+
g_connection_condition.wait_for(lk, std::chrono::seconds(1),
678+
[&]() { return traj_point_interface_->connected.load(); });
639679
traj_point_interface_->unregisterDisconnectionCallback(disconnection_callback_id_2);
640680
client_->close();
641-
sleep(1);
681+
g_connection_condition.wait_for(lk, std::chrono::seconds(1),
682+
[&]() { return !traj_point_interface_->connected.load(); });
642683
EXPECT_FALSE(disconnect_called_1);
643684
EXPECT_FALSE(disconnect_called_2);
644685
}
@@ -648,4 +689,4 @@ int main(int argc, char* argv[])
648689
::testing::InitGoogleTest(&argc, argv);
649690

650691
return RUN_ALL_TESTS();
651-
}
692+
}

0 commit comments

Comments
 (0)