public async Task Save_fails_when_data_changes_between_read_and_update() { var sagaId = Guid.NewGuid(); var saga = new TestSagaData { Id = sagaId, SomeId = sagaId.ToString() }; var persister = new NonDurableSagaPersister(); var insertSession = new NonDurableSynchronizedStorageSession(); await persister.Save(saga, SagaMetadataHelper.GetMetadata <TestSaga>(saga), insertSession, new ContextBag()); await insertSession.CompleteAsync(); var winningContext = new ContextBag(); var losingContext = new ContextBag(); var returnedSaga1 = await Task.Run(() => persister.Get <TestSagaData>(saga.Id, new NonDurableSynchronizedStorageSession(), winningContext)); var returnedSaga2 = await persister.Get <TestSagaData>("SomeId", sagaId.ToString(), new NonDurableSynchronizedStorageSession(), losingContext); var winningSaveSession = new NonDurableSynchronizedStorageSession(); var losingSaveSession = new NonDurableSynchronizedStorageSession(); await persister.Update(returnedSaga1, winningSaveSession, winningContext); await persister.Update(returnedSaga2, losingSaveSession, losingContext); await winningSaveSession.CompleteAsync(); Assert.That(async() => await losingSaveSession.CompleteAsync(), Throws.InstanceOf <Exception>().And.Message .StartsWith( $"NonDurableSagaPersister concurrency violation: saga entity Id[{saga.Id}] already saved.")); }
public async Task Persister_returns_different_instance_of_saga_data() { var saga = new TestSagaData { Id = Guid.NewGuid() }; var persister = new NonDurableSagaPersister(); var insertSession = new NonDurableSynchronizedStorageSession(); await persister.Save(saga, SagaMetadataHelper.GetMetadata <TestSaga>(saga), insertSession, new ContextBag()); await insertSession.CompleteAsync(); var returnedSaga1 = await persister.Get <TestSagaData>(saga.Id, new NonDurableSynchronizedStorageSession(), new ContextBag()); var returnedSaga2 = await persister.Get <TestSagaData>("Id", saga.Id, new NonDurableSynchronizedStorageSession(), new ContextBag()); Assert.AreNotSame(returnedSaga2, returnedSaga1); Assert.AreNotSame(returnedSaga1, saga); Assert.AreNotSame(returnedSaga2, saga); }