feat(boot): add server boot integration tests and fix ValueTask sync blocking

Fix Shutdown()/LameDuckMode() deadlock caused by calling .GetAwaiter().GetResult()
on pending ValueTask from Channel.ReadAsync(). ValueTask does not support synchronous
blocking — must convert via .AsTask() first. Add two integration tests validating the
full Start() → AcceptLoop → client connection → Shutdown lifecycle.
This commit is contained in:
Joseph Doherty
2026-03-01 15:13:48 -05:00
parent bd40b36c23
commit be1eb3392e
3 changed files with 120 additions and 3 deletions

View File

@@ -127,8 +127,9 @@ public sealed partial class NatsServer
} }
// Wait for accept loops to exit. // Wait for accept loops to exit.
// Must use .AsTask() because ValueTask cannot be synchronously blocked on.
for (int i = 0; i < doneExpected; i++) for (int i = 0; i < doneExpected; i++)
_done.Reader.ReadAsync().GetAwaiter().GetResult(); _done.Reader.ReadAsync().AsTask().GetAwaiter().GetResult();
// Wait for all goroutines. // Wait for all goroutines.
_grWg.Wait(); _grWg.Wait();
@@ -655,8 +656,9 @@ public sealed partial class NatsServer
ShutdownRaftNodes(); ShutdownRaftNodes();
// Wait for accept loops. // Wait for accept loops.
// Must use .AsTask() because ValueTask cannot be synchronously blocked on.
for (int i = 0; i < expected; i++) for (int i = 0; i < expected; i++)
_ldmCh.Reader.ReadAsync().GetAwaiter().GetResult(); _ldmCh.Reader.ReadAsync().AsTask().GetAwaiter().GetResult();
_mu.EnterWriteLock(); _mu.EnterWriteLock();
var clients = new List<ClientConnection>(_clients.Values); var clients = new List<ClientConnection>(_clients.Values);

View File

@@ -0,0 +1,115 @@
// Copyright 2013-2026 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 System.Net;
using System.Text;
using Shouldly;
using ZB.MOM.NatsNet.Server;
namespace ZB.MOM.NatsNet.Server.IntegrationTests;
/// <summary>
/// End-to-end server boot tests that validate the full
/// Start() → AcceptLoop → client connection lifecycle.
/// </summary>
[Trait("Category", "Integration")]
public sealed class ServerBootTests : IDisposable
{
private readonly string _storeDir;
public ServerBootTests()
{
_storeDir = Path.Combine(Path.GetTempPath(), $"natsnet-test-{Guid.NewGuid():N}");
}
public void Dispose()
{
try { Directory.Delete(_storeDir, true); } catch { }
}
/// <summary>
/// Validates that a server can boot, accept a TCP connection, and send
/// a NATS protocol INFO line. This proves the full Start() → AcceptLoop
/// → CreateClient pipeline works end-to-end.
/// </summary>
/// <summary>
/// Validates that a server can boot, bind a port, and accept a TCP connection.
/// Note: GenerateClientInfoJSON is currently a stub (returns empty), so we only
/// verify the TCP handshake succeeds — not the INFO protocol line.
/// </summary>
[Fact]
public async Task ServerBoot_AcceptsTcpConnection_ShouldSucceed()
{
// Arrange — create server with ephemeral port
var opts = new ServerOptions
{
Host = "127.0.0.1",
Port = 0, // ephemeral
};
var (server, err) = NatsServer.NewServer(opts);
err.ShouldBeNull("NewServer should succeed");
server.ShouldNotBeNull();
try
{
// Act — start the server
server!.Start();
// Get the actual bound port
var addr = server.Addr() as IPEndPoint;
addr.ShouldNotBeNull("Server should have a listener address after Start()");
addr!.Port.ShouldBeGreaterThan(0);
// Connect a raw TCP client — proves AcceptLoop is working
using var tcp = new System.Net.Sockets.TcpClient();
using var cts = new CancellationTokenSource(TimeSpan.FromSeconds(5));
await tcp.ConnectAsync(addr.Address, addr.Port, cts.Token);
tcp.Connected.ShouldBeTrue();
// Verify the server registered the client
await Task.Delay(100); // Give CreateClient a moment to run
server.NumClients().ShouldBeGreaterThan(0);
}
finally
{
server!.Shutdown();
}
}
/// <summary>
/// Validates that Shutdown() after Start() completes cleanly.
/// Uses DontListen to skip TCP binding — tests lifecycle only.
/// </summary>
[Fact]
public void ServerBoot_StartAndShutdown_ShouldSucceed()
{
var opts = new ServerOptions
{
Host = "127.0.0.1",
Port = 0,
DontListen = true,
};
var (server, err) = NatsServer.NewServer(opts);
err.ShouldBeNull();
server.ShouldNotBeNull();
server!.Start();
server.Running().ShouldBeTrue();
server.Shutdown();
server.Running().ShouldBeFalse();
}
}

View File

@@ -1,6 +1,6 @@
# NATS .NET Porting Status Report # NATS .NET Porting Status Report
Generated: 2026-03-01 20:04:26 UTC Generated: 2026-03-01 20:13:49 UTC
## Modules (12 total) ## Modules (12 total)