diff --git a/samples/Sample9/Sample9.sln b/samples/Sample9/Sample9.sln index f1b5854a..5e99e3a9 100644 --- a/samples/Sample9/Sample9.sln +++ b/samples/Sample9/Sample9.sln @@ -1,7 +1,7 @@  Microsoft Visual Studio Solution File, Format Version 12.00 -# Visual Studio Version 16 -VisualStudioVersion = 16.0.30804.86 +# Visual Studio Version 17 +VisualStudioVersion = 17.2.32802.462 MinimumVisualStudioVersion = 10.0.40219.1 Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "OpenSleigh.Samples.Sample9.API", "OpenSleigh.Samples.Sample9.API\OpenSleigh.Samples.Sample9.API.csproj", "{A94C718C-3B49-4E67-BACB-4B6615C97839}" EndProject @@ -17,6 +17,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "OpenSleigh.Samples.Sample9. EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "OpenSleigh.Transport.Kafka", "..\..\src\OpenSleigh.Transport.Kafka\OpenSleigh.Transport.Kafka.csproj", "{36F37F51-015C-4B51-8EEE-0D5C93D5314A}" EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "OpenSleigh.Abstractions", "..\..\src\OpenSleigh.Abstractions\OpenSleigh.Abstractions.csproj", "{3C08B877-5589-465E-8C51-D338AB0CC4C9}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -47,6 +49,10 @@ Global {36F37F51-015C-4B51-8EEE-0D5C93D5314A}.Debug|Any CPU.Build.0 = Debug|Any CPU {36F37F51-015C-4B51-8EEE-0D5C93D5314A}.Release|Any CPU.ActiveCfg = Release|Any CPU {36F37F51-015C-4B51-8EEE-0D5C93D5314A}.Release|Any CPU.Build.0 = Release|Any CPU + {3C08B877-5589-465E-8C51-D338AB0CC4C9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {3C08B877-5589-465E-8C51-D338AB0CC4C9}.Debug|Any CPU.Build.0 = Debug|Any CPU + {3C08B877-5589-465E-8C51-D338AB0CC4C9}.Release|Any CPU.ActiveCfg = Release|Any CPU + {3C08B877-5589-465E-8C51-D338AB0CC4C9}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -55,6 +61,7 @@ Global {CE375B34-1590-4544-9B33-70EF4C25D54C} = {179EFC5F-B0C8-4649-BF7C-24E9EBDFBBA8} {02F45547-81D2-4F7A-8571-CEA585FCE8A7} = {179EFC5F-B0C8-4649-BF7C-24E9EBDFBBA8} {36F37F51-015C-4B51-8EEE-0D5C93D5314A} = {179EFC5F-B0C8-4649-BF7C-24E9EBDFBBA8} + {3C08B877-5589-465E-8C51-D338AB0CC4C9} = {179EFC5F-B0C8-4649-BF7C-24E9EBDFBBA8} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {BC78DE5A-D1CC-46D6-ADDA-3A8F41324E03} diff --git a/src/OpenSleigh.Abstractions/Saga.cs b/src/OpenSleigh.Abstractions/Saga.cs index 38b1906a..560375b3 100644 --- a/src/OpenSleigh.Abstractions/Saga.cs +++ b/src/OpenSleigh.Abstractions/Saga.cs @@ -2,6 +2,7 @@ using OpenSleigh.Core.Persistence; using System; using System.Collections.Generic; +using System.Linq; using System.Threading; using System.Threading.Tasks; @@ -31,7 +32,10 @@ internal async Task PersistOutboxAsync(IOutboxRepository outboxRepository, Cance { if (outboxRepository is null) throw new ArgumentNullException(nameof(outboxRepository)); - + + if (!_outbox.Any()) + return; + await outboxRepository.AppendAsync(_outbox, cancellationToken) .ConfigureAwait(false); diff --git a/tests/OpenSleigh.Core.Tests/Unit/SagaTests.cs b/tests/OpenSleigh.Core.Tests/Unit/SagaTests.cs index 70d6215d..58f5ebfe 100644 --- a/tests/OpenSleigh.Core.Tests/Unit/SagaTests.cs +++ b/tests/OpenSleigh.Core.Tests/Unit/SagaTests.cs @@ -89,8 +89,21 @@ public async Task PersistOutboxAsync_should_empty_outbox() await sut.PersistOutboxAsync(outboxRepo, CancellationToken.None); called.Should().BeTrue(); + called = false; await sut.PersistOutboxAsync(outboxRepo, CancellationToken.None); called.Should().BeFalse(); } + + [Fact] + public async Task PersistOutboxAsync_should_not_call_outbox_when_no_messages_to_send() + { + var state = new DummySagaState(Guid.NewGuid()); + var sut = new DummySaga(state); + + var outboxRepo = NSubstitute.Substitute.For(); + + await sut.PersistOutboxAsync(outboxRepo, CancellationToken.None); + await outboxRepo.DidNotReceiveWithAnyArgs().AppendAsync(Arg.Any>(), Arg.Any()); + } } }