feat: port session 01 — Foundation Types (const, errors, proto, ring, rate_counter, sdm)

Ports server/const.go, errors.go, proto.go, ring.go, rate_counter.go, sdm.go.
- ServerConstants: all protocol constants and version info from const.go
- ServerErrors: ~60 sentinel exceptions plus errCtx/configErr/processConfigErr types
- ProtoWire: protobuf varint encode/decode helpers (proto.go)
- RateCounter: sliding-window rate limiter (rate_counter.go)
- ClosedRingBuffer: fixed-size ring buffer for /connz (ring.go)
- StreamDeletionMeta: SDM tracking for JetStream cluster consensus (sdm.go)
- 5 unit tests passing (errors, ring buffer, rate counter)
- errors_gen.go (code generator tool) and nkey.go Server methods marked n_a
This commit is contained in:
Joseph Doherty
2026-02-26 09:15:20 -05:00
parent 66628bc25a
commit 8050ee1897
12 changed files with 1538 additions and 9 deletions

View File

@@ -0,0 +1,87 @@
// Copyright 2020-2025 The NATS Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
using Shouldly;
using ZB.MOM.NatsNet.Server;
namespace ZB.MOM.NatsNet.Server.Tests.Foundation;
/// <summary>
/// Tests for <see cref="ErrorCtx"/> and <see cref="ErrorContextHelper"/>.
/// Mirrors server/errors_test.go: TestErrCtx (ID 297) and TestErrCtxWrapped (ID 298).
/// </summary>
public sealed class ServerErrorsTests
{
[Fact]
public void ErrCtx_ShouldPreserveOriginalMessageAndAddContext()
{
// Mirror: TestErrCtx
var ctx = "Extra context information";
var e = ErrorContextHelper.NewErrorCtx(ServerErrors.ErrWrongGateway, "{0}", ctx);
// Message should match the underlying error.
e.Message.ShouldBe(ServerErrors.ErrWrongGateway.Message);
// Must not be reference-equal to the sentinel.
e.ShouldNotBeSameAs(ServerErrors.ErrWrongGateway);
// ErrorIs should find the sentinel in the chain.
ErrorContextHelper.ErrorIs(e, ServerErrors.ErrWrongGateway).ShouldBeTrue();
// UnpackIfErrorCtx on a non-ctx error returns Message unchanged.
ErrorContextHelper.UnpackIfErrorCtx(ServerErrors.ErrWrongGateway)
.ShouldBe(ServerErrors.ErrWrongGateway.Message);
// UnpackIfErrorCtx should start with the original error message.
var trace = ErrorContextHelper.UnpackIfErrorCtx(e);
trace.ShouldStartWith(ServerErrors.ErrWrongGateway.Message);
// And end with the context string.
trace.ShouldEndWith(ctx);
}
[Fact]
public void ErrCtxWrapped_ShouldContainAllContextLayers()
{
// Mirror: TestErrCtxWrapped
var ctxO = "Original Ctx";
var eO = ErrorContextHelper.NewErrorCtx(ServerErrors.ErrWrongGateway, "{0}", ctxO);
var ctx = "Extra context information";
var e = ErrorContextHelper.NewErrorCtx(eO, "{0}", ctx);
// Message should still match the underlying error.
e.Message.ShouldBe(ServerErrors.ErrWrongGateway.Message);
// Must not be reference-equal to the sentinel.
e.ShouldNotBeSameAs(ServerErrors.ErrWrongGateway);
// ErrorIs should walk the chain.
ErrorContextHelper.ErrorIs(e, ServerErrors.ErrWrongGateway).ShouldBeTrue();
// UnpackIfErrorCtx on a non-ctx error returns Message unchanged.
ErrorContextHelper.UnpackIfErrorCtx(ServerErrors.ErrWrongGateway)
.ShouldBe(ServerErrors.ErrWrongGateway.Message);
var trace = ErrorContextHelper.UnpackIfErrorCtx(e);
// Must start with the original error.
trace.ShouldStartWith(ServerErrors.ErrWrongGateway.Message);
// Must end with the outermost context.
trace.ShouldEndWith(ctx);
// Must also contain the inner context.
trace.ShouldContain(ctxO);
}
}

View File

@@ -0,0 +1,79 @@
// Copyright 2018-2025 The NATS Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
using Shouldly;
using ZB.MOM.NatsNet.Server.Internal;
namespace ZB.MOM.NatsNet.Server.Tests.Internal;
/// <summary>
/// Tests for <see cref="ClosedRingBuffer"/>.
/// Mirrors server/ring_test.go: TestRBAppendAndLenAndTotal (ID 2794)
/// and TestRBclosedClients (ID 2795).
/// </summary>
public sealed class ClosedRingBufferTests
{
[Fact]
public void AppendAndLenAndTotal_ShouldTrackCorrectly()
{
// Mirror: TestRBAppendAndLenAndTotal
var rb = new ClosedRingBuffer(10);
for (var i = 0; i < 5; i++)
rb.Append(new ClosedClient());
rb.Len().ShouldBe(5);
rb.TotalConns().ShouldBe(5UL);
for (var i = 0; i < 25; i++)
rb.Append(new ClosedClient());
rb.Len().ShouldBe(10);
rb.TotalConns().ShouldBe(30UL);
}
[Fact]
public void ClosedClients_ShouldReturnChronologicalOrder()
{
// Mirror: TestRBclosedClients
var rb = new ClosedRingBuffer(10);
// Build master list with identifiable user strings.
const int max = 100;
var master = new ClosedClient[max];
for (var i = 1; i <= max; i++)
master[i - 1] = new ClosedClient { User = i.ToString() };
var ui = 0;
void AddConn()
{
ui++;
rb.Append(new ClosedClient { User = ui.ToString() });
}
for (var i = 0; i < max; i++)
{
AddConn();
var ccs = rb.ClosedClients();
var start = (int)rb.TotalConns() - ccs.Length;
var ms = master[start..(start + ccs.Length)];
// Verify order matches master using User strings.
ccs.Length.ShouldBe(ms.Length);
for (var j = 0; j < ccs.Length; j++)
ccs[j]!.User.ShouldBe(ms[j].User, $"iteration {i}, slot {j}");
}
}
}

View File

@@ -0,0 +1,47 @@
// Copyright 2021-2025 The NATS Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
using Shouldly;
using ZB.MOM.NatsNet.Server.Internal;
namespace ZB.MOM.NatsNet.Server.Tests.Internal;
/// <summary>
/// Tests for <see cref="RateCounter"/>.
/// Mirrors server/rate_counter_test.go: TestRateCounter (ID 2720).
/// </summary>
public sealed class RateCounterTests
{
[Fact]
public async Task RateCounter_ShouldAllowUpToLimitThenBlockAndReset()
{
// Mirror: TestRateCounter
var counter = new RateCounter(10) { Interval = TimeSpan.FromMilliseconds(100) };
// First 10 calls should be allowed (counts 09 < limit 10).
for (var i = 0; i < 10; i++)
counter.Allow().ShouldBeTrue($"should allow on iteration {i}");
// Next 5 should be blocked.
for (var i = 0; i < 5; i++)
counter.Allow().ShouldBeFalse($"should not allow on iteration {i}");
// countBlocked returns and resets the blocked count.
counter.CountBlocked().ShouldBe(5UL);
counter.CountBlocked().ShouldBe(0UL);
// After the window expires, should allow again.
await Task.Delay(150);
counter.Allow().ShouldBeTrue("should allow after window expired");
}
}