Path: redb-route/tests/redb.Route.Tests.IbmMq/IbmMqXmsReconnectUnitTests.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.IbmMq/IbmMqXmsReconnectUnitTests.cs
----
using IBM.XMS;
using redb.Route.Abstractions;
using redb.Route.Core;
using redb.Route.IbmMq;
namespace redb.Route.Tests.IbmMq;
///
/// An XMS listener consumer whose connection fails (the exception listener fires, as on a queue manager restart) closes
/// it and opens a new one on its connection supervisor's schedule, while the endpoint's health says so. Driven by fake
/// XMS connections through the endpoint's XmsConnectionSource, no broker needed; the restart against a real queue
/// manager is . The schedule is the default one: first attempt 1 s after the loss.
///
public sealed class IbmMqXmsReconnectUnitTests
{
private static readonly TimeSpan Wait = TimeSpan.FromSeconds(15);
private sealed class FakeConnection
{
public IConnection Connection { get; } = Substitute.For();
public ISession Session { get; } = Substitute.For();
public IMessageConsumer Consumer { get; } = Substitute.For();
public ExceptionListener? Listener { get; private set; }
public FakeConnection(bool refuseSession = false)
{
Session.CreateQueue(Arg.Any()).Returns(Substitute.For());
Session.CreateConsumer(Arg.Any()).Returns(Consumer);
Connection.CreateSession(Arg.Any(), Arg.Any()).Returns(_ =>
refuseSession ? throw new XMSException("MQRC_Q_MGR_NOT_AVAILABLE") : Session);
Connection.When(c => c.ExceptionListener = Arg.Any())
.Do(call => Listener = call.Arg());
}
public void Break() => Listener!(new XMSException("MQRC_CONNECTION_BROKEN"));
}
private static (IbmMqEndpoint Endpoint, IbmMqConsumer Consumer, Func Opens) Listener(params FakeConnection[] connections)
{
var endpoint = (IbmMqEndpoint)new IbmMqComponent().CreateEndpoint(EndpointUriParser.Parse(
"wmq:DEV.QUEUE.1?host=mq.invalid&port=1414&channel=DEV.APP.SVRCONN&queueManager=QM1&receiveMode=listener"));
var queue = new Queue(connections);
var opens = 0;
endpoint.XmsConnectionSource = () =>
{
Interlocked.Increment(ref opens);
lock (queue) return queue.Dequeue().Connection;
};
var consumer = (IbmMqConsumer)endpoint.CreateConsumer(Substitute.For());
return (endpoint, consumer, () => Volatile.Read(ref opens));
}
[Fact]
public async Task A_broken_connection_is_closed_and_a_new_one_opened_and_health_follows()
{
var first = new FakeConnection();
var second = new FakeConnection();
var (endpoint, consumer, opens) = Listener(first, second);
await consumer.Start();
endpoint.HealthStatus.Should().Be(EndpointHealthStatus.Healthy);
first.Break();
endpoint.HealthStatus.Should().Be(EndpointHealthStatus.Warning, "the connection is lost, the first attempt is not due yet");
(await Until(() => opens() == 2)).Should().BeTrue("the supervisor opens a new connection once the attempt is due");
(await Until(() => endpoint.Reconnects == 1)).Should().BeTrue();
second.Connection.Received(1).Start();
first.Consumer.Received(1).Close();
first.Session.Received(1).Close();
first.Connection.Received(1).Close();
endpoint.HealthStatus.Should().Be(EndpointHealthStatus.Healthy, "the connection is back; a loss is not an error");
first.Break();
await Task.Delay(1500);
opens().Should().Be(2, "a late report from the replaced connection is ignored");
await consumer.Stop();
second.Connection.Received(1).Close();
}
[Fact]
public async Task A_refused_attempt_turns_the_endpoint_Critical_and_the_next_attempt_restores_it()
{
var first = new FakeConnection();
var refused = new FakeConnection(refuseSession: true);
var third = new FakeConnection();
var (endpoint, consumer, opens) = Listener(first, refused, third);
await consumer.Start();
first.Break();
(await Until(() => opens() == 2)).Should().BeTrue();
(await Until(() => endpoint.HealthStatus == EndpointHealthStatus.Critical)).Should()
.BeTrue("a failed attempt makes the loss Critical");
refused.Connection.Received(1).Close();
(await Until(() => endpoint.Reconnects == 1)).Should().BeTrue("the next attempt, 2 s later, connects");
opens().Should().Be(3);
endpoint.HealthStatus.Should().NotBe(EndpointHealthStatus.Critical);
await consumer.Stop();
third.Connection.Received(1).Close();
}
[Fact]
public async Task A_stop_during_the_reconnect_ends_it_and_opens_nothing_more()
{
var first = new FakeConnection();
var (endpoint, consumer, opens) = Listener(first);
await consumer.Start();
first.Break();
await consumer.Stop();
await Task.Delay(1500);
opens().Should().Be(1, "the reconnect ended with the stop before its first attempt");
endpoint.HealthReason.Should().NotContain("listener", "the stopped consumer's supervisor is removed");
}
[Fact]
public async Task A_stop_while_the_lost_connection_waits_for_its_first_attempt_closes_it_and_opens_nothing()
{
var first = new FakeConnection();
var (_, consumer, opens) = Listener(first);
await consumer.Start();
first.Break();
await consumer.Stop().WaitAsync(TimeSpan.FromSeconds(5));
await Task.Delay(1500);
opens().Should().Be(1);
first.Connection.Received(1).Close();
}
[Fact]
public async Task A_stop_after_a_refused_attempt_ends_the_reconnect_before_the_next_one()
{
var first = new FakeConnection();
var refused = new FakeConnection(refuseSession: true);
var (endpoint, consumer, opens) = Listener(first, refused);
await consumer.Start();
first.Break();
(await Until(() => endpoint.HealthStatus == EndpointHealthStatus.Critical)).Should().BeTrue();
await consumer.Stop().WaitAsync(TimeSpan.FromSeconds(5));
await Task.Delay(2500); // the next attempt would be due 2 s after the refused one
opens().Should().Be(2, "no attempt after the stop");
refused.Connection.Received(1).Close();
endpoint.HealthStatus.Should().Be(EndpointHealthStatus.Healthy, "the stopped consumer's supervisor is removed");
}
[Fact]
public async Task A_stop_during_an_attempt_closes_the_connection_the_attempt_opens_late()
{
var first = new FakeConnection();
var late = new FakeConnection();
using var gate = new ManualResetEventSlim();
var endpoint = (IbmMqEndpoint)new IbmMqComponent().CreateEndpoint(EndpointUriParser.Parse(
"wmq:DEV.QUEUE.1?host=mq.invalid&port=1414&channel=DEV.APP.SVRCONN&queueManager=QM1&receiveMode=listener"));
var opens = 0;
endpoint.XmsConnectionSource = () =>
{
if (Interlocked.Increment(ref opens) == 1)
return first.Connection;
gate.Wait(); // the second connect hangs, as one made while the queue manager starts can
return late.Connection;
};
var consumer = (IbmMqConsumer)endpoint.CreateConsumer(Substitute.For());
await consumer.Start();
first.Break();
(await Until(() => Volatile.Read(ref opens) == 2)).Should().BeTrue("the attempt is in its connect");
await consumer.Stop().WaitAsync(TimeSpan.FromSeconds(5));
gate.Set();
(await Until(() => late.Connection.ReceivedCalls().Any(c => c.GetMethodInfo().Name == "Close")))
.Should().BeTrue("the connection the attempt opened after the stop is closed");
Volatile.Read(ref opens).Should().Be(2);
}
private static async Task Until(Func done)
{
var deadline = DateTime.UtcNow + Wait;
while (DateTime.UtcNow < deadline)
{
if (done()) return true;
await Task.Delay(50);
}
return done();
}
}