Path: redb-route/tests/redb.Route.Tests.AzureServiceBus/AsbFaultStand.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.AzureServiceBus/AsbFaultStand.cs
----
using System.Text;
using Azure.Messaging.ServiceBus;
using redb.Route.Tests.Conformance;
namespace redb.Route.Tests.AzureServiceBus;
///
/// The Service Bus stand of the fault runs: the emulator and its own SQL container, started by
/// tests/docker/run-fault-suite.sh --broker servicebus with the entities of tests/docker/servicebus/Config.json
/// (the emulator creates none on request), reached through Azure.Messaging.ServiceBus directly, not through the connector.
/// Both run with host networking: AMQP at localhost:5672 (the compose file publishes it at 5300 instead), SQL at 14330,
/// away from a SQL Server the host may run on 1433. Only the emulator's container is stopped and started; the SQL
/// container keeps the messages. The emulator image has no shell, so it is ready when it logs
/// after the fault.
///
public sealed class AsbFaultStand : IConnectorStand
{
/// The variable that names the emulator container a test may stop and restart.
public const string ContainerVariable = "REDB_TEST_ASB_CONTAINER";
/// The line the emulator logs once it serves AMQP.
public const string ReadyLog = "Emulator Service is Successfully Up!";
/// The connection string of the emulator.
public const string ConnectionString =
"Endpoint=sb://localhost:5672;SharedAccessKeyName=RootManageSharedAccessKey;" +
"SharedAccessKey=SAS_KEY_VALUE;UseDevelopmentEmulator=true;";
/// The session every message put on a session queue carries.
public const string Session = "fault-session";
/// The stand of this process.
public static AsbFaultStand Instance { get; } = new();
private AsbFaultStand()
{
}
///
public BrokerFault? Fault { get; } = BrokerFault.FromEnvironmentWithReadyLog(ContainerVariable, ReadyLog);
///
public TimeSpan Wait { get; } = BrokerFault.DefaultWait;
/// Whether is a session queue of the stand's Config.json (its name says so).
public static bool IsSessionQueue(string queue) => queue.Contains(".session", StringComparison.Ordinal);
///
public async Task PutAsync(string destination, string body, CancellationToken ct = default)
{
await using var client = new ServiceBusClient(ConnectionString);
await using var sender = client.CreateSender(destination);
var message = new ServiceBusMessage(Encoding.UTF8.GetBytes(body));
if (IsSessionQueue(destination))
message.SessionId = Session;
await sender.SendMessageAsync(message, ct);
}
///
/// Takes what holds. It locks and completes each message: a message the namespace
/// hands over after the receive has timed out stays in the closing receiver's buffer, and in ReceiveAndDelete it
/// would be gone with it; locked, it comes back when the lock ends.
///
public async Task> DrainAsync(string destination, TimeSpan wait, CancellationToken ct = default)
{
await using var client = new ServiceBusClient(ConnectionString);
var bodies = new List();
var (queue, subQueue) = destination.EndsWith("/$deadletterqueue", StringComparison.Ordinal)
? (destination[..^"/$deadletterqueue".Length], SubQueue.DeadLetter)
: (destination, SubQueue.None);
var options = new ServiceBusReceiverOptions { ReceiveMode = ServiceBusReceiveMode.PeekLock, SubQueue = subQueue };
ServiceBusReceiver receiver;
if (subQueue == SubQueue.None && IsSessionQueue(queue))
{
try
{
receiver = await client.AcceptSessionAsync(queue, Session,
new ServiceBusSessionReceiverOptions { ReceiveMode = ServiceBusReceiveMode.PeekLock }, ct);
}
catch (ServiceBusException ex) when (ex.Reason == ServiceBusFailureReason.SessionCannotBeLocked)
{
return bodies;
}
}
else
{
receiver = client.CreateReceiver(queue, options);
}
await using (receiver)
{
var first = wait;
while (true)
{
var batch = await receiver.ReceiveMessagesAsync(100, first, ct);
if (batch.Count == 0)
return bodies;
foreach (var message in batch)
{
await receiver.CompleteMessageAsync(message, ct);
bodies.Add(message.Body.ToString());
}
first = TimeSpan.FromMilliseconds(500);
}
}
}
}