- Add Microsoft.Extensions.Logging + Serilog to NatsServer and NatsClient - Convert all test assertions from xUnit Assert to Shouldly - Add NSubstitute package for future mocking needs - Introduce Central Package Management via Directory.Packages.props - Add documentation_rules.md with style guide, generation/update rules, component map - Generate 10 documentation files across 5 component folders (GettingStarted, Protocol, Subscriptions, Server, Configuration/Operations) - Update CLAUDE.md with logging, testing, porting, agent model, CPM, and documentation guidance
160 lines
4.2 KiB
C#
160 lines
4.2 KiB
C#
using NATS.Server.Subscriptions;
|
|
|
|
namespace NATS.Server.Tests;
|
|
|
|
public class SubListTests
|
|
{
|
|
private static Subscription MakeSub(string subject, string? queue = null, string sid = "1")
|
|
=> new() { Subject = subject, Queue = queue, Sid = sid };
|
|
|
|
[Fact]
|
|
public void Insert_and_match_literal_subject()
|
|
{
|
|
var sl = new SubList();
|
|
var sub = MakeSub("foo.bar");
|
|
sl.Insert(sub);
|
|
|
|
var r = sl.Match("foo.bar");
|
|
r.PlainSubs.ShouldHaveSingleItem();
|
|
r.PlainSubs[0].ShouldBeSameAs(sub);
|
|
r.QueueSubs.ShouldBeEmpty();
|
|
}
|
|
|
|
[Fact]
|
|
public void Match_returns_empty_for_no_match()
|
|
{
|
|
var sl = new SubList();
|
|
sl.Insert(MakeSub("foo.bar"));
|
|
|
|
var r = sl.Match("foo.baz");
|
|
r.PlainSubs.ShouldBeEmpty();
|
|
}
|
|
|
|
[Fact]
|
|
public void Match_partial_wildcard()
|
|
{
|
|
var sl = new SubList();
|
|
var sub = MakeSub("foo.*");
|
|
sl.Insert(sub);
|
|
|
|
sl.Match("foo.bar").PlainSubs.ShouldHaveSingleItem();
|
|
sl.Match("foo.baz").PlainSubs.ShouldHaveSingleItem();
|
|
sl.Match("foo.bar.baz").PlainSubs.ShouldBeEmpty();
|
|
}
|
|
|
|
[Fact]
|
|
public void Match_full_wildcard()
|
|
{
|
|
var sl = new SubList();
|
|
var sub = MakeSub("foo.>");
|
|
sl.Insert(sub);
|
|
|
|
sl.Match("foo.bar").PlainSubs.ShouldHaveSingleItem();
|
|
sl.Match("foo.bar.baz").PlainSubs.ShouldHaveSingleItem();
|
|
sl.Match("foo").PlainSubs.ShouldBeEmpty();
|
|
}
|
|
|
|
[Fact]
|
|
public void Match_root_full_wildcard()
|
|
{
|
|
var sl = new SubList();
|
|
sl.Insert(MakeSub(">"));
|
|
|
|
sl.Match("foo").PlainSubs.ShouldHaveSingleItem();
|
|
sl.Match("foo.bar").PlainSubs.ShouldHaveSingleItem();
|
|
sl.Match("foo.bar.baz").PlainSubs.ShouldHaveSingleItem();
|
|
}
|
|
|
|
[Fact]
|
|
public void Match_collects_multiple_subs()
|
|
{
|
|
var sl = new SubList();
|
|
sl.Insert(MakeSub("foo.bar", sid: "1"));
|
|
sl.Insert(MakeSub("foo.*", sid: "2"));
|
|
sl.Insert(MakeSub("foo.>", sid: "3"));
|
|
sl.Insert(MakeSub(">", sid: "4"));
|
|
|
|
var r = sl.Match("foo.bar");
|
|
r.PlainSubs.Length.ShouldBe(4);
|
|
}
|
|
|
|
[Fact]
|
|
public void Remove_subscription()
|
|
{
|
|
var sl = new SubList();
|
|
var sub = MakeSub("foo.bar");
|
|
sl.Insert(sub);
|
|
sl.Match("foo.bar").PlainSubs.ShouldHaveSingleItem();
|
|
|
|
sl.Remove(sub);
|
|
sl.Match("foo.bar").PlainSubs.ShouldBeEmpty();
|
|
}
|
|
|
|
[Fact]
|
|
public void Queue_group_subscriptions()
|
|
{
|
|
var sl = new SubList();
|
|
sl.Insert(MakeSub("foo.bar", queue: "workers", sid: "1"));
|
|
sl.Insert(MakeSub("foo.bar", queue: "workers", sid: "2"));
|
|
sl.Insert(MakeSub("foo.bar", queue: "loggers", sid: "3"));
|
|
|
|
var r = sl.Match("foo.bar");
|
|
r.PlainSubs.ShouldBeEmpty();
|
|
r.QueueSubs.Length.ShouldBe(2); // 2 queue groups
|
|
}
|
|
|
|
[Fact]
|
|
public void Count_tracks_subscriptions()
|
|
{
|
|
var sl = new SubList();
|
|
sl.Count.ShouldBe(0u);
|
|
|
|
sl.Insert(MakeSub("foo", sid: "1"));
|
|
sl.Insert(MakeSub("bar", sid: "2"));
|
|
sl.Count.ShouldBe(2u);
|
|
|
|
sl.Remove(MakeSub("foo", sid: "1"));
|
|
// Remove by reference won't work — we need the same instance
|
|
}
|
|
|
|
[Fact]
|
|
public void Count_tracks_with_same_instance()
|
|
{
|
|
var sl = new SubList();
|
|
var sub = MakeSub("foo");
|
|
sl.Insert(sub);
|
|
sl.Count.ShouldBe(1u);
|
|
sl.Remove(sub);
|
|
sl.Count.ShouldBe(0u);
|
|
}
|
|
|
|
[Fact]
|
|
public void Cache_invalidation_on_insert()
|
|
{
|
|
var sl = new SubList();
|
|
sl.Insert(MakeSub("foo.bar", sid: "1"));
|
|
|
|
// Prime the cache
|
|
var r1 = sl.Match("foo.bar");
|
|
r1.PlainSubs.ShouldHaveSingleItem();
|
|
|
|
// Insert a wildcard that matches — cache should be invalidated
|
|
sl.Insert(MakeSub("foo.*", sid: "2"));
|
|
|
|
var r2 = sl.Match("foo.bar");
|
|
r2.PlainSubs.Length.ShouldBe(2);
|
|
}
|
|
|
|
[Fact]
|
|
public void Match_partial_wildcard_at_different_levels()
|
|
{
|
|
var sl = new SubList();
|
|
sl.Insert(MakeSub("*.bar.baz", sid: "1"));
|
|
sl.Insert(MakeSub("foo.*.baz", sid: "2"));
|
|
sl.Insert(MakeSub("foo.bar.*", sid: "3"));
|
|
|
|
var r = sl.Match("foo.bar.baz");
|
|
r.PlainSubs.Length.ShouldBe(3);
|
|
}
|
|
}
|