-
Notifications
You must be signed in to change notification settings - Fork 3
feat(cqrs): 补齐流式管道行为接缝 #339
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
GFramework.Core.Tests/Architectures/ModuleStreamBehaviorRequest.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| // Copyright (c) 2025-2026 GeWuYou | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| using GFramework.Cqrs.Abstractions.Cqrs; | ||
|
|
||
| namespace GFramework.Core.Tests.Architectures; | ||
|
|
||
| /// <summary> | ||
| /// 用于验证架构公开 stream pipeline 行为注册入口的最小流式请求。 | ||
| /// </summary> | ||
| public sealed class ModuleStreamBehaviorRequest : IStreamRequest<int> | ||
| { | ||
| } |
28 changes: 28 additions & 0 deletions
28
GFramework.Core.Tests/Architectures/ModuleStreamBehaviorRequestHandler.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| // Copyright (c) 2025-2026 GeWuYou | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| using System.Collections.Generic; | ||
| using System.Runtime.CompilerServices; | ||
| using GFramework.Cqrs.Abstractions.Cqrs; | ||
|
|
||
| namespace GFramework.Core.Tests.Architectures; | ||
|
|
||
| /// <summary> | ||
| /// 处理 <see cref="ModuleStreamBehaviorRequest" /> 并返回一个固定元素。 | ||
| /// </summary> | ||
| public sealed class ModuleStreamBehaviorRequestHandler : IStreamRequestHandler<ModuleStreamBehaviorRequest, int> | ||
| { | ||
| /// <summary> | ||
| /// 返回一个固定元素,供架构 stream pipeline 行为回归断言使用。 | ||
| /// </summary> | ||
| /// <param name="request">当前流式请求。</param> | ||
| /// <param name="cancellationToken">取消令牌。</param> | ||
| /// <returns>包含一个固定元素的异步流。</returns> | ||
| public async IAsyncEnumerable<int> Handle( | ||
| ModuleStreamBehaviorRequest request, | ||
| [EnumeratorCancellation] CancellationToken cancellationToken) | ||
| { | ||
| yield return 7; | ||
| await ValueTask.CompletedTask.ConfigureAwait(false); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
GFramework.Core.Tests/Architectures/TrackingStreamPipelineBehavior.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| // Copyright (c) 2025-2026 GeWuYou | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| using System.Threading; | ||
| using GFramework.Cqrs.Abstractions.Cqrs; | ||
|
|
||
| namespace GFramework.Core.Tests.Architectures; | ||
|
|
||
| /// <summary> | ||
| /// 记录流式请求通过管道次数的测试行为。 | ||
| /// </summary> | ||
| /// <typeparam name="TRequest">流式请求类型。</typeparam> | ||
| /// <typeparam name="TResponse">流式响应元素类型。</typeparam> | ||
| public sealed class TrackingStreamPipelineBehavior<TRequest, TResponse> : IStreamPipelineBehavior<TRequest, TResponse> | ||
| where TRequest : IStreamRequest<TResponse> | ||
| { | ||
| private static int _invocationCount; | ||
|
|
||
| /// <summary> | ||
| /// 获取当前测试进程中该流式请求类型对应的行为触发次数。 | ||
| /// 该计数器是按泛型闭包共享的静态状态,测试需要在每次运行前显式重置。 | ||
| /// </summary> | ||
| public static int InvocationCount | ||
| { | ||
| get => Volatile.Read(ref _invocationCount); | ||
| set => Volatile.Write(ref _invocationCount, value); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// 以线程安全方式记录一次行为执行,然后继续执行下一个处理阶段。 | ||
| /// </summary> | ||
| /// <param name="message">当前流式请求消息。</param> | ||
| /// <param name="next">下一个处理阶段。</param> | ||
| /// <param name="cancellationToken">取消令牌。</param> | ||
| /// <returns>下游处理阶段返回的异步流。</returns> | ||
| public IAsyncEnumerable<TResponse> Handle( | ||
| TRequest message, | ||
| StreamMessageHandlerDelegate<TRequest, TResponse> next, | ||
| CancellationToken cancellationToken) | ||
| { | ||
| Interlocked.Increment(ref _invocationCount); | ||
| return next(message, cancellationToken); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
GFramework.Cqrs.Abstractions/Cqrs/IStreamPipelineBehavior.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| // Copyright (c) 2025-2026 GeWuYou | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| namespace GFramework.Cqrs.Abstractions.Cqrs; | ||
|
|
||
| /// <summary> | ||
| /// 定义流式 CQRS 请求在建流阶段使用的管道行为。 | ||
| /// </summary> | ||
| /// <typeparam name="TRequest">流式请求类型。</typeparam> | ||
| /// <typeparam name="TResponse">流式响应元素类型。</typeparam> | ||
| public interface IStreamPipelineBehavior<TRequest, TResponse> | ||
| where TRequest : IStreamRequest<TResponse> | ||
| { | ||
| /// <summary> | ||
| /// 处理当前流式请求,并决定是否继续调用后续行为或最终处理器。 | ||
| /// </summary> | ||
| /// <param name="message">当前流式请求消息。</param> | ||
| /// <param name="next">下一个处理委托。</param> | ||
| /// <param name="cancellationToken">取消令牌。</param> | ||
| /// <returns>异步响应序列。</returns> | ||
| IAsyncEnumerable<TResponse> Handle( | ||
| TRequest message, | ||
| StreamMessageHandlerDelegate<TRequest, TResponse> next, | ||
| CancellationToken cancellationToken); | ||
| } |
22 changes: 22 additions & 0 deletions
22
GFramework.Cqrs.Abstractions/Cqrs/StreamMessageHandlerDelegate.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| // Copyright (c) 2025-2026 GeWuYou | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| namespace GFramework.Cqrs.Abstractions.Cqrs; | ||
|
|
||
| /// <summary> | ||
| /// 表示流式 CQRS 请求在管道中继续向下执行的处理委托。 | ||
| /// </summary> | ||
| /// <remarks> | ||
| /// <para>stream 行为可以通过不调用该委托来短路整个流式处理链。</para> | ||
| /// <para>除显式实现重试、回放或分支等高级语义外,行为通常应最多调用一次该委托,以维持单次建流的确定性。</para> | ||
| /// <para>调用方应传递当前收到的 <paramref name="cancellationToken" />,确保取消信号沿建流入口与后续枚举链路一致传播。</para> | ||
| /// </remarks> | ||
| /// <typeparam name="TRequest">流式请求类型。</typeparam> | ||
| /// <typeparam name="TResponse">流式响应元素类型。</typeparam> | ||
| /// <param name="message">当前流式请求消息。</param> | ||
| /// <param name="cancellationToken">取消令牌。</param> | ||
| /// <returns>异步响应序列。</returns> | ||
| public delegate IAsyncEnumerable<TResponse> StreamMessageHandlerDelegate<in TRequest, out TResponse>( | ||
| TRequest message, | ||
| CancellationToken cancellationToken) | ||
| where TRequest : IStreamRequest<TResponse>; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.