feat: complete final jetstream parity transport and runtime baselines
This commit is contained in:
@@ -27,6 +27,8 @@ internal sealed class RouteFixture : IAsyncDisposable
|
||||
private readonly CancellationTokenSource _ctsA;
|
||||
private readonly CancellationTokenSource _ctsB;
|
||||
private Socket? _subscriberOnB;
|
||||
private Socket? _publisherOnA;
|
||||
private Socket? _manualRouteToA;
|
||||
|
||||
private RouteFixture(NatsServer serverA, NatsServer serverB, CancellationTokenSource ctsA, CancellationTokenSource ctsB)
|
||||
{
|
||||
@@ -91,22 +93,82 @@ internal sealed class RouteFixture : IAsyncDisposable
|
||||
await ReadUntilAsync(sock, "PONG");
|
||||
}
|
||||
|
||||
public async Task<bool> ServerAHasRemoteInterestAsync(string subject)
|
||||
public async Task SendRouteSubFrameAsync(string subject)
|
||||
{
|
||||
var (host, port) = ParseHostPort(_serverA.ClusterListen!);
|
||||
var sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
|
||||
await sock.ConnectAsync(IPAddress.Parse(host), port);
|
||||
_manualRouteToA = sock;
|
||||
|
||||
await sock.SendAsync(Encoding.ASCII.GetBytes("ROUTE test-remote\r\n"));
|
||||
_ = await ReadLineAsync(sock); // ROUTE <id>
|
||||
await sock.SendAsync(Encoding.ASCII.GetBytes($"RS+ {subject}\r\n"));
|
||||
}
|
||||
|
||||
public async Task SendRouteUnsubFrameAsync(string subject)
|
||||
{
|
||||
if (_manualRouteToA == null)
|
||||
throw new InvalidOperationException("Route frame socket not established.");
|
||||
|
||||
await _manualRouteToA.SendAsync(Encoding.ASCII.GetBytes($"RS- {subject}\r\n"));
|
||||
}
|
||||
|
||||
public async Task PublishFromServerAAsync(string subject, string payload)
|
||||
{
|
||||
var sock = _publisherOnA;
|
||||
if (sock == null)
|
||||
{
|
||||
sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
|
||||
await sock.ConnectAsync(IPAddress.Loopback, _serverA.Port);
|
||||
_publisherOnA = sock;
|
||||
_ = await ReadLineAsync(sock); // INFO
|
||||
await sock.SendAsync(Encoding.ASCII.GetBytes("CONNECT {}\r\nPING\r\n"));
|
||||
await ReadUntilAsync(sock, "PONG");
|
||||
}
|
||||
|
||||
await sock.SendAsync(Encoding.ASCII.GetBytes($"PUB {subject} {payload.Length}\r\n{payload}\r\nPING\r\n"));
|
||||
await ReadUntilAsync(sock, "PONG");
|
||||
}
|
||||
|
||||
public async Task<string> ReadServerBMessageAsync()
|
||||
{
|
||||
if (_subscriberOnB == null)
|
||||
throw new InvalidOperationException("No subscriber socket on server B.");
|
||||
|
||||
return await ReadUntilAsync(_subscriberOnB, "MSG ");
|
||||
}
|
||||
|
||||
public async Task<bool> ServerAHasRemoteInterestAsync(string subject, bool expected = true)
|
||||
{
|
||||
using var timeout = new CancellationTokenSource(TimeSpan.FromSeconds(5));
|
||||
while (!timeout.IsCancellationRequested)
|
||||
{
|
||||
if (_serverA.HasRemoteInterest(subject))
|
||||
return true;
|
||||
if (_serverA.HasRemoteInterest(subject) == expected)
|
||||
return expected;
|
||||
await Task.Delay(50, timeout.Token).ContinueWith(_ => { }, TaskScheduler.Default);
|
||||
}
|
||||
|
||||
return false;
|
||||
return !expected;
|
||||
}
|
||||
|
||||
public async Task<int> ServerARouteLinkCountToServerBAsync()
|
||||
{
|
||||
using var timeout = new CancellationTokenSource(TimeSpan.FromSeconds(5));
|
||||
while (!timeout.IsCancellationRequested)
|
||||
{
|
||||
if (_serverA.Stats.Routes >= 3)
|
||||
return (int)_serverA.Stats.Routes;
|
||||
await Task.Delay(50, timeout.Token).ContinueWith(_ => { }, TaskScheduler.Default);
|
||||
}
|
||||
|
||||
return (int)_serverA.Stats.Routes;
|
||||
}
|
||||
|
||||
public async ValueTask DisposeAsync()
|
||||
{
|
||||
_subscriberOnB?.Dispose();
|
||||
_publisherOnA?.Dispose();
|
||||
_manualRouteToA?.Dispose();
|
||||
await _ctsA.CancelAsync();
|
||||
await _ctsB.CancelAsync();
|
||||
_serverA.Dispose();
|
||||
@@ -138,4 +200,10 @@ internal sealed class RouteFixture : IAsyncDisposable
|
||||
|
||||
return sb.ToString();
|
||||
}
|
||||
|
||||
private static (string Host, int Port) ParseHostPort(string endpoint)
|
||||
{
|
||||
var parts = endpoint.Split(':', 2, StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries);
|
||||
return (parts[0], int.Parse(parts[1]));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user