149 lines
5.2 KiB
C++
149 lines
5.2 KiB
C++
#include <Void/Messaging/Queue.hpp>
|
|
#include <Void/Testing/Assert.hpp>
|
|
#include <Void/Testing/Test.hpp>
|
|
|
|
#include <chrono>
|
|
#include <string>
|
|
#include <thread>
|
|
#include <vector>
|
|
|
|
namespace
|
|
{
|
|
Void::Messaging::Message make_msg(std::string id, std::string body)
|
|
{
|
|
Void::Messaging::Message m{};
|
|
m.id = std::move(id);
|
|
m.payload.reserve(body.size());
|
|
for (char c : body) {
|
|
m.payload.push_back(static_cast<std::byte>(c));
|
|
}
|
|
return m;
|
|
}
|
|
} // namespace
|
|
|
|
Void::Testing::Test messaging_ephemeral_durability_visibility_fencing_and_dlq()
|
|
{
|
|
using namespace Void::Messaging;
|
|
using namespace std::chrono_literals;
|
|
|
|
Void::Time::ManualClock mono{};
|
|
Void::Time::ManualCivilClock civil{
|
|
std::chrono::system_clock::time_point{std::chrono::seconds{1700000000}}};
|
|
|
|
InMemoryQueue q{InMemoryQueueOptions{
|
|
.monotonic_clock = &mono,
|
|
.civil_clock = &civil,
|
|
.rng = []() -> std::uint64_t { return 0ULL; }, // zero retry delay
|
|
}};
|
|
|
|
QueueConfig qcfg{};
|
|
qcfg.name = "jobs";
|
|
qcfg.retry.max_attempts = 2;
|
|
qcfg.retry.base_delay = 1s;
|
|
qcfg.retry.max_delay = 5s;
|
|
Void::Testing::require(q.create_queue(qcfg).has_value());
|
|
Void::Testing::require(
|
|
q.create_group("jobs", GroupConfig{
|
|
.name = "workers",
|
|
.mode = ConsumerMode::Competing,
|
|
.visibility_timeout = 30s,
|
|
})
|
|
.has_value());
|
|
|
|
// Explicitly nondurable queue MUST reject PublishDurability::Durable!
|
|
auto bad_dur =
|
|
q.publish("jobs", make_msg("m1", "hello"), PublishDurability::Durable);
|
|
Void::Testing::expect(!bad_dur.has_value());
|
|
Void::Testing::expect_eq(
|
|
bad_dur.error().code().value,
|
|
static_cast<std::int32_t>(MessagingErrorCode::UnsupportedDurability));
|
|
|
|
auto pub =
|
|
q.publish("jobs", make_msg("m1", "hello"), PublishDurability::Memory);
|
|
Void::Testing::require(pub.has_value());
|
|
Void::Testing::expect(pub->durability == PublishDurability::Memory);
|
|
Void::Testing::expect(!pub->deduplicated);
|
|
|
|
// Idempotent duplicate publish within 24h returns same sequence.
|
|
auto dup =
|
|
q.publish("jobs", make_msg("m1", "hello"), PublishDurability::Memory);
|
|
Void::Testing::require(dup.has_value());
|
|
Void::Testing::expect(dup->deduplicated);
|
|
Void::Testing::expect_eq(dup->sequence, pub->sequence);
|
|
|
|
// Receive generation 1, then advance past 30s visibility timeout.
|
|
auto d1 = q.try_receive("jobs", "workers");
|
|
Void::Testing::require(d1.has_value() && d1->has_value());
|
|
Void::Testing::expect_eq((*d1)->receipt.generation, 1ULL);
|
|
Void::Testing::expect_eq((*d1)->attempt, 1U);
|
|
|
|
// Extend visibility works within 5m limit, rejects >5m.
|
|
Void::Testing::expect(
|
|
!q.extend_visibility((*d1)->receipt, std::chrono::minutes{6})
|
|
.has_value());
|
|
Void::Testing::require(
|
|
q.extend_visibility((*d1)->receipt, 20s).has_value());
|
|
|
|
// Advance past extended 20s visibility -> redelivery generation 2!
|
|
Void::Testing::require(mono.advance(25s).has_value());
|
|
auto d2 = q.try_receive("jobs", "workers");
|
|
Void::Testing::require(d2.has_value() && d2->has_value());
|
|
Void::Testing::expect_eq((*d2)->receipt.generation, 2ULL);
|
|
Void::Testing::expect_eq((*d2)->attempt, 2U);
|
|
|
|
// Stale receipt from generation 1 MUST fail with StaleReceipt!
|
|
auto stale_ack = q.ack((*d1)->receipt);
|
|
Void::Testing::expect(!stale_ack.has_value());
|
|
Void::Testing::expect_eq(
|
|
stale_ack.error().code().value,
|
|
static_cast<std::int32_t>(MessagingErrorCode::StaleReceipt));
|
|
|
|
// Exhausting attempt 2 via nack moves atomically to DLQ.
|
|
Void::Testing::require(q.nack((*d2)->receipt).has_value());
|
|
auto dlq = q.list_dead_letters("jobs", "workers");
|
|
Void::Testing::require(dlq.has_value());
|
|
Void::Testing::require(dlq->size() == 1U);
|
|
Void::Testing::expect_eq(dlq->front().message.id, std::string{"m1"});
|
|
Void::Testing::expect_eq(dlq->front().attempts, 2U);
|
|
|
|
// Test idempotent repeat ack + SingleActive head-of-line blocking!
|
|
Void::Testing::require(
|
|
q.create_group("jobs", GroupConfig{
|
|
.name = "ordered",
|
|
.mode = ConsumerMode::SingleActive,
|
|
.visibility_timeout = 30s,
|
|
})
|
|
.has_value());
|
|
Void::Testing::require(
|
|
q.publish("jobs", make_msg("m2", "a"), PublishDurability::Memory)
|
|
.has_value());
|
|
Void::Testing::require(
|
|
q.publish("jobs", make_msg("m3", "b"), PublishDurability::Memory)
|
|
.has_value());
|
|
|
|
auto s1 = q.try_receive("jobs", "ordered");
|
|
Void::Testing::require(s1.has_value() && s1->has_value());
|
|
Void::Testing::expect_eq((*s1)->message.id, std::string{"m1"});
|
|
// While head is InFlight, SingleActive blocks delivery of m2!
|
|
auto s_blocked = q.try_receive("jobs", "ordered");
|
|
Void::Testing::require(s_blocked.has_value());
|
|
Void::Testing::expect(!s_blocked->has_value());
|
|
|
|
// Ack and repeat ack of same receipt -> idempotent OK!
|
|
Void::Testing::require(q.ack((*s1)->receipt).has_value());
|
|
Void::Testing::require(q.ack((*s1)->receipt).has_value());
|
|
auto st = q.query_receipt((*s1)->receipt);
|
|
Void::Testing::require(st.has_value());
|
|
Void::Testing::expect(*st == DeliveryState::Acked);
|
|
|
|
// Next delivery is strictly m2.
|
|
auto s2 = q.try_receive("jobs", "ordered");
|
|
Void::Testing::require(s2.has_value() && s2->has_value());
|
|
Void::Testing::expect_eq((*s2)->message.id, std::string{"m2"});
|
|
Void::Testing::require(q.ack((*s2)->receipt).has_value());
|
|
|
|
auto report = q.close_and_join();
|
|
Void::Testing::require(report.has_value());
|
|
Void::Testing::expect(report->complete);
|
|
}
|