InMemoryBridgeTransport class

Namespace Runic.Application.Testing in Runic.Application.Testing 0.23.0

public sealed class InMemoryBridgeTransport : IBridgeConnectionTransport, IBridgeTransport, IDisposable

Inherits or implements IBridgeConnectionTransport, IBridgeTransport, IDisposable.

An in-memory IBridgeTransport for headless tests: requests run the window's real request handler and generated Bridges, and pushes are recorded instead of reaching a page. No native UI runs.

Remarks

InMemoryBridgeTransport.Request and InMemoryBridgeTransport.RequestAsync send one Views protocol 4 request as JSON on a connection and return the reply. A connection opens with its first request and stays open until InMemoryBridgeTransport.CloseConnection; like a page, it sends hello first. InMemoryBridgeTransport.Messages lists the pushes the window sent, broadcasts and pushes to one connection alike, in order.

RunicViewDriver and RunicWindowTestHost drive Views through this transport by member, as the generated TypeScript clients do.

Constructors

InMemoryBridgeTransport

public InMemoryBridgeTransport()

No documentation.

Fields

DefaultClientKey

public const string DefaultClientKey = "client-1"

The client key of requests that name none.

DefaultConnectionKey

public const string DefaultConnectionKey = "connection-1"

The connection key of requests that name none.

Properties

FieldFlush

public Func<string, ValueTask>? FieldFlush { get; set; }

The field drafts of the test's pages (field-flush/1). When it is set before a connection's first request, the connection lists field-flush/1 in its hello and answers each flush push as a page does: it awaits this function with the connection's key, in which the test writes what the page would (for example driver.Set(vm => vm.Title, "typed")), and then sends flush-reply. A departure that the window starts, such as a navigation with a departure guard or RequestCloseAsync, waits for it up to 1 second.

Messages

public IReadOnlyList<RunicBridgeMessage> Messages { get; }

The pushes the window sent, in order, that InMemoryBridgeTransport.TakeMessages has not taken.

Methods

BroadcastAsync

public ValueTask BroadcastAsync(ReadOnlyMemory<byte> message, CancellationToken cancellationToken = default)

Records a push to every connection.

CloseConnection

public void CloseConnection(string connectionKey = "connection-1")

Closes a connection, as a page that reloads or loses its connection does, and reports it to the window.

Parameters

connectionKey

The connection to close.

Dispose

public void Dispose()

Stops serving the window and removes all recorded pushes and publications.

Request

public string Request(string json, string connectionKey = "connection-1", string? clientKey = null)

Sends one request on a connection and blocks the calling thread until its reply.

Parameters

json

The request, one JSON object.

connectionKey

The connection; it opens with its first request.

clientKey

The client of a connection's first request; by default InMemoryBridgeTransport.DefaultClientKey.

Returns

The reply's JSON.

Exceptions

InvalidOperationException

No window serves the transport.

Remarks

The calling thread waits while the window handles the request, and the window's own asynchronous work, such as a model turn or an awaited command, continues on thread-pool threads. A suite that runs many tests in parallel on thread-pool threads can starve the pool this way: each blocked test holds a thread that the replies it waits for need. Use InMemoryBridgeTransport.RequestAsync in such suites; it never blocks.

RequestAsync

public ValueTask<string> RequestAsync(string json, string connectionKey = "connection-1", string? clientKey = null, CancellationToken cancellationToken = default)

Sends one request on a connection and returns its reply when it arrives, without blocking a thread.

Parameters

json

The request, one JSON object.

connectionKey

The connection; it opens with its first request.

clientKey

The client of a connection's first request; by default InMemoryBridgeTransport.DefaultClientKey.

cancellationToken

Cancels the request, as a closing page does.

Returns

The reply's JSON.

Exceptions

InvalidOperationException

No window serves the transport.

SendAsync

public ValueTask<bool> SendAsync(string connectionKey, ReadOnlyMemory<byte> message, CancellationToken cancellationToken = default)

Records a push to one open connection.

Serve

public IDisposable Serve(IBridgeRequestHandler handler)

Delivers the requests of every connection of this transport to handler.

Parameters

handler

The window's request handler.

Returns

A lease that stops delivering requests to handler.

Exceptions

InvalidOperationException

The transport already serves a handler.

ObjectDisposedException

The transport was disposed.

Remarks

A transport serves one handler at a time. It calls IBridgeRequestHandler.HandleAsync for the requests of one connection in the order they arrived, without waiting for a reply before it passes the next request: some requests, such as receive and awaited commands, stay open for a long time. The handler admits a request before its returned task first yields, so calling it in arrival order keeps the connection's admission order. A transport reports each connection that closes with IBridgeRequestHandler.ConnectionClosed.

A request that arrives while no handler is served, or after the lease was disposed, is answered with the released reply of the Views protocol, or not at all when the page's connection is closing.

Documentation from IBridgeTransport.Serve.

SetLanguages

public void SetLanguages(string connectionKey, IReadOnlyList<string> languages)

Sets the languages a test connection reports in its hello (feature client-languages/1), as a browser reports its navigator.languages. Set them before the connection's first request; the window then lists them as the connection's RunicConnection.Languages. A connection without languages set reports none.

Parameters

connectionKey

The connection.

languages

BCP 47 tags, most preferred first; at most 16.

Remarks

host.Transport.SetLanguages("bob-tab", ["de-CH", "de", "en"]);
var bob = host.Root.ForConnection("bob-tab", clientKey: "bob");

TakeMessages

public IReadOnlyList<RunicBridgeMessage> TakeMessages()

Returns and removes the pushes the window sent since the previous call, in order.