-
Notifications
You must be signed in to change notification settings - Fork 194
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3098 from traversaro/addnetworkclocktest
Add test for NetworkClock
- Loading branch information
Showing
2 changed files
with
68 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |