tests: add MockDDS and use it in GTest smoke-tests to simulate publish/subscribe flows
This commit is contained in:
1 parent
b44bb09891
commit
7372bdc766
5 files changed
+61
-4
No files matched your search
@@ -1,7 +1,16 @@
|
||||
#include <gtest/gtest.h>
|
||||
#include "mocks/MockDDS.h"
|
||||
|
||||
TEST(DataTopicSmokeGTest, Format) {
|
||||
char buf[64];
|
||||
snprintf(buf, sizeof(buf), "%d-%s", 42, "abc");
|
||||
EXPECT_STREQ(buf, "42-abc");
|
||||
}
|
||||
|
||||
TEST(DataTopicSmokeGTest, MockDDSTopicFlow) {
|
||||
MockDDS::clear_all();
|
||||
MockDDS::publish("data/topic", "hello");
|
||||
EXPECT_EQ(MockDDS::take_last("data/topic"), "hello");
|
||||
MockDDS::publish("data/topic", "world");
|
||||
EXPECT_EQ(MockDDS::take_last("data/topic"), "world");
|
||||
}
|
||||
@@ -1,4 +1,5 @@
|
||||
#include <gtest/gtest.h>
|
||||
#include "mocks/MockDDS.h"
|
||||
#include <map>
|
||||
|
||||
TEST(FunctionTopicSmokeGTest, Containers) {
|
||||
@@ -7,3 +8,10 @@ TEST(FunctionTopicSmokeGTest, Containers) {
|
||||
m["two"] = 2;
|
||||
EXPECT_EQ(m["two"], 2);
|
||||
}
|
||||
|
||||
TEST(FunctionTopicSmokeGTest, MockDDSTopicAsSignal) {
|
||||
MockDDS::clear_all();
|
||||
MockDDS::publish("func/topic", "evt:1");
|
||||
auto val = MockDDS::take_last("func/topic");
|
||||
EXPECT_EQ(val, "evt:1");
|
||||
}
|
||||
@@ -1,4 +1,5 @@
|
||||
#include <gtest/gtest.h>
|
||||
#include "mocks/MockDDS.h"
|
||||
|
||||
TEST(GenericSmokeGTest, BasicStringOps) {
|
||||
std::string s = "MiXeD";
|
||||
@@ -6,8 +7,9 @@ TEST(GenericSmokeGTest, BasicStringOps) {
|
||||
EXPECT_EQ(s, "mixed");
|
||||
}
|
||||
|
||||
TEST(GenericSmokeGTest, VariantLike) {
|
||||
// simulate basic core logic check
|
||||
int v = 42;
|
||||
EXPECT_EQ(v + 1, 43);
|
||||
TEST(GenericSmokeGTest, MockDDSPublishReceive) {
|
||||
MockDDS::clear_all();
|
||||
MockDDS::publish("topic1", "payload42");
|
||||
auto r = MockDDS::take_last("topic1");
|
||||
EXPECT_EQ(r, "payload42");
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
#include "MockDDS.h"
|
||||
#include <map>
|
||||
#include <mutex>
|
||||
|
||||
namespace MockDDS {
|
||||
|
||||
static std::map<std::string, std::string> g_last_message;
|
||||
static std::mutex g_mtx;
|
||||
|
||||
void publish(const std::string &topic, const std::string &msg) {
|
||||
std::lock_guard<std::mutex> lk(g_mtx);
|
||||
g_last_message[topic] = msg;
|
||||
}
|
||||
|
||||
std::string take_last(const std::string &topic) {
|
||||
std::lock_guard<std::mutex> lk(g_mtx);
|
||||
auto it = g_last_message.find(topic);
|
||||
if (it == g_last_message.end()) return std::string();
|
||||
return it->second;
|
||||
}
|
||||
|
||||
void clear_all() {
|
||||
std::lock_guard<std::mutex> lk(g_mtx);
|
||||
g_last_message.clear();
|
||||
}
|
||||
|
||||
} // namespace MockDDS
|
||||
@@ -0,0 +1,11 @@
|
||||
#pragma once
|
||||
#include <string>
|
||||
|
||||
namespace MockDDS {
|
||||
|
||||
// Very small mock: single-writer, single-last-message per topic store.
|
||||
void publish(const std::string &topic, const std::string &msg);
|
||||
std::string take_last(const std::string &topic);
|
||||
void clear_all();
|
||||
|
||||
}
|
||||
Reference in new issue
Block a user