Path: redb-route/tests/redb.Route.Tests.IbmMq/IbmMqXmsFailedStartUnitTests.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/IbmMqXmsFailedStartUnitTests.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 start fails part-way (the connection and the first worker's session are up, the second
/// session is refused) closes what came up, and a later start opens one connection. Driven by a fake XMS connection
/// through the endpoint's XmsConnectionSource, no broker needed ( proves the
/// same against a real one).
///
public sealed class IbmMqXmsFailedStartUnitTests
{
private sealed record FakeConnection(IConnection Connection, ISession Session, IMessageConsumer Consumer);
private static FakeConnection Fake(bool refuseSecondSession)
{
var connection = Substitute.For();
var session = Substitute.For();
var consumer = Substitute.For();
session.CreateQueue(Arg.Any()).Returns(Substitute.For());
session.CreateConsumer(Arg.Any()).Returns(consumer);
var sessions = 0;
connection.CreateSession(Arg.Any(), Arg.Any()).Returns(_ =>
++sessions == 2 && refuseSecondSession ? throw new XMSException("MQRC_NOT_AUTHORIZED") : session);
return new FakeConnection(connection, session, consumer);
}
[Fact]
public async Task A_start_refused_part_way_closes_what_came_up_and_the_next_start_opens_one_connection()
{
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&concurrentConsumers=2"));
var failing = Fake(refuseSecondSession: true);
var working = Fake(refuseSecondSession: false);
var opened = new Queue([failing, working]);
var opens = 0;
endpoint.XmsConnectionSource = () => { opens++; return opened.Dequeue().Connection; };
var consumer = (IbmMqConsumer)endpoint.CreateConsumer(Substitute.For());
var first = () => consumer.Start();
await first.Should().ThrowAsync();
failing.Consumer.Received(1).Close();
failing.Session.Received(1).Close();
failing.Connection.Received(1).Close();
await consumer.Start();
opens.Should().Be(2, "the second start opens one connection, none is left from the first");
working.Connection.Received(1).Start();
working.Connection.DidNotReceive().Close();
await consumer.Stop();
working.Connection.Received(1).Close();
}
}