Skip to main content
Table of Contents

Class InMemoryMessageBus

Namespace
Stratara.Testing
Assembly
Stratara.Testing.dll

In-memory IMessageBus test double with synchronous, in-process dispatch: a PublishAsync<T>(string, T, CancellationToken) call immediately awaits every handler subscribed to the topic whose message type is compatible, so tests observe handler effects without a broker.

public sealed class InMemoryMessageBus : IMessageBus
Inheritance
InMemoryMessageBus
Implements
Inherited Members
Extension Methods

Remarks

Like a real broker, a message published to a topic with no current subscriber is dropped (but still recorded in Published for assertions). Dispatch order follows subscription order. All members are thread-safe.

Properties

Published

Every message handed to PublishAsync<T>(string, T, CancellationToken), in publish order, for assertions.

public IReadOnlyList<PublishedMessage> Published { get; }

Property Value

IReadOnlyList<PublishedMessage>

Methods

EnsureSubscriptionAsync(string, string, CancellationToken)

Establish subscription on topic so that messages published from this point on are retained for it, without dispatching any of them yet.

public Task EnsureSubscriptionAsync(string topic, string subscription, CancellationToken cancellationToken = default)

Parameters

topic string

The topic the subscription belongs to.

subscription string

The subscription to establish.

cancellationToken CancellationToken

Cancels the operation.

Returns

Task

A task that completes once the subscription exists.

Remarks

Call this during start-up, before anything in the system can publish. A subscription that is established only when its handler attaches receives nothing published in the meantime, and on a topic carrying more than one subscription that loss is silent: the publisher is told the publication succeeded as soon as any subscription takes it, so a missing one is indistinguishable from a delivered one.

Idempotent — establishing a subscription that already exists changes nothing and loses nothing already held for it. SubscribeAsync<T>(string, string, Func<T, Task>, CancellationToken) establishes the subscription too, so a caller that only ever subscribes stays correct; this member exists so that establishing can happen earlier than the handler is ready.

An implementation whose subscriptions exist before the application runs — one where they are provisioned administratively — satisfies this by doing nothing. An implementation that cannot establish a particular subscription ahead of its consumer SHOULD throw rather than return successfully, because a caller cannot otherwise tell that the guarantee it asked for is absent.

PublishAsync<T>(string, T, CancellationToken)

Publish message to topic.

public Task PublishAsync<T>(string topic, T message, CancellationToken cancellationToken = default)

Parameters

topic string
message T
cancellationToken CancellationToken

Returns

Task

Type Parameters

T

SubscribeAsync<T>(string, string, Func<T, Task>, CancellationToken)

Subscribe to topic under subscription and dispatch every incoming message to handler. Establishes the subscription if EnsureSubscriptionAsync(string, string, CancellationToken) has not already done so.

public Task SubscribeAsync<T>(string topic, string subscription, Func<T, Task> handler, CancellationToken cancellationToken = default)

Parameters

topic string
subscription string
handler Func<T, Task>
cancellationToken CancellationToken

Returns

Task

Type Parameters

T