diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml
index ff39803..5f84662 100644
--- a/.github/workflows/ci.yml
+++ b/.github/workflows/ci.yml
@@ -59,6 +59,14 @@ jobs:
if: github.event_name != 'pull_request'
run: dotnet test --no-build -c Release --logger trx --collect:"XPlat Code Coverage"
+ # Microsoft.Extensions.DependencyInjection 8.0.0 resolves the Abstractions 8.0.1 floor that src/RequestFlow declares.
+ # The separate artifacts path keeps this build away from the outputs Pack reads.
+ - name: Test at the dependency floor
+ run: >
+ dotnet test tests/RequestFlow.Tests.Unit -c Release -f net8.0
+ -p:DependencyInjectionVersion=8.0.0
+ --artifacts-path artifacts/dependency-floor
+
- name: Pack
run: dotnet pack --no-build -c Release -o artifacts/packages
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 43db70b..6423b63 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -4,6 +4,19 @@ Format follows [Keep a Changelog](https://keepachangelog.com/en/1.1.0/). Version
The `release` workflow uses the section matching the pushed tag as the GitHub Release body. A missing section fails the build before publishing. Before tagging, rename `[Unreleased]` to the release version and add its date.
+## [Unreleased]
+
+### Fixed
+
+- `AddRequestFlow` no longer throws `InvalidOperationException` ("This service descriptor is keyed") when a keyed service is registered first and the application resolves `Microsoft.Extensions.DependencyInjection.Abstractions` 8.0.0 or 8.0.1, on .NET Framework as well as .NET.
+- An interface declared as an event publish strategy for more than one target reports `RF0013` once. The repeat no longer adds `RF0014`.
+- `ICommandDispatcher` and `IQueryDispatcher` resolve to separate classes, so a query dispatcher can no longer be cast to a command dispatcher. The ValueTask pair is split the same way.
+- The detached strategy sample in [events.md](docs/events.md#detach-entry-work-from-the-publishing-token) takes `IHostApplicationLifetime`, which a hosted application registers. The earlier constructor took a `CancellationToken`, which the container cannot resolve.
+
+### Performance
+
+- Publication allocates less for a `ParallelPublishStrategy` event with no applicable handler. Cancellation and completion behavior are unchanged.
+
## [1.0.0] - 2026-10-17
First stable release.
@@ -176,6 +189,7 @@ First public preview.
- `RequestFlow.Cqrs.Abstractions` and `RequestFlow.Cqrs`: command and query contracts with typed dispatchers, registered through `AddCqrs`, for codebases that want the split enforced by the compiler.
- `provider.ValidateRequestFlow()` to force validation at startup instead of at the first dispatch.
+[Unreleased]: https://github.com/illia1f/RequestFlow/compare/v1.0.0...HEAD
[1.0.0]: https://github.com/illia1f/RequestFlow/compare/v1.0.0-preview.9...v1.0.0
[1.0.0-preview.9]: https://github.com/illia1f/RequestFlow/compare/v1.0.0-preview.8...v1.0.0-preview.9
[1.0.0-preview.8]: https://github.com/illia1f/RequestFlow/compare/v1.0.0-preview.7...v1.0.0-preview.8
diff --git a/README.md b/README.md
index fe09f33..863f482 100644
--- a/README.md
+++ b/README.md
@@ -17,7 +17,7 @@ Supports Task and ValueTask requests, stages, streams, and events. Additionally,
> dotnet add package RequestFlow
> ```
-See the [1.0.0 release notes](https://github.com/illia1f/RequestFlow/blob/main/CHANGELOG.md#100---2026-10-14) for changes since preview.9.
+See the [1.0.0 release notes](https://github.com/illia1f/RequestFlow/blob/main/CHANGELOG.md#100---2026-10-17) for changes since preview.9.
**[Getting started: define a request, register its handler, and send it](https://github.com/illia1f/RequestFlow/blob/main/docs/getting-started.md).**
diff --git a/ROADMAP.md b/ROADMAP.md
index 37a7f7a..5471697 100644
--- a/ROADMAP.md
+++ b/ROADMAP.md
@@ -2,7 +2,7 @@
Anything past v1 is direction, not commitment.
-Release: [1.0.0](CHANGELOG.md#100---2026-10-14).
+Release: [1.0.0](CHANGELOG.md#100---2026-10-17).
## v1.0
diff --git a/docs/events.md b/docs/events.md
index 430c769..da8a7de 100644
--- a/docs/events.md
+++ b/docs/events.md
@@ -200,7 +200,7 @@ public async Task PublishAsync(
`CancellationToken.None` means "use the publishing token" for `StartAsync` and `RunAsync`. Pass a real strategy-owned or application-lifetime token to detach work:
```csharp
-public sealed class DetachedStrategy(CancellationToken lifetimeToken)
+public sealed class DetachedStrategy(IHostApplicationLifetime lifetime)
: IEventPublishStrategy
{
public async Task PublishAsync(
@@ -211,7 +211,7 @@ public sealed class DetachedStrategy(CancellationToken lifetimeToken)
throw delivery.Canceled(null, delivery.Count, cancellationToken);
for (int i = 0; i < delivery.Count; i++)
- _ = ObserveAsync(delivery, i, lifetimeToken);
+ _ = ObserveAsync(delivery, i, lifetime.ApplicationStopping);
await Task.CompletedTask;
}
@@ -229,6 +229,8 @@ public sealed class DetachedStrategy(CancellationToken lifetimeToken)
}
```
+`IHostApplicationLifetime` is in `Microsoft.Extensions.Hosting`, and only a host registers it: the Generic Host or ASP.NET Core. Without a host, inject a service of your own that exposes the token.
+
Detached work may outlive the publishing scope. Do not use this pattern when handlers depend on scoped services that can be disposed after `PublishAsync` returns.
## Which handlers receive an event
diff --git a/src/RequestFlow.Cqrs/CqrsDispatcher.cs b/src/RequestFlow.Cqrs/CqrsCommandDispatcher.cs
similarity index 58%
rename from src/RequestFlow.Cqrs/CqrsDispatcher.cs
rename to src/RequestFlow.Cqrs/CqrsCommandDispatcher.cs
index 83c24b9..d194fff 100644
--- a/src/RequestFlow.Cqrs/CqrsDispatcher.cs
+++ b/src/RequestFlow.Cqrs/CqrsCommandDispatcher.cs
@@ -4,10 +4,7 @@
namespace RequestFlow.Cqrs;
-///
-/// Default and forwarding to .
-///
-internal sealed class CqrsDispatcher(IRequestDispatcher dispatcher) : ICommandDispatcher, IQueryDispatcher
+internal sealed class CqrsCommandDispatcher(IRequestDispatcher dispatcher) : ICommandDispatcher
{
private readonly IRequestDispatcher _dispatcher = dispatcher ?? throw new ArgumentNullException(nameof(dispatcher));
@@ -26,12 +23,4 @@ public Task SendAsync(ICommand command, CancellationToken cancellationToken = de
return _dispatcher.SendAsync(command, cancellationToken);
}
-
- ///
- public Task SendAsync(IQuery query, CancellationToken cancellationToken = default)
- {
- ThrowHelper.ThrowIfNull(query);
-
- return _dispatcher.SendAsync(query, cancellationToken);
- }
}
diff --git a/src/RequestFlow.Cqrs/CqrsQueryDispatcher.cs b/src/RequestFlow.Cqrs/CqrsQueryDispatcher.cs
new file mode 100644
index 0000000..40490d8
--- /dev/null
+++ b/src/RequestFlow.Cqrs/CqrsQueryDispatcher.cs
@@ -0,0 +1,18 @@
+using System;
+using System.Threading;
+using System.Threading.Tasks;
+
+namespace RequestFlow.Cqrs;
+
+internal sealed class CqrsQueryDispatcher(IRequestDispatcher dispatcher) : IQueryDispatcher
+{
+ private readonly IRequestDispatcher _dispatcher = dispatcher ?? throw new ArgumentNullException(nameof(dispatcher));
+
+ ///
+ public Task SendAsync(IQuery query, CancellationToken cancellationToken = default)
+ {
+ ThrowHelper.ThrowIfNull(query);
+
+ return _dispatcher.SendAsync(query, cancellationToken);
+ }
+}
diff --git a/src/RequestFlow.Cqrs/CqrsRequestFlowBuilderExtensions.cs b/src/RequestFlow.Cqrs/CqrsRequestFlowBuilderExtensions.cs
index 79124f2..7e01c57 100644
--- a/src/RequestFlow.Cqrs/CqrsRequestFlowBuilderExtensions.cs
+++ b/src/RequestFlow.Cqrs/CqrsRequestFlowBuilderExtensions.cs
@@ -23,10 +23,10 @@ public static RequestFlowBuilder AddCqrs(this RequestFlowBuilder builder)
if (builder is null)
throw new ArgumentNullException(nameof(builder));
- builder.Services.TryAddTransient();
- builder.Services.TryAddTransient();
- builder.Services.TryAddTransient();
- builder.Services.TryAddTransient();
+ builder.Services.TryAddTransient();
+ builder.Services.TryAddTransient();
+ builder.Services.TryAddTransient();
+ builder.Services.TryAddTransient();
builder.Services.TryAddTransient();
builder.Services.TryAddEnumerable(
diff --git a/src/RequestFlow.Cqrs/CqrsValueDispatcher.cs b/src/RequestFlow.Cqrs/CqrsValueCommandDispatcher.cs
similarity index 65%
rename from src/RequestFlow.Cqrs/CqrsValueDispatcher.cs
rename to src/RequestFlow.Cqrs/CqrsValueCommandDispatcher.cs
index 5fa3c34..4992249 100644
--- a/src/RequestFlow.Cqrs/CqrsValueDispatcher.cs
+++ b/src/RequestFlow.Cqrs/CqrsValueCommandDispatcher.cs
@@ -4,8 +4,8 @@
namespace RequestFlow.Cqrs;
-internal sealed class CqrsValueDispatcher(IValueRequestDispatcher dispatcher)
- : IValueCommandDispatcher, IValueQueryDispatcher
+internal sealed class CqrsValueCommandDispatcher(IValueRequestDispatcher dispatcher)
+ : IValueCommandDispatcher
{
private readonly IValueRequestDispatcher _dispatcher =
dispatcher ?? throw new ArgumentNullException(nameof(dispatcher));
@@ -29,14 +29,4 @@ public ValueTask SendAsync(
return _dispatcher.SendAsync(command, cancellationToken);
}
-
- ///
- public ValueTask SendAsync(
- IValueQuery query,
- CancellationToken cancellationToken = default)
- {
- ThrowHelper.ThrowIfNull(query);
-
- return _dispatcher.SendAsync(query, cancellationToken);
- }
}
diff --git a/src/RequestFlow.Cqrs/CqrsValueQueryDispatcher.cs b/src/RequestFlow.Cqrs/CqrsValueQueryDispatcher.cs
new file mode 100644
index 0000000..1c004d6
--- /dev/null
+++ b/src/RequestFlow.Cqrs/CqrsValueQueryDispatcher.cs
@@ -0,0 +1,22 @@
+using System;
+using System.Threading;
+using System.Threading.Tasks;
+
+namespace RequestFlow.Cqrs;
+
+internal sealed class CqrsValueQueryDispatcher(IValueRequestDispatcher dispatcher)
+ : IValueQueryDispatcher
+{
+ private readonly IValueRequestDispatcher _dispatcher =
+ dispatcher ?? throw new ArgumentNullException(nameof(dispatcher));
+
+ ///
+ public ValueTask SendAsync(
+ IValueQuery query,
+ CancellationToken cancellationToken = default)
+ {
+ ThrowHelper.ThrowIfNull(query);
+
+ return _dispatcher.SendAsync(query, cancellationToken);
+ }
+}
diff --git a/src/RequestFlow/Events/EventPlanFactory.cs b/src/RequestFlow/Events/EventPlanFactory.cs
index b4be81f..3e55d0e 100644
--- a/src/RequestFlow/Events/EventPlanFactory.cs
+++ b/src/RequestFlow/Events/EventPlanFactory.cs
@@ -43,7 +43,7 @@ private static EventPlan CreatePlan(
if (strategyType == typeof(SequentialPublishStrategy))
return new SequentialEventPlan(@event, entries);
if (strategyType == typeof(ParallelPublishStrategy))
- return entries.Length == 1
+ return entries.Length <= 1
? new SequentialEventPlan(@event, entries)
: new ParallelEventPlan(@event, entries);
if (strategyType == typeof(FailFastPublishStrategy))
diff --git a/src/RequestFlow/Registration/ServiceCollectionExtensions.cs b/src/RequestFlow/Registration/ServiceCollectionExtensions.cs
index c76df27..aa97f90 100644
--- a/src/RequestFlow/Registration/ServiceCollectionExtensions.cs
+++ b/src/RequestFlow/Registration/ServiceCollectionExtensions.cs
@@ -147,8 +147,12 @@ private static RequestFlowRegistry GetOrAddRegistry(IServiceCollection services)
{
foreach (var descriptor in services)
{
- if (descriptor.ImplementationInstance is RequestFlowRegistry existing)
+ // ImplementationInstance throws on a keyed descriptor in Microsoft.Extensions.DependencyInjection.Abstractions 8.0.0 and 8.0.1.
+ if (descriptor.ServiceType == typeof(RequestFlowRegistry)
+ && descriptor.ImplementationInstance is RequestFlowRegistry existing)
+ {
return existing;
+ }
}
var registry = new RequestFlowRegistry();
diff --git a/src/RequestFlow/Validation/Rules/EventStrategyRule.cs b/src/RequestFlow/Validation/Rules/EventStrategyRule.cs
index 9c2009c..b25447e 100644
--- a/src/RequestFlow/Validation/Rules/EventStrategyRule.cs
+++ b/src/RequestFlow/Validation/Rules/EventStrategyRule.cs
@@ -62,8 +62,11 @@ private static IEnumerable FindShapeProblems(
foreach (EventStrategyModel strategy in strategies)
{
Type strategyType = strategy.StrategyType;
- if (strategyType.IsInterface && reportedInterfaces.Add(strategyType))
+ if (strategyType.IsInterface)
{
+ if (!reportedInterfaces.Add(strategyType))
+ continue;
+
yield return new RequestFlowValidationProblem(
ProblemCodes.EventStrategyIsInterface,
$"Event publish strategy '{strategyType.FullName}' is an interface, so the " +
diff --git a/tests/Directory.Build.props b/tests/Directory.Build.props
index bbbcf1b..c19d2cf 100644
--- a/tests/Directory.Build.props
+++ b/tests/Directory.Build.props
@@ -12,12 +12,13 @@
false
true
+ 10.0.9
-
+
diff --git a/tests/RequestFlow.Cqrs.Tests.Unit/AddCqrsTests.cs b/tests/RequestFlow.Cqrs.Tests.Unit/AddCqrsTests.cs
index 9f9edd1..03d84ba 100644
--- a/tests/RequestFlow.Cqrs.Tests.Unit/AddCqrsTests.cs
+++ b/tests/RequestFlow.Cqrs.Tests.Unit/AddCqrsTests.cs
@@ -60,6 +60,20 @@ public async Task Given_Registered_Cqrs_When_Sending_Value_Query_Then_Returns_Va
result.ShouldBe("found 42");
}
+ [Fact]
+ public void Given_Registered_Cqrs_When_Resolving_Task_Dispatchers_Then_Neither_Implements_The_Other_Side()
+ {
+ _queries.ShouldNotBeAssignableTo();
+ _commands.ShouldNotBeAssignableTo();
+ }
+
+ [Fact]
+ public void Given_Registered_Cqrs_When_Resolving_Value_Dispatchers_Then_Neither_Implements_The_Other_Side()
+ {
+ _valueQueries.ShouldNotBeAssignableTo();
+ _valueCommands.ShouldNotBeAssignableTo();
+ }
+
[Fact]
public async Task Given_Registered_Cqrs_When_Streaming_Query_Then_Yields_Query_Handler_Items()
{
@@ -232,11 +246,11 @@ public void Given_Registered_Request_Flow_When_Registering_Cqrs_Then_Dispatchers
d.ServiceType == typeof(IStreamQueryDispatcher) && d.Lifetime == ServiceLifetime.Transient);
services.ShouldContain(d =>
d.ServiceType == typeof(IValueCommandDispatcher)
- && d.ImplementationType == typeof(CqrsValueDispatcher)
+ && d.ImplementationType == typeof(CqrsValueCommandDispatcher)
&& d.Lifetime == ServiceLifetime.Transient);
services.ShouldContain(d =>
d.ServiceType == typeof(IValueQueryDispatcher)
- && d.ImplementationType == typeof(CqrsValueDispatcher)
+ && d.ImplementationType == typeof(CqrsValueQueryDispatcher)
&& d.Lifetime == ServiceLifetime.Transient);
}
diff --git a/tests/RequestFlow.Cqrs.Tests.Unit/CqrsDispatcherTests.cs b/tests/RequestFlow.Cqrs.Tests.Unit/CqrsCommandDispatcherTests.cs
similarity index 67%
rename from tests/RequestFlow.Cqrs.Tests.Unit/CqrsDispatcherTests.cs
rename to tests/RequestFlow.Cqrs.Tests.Unit/CqrsCommandDispatcherTests.cs
index aa23cf3..5611e31 100644
--- a/tests/RequestFlow.Cqrs.Tests.Unit/CqrsDispatcherTests.cs
+++ b/tests/RequestFlow.Cqrs.Tests.Unit/CqrsCommandDispatcherTests.cs
@@ -3,7 +3,7 @@
namespace RequestFlow.Cqrs.Tests.Unit;
-public sealed class CqrsDispatcherTests
+public sealed class CqrsCommandDispatcherTests
{
[Fact]
public async Task Given_Typed_Command_When_Sending_Command_Then_Forwards_Command_And_Token_And_Returns_The_Same_Task()
@@ -36,21 +36,6 @@ public async Task Given_Void_Command_When_Sending_Command_Then_Forwards_To_Void_
await _requests.Received(1).SendAsync((IRequest)command, cts.Token);
}
- [Fact]
- public async Task Given_Query_When_Sending_Query_Then_Forwards_Query_And_Token_And_Returns_The_Same_Task()
- {
- var query = new FindName("42");
- using var cts = new CancellationTokenSource();
- Task expected = Task.FromResult("bob");
- _requests.SendAsync(query, cts.Token).Returns(expected);
-
- Task result = _sut.SendAsync(query, cts.Token);
-
- result.ShouldBeSameAs(expected);
- (await result).ShouldBe("bob");
- await _requests.Received(1).SendAsync(query, cts.Token);
- }
-
[Fact]
public void Given_Null_Typed_Command_When_Sending_Command_Then_Throws_Argument_Null_Exception()
{
@@ -69,30 +54,21 @@ public void Given_Null_Void_Command_When_Sending_Command_Then_Throws_Argument_Nu
exception.ParamName.ShouldBe("command");
}
- [Fact]
- public void Given_Null_Query_When_Sending_Query_Then_Throws_Argument_Null_Exception()
- {
- var exception = Should.Throw(
- () => { _sut.SendAsync((IQuery)null!); });
-
- exception.ParamName.ShouldBe("query");
- }
-
[Fact]
public void Given_Null_Request_Dispatcher_When_Creating_Dispatcher_Then_Throws_Argument_Null_Exception()
{
- Should.Throw(() => new CqrsDispatcher(null!));
+ Should.Throw(() => new CqrsCommandDispatcher(null!));
}
#region Initialization
private readonly IRequestDispatcher _requests;
- private readonly CqrsDispatcher _sut;
+ private readonly CqrsCommandDispatcher _sut;
- public CqrsDispatcherTests()
+ public CqrsCommandDispatcherTests()
{
_requests = Substitute.For();
- _sut = new CqrsDispatcher(_requests);
+ _sut = new CqrsCommandDispatcher(_requests);
}
#endregion
@@ -104,8 +80,6 @@ public sealed record Rename(string Name) : ICommand;
public sealed record Purge : ICommand;
- public sealed record FindName(string Id) : IQuery;
-
// The assembly scan demands one handler per request type, even for requests only mocked here.
public sealed class RenameHandler : ICommandHandler
{
@@ -119,11 +93,5 @@ public Task HandleAsync(Purge request, CancellationToken cancellationToken)
=> Task.CompletedTask;
}
- public sealed class FindNameHandler : IQueryHandler
- {
- public Task HandleAsync(FindName request, CancellationToken cancellationToken)
- => Task.FromResult(request.Id);
- }
-
#endregion
}
diff --git a/tests/RequestFlow.Cqrs.Tests.Unit/CqrsQueryDispatcherTests.cs b/tests/RequestFlow.Cqrs.Tests.Unit/CqrsQueryDispatcherTests.cs
new file mode 100644
index 0000000..899ddd9
--- /dev/null
+++ b/tests/RequestFlow.Cqrs.Tests.Unit/CqrsQueryDispatcherTests.cs
@@ -0,0 +1,64 @@
+using RequestFlow;
+using RequestFlow.Cqrs;
+
+namespace RequestFlow.Cqrs.Tests.Unit;
+
+public sealed class CqrsQueryDispatcherTests
+{
+ [Fact]
+ public async Task Given_Query_When_Sending_Query_Then_Forwards_Query_And_Token_And_Returns_The_Same_Task()
+ {
+ var query = new FindName("42");
+ using var cts = new CancellationTokenSource();
+ Task expected = Task.FromResult("bob");
+ _requests.SendAsync(query, cts.Token).Returns(expected);
+
+ Task result = _sut.SendAsync(query, cts.Token);
+
+ result.ShouldBeSameAs(expected);
+ (await result).ShouldBe("bob");
+ await _requests.Received(1).SendAsync(query, cts.Token);
+ }
+
+ [Fact]
+ public void Given_Null_Query_When_Sending_Query_Then_Throws_Argument_Null_Exception()
+ {
+ var exception = Should.Throw(
+ () => { _sut.SendAsync((IQuery)null!); });
+
+ exception.ParamName.ShouldBe("query");
+ }
+
+ [Fact]
+ public void Given_Null_Request_Dispatcher_When_Creating_Dispatcher_Then_Throws_Argument_Null_Exception()
+ {
+ Should.Throw(() => new CqrsQueryDispatcher(null!));
+ }
+
+ #region Initialization
+
+ private readonly IRequestDispatcher _requests;
+ private readonly CqrsQueryDispatcher _sut;
+
+ public CqrsQueryDispatcherTests()
+ {
+ _requests = Substitute.For();
+ _sut = new CqrsQueryDispatcher(_requests);
+ }
+
+ #endregion
+
+ #region Helpers
+
+ // Public so NSubstitute can proxy dispatcher interfaces closed over this type.
+ public sealed record FindName(string Id) : IQuery;
+
+ // The assembly scan demands one handler per request type, even for requests only mocked here.
+ public sealed class FindNameHandler : IQueryHandler
+ {
+ public Task HandleAsync(FindName request, CancellationToken cancellationToken)
+ => Task.FromResult(request.Id);
+ }
+
+ #endregion
+}
diff --git a/tests/RequestFlow.Cqrs.Tests.Unit/CqrsValueDispatcherTests.cs b/tests/RequestFlow.Cqrs.Tests.Unit/CqrsValueCommandDispatcherTests.cs
similarity index 64%
rename from tests/RequestFlow.Cqrs.Tests.Unit/CqrsValueDispatcherTests.cs
rename to tests/RequestFlow.Cqrs.Tests.Unit/CqrsValueCommandDispatcherTests.cs
index 0f6cebc..44396ed 100644
--- a/tests/RequestFlow.Cqrs.Tests.Unit/CqrsValueDispatcherTests.cs
+++ b/tests/RequestFlow.Cqrs.Tests.Unit/CqrsValueCommandDispatcherTests.cs
@@ -1,6 +1,6 @@
namespace RequestFlow.Cqrs.Tests.Unit;
-public sealed class CqrsValueDispatcherTests
+public sealed class CqrsValueCommandDispatcherTests
{
[Fact]
public async Task Given_A_Value_Command_When_Sending_Then_Forwards_It_And_The_Token()
@@ -38,24 +38,6 @@ public async Task Given_A_Void_Value_Command_When_Sending_Then_Forwards_It_And_T
await _core.Received(1).SendAsync((IValueRequest)command, source.Token);
}
- [Fact]
- public async Task Given_A_Value_Query_When_Sending_Then_Forwards_It_And_The_Token()
- {
- using var source = new CancellationTokenSource();
- var completion = new TaskCompletionSource();
- var query = new FindName("42");
- var expected = new ValueTask(completion.Task);
- _core.SendAsync(query, source.Token)
- .Returns(expected);
-
- ValueTask result = _sut.SendAsync(query, source.Token);
-
- result.ShouldBe(expected);
- completion.SetResult("name");
- (await result).ShouldBe("name");
- await _core.Received(1).SendAsync(query, source.Token);
- }
-
[Fact]
public void Given_A_Null_Value_Command_When_Sending_Then_Throws_Argument_Null_Exception()
{
@@ -74,29 +56,20 @@ public void Given_A_Null_Void_Value_Command_When_Sending_Then_Throws_Argument_Nu
exception.ParamName.ShouldBe("command");
}
- [Fact]
- public void Given_A_Null_Value_Query_When_Sending_Then_Throws_Argument_Null_Exception()
- {
- var exception = Should.Throw(
- () => { _sut.SendAsync((IValueQuery)null!); });
-
- exception.ParamName.ShouldBe("query");
- }
-
[Fact]
public void Given_A_Null_Core_Dispatcher_When_Creating_Then_Throws_Argument_Null_Exception()
{
- Should.Throw(() => new CqrsValueDispatcher(null!));
+ Should.Throw(() => new CqrsValueCommandDispatcher(null!));
}
#region Initialization
private readonly IValueRequestDispatcher _core =
Substitute.For();
- private readonly CqrsValueDispatcher _sut;
+ private readonly CqrsValueCommandDispatcher _sut;
- public CqrsValueDispatcherTests()
- => _sut = new CqrsValueDispatcher(_core);
+ public CqrsValueCommandDispatcherTests()
+ => _sut = new CqrsValueCommandDispatcher(_core);
#endregion
@@ -106,8 +79,6 @@ public sealed record Rename(string Name) : IValueCommand;
public sealed record Purge : IValueCommand;
- public sealed record FindName(string Id) : IValueQuery;
-
public sealed class RenameHandler : IValueCommandHandler
{
public ValueTask HandleAsync(
@@ -122,13 +93,5 @@ public ValueTask HandleAsync(Purge request, CancellationToken cancellationToken)
=> default;
}
- public sealed class FindNameHandler : IValueQueryHandler
- {
- public ValueTask HandleAsync(
- FindName request,
- CancellationToken cancellationToken)
- => new(request.Id);
- }
-
#endregion
}
diff --git a/tests/RequestFlow.Cqrs.Tests.Unit/CqrsValueQueryDispatcherTests.cs b/tests/RequestFlow.Cqrs.Tests.Unit/CqrsValueQueryDispatcherTests.cs
new file mode 100644
index 0000000..e3fccdd
--- /dev/null
+++ b/tests/RequestFlow.Cqrs.Tests.Unit/CqrsValueQueryDispatcherTests.cs
@@ -0,0 +1,62 @@
+namespace RequestFlow.Cqrs.Tests.Unit;
+
+public sealed class CqrsValueQueryDispatcherTests
+{
+ [Fact]
+ public async Task Given_A_Value_Query_When_Sending_Then_Forwards_It_And_The_Token()
+ {
+ using var source = new CancellationTokenSource();
+ var completion = new TaskCompletionSource();
+ var query = new FindName("42");
+ var expected = new ValueTask(completion.Task);
+ _core.SendAsync(query, source.Token)
+ .Returns(expected);
+
+ ValueTask result = _sut.SendAsync(query, source.Token);
+
+ result.ShouldBe(expected);
+ completion.SetResult("name");
+ (await result).ShouldBe("name");
+ await _core.Received(1).SendAsync(query, source.Token);
+ }
+
+ [Fact]
+ public void Given_A_Null_Value_Query_When_Sending_Then_Throws_Argument_Null_Exception()
+ {
+ var exception = Should.Throw(
+ () => { _sut.SendAsync((IValueQuery)null!); });
+
+ exception.ParamName.ShouldBe("query");
+ }
+
+ [Fact]
+ public void Given_A_Null_Core_Dispatcher_When_Creating_Then_Throws_Argument_Null_Exception()
+ {
+ Should.Throw(() => new CqrsValueQueryDispatcher(null!));
+ }
+
+ #region Initialization
+
+ private readonly IValueRequestDispatcher _core =
+ Substitute.For();
+ private readonly CqrsValueQueryDispatcher _sut;
+
+ public CqrsValueQueryDispatcherTests()
+ => _sut = new CqrsValueQueryDispatcher(_core);
+
+ #endregion
+
+ #region Helpers
+
+ public sealed record FindName(string Id) : IValueQuery;
+
+ public sealed class FindNameHandler : IValueQueryHandler
+ {
+ public ValueTask HandleAsync(
+ FindName request,
+ CancellationToken cancellationToken)
+ => new(request.Id);
+ }
+
+ #endregion
+}
diff --git a/tests/RequestFlow.Tests.Unit/Events/HandlerlessParallelEventTests.cs b/tests/RequestFlow.Tests.Unit/Events/HandlerlessParallelEventTests.cs
new file mode 100644
index 0000000..842085a
--- /dev/null
+++ b/tests/RequestFlow.Tests.Unit/Events/HandlerlessParallelEventTests.cs
@@ -0,0 +1,99 @@
+using Microsoft.Extensions.DependencyInjection;
+
+namespace RequestFlow.Tests.Unit.Events;
+
+public sealed class HandlerlessParallelEventTests
+{
+#if NET8_0_OR_GREATER
+#if DEBUG
+ [Fact(Skip = "Debug allocates the async state machine. Run this allocation test in Release.")]
+#else
+ [Fact]
+#endif
+ public async Task Given_A_Parallel_Event_With_No_Handler_When_Publishing_Then_No_Dispatch_Allocation_Is_Added()
+ {
+ using var context = new Context();
+ for (int i = 0; i < 64; i++) await context.Publish();
+
+ long before = GC.GetAllocatedBytesForCurrentThread();
+ for (int i = 0; i < 64; i++) await context.Publish();
+ long allocated = GC.GetAllocatedBytesForCurrentThread() - before;
+
+ allocated.ShouldBe(0);
+ }
+#endif
+
+ [Theory]
+ [InlineData(false)]
+ [InlineData(true)]
+ public async Task Given_A_Parallel_Event_With_No_Handler_When_Publishing_Then_Publication_Succeeds(bool typedSelection)
+ {
+ using var context = new Context(typedSelection);
+
+ Task result = context.Publish();
+ await result;
+
+ result.Status.ShouldBe(TaskStatus.RanToCompletion);
+ }
+
+ [Theory]
+ [InlineData(false)]
+ [InlineData(true)]
+ public async Task Given_A_Pre_Canceled_Publish_When_The_Parallel_Event_Has_No_Handler_Then_Publication_Is_Canceled(bool typedSelection)
+ {
+ using var context = new Context(typedSelection);
+ using var cancellation = new CancellationTokenSource();
+ cancellation.Cancel();
+
+ Task result = context.Publish(cancellation.Token);
+ Exception? caught = null;
+ try { await result; }
+ catch (Exception observed) { caught = observed; }
+
+ result.IsCanceled.ShouldBeTrue();
+ var exception = caught.ShouldBeOfType();
+ exception.CancellationToken.ShouldBe(cancellation.Token);
+ exception.Failures.ShouldBeEmpty();
+ exception.SkippedHandlerCount.ShouldBe(0);
+ }
+
+ #region Helpers
+
+ private sealed class Context : IDisposable
+ {
+ private readonly ServiceProvider _provider;
+ private readonly IServiceScope _scope;
+ private readonly IEventPublisher _publisher;
+ private readonly Notice _notice = new();
+
+ public Context(bool typedSelection = false)
+ {
+ var services = new ServiceCollection();
+ services.AddRequestFlow(options =>
+ {
+ options.AddEvent().AllowUnhandledEvent();
+ if (typedSelection) options.PublishEventsInParallel();
+ else options.PublishEventsInParallel();
+ });
+ _provider = services.BuildServiceProvider(new ServiceProviderOptions
+ {
+ ValidateOnBuild = true,
+ ValidateScopes = true,
+ });
+ _scope = _provider.CreateScope();
+ _publisher = _scope.ServiceProvider.GetRequiredService();
+ }
+
+ public Task Publish(CancellationToken token = default) => _publisher.PublishAsync(_notice, token);
+
+ public void Dispose()
+ {
+ _scope.Dispose();
+ _provider.Dispose();
+ }
+ }
+
+ public sealed class Notice : IEvent;
+
+ #endregion
+}
diff --git a/tests/RequestFlow.Tests.Unit/Validation/Rules/EventStrategyRuleTests.cs b/tests/RequestFlow.Tests.Unit/Validation/Rules/EventStrategyRuleTests.cs
index b9af810..bcaa648 100644
--- a/tests/RequestFlow.Tests.Unit/Validation/Rules/EventStrategyRuleTests.cs
+++ b/tests/RequestFlow.Tests.Unit/Validation/Rules/EventStrategyRuleTests.cs
@@ -26,6 +26,20 @@ public void Given_An_Abstract_Strategy_When_Validating_Then_Reports_RF0014()
problem.Subject.ShouldBe(typeof(AbstractStrategy));
}
+ [Fact]
+ public void Given_An_Interface_Strategy_Declared_For_Two_Targets_When_Validating_Then_Reports_RF0013_Once()
+ {
+ RequestFlowValidationProblem problem = Validate(
+ [typeof(TestEvent), typeof(MultiInterfaceEvent)],
+ [
+ Strategy(typeof(TestEvent), typeof(IInvalidStrategy)),
+ Strategy(typeof(MultiInterfaceEvent), typeof(IInvalidStrategy)),
+ ]).ShouldHaveSingleItem();
+
+ problem.Code.ShouldBe("RF0013");
+ problem.Subject.ShouldBe(typeof(IInvalidStrategy));
+ }
+
[Fact]
public void Given_Two_Different_Global_Strategies_When_Validating_Then_Reports_RF0119()
{