feat(batch17): port subscription and delivery client core features

This commit is contained in:
Joseph Doherty
2026-02-28 19:12:58 -05:00
parent aeeb73f699
commit 1baba5ac0e
5 changed files with 577 additions and 1 deletions

View File

@@ -6,7 +6,9 @@ using System.Text;
using System.Linq;
using Shouldly;
using ZB.MOM.NatsNet.Server;
using ZB.MOM.NatsNet.Server.Auth;
using ZB.MOM.NatsNet.Server.Internal;
using ZB.MOM.NatsNet.Server.Internal.DataStructures;
namespace ZB.MOM.NatsNet.Server.Tests;
@@ -152,4 +154,89 @@ public sealed class ClientConnectionStubFeaturesTests
result.sub.ShouldNotBeNull();
c.Subs.Count.ShouldBe(2);
}
[Fact]
public void CanSubscribe_WithAllowAndDenyQueues_ShouldMatchExpected()
{
var c = new ClientConnection(ClientKind.Client)
{
Perms = new ClientPermissions(),
};
c.Perms.Sub.Allow = SubscriptionIndex.NewSublistWithCache();
c.Perms.Sub.Deny = SubscriptionIndex.NewSublistWithCache();
c.Perms.Sub.Allow.Insert(new Subscription
{
Subject = Encoding.ASCII.GetBytes("foo.*"),
Queue = Encoding.ASCII.GetBytes("q"),
});
c.Perms.Sub.Deny.Insert(new Subscription
{
Subject = Encoding.ASCII.GetBytes("foo.blocked"),
});
c.CanSubscribe("foo.bar", "q").ShouldBeTrue();
c.CanSubscribe("foo.bar", "other").ShouldBeFalse();
c.CanSubscribe("foo.blocked").ShouldBeFalse();
}
[Fact]
public void ProcessUnsub_WithKnownSid_ShouldRemoveSubscription()
{
var c = new ClientConnection(ClientKind.Client);
c.ParseSub(Encoding.ASCII.GetBytes("foo sid1"), noForward: false).ShouldBeNull();
c.Subs.Count.ShouldBe(1);
c.ProcessUnsub(Encoding.ASCII.GetBytes("sid1")).ShouldBeNull();
c.Subs.ShouldNotContainKey("sid1");
}
[Fact]
public void MsgHeaderAndRouteHeader_ShouldIncludeSubjectsAndSizes()
{
var c = new ClientConnection(ClientKind.Client);
c.ParseCtx.Pa.HeaderSize = 10;
c.ParseCtx.Pa.Size = 30;
c.ParseCtx.Pa.HeaderBytes = Encoding.ASCII.GetBytes("10");
c.ParseCtx.Pa.SizeBytes = Encoding.ASCII.GetBytes("30");
var sub = new Subscription { Sid = Encoding.ASCII.GetBytes("22") };
var mh = c.MsgHeader(Encoding.ASCII.GetBytes("foo.bar"), Encoding.ASCII.GetBytes("_R_.x"), sub);
Encoding.ASCII.GetString(mh).ShouldContain("foo.bar 22 _R_.x");
Encoding.ASCII.GetString(mh).ShouldContain("30");
var routeTarget = new RouteTarget { Sub = sub, Qs = Encoding.ASCII.GetBytes("q1 q2") };
var rmh = c.MsgHeaderForRouteOrLeaf(
Encoding.ASCII.GetBytes("foo.bar"),
Encoding.ASCII.GetBytes("_R_.x"),
routeTarget,
null);
Encoding.ASCII.GetString(rmh).ShouldContain("foo.bar");
Encoding.ASCII.GetString(rmh).ShouldContain("q1 q2");
}
[Fact]
public void PubAllowedFullCheck_ShouldHonorResponseReplyCache()
{
var c = new ClientConnection(ClientKind.Client)
{
Perms = new ClientPermissions
{
Resp = new ResponsePermission
{
MaxMsgs = 2,
Expires = TimeSpan.FromMinutes(1),
},
},
Replies = new Dictionary<string, RespEntry>(StringComparer.Ordinal)
{
["_R_.x"] = new RespEntry { Time = DateTime.UtcNow, N = 0 },
},
};
c.Perms.Pub.Deny = SubscriptionIndex.NewSublistWithCache();
c.Perms.Pub.Deny.Insert(new Subscription { Subject = Encoding.ASCII.GetBytes(">") });
c.PubAllowed("_R_.x").ShouldBeTrue();
c.PubAllowedFullCheck("_R_.x", fullCheck: true, hasLock: true).ShouldBeTrue();
c.PubAllowedFullCheck("_R_.x", fullCheck: true, hasLock: true).ShouldBeFalse();
}
}