Skip to content
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

Fix MarkAsComplete on new saga instance #384

Merged
merged 2 commits into from
Apr 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions src/NServiceBus.Testing.Tests/Sagas/SagaCompletion.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
namespace NServiceBus.Testing.Tests.Sagas
{
using System;
using System.Threading.Tasks;
using NUnit.Framework;

[TestFixture]
public class SagaCompletion
{
[Test]
public async Task MarkStartedSagaAsComplete()
{
var saga = new TestableSaga<TestSaga, TestSagaData>();

var result = await saga.Handle(new StartSagaMessage() { CorrelationId = Guid.NewGuid() });

Assert.IsTrue(result.Completed);
}

class TestSaga : NServiceBus.Saga<TestSagaData>, IAmStartedByMessages<StartSagaMessage>
{
protected override void ConfigureHowToFindSaga(SagaPropertyMapper<TestSagaData> mapper) => mapper
.MapSaga(d => d.CorrelationId)
.ToMessage<StartSagaMessage>(m => m.CorrelationId);

public Task Handle(StartSagaMessage message, IMessageHandlerContext context)
{
MarkAsComplete();
return Task.CompletedTask;
}
}

class TestSagaData : ContainSagaData
{
public Guid CorrelationId { get; set; }
}

class StartSagaMessage : IMessage
{
public Guid CorrelationId { get; set; }
}
}
}
5 changes: 5 additions & 0 deletions src/NServiceBus.Testing/Sagas/TestableSaga.cs
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,11 @@ async Task SaveSagaData(
{
if (saga.Completed)
{
if (isNew)
{
// Can't update a brand new but already-completed saga
return;
}
await persister.Complete(saga.Entity, session, contextBag, cancellationToken).ConfigureAwait(false);
}
else if (isNew)
Expand Down