Fix E2E test gaps and add comprehensive E2E + parity test suites
- Fix pull consumer fetch: send original stream subject in HMSG (not inbox) so NATS client distinguishes data messages from control messages - Fix MaxAge expiry: add background timer in StreamManager for periodic pruning - Fix JetStream wire format: Go-compatible anonymous objects with string enums, proper offset-based pagination for stream/consumer list APIs - Add 42 E2E black-box tests (core messaging, auth, TLS, accounts, JetStream) - Add ~1000 parity tests across all subsystems (gaps closure) - Update gap inventory docs to reflect implementation status
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
using System.Buffers;
|
||||
using System.Text;
|
||||
using System.Text.Json;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace NATS.Server.Protocol;
|
||||
@@ -80,7 +81,14 @@ public sealed class NatsParser
|
||||
|
||||
// Control line size check
|
||||
if (line.Length > NatsProtocol.MaxControlLineSize)
|
||||
throw new ProtocolViolationException("Maximum control line exceeded");
|
||||
{
|
||||
var snippetLength = (int)Math.Min(line.Length, NatsProtocol.MaxControlLineSnippetSize);
|
||||
var snippetBytes = new byte[snippetLength];
|
||||
line.Slice(0, snippetLength).CopyTo(snippetBytes);
|
||||
var snippet = ProtoSnippet(0, NatsProtocol.MaxControlLineSnippetSize, snippetBytes);
|
||||
throw new ProtocolViolationException(
|
||||
$"Maximum control line exceeded (max={NatsProtocol.MaxControlLineSize}, len={line.Length}, snip={snippet}...)");
|
||||
}
|
||||
|
||||
// Get line as contiguous span
|
||||
Span<byte> lineSpan = stackalloc byte[(int)line.Length];
|
||||
@@ -95,7 +103,7 @@ public sealed class NatsParser
|
||||
return false;
|
||||
}
|
||||
|
||||
throw new ProtocolViolationException("Unknown protocol operation");
|
||||
throw new ProtocolViolationException($"Unknown protocol operation: {ProtoSnippet(lineSpan)}");
|
||||
}
|
||||
|
||||
byte b0 = (byte)(lineSpan[0] | 0x20); // lowercase
|
||||
@@ -192,9 +200,29 @@ public sealed class NatsParser
|
||||
return true;
|
||||
}
|
||||
|
||||
throw new ProtocolViolationException("Unknown protocol operation");
|
||||
throw new ProtocolViolationException($"Unknown protocol operation: {ProtoSnippet(lineSpan)}");
|
||||
}
|
||||
|
||||
// Go reference: parser.go protoSnippet(start, max, buf).
|
||||
internal static string ProtoSnippet(int start, int max, ReadOnlySpan<byte> buffer)
|
||||
{
|
||||
if (start >= buffer.Length)
|
||||
return "\"\"";
|
||||
|
||||
var stop = start + max;
|
||||
if (stop > buffer.Length)
|
||||
stop = buffer.Length - 1;
|
||||
|
||||
if (stop <= start)
|
||||
return "\"\"";
|
||||
|
||||
var slice = buffer[start..stop];
|
||||
return JsonSerializer.Serialize(Encoding.ASCII.GetString(slice));
|
||||
}
|
||||
|
||||
internal static string ProtoSnippet(ReadOnlySpan<byte> buffer) =>
|
||||
ProtoSnippet(0, NatsProtocol.ProtoSnippetSize, buffer);
|
||||
|
||||
private bool ParsePub(
|
||||
Span<byte> line,
|
||||
ref ReadOnlySequence<byte> buffer,
|
||||
|
||||
@@ -5,9 +5,46 @@ namespace NATS.Server.Protocol;
|
||||
public static class NatsProtocol
|
||||
{
|
||||
public const int MaxControlLineSize = 4096;
|
||||
public const int MaxControlLineSnippetSize = 128;
|
||||
public const int ProtoSnippetSize = 32;
|
||||
public const int MaxPayloadSize = 1024 * 1024; // 1MB
|
||||
public const int MaxPayloadMaxSize = 8 * 1024 * 1024; // 8MB
|
||||
public const long MaxPendingSize = 64 * 1024 * 1024; // 64MB default max pending
|
||||
public const string DefaultHost = "0.0.0.0";
|
||||
public const int DefaultPort = 4222;
|
||||
public const int DefaultHttpPort = 8222;
|
||||
public const string DefaultHttpBasePath = "/";
|
||||
public const int DefaultRoutePoolSize = 3;
|
||||
public const int DefaultLeafNodePort = 7422;
|
||||
public const int DefaultMaxConnections = 64 * 1024;
|
||||
public const int DefaultPingMaxOut = 2;
|
||||
public const int DefaultMaxClosedClients = 10_000;
|
||||
public const int DefaultConnectErrorReports = 3600;
|
||||
public const int DefaultReconnectErrorReports = 1;
|
||||
public const int DefaultAllowResponseMaxMsgs = 1;
|
||||
public const int DefaultServiceLatencySampling = 100;
|
||||
public const string DefaultSystemAccount = "$SYS";
|
||||
public const string DefaultGlobalAccount = "$G";
|
||||
public static readonly TimeSpan TlsTimeout = TimeSpan.FromSeconds(2);
|
||||
public static readonly TimeSpan DefaultTlsHandshakeFirstFallbackDelay = TimeSpan.FromMilliseconds(50);
|
||||
public static readonly TimeSpan AuthTimeout = TimeSpan.FromSeconds(2);
|
||||
public static readonly TimeSpan DefaultRouteConnect = TimeSpan.FromSeconds(1);
|
||||
public static readonly TimeSpan DefaultRouteConnectMax = TimeSpan.FromSeconds(30);
|
||||
public static readonly TimeSpan DefaultRouteReconnect = TimeSpan.FromSeconds(1);
|
||||
public static readonly TimeSpan DefaultRouteDial = TimeSpan.FromSeconds(1);
|
||||
public static readonly TimeSpan DefaultLeafNodeReconnect = TimeSpan.FromSeconds(1);
|
||||
public static readonly TimeSpan DefaultLeafTlsTimeout = TimeSpan.FromSeconds(2);
|
||||
public static readonly TimeSpan DefaultLeafNodeInfoWait = TimeSpan.FromSeconds(1);
|
||||
public static readonly TimeSpan DefaultRttMeasurementInterval = TimeSpan.FromHours(1);
|
||||
public static readonly TimeSpan DefaultAllowResponseExpiration = TimeSpan.FromMinutes(2);
|
||||
public static readonly TimeSpan DefaultServiceExportResponseThreshold = TimeSpan.FromMinutes(2);
|
||||
public static readonly TimeSpan DefaultAccountFetchTimeout = TimeSpan.FromMilliseconds(1900);
|
||||
public static readonly TimeSpan DefaultPingInterval = TimeSpan.FromMinutes(2);
|
||||
public static readonly TimeSpan DefaultFlushDeadline = TimeSpan.FromSeconds(10);
|
||||
public static readonly TimeSpan AcceptMinSleep = TimeSpan.FromMilliseconds(10);
|
||||
public static readonly TimeSpan AcceptMaxSleep = TimeSpan.FromSeconds(1);
|
||||
public static readonly TimeSpan DefaultLameDuckDuration = TimeSpan.FromMinutes(2);
|
||||
public static readonly TimeSpan DefaultLameDuckGracePeriod = TimeSpan.FromSeconds(10);
|
||||
public const string Version = "0.1.0";
|
||||
public const int ProtoVersion = 1;
|
||||
|
||||
|
||||
89
src/NATS.Server/Protocol/ProtoWire.cs
Normal file
89
src/NATS.Server/Protocol/ProtoWire.cs
Normal file
@@ -0,0 +1,89 @@
|
||||
namespace NATS.Server.Protocol;
|
||||
|
||||
public static class ProtoWire
|
||||
{
|
||||
public const string ErrProtoInsufficient = "insufficient data to read a value";
|
||||
public const string ErrProtoOverflow = "too much data for a value";
|
||||
public const string ErrProtoInvalidFieldNumber = "invalid field number";
|
||||
|
||||
public static (int Number, int WireType, int Size) ScanField(ReadOnlySpan<byte> buffer)
|
||||
{
|
||||
var (number, wireType, tagSize) = ScanTag(buffer);
|
||||
var valueSize = ScanFieldValue(wireType, buffer[tagSize..]);
|
||||
return (number, wireType, tagSize + valueSize);
|
||||
}
|
||||
|
||||
public static (int Number, int WireType, int Size) ScanTag(ReadOnlySpan<byte> buffer)
|
||||
{
|
||||
var (tag, size) = ScanVarint(buffer);
|
||||
var fieldNumber = tag >> 3;
|
||||
if (fieldNumber > int.MaxValue || fieldNumber < 1)
|
||||
throw new ProtoWireException(ErrProtoInvalidFieldNumber);
|
||||
|
||||
return ((int)fieldNumber, (int)(tag & 0x7), size);
|
||||
}
|
||||
|
||||
public static int ScanFieldValue(int wireType, ReadOnlySpan<byte> buffer)
|
||||
{
|
||||
return wireType switch
|
||||
{
|
||||
0 => ScanVarint(buffer).Size,
|
||||
5 => 4,
|
||||
1 => 8,
|
||||
2 => ScanBytes(buffer),
|
||||
_ => throw new ProtoWireException($"unsupported type: {wireType}"),
|
||||
};
|
||||
}
|
||||
|
||||
public static (ulong Value, int Size) ScanVarint(ReadOnlySpan<byte> buffer)
|
||||
{
|
||||
ulong value = 0;
|
||||
|
||||
for (var i = 0; i < 10; i++)
|
||||
{
|
||||
if (i >= buffer.Length)
|
||||
throw new ProtoWireException(ErrProtoInsufficient);
|
||||
|
||||
var b = buffer[i];
|
||||
if (i == 9)
|
||||
{
|
||||
if (b > 1)
|
||||
throw new ProtoWireException(ErrProtoOverflow);
|
||||
|
||||
value |= (ulong)b << 63;
|
||||
return (value, 10);
|
||||
}
|
||||
|
||||
value |= (ulong)(b & 0x7F) << (i * 7);
|
||||
if ((b & 0x80) == 0)
|
||||
return (value, i + 1);
|
||||
}
|
||||
|
||||
throw new ProtoWireException(ErrProtoOverflow);
|
||||
}
|
||||
|
||||
public static int ScanBytes(ReadOnlySpan<byte> buffer)
|
||||
{
|
||||
var (length, lenSize) = ScanVarint(buffer);
|
||||
if (length > (ulong)buffer[lenSize..].Length)
|
||||
throw new ProtoWireException(ErrProtoInsufficient);
|
||||
|
||||
return lenSize + (int)length;
|
||||
}
|
||||
|
||||
public static byte[] EncodeVarint(ulong value)
|
||||
{
|
||||
Span<byte> scratch = stackalloc byte[10];
|
||||
var i = 0;
|
||||
while (value >= 0x80)
|
||||
{
|
||||
scratch[i++] = (byte)((value & 0x7F) | 0x80);
|
||||
value >>= 7;
|
||||
}
|
||||
|
||||
scratch[i++] = (byte)value;
|
||||
return scratch[..i].ToArray();
|
||||
}
|
||||
}
|
||||
|
||||
public sealed class ProtoWireException(string message) : Exception(message);
|
||||
Reference in New Issue
Block a user