Path: redb-route/tests/redb.Route.Tests.IbmMq/IbmMqBackoutDelayTests.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/IbmMqBackoutDelayTests.cs
----
using System.Diagnostics;
using redb.Route.Core;
using redb.Route.IbmMq;
namespace redb.Route.Tests.IbmMq;
///
/// The consumer's pause after a backout (backoutDelay) against IBM MQ, in poll and batch modes: a message the
/// route failed on is backed out and not read again before the delay; with backoutDelay=0 it comes back at once.
/// Expects IBM MQ at localhost:1414 with the queues of tests/docker/ibmmq/redb-tests.mqsc.
///
[Trait("Category", "Integration")]
[Collection("IbmMqIntegration")]
public sealed class IbmMqBackoutDelayTests
{
private const string Queue = "DEV.QUEUE.POISON";
private const string BackoutQueue = "DEV.QUEUE.POISON.BOQ";
private const string Connection =
"host=localhost&port=1414&channel=DEV.APP.SVRCONN&queueManager=QM1&user=app&password=admin";
private const int Delay = 3000;
///
/// Runs a route that fails on the first delivery and succeeds on the second; returns the time between the two.
///
private static async Task TimeBetweenDeliveries(string options)
{
IbmMqPoisonTests.Drain(Queue);
IbmMqPoisonTests.Drain(BackoutQueue);
IbmMqPoisonTests.Put(Queue, "again");
var runs = new List();
var clock = Stopwatch.StartNew();
var second = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously);
await using (var context = new RouteContext())
{
context.AddComponent(new IbmMqComponent());
context.AddRoutes(r => r
.From($"wmq:{Queue}?{Connection}&{options}")
.Process(_ =>
{
int run;
lock (runs)
{
runs.Add(clock.Elapsed);
run = runs.Count;
}
if (run == 1)
throw new InvalidOperationException("fails the first time");
second.TrySetResult();
}));
await context.Start();
await second.Task.WaitAsync(TimeSpan.FromSeconds(30));
}
IbmMqPoisonTests.Drain(Queue).Should().BeEmpty("the second delivery committed the message");
IbmMqPoisonTests.Drain(BackoutQueue).Should().BeEmpty();
return runs[1] - runs[0];
}
[Fact]
public async Task Poll_mode_does_not_read_a_backed_out_message_again_before_the_delay()
{
var gap = await TimeBetweenDeliveries($"transacted=true&backoutThreshold=5&backoutQueue={BackoutQueue}&backoutDelay={Delay}");
gap.Should().BeGreaterThanOrEqualTo(TimeSpan.FromMilliseconds(Delay));
}
[Fact]
public async Task Batch_mode_does_not_read_a_backed_out_batch_again_before_the_delay()
{
var gap = await TimeBetweenDeliveries(
$"batching=true&maxBatchMessages=1&pollTimeoutMs=200&backoutThreshold=5&backoutQueue={BackoutQueue}&backoutDelay={Delay}");
gap.Should().BeGreaterThanOrEqualTo(TimeSpan.FromMilliseconds(Delay));
}
[Fact]
public async Task A_delay_of_zero_reads_a_backed_out_message_again_at_once()
{
var gap = await TimeBetweenDeliveries($"transacted=true&backoutThreshold=5&backoutQueue={BackoutQueue}&backoutDelay=0");
gap.Should().BeLessThan(TimeSpan.FromMilliseconds(Delay), "without the pause nothing holds the next MQGET back");
}
}