Class RecordedMessageBus

Namespace
XBullet.EasyTesting.Messaging
Assembly
XBullet.EasyTesting.Messaging.dll

Thread-safe, in-memory recording bus used by test publisher adapters.

public sealed class RecordedMessageBus : ITestScenarioResource
Inheritance
RecordedMessageBus
Implements
Inherited Members

Remarks

Constructors

RecordedMessageBus(JsonSerializerOptions?)

Creates a recorder with optional JSON serialization settings.

public RecordedMessageBus(JsonSerializerOptions? serializerOptions = null)

Parameters

serializerOptions JsonSerializerOptions

Options used to serialize every recorded payload and, by default, expected assertion payloads. When null, a new web-default instance is created. A non-null instance is retained but not owned or disposed; do not mutate it after recording begins.

Properties

Count

Gets the number of messages recorded since construction or the last reset.

public int Count { get; }

Property Value

int

The current message count as a thread-safe point-in-time value.

Messages

Gets a stable copy of all messages in publication order.

public IReadOnlyList<RecordedMessage> Messages { get; }

Property Value

IReadOnlyList<RecordedMessage>

A newly allocated array containing the recorded message objects in append order. Later records and resets do not change the array. The bus owns each message and its header dictionary; callers must not mutate them.

Methods

CaptureDiagnosticsAsync(CancellationToken)

Captures the current recorded-message state for scenario diagnostics.

public ValueTask<object?> CaptureDiagnosticsAsync(CancellationToken cancellationToken = default)

Parameters

cancellationToken CancellationToken

Cancels before diagnostic state is copied. The default token does not request cancellation.

Returns

ValueTask<object>

An already-completed value task containing a newly allocated serializable object with the current count and a message-array snapshot. Headers and JSON payloads are included without automatic redaction.

For(string, string)

Returns messages for one transport and destination.

public IReadOnlyList<RecordedMessage> For(string transport, string destination)

Parameters

transport string

The non-empty transport identifier matched without regard to case.

destination string

The non-empty destination matched using ordinal, case-sensitive comparison.

Returns

IReadOnlyList<RecordedMessage>

A newly allocated array containing matching handler-owned message objects in append order. Later records and resets do not change the array.

RecordAsync<T>(string, string, T, IReadOnlyDictionary<string, string>?, CancellationToken)

Records a serialized copy of one published message asynchronously.

public Task RecordAsync<T>(string transport, string destination, T payload, IReadOnlyDictionary<string, string>? headers = null, CancellationToken cancellationToken = default)

Parameters

transport string

The non-empty transport identifier. Custom values and names from MessageTransportNames are accepted.

destination string

The non-empty transport-specific queue, topic, hub, or other destination name.

payload T

The payload serialized synchronously to a self-contained JsonElement. It may be null and is not retained, owned, or disposed.

headers IReadOnlyDictionary<string, string>

Headers copied synchronously into a case-insensitive dictionary, or null to record no headers. The bus does not retain, own, or mutate the supplied dictionary. Values are stored without redaction.

cancellationToken CancellationToken

Cancels before serialization or recorder mutation. After the initial check, recording completes synchronously and no longer observes cancellation. The default token does not request cancellation.

Returns

Task

A task that is already complete after the message has been recorded.

Type Parameters

T

The compile-time payload type recorded in MessageType and used for JSON serialization.

Record<T>(string, string, T, IReadOnlyDictionary<string, string>?)

Records a serialized copy of one published message.

public void Record<T>(string transport, string destination, T payload, IReadOnlyDictionary<string, string>? headers = null)

Parameters

transport string

The non-empty transport identifier. Custom values and names from MessageTransportNames are accepted.

destination string

The non-empty transport-specific queue, topic, hub, or other destination name.

payload T

The payload serialized immediately to a self-contained JsonElement. It may be null and is not retained, owned, or disposed.

headers IReadOnlyDictionary<string, string>

Headers copied immediately into a case-insensitive dictionary, or null to record no headers. The bus does not retain, own, or mutate the supplied dictionary. Values are stored without redaction.

Type Parameters

T

The compile-time payload type recorded in MessageType and used for JSON serialization.

Reset()

Removes every recorded message and returns this recorder.

public RecordedMessageBus Reset()

Returns

RecordedMessageBus

This recorder for reuse. Messages appended concurrently after the reset lock is released are retained.

ResetAsync(CancellationToken)

Removes every recorded message.

public ValueTask ResetAsync(CancellationToken cancellationToken = default)

Parameters

cancellationToken CancellationToken

Cancels before any state is changed. Once cancellation is checked, reset completes synchronously. The default token does not request cancellation.

Returns

ValueTask

A value task that is already complete after the messages have been cleared.

Should()

Starts a fluent assertion chain over the recorded messages.

public RecordedMessageBusAssertions Should()

Returns

RecordedMessageBusAssertions

A new assertion object referencing this bus. Each bus-level assertion reads current recorder state when it runs.