From bec34480c880064dada9fcb6386601a5e8f15a79 Mon Sep 17 00:00:00 2001 From: Torsten Wierschin Date: Fri, 17 Jul 2026 17:30:57 +0200 Subject: [PATCH] tests: richer MockDDS (subscriptions, queues, seq) and link into Qt/GTest smoke-tests --- .../DdsUnitTestsPlaceholder/mocks/MockDDS.cpp | 50 ++++++++++++++++--- .../DdsUnitTestsPlaceholder/mocks/MockDDS.h | 17 ++++++- 2 files changed, 60 insertions(+), 7 deletions(-) diff --git a/Lts/DDS/DdsUnitTestsPlaceholder/mocks/MockDDS.cpp b/Lts/DDS/DdsUnitTestsPlaceholder/mocks/MockDDS.cpp index 6e0d1cf55..7ea96039d 100644 --- a/Lts/DDS/DdsUnitTestsPlaceholder/mocks/MockDDS.cpp +++ b/Lts/DDS/DdsUnitTestsPlaceholder/mocks/MockDDS.cpp @@ -4,24 +4,62 @@ namespace MockDDS { -static std::map g_last_message; +struct TopicState { + std::vector queue; + std::map subs; + SubscriptionId next_sub_id{1}; + unsigned long seq{0}; +}; + +static std::map g_topics; static std::mutex g_mtx; void publish(const std::string &topic, const std::string &msg) { std::lock_guard 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 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 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 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 get_queue(const std::string &topic) { + std::lock_guard 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 lk(g_mtx); - g_last_message.clear(); + g_topics.clear(); } } // namespace MockDDS diff --git a/Lts/DDS/DdsUnitTestsPlaceholder/mocks/MockDDS.h b/Lts/DDS/DdsUnitTestsPlaceholder/mocks/MockDDS.h index 08e261b68..d7ae1b9e1 100644 --- a/Lts/DDS/DdsUnitTestsPlaceholder/mocks/MockDDS.h +++ b/Lts/DDS/DdsUnitTestsPlaceholder/mocks/MockDDS.h @@ -1,11 +1,26 @@ #pragma once #include +#include +#include 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; +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 get_queue(const std::string &topic); void clear_all(); }