Skip to content

Commit

Permalink
Merge branch 'hotfix-3.0.5'
Browse files Browse the repository at this point in the history
  • Loading branch information
SzymonPobiega committed Mar 5, 2018
2 parents 3f1e7e9 + 772947d commit 7ef7a57
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 2 deletions.
41 changes: 41 additions & 0 deletions src/SqlPersistence.Tests/Saga/SagaPersisterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,47 @@ public async Task Complete()
}
}

[Test]
public async Task CompleteFailsOnConcurrencyViolation()
{
var endpointName = nameof(Complete);
var definition = new SagaDefinition(
tableSuffix: "SagaWithCorrelation",
name: "SagaWithCorrelation",
correlationProperty: new CorrelationProperty
(
name: "CorrelationProperty",
type: CorrelationPropertyType.String
),
transitionalCorrelationProperty: new CorrelationProperty
(
name: "TransitionalCorrelationProperty",
type: CorrelationPropertyType.String
)
);
DropAndCreate(definition, endpointName, schema);
var id = Guid.NewGuid();
var sagaData = new SagaWithCorrelation.SagaData
{
Id = id,
OriginalMessageId = "theOriginalMessageId",
Originator = "theOriginator",
CorrelationProperty = "theCorrelationProperty"
};

var persister = SetUp(endpointName, schema);

using (var connection = dbConnection())
using (var transaction = connection.BeginTransaction())
using (var storageSession = new StorageSession(connection, transaction, true, null))
{
await persister.Save(sagaData, storageSession, "theProperty").ConfigureAwait(false);

const int invalidConcurrencyVersion = 42;
Assert.ThrowsAsync<Exception>(() => persister.Complete(sagaData, storageSession, invalidConcurrencyVersion));
}
}

public class SagaWithWeirdCharactersಠ_ಠ :
SqlSaga<SagaWithWeirdCharactersಠ_ಠ.SagaData>,
IAmStartedByMessages<AMessage>
Expand Down
9 changes: 7 additions & 2 deletions src/SqlPersistence/Saga/SagaPersister_Complete.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Threading.Tasks;
using System;
using System.Threading.Tasks;
using NServiceBus;
using NServiceBus.Extensibility;
using NServiceBus.Persistence;
Expand All @@ -21,7 +22,11 @@ internal async Task Complete(IContainSagaData sagaData, SynchronizedStorageSessi
command.Transaction = sqlSession.Transaction;
command.AddParameter("Id", sagaData.Id);
command.AddParameter("Concurrency", concurrency);
await command.ExecuteNonQueryAsync().ConfigureAwait(false);
var affected = await command.ExecuteNonQueryAsync().ConfigureAwait(false);
if (affected != 1)
{
throw new Exception($"Optimistic concurrency violation when trying to complete saga {sagaInfo.SagaType.FullName} {sagaData.Id}. Expected version {concurrency}.");
}
}
}
}

0 comments on commit 7ef7a57

Please sign in to comment.