Path: redb-route/tests/redb.Route.Tests/Conformance/BrokerFaultTests.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/Conformance/BrokerFaultTests.cs ---- using System.Collections.Concurrent; namespace redb.Route.Tests.Conformance; /// /// without docker: the CLI calls it makes, the readiness wait by the broker's own health /// command, and the failure texts that name the container and the command. /// public sealed class BrokerFaultTests { [Fact] public async Task A_restart_restarts_the_container_and_waits_for_the_health_command() { var docker = new FakeDocker { NotReadyTimes = 3 }; var fault = docker.Fault(TimeSpan.FromSeconds(10)); await fault.RestartAsync(); docker.Calls.Should().Equal( "restart -t 10 qm", "exec qm chkmqhealthy", "exec qm chkmqhealthy", "exec qm chkmqhealthy", "exec qm chkmqhealthy"); } [Fact] public async Task A_broker_that_does_not_come_back_fails_the_restart_with_the_health_output() { var docker = new FakeDocker { NotReadyTimes = int.MaxValue }; var fault = docker.Fault(TimeSpan.FromMilliseconds(200)); var act = () => fault.RestartAsync(); (await act.Should().ThrowAsync()).Which.Message.Should() .Contain("qm").And.Contain("chkmqhealthy").And.Contain("not running"); } [Fact] public async Task A_failing_docker_command_fails_with_its_output() { var docker = new FakeDocker { Refuse = "pause qm" }; var fault = docker.Fault(TimeSpan.FromSeconds(1)); var act = () => fault.PauseAsync(); (await act.Should().ThrowAsync()).Which.Message.Should() .Contain("docker pause qm").And.Contain("No such container"); } [Fact] public async Task Pause_and_unpause_freeze_and_release_the_container_without_a_readiness_wait() { var docker = new FakeDocker(); var fault = docker.Fault(TimeSpan.FromSeconds(1)); await fault.PauseAsync(); await fault.UnpauseAsync(); docker.Calls.Should().Equal("pause qm", "unpause qm"); } [Fact] public async Task Until_retries_an_attempt_until_it_succeeds() { var attempts = 0; var done = await BrokerFault.UntilAsync(() => Task.FromResult(++attempts == 3), TimeSpan.FromSeconds(5), TimeSpan.FromMilliseconds(10)); done.Should().BeTrue(); attempts.Should().Be(3); } [Fact] public async Task A_health_command_argument_with_spaces_stays_one_argument() { var docker = new FakeDocker(); var fault = new BrokerFault("rmq", ["sh", "-c", "test -f cookie && check"], TimeSpan.FromSeconds(1), docker.Run, TimeSpan.FromMilliseconds(5)); await fault.WaitReadyAsync(); docker.Arguments.Should().ContainSingle().Which.Should().Equal("exec", "rmq", "sh", "-c", "test -f cookie && check"); } [Fact] public void A_fault_needs_a_container_and_a_health_command() { FluentActions.Invoking(() => new BrokerFault("", "chkmqhealthy")).Should().Throw(); FluentActions.Invoking(() => new BrokerFault("qm")).Should().Throw(); } private sealed class FakeDocker { public ConcurrentQueue CallQueue { get; } = new(); public ConcurrentQueue ArgumentQueue { get; } = new(); public string[] Calls => CallQueue.ToArray(); public string[][] Arguments => ArgumentQueue.ToArray(); public int NotReadyTimes { get; init; } public string? Refuse { get; init; } private int _checks; public BrokerFault Fault(TimeSpan readyWithin) => new("qm", ["chkmqhealthy"], readyWithin, Run, TimeSpan.FromMilliseconds(5)); public Task Run(IReadOnlyList argumentList, CancellationToken ct) { ArgumentQueue.Enqueue(argumentList.ToArray()); var arguments = string.Join(' ', argumentList); CallQueue.Enqueue(arguments); if (arguments == Refuse) return Task.FromResult(new DockerResult(1, "Error: No such container: qm")); if (arguments.StartsWith("exec ", StringComparison.Ordinal) && _checks++ < NotReadyTimes) return Task.FromResult(new DockerResult(1, "queue manager not running")); return Task.FromResult(new DockerResult(0, "")); } } }