tests(dds): enable GTest smoke-tests and add async-capable realistic MockDDS with GTest async subscription test
This commit is contained in:
1 parent
14988660cd
commit
6d8d843ee0
4 files changed
+115
-8
No files matched your search
@@ -1,5 +1,7 @@
|
||||
#include <gtest/gtest.h>
|
||||
#include "mocks/MockDDS.h"
|
||||
#include <mutex>
|
||||
#include <condition_variable>
|
||||
|
||||
TEST(GenericSmokeGTest, BasicStringOps) {
|
||||
std::string s = "MiXeD";
|
||||
@@ -19,3 +21,35 @@ TEST(GenericSmokeGTest, MockDDSPublishReceive) {
|
||||
EXPECT_EQ(payload, "payload42");
|
||||
EXPECT_EQ(seq, 1UL);
|
||||
}
|
||||
|
||||
TEST(GenericSmokeGTest, MockDDSAsyncDelivery) {
|
||||
MockDDS::clear_all();
|
||||
std::mutex mtx;
|
||||
std::condition_variable cv;
|
||||
bool received = false;
|
||||
std::string last;
|
||||
|
||||
auto cb = [&](const std::string &payload){
|
||||
std::lock_guard<std::mutex> lk(mtx);
|
||||
last = payload;
|
||||
received = true;
|
||||
cv.notify_one();
|
||||
};
|
||||
|
||||
auto sub = MockDDS::subscribe("topic-async", cb);
|
||||
// publish asynchronously with small delay; callback should be invoked
|
||||
MockDDS::publish_async("topic-async", "async-payload", 20);
|
||||
|
||||
std::unique_lock<std::mutex> lk(mtx);
|
||||
if (!cv.wait_for(lk, std::chrono::seconds(2), [&]{ return received; })) {
|
||||
// timeout
|
||||
MockDDS::unsubscribe("topic-async", sub);
|
||||
FAIL() << "Did not receive async callback in time";
|
||||
}
|
||||
MockDDS::unsubscribe("topic-async", sub);
|
||||
// parse annotation
|
||||
auto pos = last.find(':');
|
||||
ASSERT_NE(pos, std::string::npos);
|
||||
auto payload = last.substr(pos + 1);
|
||||
EXPECT_EQ(payload, "async-payload");
|
||||
}
|
||||
Reference in new issue
Block a user