CSharpEssentials.Mediator.Testing 9.0.0

dotnet add package CSharpEssentials.Mediator.Testing --version 9.0.0
                    
NuGet\Install-Package CSharpEssentials.Mediator.Testing -Version 9.0.0
                    
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="CSharpEssentials.Mediator.Testing" Version="9.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="CSharpEssentials.Mediator.Testing" Version="9.0.0" />
                    
Directory.Packages.props
<PackageReference Include="CSharpEssentials.Mediator.Testing" />
                    
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 CSharpEssentials.Mediator.Testing --version 9.0.0
                    
#r "nuget: CSharpEssentials.Mediator.Testing, 9.0.0"
                    
#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 CSharpEssentials.Mediator.Testing@9.0.0
                    
#: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=CSharpEssentials.Mediator.Testing&version=9.0.0
                    
Install as a Cake Addin
#tool nuget:?package=CSharpEssentials.Mediator.Testing&version=9.0.0
                    
Install as a Cake Tool

CSharpEssentials.Mediator.Testing

Test helpers for the resource locks of CSharpEssentials.Mediator:

  • ResourceLockContract: contract scenarios for any IResourceLock. Give it your lock and run the scenarios that the built-in locks pass. It references no test framework, so it works from xUnit (v2 or v3), NUnit or MSTest.
  • RecordingResourceLock: a public in-process IResourceLock that records every acquire and release, for testing handlers behind LockBehavior.

Installation

dotnet add package CSharpEssentials.Mediator.Testing

Add it to a test project of any framework. Targets net11.0, net10.0, net9.0 and net8.0.

Contract tests

Each scenario is a public method that throws ResourceLockContractException when the lock breaks the contract. ScenarioNames lists them and RunAsync(name) runs one, so a single parameterized test covers all of them. With xUnit:

using CSharpEssentials.Locking;
using CSharpEssentials.Locking.Testing;

public sealed class RedisResourceLockTests
{
    public static TheoryData<string> Scenarios => new(ResourceLockContract.ScenarioNames);

    [Theory]
    [MemberData(nameof(Scenarios))]
    public Task Scenario(string name) => new ResourceLockContract(new RedisResourceLock(Connection)).RunAsync(name);
}

With NUnit, use [TestCaseSource] over ResourceLockContract.ScenarioNames; with MSTest, [DynamicData].

The scenarios cover:

  • try-acquire of a free key, of a held key, and after the holder disposed its handle;
  • independent keys;
  • TimeoutException when the key stays held, including a zero timeout;
  • a waiting acquire that completes when the holder releases, and one cancelled while it waits;
  • a second dispose of a handle that does not release the next holder;
  • parallel acquirers that never overlap;
  • empty and null keys.

Each scenario uses keys of its own, so the scenarios can share one database or cache. The waits are real: ShortWait (300 ms) is how long a held key must keep a waiter out, and LongWait (10 seconds) bounds every wait that must succeed. Pass other values to the constructor for a slow backend, and make a polling lock poll more often than ShortWait.

Recording lock

RecordingResourceLock locks = new();
services.AddSingleton<IResourceLock>(locks);

await mediator.Send(new CapturePaymentCommand(orderId));

locks.Acquisitions.Should().ContainSingle().Which.Key.Should().Be($"order:{orderId}");
locks.HeldKeys.Should().BeEmpty();
Member What it does
Acquisitions Every AcquireAsync and TryAcquireAsync call in order, as LockAcquisition(Key, Timeout, TryOnly), including calls that failed
Releases The key of every released handle, in order. A second dispose of a handle is not recorded
HeldKeys The keys held right now
FailWith When set, the next calls are recorded and then throw this exception

It locks for real through InProcessResourceLock, so a test sees the same blocking and TimeoutException behavior as the default lock.

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 is compatible.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  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.  net11.0 is compatible. 
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
9.0.0 0 11/28/2026