From 756f7aa2efde6d83bd81e27db1f8744c80fb7469 Mon Sep 17 00:00:00 2001 From: Silvio Traversaro Date: Fri, 22 Mar 2024 10:24:12 +0100 Subject: [PATCH] Add test for NetworkClock --- src/libYARP_os/tests/CMakeLists.txt | 1 + src/libYARP_os/tests/NetworkClockTest.cpp | 67 +++++++++++++++++++++++ 2 files changed, 68 insertions(+) create mode 100644 src/libYARP_os/tests/NetworkClockTest.cpp diff --git a/src/libYARP_os/tests/CMakeLists.txt b/src/libYARP_os/tests/CMakeLists.txt index 95405be88f..57cf8f87a8 100644 --- a/src/libYARP_os/tests/CMakeLists.txt +++ b/src/libYARP_os/tests/CMakeLists.txt @@ -19,6 +19,7 @@ target_sources(harness_os PRIVATE LogTest.cpp MessageStackTest.cpp NetTypeTest.cpp + NetworkClockTest.cpp NetworkTest.cpp NodeTest.cpp PeriodicThreadTest.cpp diff --git a/src/libYARP_os/tests/NetworkClockTest.cpp b/src/libYARP_os/tests/NetworkClockTest.cpp new file mode 100644 index 0000000000..4897b57adb --- /dev/null +++ b/src/libYARP_os/tests/NetworkClockTest.cpp @@ -0,0 +1,67 @@ +/* + * SPDX-FileCopyrightText: 2024 Istituto Italiano di Tecnologia (IIT) + * SPDX-License-Identifier: BSD-3-Clause + */ + +#include +#include +#include +#include +#include + +#include +#include + +#include +#include + +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 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(timePublished)) + { + clockNowReturnedExpecteValue = true; + break; + } + yarp::os::SystemClock::delaySystem(0.01); + } + + CHECK(clockNowReturnedExpecteValue); + } + +}