Path: redb-route/tests/redb.Route.Tests.WebSocket/WsRestartTests.cs
Snapshot: redb-route, development snapshot 2026-12-06. This is the development state of the public mirrors; it may differ from the published packages.
License: Apache-2.0. Repository: https://github.com/redbase-app/redb-route
Page: https://redbase.app/src/dev-2026-12-06/redb-route/tests/redb.Route.Tests.WebSocket/WsRestartTests.cs
----
using System.Net.WebSockets;
using System.Text;
using redb.Route.Abstractions;
using redb.Route.Core;
using redb.Route.Http;
using redb.Route.WebSocket;
namespace redb.Route.Tests.WebSocket;
///
/// A WebSocket consumer keeps serving after Stop() and Start() of the same route context: the stop
/// closes the listener's path, the start opens it again on the cached endpoint.
///
public sealed class WsRestartTests : IAsyncLifetime
{
private readonly SharedHttpServerManager _manager = new();
private readonly RouteContext _ctx = new();
public Task InitializeAsync() => Task.CompletedTask;
public async Task DisposeAsync()
{
await _ctx.DisposeAsync();
await _manager.DisposeAsync();
}
private static async Task EchoAsync(int port, string text)
{
using var client = new ClientWebSocket();
await client.ConnectAsync(new Uri($"ws://127.0.0.1:{port}/chat"), CancellationToken.None);
await client.SendAsync(Encoding.UTF8.GetBytes(text), WebSocketMessageType.Text, true, CancellationToken.None);
var buffer = new byte[64];
var result = await client.ReceiveAsync(buffer, CancellationToken.None);
return Encoding.UTF8.GetString(buffer, 0, result.Count);
}
[Fact]
public async Task Consumer_serves_after_the_context_is_stopped_and_started_again()
{
var port = global::redb.Route.Tests.Shared.TestPorts.Next();
_ctx.AddComponent(new WsComponent { ServerManager = _manager });
_ctx.AddRoutes(r => r.From($"ws://127.0.0.1:{port}/chat?inOut=true").Process(e => e.Out = new Message("echo:" + e.In.Body)));
await _ctx.Start();
(await EchoAsync(port, "first")).Should().Be("echo:first");
await _ctx.Stop();
await _ctx.Start();
(await EchoAsync(port, "second")).Should().Be("echo:second", "the WebSocket receiver replies after a restart");
}
}