GFramework/GFramework.Core.Tests/Command/RecordingCqrsRuntime.cs
gewuyou dc3bd3744e fix(core): 收口 legacy bridge 同步评审问题
- 修复 legacy 同步 bridge 的 runtime 等待方式,统一通过共享 helper 隔离同步上下文并收口重复 dispatch-context 解析逻辑

- 补充 legacy async command bridge 的取消可见性,并更新 ICqrsRuntime 与相关入口的契约说明

- 新增 bridge 回归测试并更新 cqrs-rewrite active tracking,覆盖同步上下文隔离、测试容器释放与取消语义
2026-05-07 19:00:49 +08:00

67 lines
2.1 KiB
C#

// Copyright (c) 2025-2026 GeWuYou
// SPDX-License-Identifier: Apache-2.0
using GFramework.Cqrs.Abstractions.Cqrs;
namespace GFramework.Core.Tests.Command;
/// <summary>
/// 记录 bridge 执行线程与收到请求的最小 CQRS runtime 测试替身。
/// </summary>
internal sealed class RecordingCqrsRuntime(Func<object?, object?>? responseFactory = null) : ICqrsRuntime
{
private static readonly Func<object?, object?> DefaultResponseFactory = _ => null;
private readonly Func<object?, object?> _responseFactory = responseFactory ?? DefaultResponseFactory;
/// <summary>
/// 获取最近一次 <see cref="SendAsync{TResponse}" /> 观察到的同步上下文类型。
/// </summary>
public Type? ObservedSynchronizationContextType { get; private set; }
/// <summary>
/// 获取最近一次收到的请求实例。
/// </summary>
public object? LastRequest { get; private set; }
/// <inheritdoc />
public ValueTask<TResponse> SendAsync<TResponse>(
ICqrsContext context,
IRequest<TResponse> request,
CancellationToken cancellationToken = default)
{
ArgumentNullException.ThrowIfNull(context);
ArgumentNullException.ThrowIfNull(request);
ObservedSynchronizationContextType = SynchronizationContext.Current?.GetType();
LastRequest = request;
object? response = request switch
{
IRequest<Unit> => Unit.Value,
_ => _responseFactory(request)
};
return ValueTask.FromResult((TResponse)response!);
}
/// <inheritdoc />
public ValueTask PublishAsync<TNotification>(
ICqrsContext context,
TNotification notification,
CancellationToken cancellationToken = default)
where TNotification : INotification
{
throw new NotSupportedException();
}
/// <inheritdoc />
public IAsyncEnumerable<TResponse> CreateStream<TResponse>(
ICqrsContext context,
IStreamRequest<TResponse> request,
CancellationToken cancellationToken = default)
{
throw new NotSupportedException();
}
}