tests: richer MockDDS (subscriptions, queues, seq) and link into Qt/GTest smoke-tests
This commit is contained in:
1 parent
69d30bc20a
commit
bec34480c8
2 files changed
+60
-7
No files matched your search
@@ -4,24 +4,62 @@
|
||||
|
||||
namespace MockDDS {
|
||||
|
||||
static std::map<std::string, std::string> g_last_message;
|
||||
struct TopicState {
|
||||
std::vector<std::string> queue;
|
||||
std::map<SubscriptionId, Subscriber> subs;
|
||||
SubscriptionId next_sub_id{1};
|
||||
unsigned long seq{0};
|
||||
};
|
||||
|
||||
static std::map<std::string, TopicState> g_topics;
|
||||
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;
|
||||
auto &ts = g_topics[topic];
|
||||
ts.seq++;
|
||||
// annotate payload with simple seq metadata for basic QoS checks
|
||||
std::string annotated = std::to_string(ts.seq) + ":" + msg;
|
||||
ts.queue.push_back(annotated);
|
||||
// notify subscribers synchronously
|
||||
for (auto &p : ts.subs) {
|
||||
auto &cb = p.second;
|
||||
if (cb) cb(annotated);
|
||||
}
|
||||
}
|
||||
|
||||
MockDDS::SubscriptionId subscribe(const std::string &topic, Subscriber cb) {
|
||||
std::lock_guard<std::mutex> lk(g_mtx);
|
||||
auto &ts = g_topics[topic];
|
||||
SubscriptionId id = ts.next_sub_id++;
|
||||
ts.subs.emplace(id, std::move(cb));
|
||||
return id;
|
||||
}
|
||||
|
||||
void unsubscribe(const std::string &topic, SubscriptionId id) {
|
||||
std::lock_guard<std::mutex> lk(g_mtx);
|
||||
auto it = g_topics.find(topic);
|
||||
if (it == g_topics.end()) return;
|
||||
it->second.subs.erase(id);
|
||||
}
|
||||
|
||||
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;
|
||||
auto it = g_topics.find(topic);
|
||||
if (it == g_topics.end() || it->second.queue.empty()) return std::string();
|
||||
return it->second.queue.back();
|
||||
}
|
||||
|
||||
std::vector<std::string> get_queue(const std::string &topic) {
|
||||
std::lock_guard<std::mutex> lk(g_mtx);
|
||||
auto it = g_topics.find(topic);
|
||||
if (it == g_topics.end()) return {};
|
||||
return it->second.queue;
|
||||
}
|
||||
|
||||
void clear_all() {
|
||||
std::lock_guard<std::mutex> lk(g_mtx);
|
||||
g_last_message.clear();
|
||||
g_topics.clear();
|
||||
}
|
||||
|
||||
} // namespace MockDDS
|
||||
@@ -1,11 +1,26 @@
|
||||
#pragma once
|
||||
#include <string>
|
||||
#include <functional>
|
||||
#include <vector>
|
||||
|
||||
namespace MockDDS {
|
||||
|
||||
// Very small mock: single-writer, single-last-message per topic store.
|
||||
// Richer mock for migration smoke-tests:
|
||||
// - publish(topic, payload)
|
||||
// - subscribe(topic, callback) -> subscription id
|
||||
// - unsubscribe(topic, subscription id)
|
||||
// - take_last(topic), get_queue(topic)
|
||||
// - clear_all()
|
||||
// - sequence numbers per topic (as simple integer appended to payload meta)
|
||||
|
||||
using Subscriber = std::function<void(const std::string &payload)>;
|
||||
using SubscriptionId = unsigned long;
|
||||
|
||||
void publish(const std::string &topic, const std::string &msg);
|
||||
SubscriptionId subscribe(const std::string &topic, Subscriber cb);
|
||||
void unsubscribe(const std::string &topic, SubscriptionId id);
|
||||
std::string take_last(const std::string &topic);
|
||||
std::vector<std::string> get_queue(const std::string &topic);
|
||||
void clear_all();
|
||||
|
||||
}
|
||||
Reference in new issue
Block a user