// Step 9: pubsub broker fan-out, ordering, drops, patterns. #include #include #include #include #include "mini.hpp" #include "pubsub/broker.hpp" #include "pubsub/channel.hpp" using namespace molo::data::pubsub; static Payload to_payload(int n) { const std::string s = std::to_string(n); return Payload(s.begin(), s.end()); } static int from_payload(const Payload& p) { return std::stoi(std::string(p.begin(), p.end())); } int main() { // Pattern matching rules. CHECK(pattern_match("orders:created", "orders:created")); CHECK(pattern_match("orders:created", "orders:*")); CHECK(pattern_match("orders:created", "*:created")); CHECK(pattern_match("a:b:c", "*:b:*")); CHECK(!pattern_match("orders:created", "orders:deleted")); CHECK(!pattern_match("a:b", "a:b:c")); CHECK(!pattern_match("a:b:c", "a:*")); // 10k messages fan out to 100 subscribers in order. { Broker broker; const int kSubs = 100; const int kMsgs = 10000; std::vector> received(kSubs); std::vector ids; for (int s = 0; s < kSubs; ++s) ids.push_back(broker.subscribe( "events:all", [&, s](const Message& m) { received[s].push_back(from_payload(m.payload)); })); for (int i = 0; i < kMsgs; ++i) { DeliveryReport r = broker.publish("events:all", to_payload(i), "text"); CHECK(!r.no_subscribers && r.callbacks == kSubs); } CHECK(broker.published() == kMsgs); for (int s = 0; s < kSubs; ++s) { CHECK(static_cast(received[s].size()) == kMsgs); for (int i = 0; i < kMsgs; ++i) if (received[s][i] != i) { CHECK(false); break; } } for (int id : ids) broker.unsubscribe(id); DeliveryReport r = broker.publish("events:all", to_payload(0)); CHECK(r.no_subscribers && broker.no_subscriber() == 1); } // Slow queued subscriber drops oldest, counted; publisher never blocks. { Broker broker; auto sub = broker.subscribe_queued("slow:work"); const int kMsgs = 3000; int dropped_reports = 0; for (int i = 0; i < kMsgs; ++i) { DeliveryReport r = broker.publish("slow:work", to_payload(i)); dropped_reports += r.dropped; } CHECK(sub->depth() == 1024); CHECK(sub->dropped() == kMsgs - 1024); CHECK(dropped_reports == kMsgs - 1024); auto batch = sub->poll(); CHECK(static_cast(batch.size()) == 1024); // Oldest surviving message is kMsgs - 1024. CHECK(from_payload(batch.front().payload) == kMsgs - 1024); CHECK(from_payload(batch.back().payload) == kMsgs - 1); } // Patterns route, exact names do not leak across topics. { Broker broker; std::atomic wild = 0; std::atomic exact = 0; broker.subscribe("orders:*", [&](const Message&) { ++wild; }); broker.subscribe("orders:created", [&](const Message&) { ++exact; }); broker.publish("orders:created", to_payload(1)); broker.publish("orders:deleted", to_payload(2)); broker.publish("users:created", to_payload(3)); CHECK(wild == 2 && exact == 1); } // Payload cap enforced, channel names validated. { Broker broker; bool threw = false; try { broker.publish("big:one", Payload(1024 * 1024 + 1, 0)); } catch (const std::invalid_argument&) { threw = true; } CHECK(threw); threw = false; try { broker.subscribe("bad name", [](const Message&) {}); } catch (const std::invalid_argument&) { threw = true; } CHECK(threw); } // Concurrent publishers do not lose inline deliveries. { Broker broker; std::atomic total = 0; std::mutex mutex; broker.subscribe("race:x", [&](const Message&) { std::lock_guard lock(mutex); ++total; }); std::vector threads; for (int t = 0; t < 8; ++t) { threads.emplace_back([&, t] { for (int i = 0; i < 500; ++i) broker.publish("race:x", to_payload(t * 1000 + i)); }); } for (auto& th : threads) th.join(); CHECK(total == 4000); } SUITE_DONE("pubsub"); }