Path: redb-route/tests/redb.Route.Tests.Amqp/AmqpManualTransactionProbeTests.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.Amqp/AmqpManualTransactionProbeTests.cs
----
using System.Diagnostics;
using Amqp;
using Amqp.Framing;
using Amqp.Transactions;
using Amqp.Types;
using Xunit.Abstractions;
namespace redb.Route.Tests.Amqp;
///
/// PROBE: does the broker (ActiveMQ Artemis on localhost:5673) answer an AMQP 1.0 transaction driven by hand — a
/// controller link to its coordinator, Declare, sends carrying TransactionalState, Discharge — the way the client's
/// System.Transactions path never got an answer to its discharge?
///
[Trait("Category", "Integration")]
public sealed class AmqpManualTransactionProbeTests
{
private const string Url = "amqp://admin:admin@localhost:5673";
private static readonly TimeSpan Wait = TimeSpan.FromSeconds(10);
private readonly ITestOutputHelper _output;
public AmqpManualTransactionProbeTests(ITestOutputHelper output) => _output = output;
private static Task SendAsync(SenderLink link, Message message, DeliveryState? state)
{
var done = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously);
link.Send(message, state, (_, _, outcome, _) => done.TrySetResult(outcome), null);
return done.Task.WaitAsync(Wait);
}
private static Message Control(object body) => new() { BodySection = new AmqpValue { Value = body } };
private async Task> Drain(Session session, string queue)
{
var receiver = new ReceiverLink(session, $"probe-receiver-{Guid.NewGuid():N}", new Attach
{
Source = new Source { Address = queue, Capabilities = [new Symbol("queue")] },
Target = new Target(),
}, null);
var bodies = new List();
while (await receiver.ReceiveAsync(TimeSpan.FromSeconds(2)) is { } message)
{
bodies.Add(message.Body?.ToString() ?? "");
receiver.Accept(message);
}
await receiver.CloseAsync();
return bodies;
}
private async Task> SendInATransaction(bool commit)
{
var queue = $"probe-tx-{Guid.NewGuid():N}";
var connection = await Connection.Factory.CreateAsync(new Address(Url));
try
{
var session = new Session(connection);
var controller = new SenderLink(session, $"probe-controller-{Guid.NewGuid():N}", new Attach
{
Source = new Source(),
Target = new Coordinator { Capabilities = [TxnCapabilities.LocalTransactions] },
}, null);
// "queue": an anycast address on Artemis, which keeps what nobody has subscribed to yet.
var sender = new SenderLink(session, $"probe-sender-{Guid.NewGuid():N}", new Attach
{
Source = new Source(),
Target = new Target { Address = queue, Capabilities = [new Symbol("queue")] },
}, null);
var clock = Stopwatch.StartNew();
var declared = await SendAsync(controller, Control(new Declare()), null);
_output.WriteLine($"declare -> {declared} in {clock.ElapsedMilliseconds} ms");
var txnId = declared.Should().BeOfType().Subject.TxnId;
foreach (var body in new[] { "one", "two" })
{
var sent = await SendAsync(sender, new Message(body), new TransactionalState { TxnId = txnId });
_output.WriteLine($"send {body} -> {sent}");
}
clock.Restart();
var discharged = await SendAsync(controller, Control(new Discharge { TxnId = txnId, Fail = !commit }), null);
_output.WriteLine($"discharge(fail={!commit}) -> {discharged} in {clock.ElapsedMilliseconds} ms");
discharged.Should().BeOfType();
await sender.CloseAsync();
await controller.CloseAsync();
return await Drain(session, queue);
}
finally
{
await connection.CloseAsync();
}
}
[Fact]
public async Task A_committed_transaction_delivers_its_messages()
{
(await SendInATransaction(commit: true)).Should().Equal("one", "two");
}
[Fact]
public async Task A_failed_transaction_delivers_nothing()
{
(await SendInATransaction(commit: false)).Should().BeEmpty();
}
}