RunicServeOnlyApplication class

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

public sealed class RunicServeOnlyApplication : IAsyncDisposable

Inherits or implements IAsyncDisposable.

A built Runic application running in serve-only mode for a browser test: started with RUNIC_APPLICATION_SERVE_ONLY=1 and without DISPLAY or WAYLAND_DISPLAY, it serves its Window's page at RunicServeOnlyApplication.Url instead of opening a window, on Runic Desktop and CS-WebUI alike.

Remarks

await using var app = await RunicServeOnlyApplication.StartAsync(
    RunicServeOnlyApplication.CreateStartInfo("path/to/MyApp.dll"));
await page.GotoAsync(app.Url.ToString());
// ...
Assert.Equal(0, await app.StopAsync());

The application writes RUNIC_APPLICATION_URL=<url> to standard output for each Window it opens; RunicServeOnlyApplication.Url is the first, RunicServeOnlyApplication.Urls lists all so far, and RunicServeOnlyApplication.WaitForWindowUrlAsync waits for a later one, by its position or by the Window type that a 0.16 host names on a RUNIC_APPLICATION_WINDOW=<url> <type> line. RunicServeOnlyApplication.StopAsync writes a line to standard input, which ends serving on every platform, Windows included. When the application has not exited within the timeout, its process tree is killed. Standard output and standard error are captured for failure messages. When the test process exits without disposing it, the application is killed on a best-effort basis: on Windows through a job object, which also covers a test process that is killed itself, and elsewhere when AppDomain.ProcessExit runs. A test process killed on Linux or macOS, for example with SIGKILL, can leave the application running.

Properties

ProcessId

public int ProcessId { get; }

The application's process id.

StandardError

public string StandardError { get; }

The standard error written so far.

StandardOutput

public string StandardOutput { get; }

The standard output written so far.

Url

public Uri Url { get; }

The URL of the first Window's page, from RUNIC_APPLICATION_URL=.

Urls

public IReadOnlyList<Uri> Urls { get; }

The URL of each Window's page the application has written so far, in the order it opened them.

Remarks

The first is RunicServeOnlyApplication.Url: an invalid first URL fails RunicServeOnlyApplication.StartAsync, and a later line with an invalid URL is not listed.

Methods

CreateStartInfo

public static ProcessStartInfo CreateStartInfo(string applicationPath)

Describes how to start the built application at applicationPath.

Parameters

applicationPath

The application's executable, or its .dll, which runs with dotnet. Its directory becomes the working directory.

Returns

Start information to pass to RunicServeOnlyApplication.StartAsync, after adding arguments or environment variables.

DisposeAsync

public ValueTask DisposeAsync()

Stops the application like RunicServeOnlyApplication.StopAsync and releases it. An application that does not exit in time is killed with its process tree without an exception; call RunicServeOnlyApplication.StopAsync to see a shutdown that hangs.

GetDescendantProcesses

public IReadOnlyList<RunicDescendantProcess> GetDescendantProcesses()

Lists the application's running descendant processes: its children, their children, and so on.

Remarks

List them while the application runs: a process whose parent has exited is no longer its descendant. After RunicServeOnlyApplication.StopAsync, RunicDescendantProcess.HasExited tells whether the application ended each one. The snapshots hold no process handle.

StartAsync

public static Task<RunicServeOnlyApplication> StartAsync(ProcessStartInfo startInfo, TimeSpan? timeout = null, CancellationToken cancellationToken = default)

Starts an application in serve-only mode and waits for its URL.

Parameters

startInfo

How to start the application, from RunicServeOnlyApplication.CreateStartInfo or your own. Its file name, arguments, working directory and environment are used, and it is not changed: the application runs with RUNIC_APPLICATION_SERVE_ONLY=1, without DISPLAY and WAYLAND_DISPLAY, and with redirected standard streams.

timeout

How long to wait for the URL; the default is 30 seconds.

cancellationToken

Cancels the wait; the application is then killed.

Returns

The serving application. Dispose it to stop it.

Exceptions

InvalidOperationException

The application exited before it wrote its URL.

TimeoutException

The application did not write its URL within timeout; it was killed.

StopAsync

public Task<int> StopAsync(TimeSpan? timeout = null, CancellationToken cancellationToken = default)

Stops serving by writing a line to standard input and waits for the application to exit. When it has not exited within timeout, its process tree is killed and TimeoutException is thrown.

Parameters

timeout

How long to wait for the exit; the default is 30 seconds.

cancellationToken

Cancels this wait; the application keeps stopping, and disposing it ends it.

Returns

The application's exit code.

Exceptions

TimeoutException

The application did not exit in time and was killed.

InvalidOperationException

The application was killed earlier, after it did not exit in time.

Remarks

The line is written once. A further call, for example with a longer timeout after a cancelled one, waits for the same exit with its own timeout, and returns the exit code at once when the application has exited. After a timeout killed the application, every call throws InvalidOperationException.

WaitForWindowUrlAsync

public Task<Uri> WaitForWindowUrlAsync(int index, TimeSpan timeout, CancellationToken cancellationToken = default)

Waits for the URL of the Window at index, in the order the application opened its Windows.

Parameters

index

The Window's position from 0; 0 is RunicServeOnlyApplication.Url.

timeout

How long to wait.

cancellationToken

Cancels the wait; the application keeps running.

Returns

The Window's URL.

Exceptions

TimeoutException

The application wrote no such URL in time; it keeps running.

InvalidOperationException

The application exited first.

WaitForWindowUrlAsync

public Task<Uri> WaitForWindowUrlAsync(string windowType, TimeSpan timeout, CancellationToken cancellationToken = default)

Waits for the URL of the first Window of type windowType.

Parameters

windowType

The Window class's full name, such as MyApp.SettingsWindow, or its name without the namespace, such as SettingsWindow. A closed generic Window type is named without spaces, by its generic definition and its arguments' names without namespaces: MyApp.DocumentWindow`1[Report] or DocumentWindow`1[Report].

timeout

How long to wait.

cancellationToken

Cancels the wait; the application keeps running.

Returns

The Window's URL.

Exceptions

TimeoutException

The application named no such Window in time; it keeps running.

InvalidOperationException

The application exited first.

Remarks

The type comes from the RUNIC_APPLICATION_WINDOW= line that Runic Desktop and CS-WebUI write from 0.16 on; an application on an earlier host never names its Windows, so wait by position there.