Skip to content

Commit

Permalink
Merge pull request #3098 from traversaro/addnetworkclocktest
Browse files Browse the repository at this point in the history
Add test for NetworkClock
  • Loading branch information
randaz81 authored Apr 2, 2024
2 parents 2e513a6 + 756f7aa commit ad79d1f
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/libYARP_os/tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ target_sources(harness_os PRIVATE
LogTest.cpp
MessageStackTest.cpp
NetTypeTest.cpp
NetworkClockTest.cpp
NetworkTest.cpp
NodeTest.cpp
PeriodicThreadTest.cpp
Expand Down
67 changes: 67 additions & 0 deletions src/libYARP_os/tests/NetworkClockTest.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* SPDX-FileCopyrightText: 2024 Istituto Italiano di Tecnologia (IIT)
* SPDX-License-Identifier: BSD-3-Clause
*/

#include <yarp/os/Bottle.h>
#include <yarp/os/BufferedPort.h>
#include <yarp/os/Network.h>
#include <yarp/os/NetworkClock.h>
#include <yarp/os/SystemClock.h>

#include <cstdlib>
#include <cstring>

#include <catch2/catch_amalgamated.hpp>
#include <harness.h>

using namespace yarp::os;

TEST_CASE("os::NetworkClockTest", "[yarp::os]")
{
NetworkBase::setLocalMode(true);

SECTION("checking network clock reads correctly")
{
std::string nameOfClockPortUsedForTest = "/nameOfClockPortUsedForTest";

// Open port in which we publish the clock
BufferedPort<Bottle> clockPort;
CHECK(clockPort.open(nameOfClockPortUsedForTest));

// Publish 24.0 seconds to the clock
Bottle& b = clockPort.prepare();
b.clear();
b.addInt32(24);
b.addInt32(0);
clockPort.write();

// Open network clock
yarp::os::NetworkClock netClock;
CHECK(netClock.open(nameOfClockPortUsedForTest));

// Check that the network clock returns timePublished, at least after some attempts
bool clockNowReturnedExpecteValue = false;
for(size_t i=0; i < 1000; i++)
{
// Publish the time timePublished
b = clockPort.prepare();
b.clear();
int32_t timePublished = 42;
b.addInt32(timePublished);
b.addInt32(0);
clockPort.write();

// Check if it has been received
if (netClock.now() == static_cast<double>(timePublished))
{
clockNowReturnedExpecteValue = true;
break;
}
yarp::os::SystemClock::delaySystem(0.01);
}

CHECK(clockNowReturnedExpecteValue);
}

}

0 comments on commit ad79d1f

Please sign in to comment.