From 4cc7060fcf423b6b74c1c4c574f73f8228b649e1 Mon Sep 17 00:00:00 2001 From: gewuyou <95328647+GeWuYou@users.noreply.github.com> Date: Tue, 12 May 2026 08:46:48 +0800 Subject: [PATCH] =?UTF-8?q?test(cqrs):=20=E8=A1=A5=E5=85=85=E9=80=9A?= =?UTF-8?q?=E7=9F=A5=E5=9B=9E=E9=80=80=E5=9B=9E=E5=BD=92=E8=A6=86=E7=9B=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 新增默认 notification publisher fallback 与缓存行为的回归测试 - 验证容器无 publisher 时继续使用顺序发布语义且不重复查容器 --- .../Cqrs/CqrsNotificationPublisherTests.cs | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/GFramework.Cqrs.Tests/Cqrs/CqrsNotificationPublisherTests.cs b/GFramework.Cqrs.Tests/Cqrs/CqrsNotificationPublisherTests.cs index 485c11e7..21a77a40 100644 --- a/GFramework.Cqrs.Tests/Cqrs/CqrsNotificationPublisherTests.cs +++ b/GFramework.Cqrs.Tests/Cqrs/CqrsNotificationPublisherTests.cs @@ -155,6 +155,46 @@ internal sealed class CqrsNotificationPublisherTests Assert.That(secondPublisher.PublishCallCount, Is.Zero); } + /// + /// 验证当容器里没有任何通知发布器时,dispatcher 会回退到内置顺序发布器, + /// 并在首次解析后缓存该 fallback 结果而不是在后续发布时重新查询容器。 + /// + [Test] + public async Task PublishAsync_Should_Fallback_To_SequentialNotificationPublisher_And_Cache_It_When_None_Is_Registered() + { + var invocationOrder = new List(); + var notificationPublisherLookupCount = 0; + var runtime = CreateRuntime( + container => + { + container + .Setup(currentContainer => currentContainer.GetAll(typeof(INotificationHandler))) + .Returns( + [ + new RecordingNotificationHandler("first", invocationOrder), + new RecordingNotificationHandler("second", invocationOrder) + ]); + container + .Setup(currentContainer => currentContainer.GetAll(typeof(INotificationPublisher))) + .Returns(() => + { + notificationPublisherLookupCount++; + return notificationPublisherLookupCount switch + { + 1 => Array.Empty(), + 2 => [new ReverseOrderNotificationPublisher()], + _ => throw new AssertionException("Notification publisher should be resolved at most once.") + }; + }); + }); + + await runtime.PublishAsync(new FakeCqrsContext(), new PublisherNotification()).ConfigureAwait(false); + await runtime.PublishAsync(new FakeCqrsContext(), new PublisherNotification()).ConfigureAwait(false); + + Assert.That(notificationPublisherLookupCount, Is.EqualTo(1)); + Assert.That(invocationOrder, Is.EqualTo(["first", "second", "first", "second"])); + } + /// /// 验证内置 `TaskWhenAll` 发布器会继续调度所有处理器,而不是沿用默认顺序发布器的失败即停语义。 ///