Trellis.Testing.Worker 3.0.0-alpha.342

This is a prerelease version of Trellis.Testing.Worker.
dotnet add package Trellis.Testing.Worker --version 3.0.0-alpha.342
                    
NuGet\Install-Package Trellis.Testing.Worker -Version 3.0.0-alpha.342
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Trellis.Testing.Worker" Version="3.0.0-alpha.342" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Trellis.Testing.Worker" Version="3.0.0-alpha.342" />
                    
Directory.Packages.props
<PackageReference Include="Trellis.Testing.Worker" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Trellis.Testing.Worker --version 3.0.0-alpha.342
                    
#r "nuget: Trellis.Testing.Worker, 3.0.0-alpha.342"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Trellis.Testing.Worker@3.0.0-alpha.342
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Trellis.Testing.Worker&version=3.0.0-alpha.342&prerelease
                    
Install as a Cake Addin
#tool nuget:?package=Trellis.Testing.Worker&version=3.0.0-alpha.342&prerelease
                    
Install as a Cake Tool

Trellis.Testing.Worker

NuGet Package

Integration-test harness for BackgroundService workers built on Trellis.

Installation

dotnet add package Trellis.Testing.Worker

Quick Example

using Microsoft.Extensions.DependencyInjection;
using Trellis.Authorization;
using Trellis.Mediator;
using Trellis.Testing.Worker;

var systemActor = Actor.Create(
    "subscription-renewal-worker",
    new HashSet<string>(["Subscriptions.Read", "Subscriptions.Write"]));

await using var harness = await WorkerHarness<SubscriptionRenewalWorker>.CreateAsync(opts =>
{
    opts.SystemActor = systemActor;
    opts.ConfigureServices(services =>
    {
        services.AddLogging();
        services.AddDomainEventDispatch();   // same registration as production
        services.AddSingleton<ISubscriptionRepository, FakeSubscriptionRepository>();
        services.AddScoped<IExternalGateway, FakeExternalGateway>();
    });
    opts.SeedAsync(async (sp, ct) =>
    {
        var repo = sp.GetRequiredService<ISubscriptionRepository>();
        await repo.AddAsync(new Subscription { RenewsAt = opts.InitialTime.AddDays(1) }, ct);
    });
});

await harness.StartAsync();
await harness.SettleAsync(); // yield real time so the worker registers its first Task.Delay
harness.Time.Advance(TimeSpan.FromDays(1));

var reminded = await harness.WaitForEventAsync<SubscriptionReminderSent>();
reminded.SubscriptionId.Should().Be(expectedId);

Deterministic alternative to SettleAsync(): have the worker REGISTER its first Task.Delay(period, time, ct) (e.g. var nextDelay = Task.Delay(period, time, ct);) BEFORE calling IWorkerTickSignal.SignalAsync("ready", ct), then await the saved delay. The Task.Delay(TimeSpan, TimeProvider, CancellationToken) overload eagerly calls timeProvider.CreateTimer(...) when invoked, so the callback is registered with FakeTimeProvider before the signal fires — await harness.WaitForTickAsync("ready") can then release and the test can safely call Time.Advance(...) with no race. Signaling before Task.Delay(...) would leave a gap during which the test could advance the clock before any callback was registered. No real-time yield, no flakiness.

Wiring EF Core with SQLite? Don't pair AddDbContextFactory<T> with Data Source=:memory: — every new connection opens a fresh database, so the seed and the worker see different empty stores. Use a shared/open SqliteConnection (see the integration-testing article) or a temp-file SQLite database.

Key Features

  • IHost with a deterministic FakeTimeProvider — advance the clock with harness.Time.Advance(...) to drive Task.Delay(interval, timeProvider, ct) continuations on demand.
  • TestActorProvider registered as the IActorProvider — gives workers an ambient system actor without HttpContext. Override with opts.SystemActor.
  • Domain-event capture — every event published through Trellis's mediator pipeline is captured. Inspect via harness.Events<TEvent>(); await one via harness.WaitForEventAsync<TEvent>(predicate).
  • Optional tick signal — workers that emit no domain events can resolve IWorkerTickSignal and call SignalAsync(name) at the end of each tick so the test can block on harness.WaitForTickAsync(name).
  • Race-proof waitsWaitForEventAsync and WaitForTickAsync snapshot existing captures before subscribing so events that fire between Advance(...) and the wait still satisfy the wait.
  • Real-time timeouts — wait timeouts are measured against the real clock, not the fake one; harness.Time.Advance(...) does not consume the timeout budget.

What the harness does not do

  • It does not call services.AddDomainEventDispatch() — worker tests are integration tests of the production composition root. Register it in your ConfigureServices callback the same way the worker's production host does.
  • It does not wire FakeLogger. Add Microsoft.Extensions.Diagnostics.Testing and opts.ConfigureLogging(b => b.AddFakeLogging()) in your test if you need it.

Documentation

Part of Trellis

This package is part of the Trellis framework.

Product Compatible and additional computed target framework versions.
.NET net10.0 is compatible.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
3.0.0-alpha.342 38 6/5/2026
3.0.0-alpha.337 40 6/3/2026
3.0.0-alpha.336 40 6/3/2026