public void Save_process_is_repeatable()
        {
            var inMemorySagaPersister = new InMemorySagaPersister();
            var saga = new TestSaga {
                Id = Guid.NewGuid()
            };

            inMemorySagaPersister.Save(saga);

            var returnedSaga1 = Task <TestSaga> .Factory.StartNew(() => inMemorySagaPersister.Get <TestSaga>(saga.Id)).Result;

            var returnedSaga2 = inMemorySagaPersister.Get <TestSaga>("Id", saga.Id);

            inMemorySagaPersister.Save(returnedSaga1);
            var exceptionFromSaga2 = Assert.Throws <Exception>(() => inMemorySagaPersister.Save(returnedSaga2));

            Assert.IsTrue(exceptionFromSaga2.Message.StartsWith(string.Format("InMemorySagaPersister concurrency violation: saga entity Id[{0}] already saved by [Worker.", saga.Id)));

            var returnedSaga3 = Task <TestSaga> .Factory.StartNew(() => inMemorySagaPersister.Get <TestSaga>("Id", saga.Id)).Result;

            var returnedSaga4 = inMemorySagaPersister.Get <TestSaga>(saga.Id);

            inMemorySagaPersister.Save(returnedSaga4);

            var exceptionFromSaga3 = Assert.Throws <Exception>(() => inMemorySagaPersister.Save(returnedSaga3));

            Assert.IsTrue(exceptionFromSaga3.Message.StartsWith(string.Format("InMemorySagaPersister concurrency violation: saga entity Id[{0}] already saved by [Worker.", saga.Id)));
        }
Beispiel #2
0
        public async Task Save_fails_when_writing_same_data_twice()
        {
            var saga = new TestSagaData
            {
                Id = Guid.NewGuid()
            };
            var persister     = new InMemorySagaPersister();
            var insertSession = new InMemorySynchronizedStorageSession();
            await persister.Save(saga, SagaMetadataHelper.GetMetadata <TestSaga>(saga), insertSession, new ContextBag());

            await insertSession.CompleteAsync();

            var returnedSaga1 = await persister.Get <TestSagaData>(saga.Id, new InMemorySynchronizedStorageSession(), new ContextBag());

            var winningSaveSession = new InMemorySynchronizedStorageSession();
            var losingSaveSession  = new InMemorySynchronizedStorageSession();

            await persister.Save(returnedSaga1, SagaMetadataHelper.GetMetadata <TestSaga>(saga), winningSaveSession, new ContextBag());

            await persister.Save(returnedSaga1, SagaMetadataHelper.GetMetadata <TestSaga>(saga), losingSaveSession, new ContextBag());

            await winningSaveSession.CompleteAsync();

            Assert.That(async() => await losingSaveSession.CompleteAsync(), Throws.InstanceOf <Exception>().And.Message.StartsWith($"InMemorySagaPersister concurrency violation: saga entity Id[{saga.Id}] already saved."));
        }
        public async Task It_should_enforce_uniqueness()
        {
            var saga1 = new SagaWithUniquePropertyData
            {
                Id           = Guid.NewGuid(),
                UniqueString = "whatever"
            };
            var saga2 = new SagaWithUniquePropertyData
            {
                Id           = Guid.NewGuid(),
                UniqueString = "whatever"
            };

            var persister      = new InMemorySagaPersister();
            var winningSession = new InMemorySynchronizedStorageSession();
            var losingSession  = new InMemorySynchronizedStorageSession();

            await persister.Save(saga1, SagaMetadataHelper.GetMetadata <SagaWithUniqueProperty>(saga1), winningSession, new ContextBag());

            await persister.Save(saga2, SagaMetadataHelper.GetMetadata <SagaWithUniqueProperty>(saga1), losingSession, new ContextBag());

            await winningSession.CompleteAsync();

            Assert.That(async() => await losingSession.CompleteAsync(), Throws.InstanceOf <InvalidOperationException>());
        }
Beispiel #4
0
        public async Task It_should_persist_successfully()
        {
            var saga1 = new SagaWithUniquePropertyData
            {
                Id           = Guid.NewGuid(),
                UniqueString = "whatever"
            };
            var saga2 = new SagaWithUniquePropertyData
            {
                Id           = Guid.NewGuid(),
                UniqueString = "whatever"
            };

            var persister = new InMemorySagaPersister();

            var firstInsertSession = new InMemorySynchronizedStorageSession();
            await persister.Save(saga1, SagaMetadataHelper.GetMetadata <SagaWithUniqueProperty>(saga1), firstInsertSession, new ContextBag());

            await firstInsertSession.CompleteAsync();

            var updateSession = new InMemorySynchronizedStorageSession();

            saga1 = await persister.Get <SagaWithUniquePropertyData>(saga1.Id, updateSession, new ContextBag());

            saga1.UniqueString = "whatever2";
            await persister.Update(saga1, updateSession, new ContextBag());

            await updateSession.CompleteAsync();

            var secondInsertSession = new InMemorySynchronizedStorageSession();
            await persister.Save(saga2, SagaMetadataHelper.GetMetadata <SagaWithUniqueProperty>(saga2), secondInsertSession, new ContextBag());

            await secondInsertSession.CompleteAsync();
        }
        public void It_should_enforce_uniqueness()
        {
            var saga1 = new SagaWithUniqueProperty { Id = Guid.NewGuid(), UniqueString = "whatever"};
            var saga2 = new SagaWithUniqueProperty { Id = Guid.NewGuid(), UniqueString = "whatever"};
            var inMemorySagaPersister = new InMemorySagaPersister() as ISagaPersister;

            inMemorySagaPersister.Save(saga1);
            Assert.Throws<InvalidOperationException>(() => inMemorySagaPersister.Save(saga2));
        }
        public void It_should_enforce_uniqueness_even_for_two_unique_properties()
        {
            var saga1 = new SagaWithTwoUniqueProperties() { Id = Guid.NewGuid(), UniqueString = "whatever", UniqueInt = 5};
            var saga2 = new SagaWithTwoUniqueProperties { Id = Guid.NewGuid(), UniqueString = "whatever1", UniqueInt = 3};
            var saga3 = new SagaWithTwoUniqueProperties { Id = Guid.NewGuid(), UniqueString = "whatever3", UniqueInt = 3 };
            var inMemorySagaPersister = new InMemorySagaPersister() as ISagaPersister;

            inMemorySagaPersister.Save(saga1);
            inMemorySagaPersister.Save(saga2);
            Assert.Throws<InvalidOperationException>(() => inMemorySagaPersister.Save(saga3));
        }
        public void Save_fails_when_data_changes_between_read_and_update()
        {
            var inMemorySagaPersister = new InMemorySagaPersister();
            var saga = new TestSaga { Id = Guid.NewGuid() };
            inMemorySagaPersister.Save(saga);

            var returnedSaga1 = Task<TestSaga>.Factory.StartNew(() => inMemorySagaPersister.Get<TestSaga>(saga.Id)).Result;
            var returnedSaga2 = inMemorySagaPersister.Get<TestSaga>("Id", saga.Id);

            inMemorySagaPersister.Save(returnedSaga1);
            Assert.Throws<ConcurrencyException>(() => inMemorySagaPersister.Save(returnedSaga2));
        }
        public void It_should_persist_successfully()
        {
            var saga1 = new SagaWithTwoUniqueProperties { Id = Guid.NewGuid(), UniqueString = "whatever", UniqueInt = 5 };
            var saga2 = new AnotherSagaWithTwoUniqueProperties { Id = Guid.NewGuid(), UniqueString = "whatever", UniqueInt = 5 };
            var saga3 = new SagaWithUniqueProperty {Id = Guid.NewGuid(), UniqueString = "whatever"};

            var inMemorySagaPersister = new InMemorySagaPersister() as ISagaPersister;

            inMemorySagaPersister.Save(saga1);
            inMemorySagaPersister.Save(saga2);
            inMemorySagaPersister.Save(saga3);
        }
        public void It_should_enforce_uniqueness()
        {
            var saga1 = new SagaWithUniqueProperty {
                Id = Guid.NewGuid(), UniqueString = "whatever"
            };
            var saga2 = new SagaWithUniqueProperty {
                Id = Guid.NewGuid(), UniqueString = "whatever"
            };
            var inMemorySagaPersister = new InMemorySagaPersister() as ISagaPersister;

            inMemorySagaPersister.Save(saga1);
            Assert.Throws <InvalidOperationException>(() => inMemorySagaPersister.Save(saga2));
        }
        public void Save_fails_when_data_changes_between_read_and_update()
        {
            var inMemorySagaPersister = new InMemorySagaPersister();
            var saga = new TestSaga { Id = Guid.NewGuid() };
            inMemorySagaPersister.Save(saga);

            var returnedSaga1 = Task<TestSaga>.Factory.StartNew(() => inMemorySagaPersister.Get<TestSaga>(saga.Id)).Result;
            var returnedSaga2 = inMemorySagaPersister.Get<TestSaga>("Id", saga.Id);

            inMemorySagaPersister.Save(returnedSaga1);
            var exception = Assert.Throws<Exception>(() => inMemorySagaPersister.Save(returnedSaga2));
            Assert.IsTrue(exception.Message.StartsWith(string.Format("InMemorySagaPersister concurrency violation: saga entity Id[{0}] already saved by [Worker.", saga.Id)));
        }
        public void It_should_persist_successfully()
        {
            var saga1 = new SagaWithUniqueProperty{Id = Guid.NewGuid(), UniqueString = "whatever"};
            var saga2 = new SagaWithUniqueProperty{Id = Guid.NewGuid(), UniqueString = "whatever"};
            var inMemorySagaPersister = new InMemorySagaPersister();
            
            inMemorySagaPersister.Save(saga1);
            saga1 = inMemorySagaPersister.Get<SagaWithUniqueProperty>(saga1.Id);
            saga1.UniqueString = "whatever2";
            inMemorySagaPersister.Update(saga1);

            inMemorySagaPersister.Save(saga2);
        }
        public void It_should_persist_successfully()
        {
            var saga1 = new SagaWithUniqueProperty { Id = Guid.NewGuid(), UniqueString = "whatever" };
            var saga2 = new SagaWithUniqueProperty { Id = Guid.NewGuid(), UniqueString = "whatever" };

             var inMemorySagaPersister = new InMemorySagaPersister() as ISagaPersister;

            inMemorySagaPersister.Save(saga1);
            inMemorySagaPersister.Complete(saga1);
            inMemorySagaPersister.Save(saga2);
            inMemorySagaPersister.Complete(saga2);
            inMemorySagaPersister.Save(saga1);
            inMemorySagaPersister.Complete(saga1);
        }
Beispiel #13
0
        public async Task Save_fails_when_data_changes_between_read_and_update_on_same_thread()
        {
            var sagaId = Guid.NewGuid();
            var saga   = new TestSagaData
            {
                Id     = sagaId,
                SomeId = sagaId.ToString()
            };
            var persister     = new InMemorySagaPersister();
            var insertSession = new InMemorySynchronizedStorageSession();
            await persister.Save(saga, SagaMetadataHelper.GetMetadata <TestSaga>(saga), insertSession, new ContextBag());

            await insertSession.CompleteAsync();

            var winningContext = new ContextBag();
            var record         = await persister.Get <TestSagaData>(saga.Id, new InMemorySynchronizedStorageSession(), winningContext);

            var losingContext = new ContextBag();
            var staleRecord   = await persister.Get <TestSagaData>("SomeId", sagaId.ToString(), new InMemorySynchronizedStorageSession(), losingContext);

            var winningSaveSession = new InMemorySynchronizedStorageSession();
            var losingSaveSession  = new InMemorySynchronizedStorageSession();

            await persister.Update(record, winningSaveSession, winningContext);

            await persister.Update(staleRecord, losingSaveSession, losingContext);

            await winningSaveSession.CompleteAsync();

            Assert.That(async() => await losingSaveSession.CompleteAsync(), Throws.InstanceOf <Exception>().And.Message.StartsWith($"InMemorySagaPersister concurrency violation: saga entity Id[{saga.Id}] already saved."));
        }
        public void Save_fails_when_data_changes_between_read_and_update()
        {
            var inMemorySagaPersister = new InMemorySagaPersister();
            var saga = new TestSaga {
                Id = Guid.NewGuid()
            };

            inMemorySagaPersister.Save(saga);

            var returnedSaga1 = Task <TestSaga> .Factory.StartNew(() => inMemorySagaPersister.Get <TestSaga>(saga.Id)).Result;

            var returnedSaga2 = inMemorySagaPersister.Get <TestSaga>("Id", saga.Id);

            inMemorySagaPersister.Save(returnedSaga1);
            Assert.Throws <ConcurrencyException>(() => inMemorySagaPersister.Save(returnedSaga2));
        }
        public async Task It_should_get_deep_copy()
        {
            var sagaData = new SagaWithComplexType
            {
                Id   = Guid.NewGuid(),
                Ints = new List <int> {
                    1, 2
                }
            };

            var persister = new InMemorySagaPersister();

            using (var session = new InMemorySynchronizedStorageSession())
            {
                await persister.Save(sagaData, null, session, new ContextBag());

                await session.CompleteAsync();
            }

            using (var session = new InMemorySynchronizedStorageSession())
            {
                var retrieved = await persister.Get <SagaWithComplexType>(sagaData.Id, session, new ContextBag());

                CollectionAssert.AreEqual(sagaData.Ints, retrieved.Ints);
                Assert.False(ReferenceEquals(sagaData.Ints, retrieved.Ints));
            }
        }
        public async Task It_should_successfully_update_the_saga()
        {
            var id       = Guid.NewGuid();
            var sagaData = new SagaWithoutUniquePropertyData
            {
                Id = id,
                NonUniqueString = "whatever"
            };

            var persister = new InMemorySagaPersister();

            // first session
            var session = new InMemorySynchronizedStorageSession();

            await persister.Save(sagaData, SagaCorrelationProperty.None, session, new ContextBag());

            await session.CompleteAsync();

            // second session
            var session2 = new InMemorySynchronizedStorageSession();

            var ctx  = new ContextBag();
            var saga = await persister.Get <SagaWithoutUniquePropertyData>(id, session2, ctx);

            saga.NonUniqueString = "asdfasdf";

            await persister.Update(saga, session2, ctx);

            await session2.CompleteAsync();

            var result = await persister.Get <SagaWithoutUniquePropertyData>(sagaData.Id, new InMemorySynchronizedStorageSession(), new ContextBag());

            Assert.That(result, Is.Not.Null);
            Assert.That(result.NonUniqueString, Is.EqualTo("asdfasdf"));
        }
        public async Task It_should_successfully_remove_the_saga()
        {
            var id = Guid.NewGuid();
            var sagaData = new SagaWithoutUniquePropertyData
            {
                Id = id,
                NonUniqueString = "whatever"
            };

            var persister = new InMemorySagaPersister();
            var savingSession = new InMemorySynchronizedStorageSession();

            await persister.Save(sagaData, null, savingSession, new ContextBag());
            await savingSession.CompleteAsync();

            // second session
            var completingSession = new InMemorySynchronizedStorageSession();
            var completingContextBag = new ContextBag();

            var saga = await persister.Get<SagaWithoutUniquePropertyData>(id, completingSession, completingContextBag);
            await persister.Complete(saga, completingSession, completingContextBag);
            await completingSession.CompleteAsync();

            var result = await persister.Get<SagaWithoutUniquePropertyData>(sagaData.Id, savingSession, new ContextBag());

            Assert.That(result, Is.Null);
        }
        public void It_should_persist_successfully()
        {
            var saga1 = new SagaWithUniqueProperty {Id = Guid.NewGuid(), UniqueString = "whatever1"};
            var saga2 = new SagaWithUniqueProperty {Id = Guid.NewGuid(), UniqueString = "whatever"};
            var inMemorySagaPersister = new InMemorySagaPersister() as ISagaPersister;

            inMemorySagaPersister.Save(saga1);
            inMemorySagaPersister.Save(saga2);

            Assert.Throws<InvalidOperationException>(() =>
            {
                var saga = inMemorySagaPersister.Get<SagaWithUniqueProperty>(saga2.Id);
                saga.UniqueString = "whatever1";
                inMemorySagaPersister.Update(saga);
            });
        }
        public async Task Should_delete_the_saga()
        {
            var saga = new SagaWithUniquePropertyData {
                Id = Guid.NewGuid(), UniqueString = "whatever"
            };

            var persister     = new InMemorySagaPersister();
            var insertSession = new InMemorySynchronizedStorageSession();
            await persister.Save(saga, SagaMetadataHelper.GetMetadata <SagaWithUniqueProperty>(saga), insertSession, new ContextBag());

            await insertSession.CompleteAsync();

            var intentionallySharedContext = new ContextBag();
            var sagaData = await persister.Get <SagaWithUniquePropertyData>(saga.Id, new InMemorySynchronizedStorageSession(), intentionallySharedContext);

            var completeSession = new InMemorySynchronizedStorageSession();
            await persister.Complete(saga, completeSession, intentionallySharedContext);

            await completeSession.CompleteAsync();

            var completedSagaData = await persister.Get <SagaWithUniquePropertyData>(saga.Id, new InMemorySynchronizedStorageSession(), new ContextBag());

            Assert.NotNull(sagaData);
            Assert.Null(completedSagaData);
        }
        public void It_should_persist_successfully_for_two_unique_properties()
        {
            var saga1 = new SagaWithTwoUniqueProperties { Id = Guid.NewGuid(), UniqueString = "whatever1", UniqueInt = 5};
            var saga2 = new SagaWithTwoUniqueProperties { Id = Guid.NewGuid(), UniqueString = "whatever", UniqueInt = 37};
            var inMemorySagaPersister = new InMemorySagaPersister() as ISagaPersister;

            inMemorySagaPersister.Save(saga1);
            inMemorySagaPersister.Save(saga2);

            Assert.Throws<InvalidOperationException>(() =>
            {
                var saga = inMemorySagaPersister.Get<SagaWithTwoUniqueProperties>(saga2.Id);
                saga.UniqueInt = 5;
                inMemorySagaPersister.Update(saga);
            });
        }
        public async Task It_should_successfully_remove_the_saga()
        {
            var id       = Guid.NewGuid();
            var sagaData = new SagaWithoutUniquePropertyData
            {
                Id = id,
                NonUniqueString = "whatever"
            };

            var persister     = new InMemorySagaPersister();
            var savingSession = new InMemorySynchronizedStorageSession();

            await persister.Save(sagaData, null, savingSession, new ContextBag());

            await savingSession.CompleteAsync();

            // second session
            var completingSession    = new InMemorySynchronizedStorageSession();
            var completingContextBag = new ContextBag();

            var saga = await persister.Get <SagaWithoutUniquePropertyData>(id, completingSession, completingContextBag);

            await persister.Complete(saga, completingSession, completingContextBag);

            await completingSession.CompleteAsync();

            var result = await persister.Get <SagaWithoutUniquePropertyData>(sagaData.Id, savingSession, new ContextBag());

            Assert.That(result, Is.Null);
        }
        public async Task It_should_successfully_update_the_saga()
        {
            var id = Guid.NewGuid();
            var sagaData = new SagaWithoutUniquePropertyData
            {
                Id = id,
                NonUniqueString = "whatever"
            };

            var persister = new InMemorySagaPersister();

            // first session
            var session = new InMemorySynchronizedStorageSession();

            await persister.Save(sagaData, SagaCorrelationProperty.None, session, new ContextBag());

            await session.CompleteAsync();

            // second session
            var session2 = new InMemorySynchronizedStorageSession();

            var ctx = new ContextBag();
            var saga = await persister.Get<SagaWithoutUniquePropertyData>(id, session2, ctx);
            saga.NonUniqueString = "asdfasdf";

            await persister.Update(saga, session2, ctx);

            await session2.CompleteAsync();

            var result = await persister.Get<SagaWithoutUniquePropertyData>(sagaData.Id, new InMemorySynchronizedStorageSession(), new ContextBag());

            Assert.That(result, Is.Not.Null);
            Assert.That(result.NonUniqueString, Is.EqualTo("asdfasdf"));
        }
        public async Task Save_fails_when_data_changes_between_read_and_update_on_same_thread()
        {
            var sagaId = Guid.NewGuid();
            var saga = new TestSagaData
            {
                Id = sagaId,
                SomeId = sagaId.ToString()
            };
            var persister = new InMemorySagaPersister();
            var insertSession = new InMemorySynchronizedStorageSession();
            await persister.Save(saga, SagaMetadataHelper.GetMetadata<TestSaga>(saga), insertSession, new ContextBag());
            await insertSession.CompleteAsync();

            var winningContext = new ContextBag();
            var record = await persister.Get<TestSagaData>(saga.Id, new InMemorySynchronizedStorageSession(), winningContext);
            var losingContext = new ContextBag();
            var staleRecord = await persister.Get<TestSagaData>("SomeId", sagaId.ToString(), new InMemorySynchronizedStorageSession(), losingContext);

            var winningSaveSession = new InMemorySynchronizedStorageSession();
            var losingSaveSession = new InMemorySynchronizedStorageSession();

            await persister.Update(record, winningSaveSession, winningContext);
            await persister.Update(staleRecord, losingSaveSession, losingContext);

            await winningSaveSession.CompleteAsync();

            Assert.That(async () => await losingSaveSession.CompleteAsync(), Throws.InstanceOf<Exception>().And.Message.StartsWith($"InMemorySagaPersister concurrency violation: saga entity Id[{saga.Id}] already saved."));
        }
Beispiel #24
0
        public void It_should_persist_successfully()
        {
            var saga1 = new SagaWithUniqueProperty {
                Id = Guid.NewGuid(), UniqueString = "whatever"
            };
            var saga2 = new SagaWithUniqueProperty {
                Id = Guid.NewGuid(), UniqueString = "whatever"
            };
            var inMemorySagaPersister = new InMemorySagaPersister() as ISagaPersister;

            inMemorySagaPersister.Save(saga1);
            saga1 = inMemorySagaPersister.Get <SagaWithUniqueProperty>(saga1.Id);
            saga1.UniqueString = "whatever2";
            inMemorySagaPersister.Update(saga1);

            inMemorySagaPersister.Save(saga2);
        }
Beispiel #25
0
        public void It_should_enforce_uniqueness_even_for_two_unique_properties()
        {
            var saga1 = new SagaWithTwoUniqueProperties {
                Id = Guid.NewGuid(), UniqueString = "whatever", UniqueInt = 5
            };
            var saga2 = new SagaWithTwoUniqueProperties {
                Id = Guid.NewGuid(), UniqueString = "whatever1", UniqueInt = 3
            };
            var saga3 = new SagaWithTwoUniqueProperties {
                Id = Guid.NewGuid(), UniqueString = "whatever3", UniqueInt = 3
            };
            var inMemorySagaPersister = new InMemorySagaPersister() as ISagaPersister;

            inMemorySagaPersister.Save(saga1);
            inMemorySagaPersister.Save(saga2);
            Assert.Throws <InvalidOperationException>(() => inMemorySagaPersister.Save(saga3));
        }
        public void  It_should_persist_successfully()
        {
            var saga1 = new SagaWithTwoUniqueProperties {
                Id = Guid.NewGuid(), UniqueString = "whatever", UniqueInt = 5
            };
            var saga2 = new AnotherSagaWithTwoUniqueProperties {
                Id = Guid.NewGuid(), UniqueString = "whatever", UniqueInt = 5
            };
            var saga3 = new SagaWithUniqueProperty {
                Id = Guid.NewGuid(), UniqueString = "whatever"
            };

            var inMemorySagaPersister = new InMemorySagaPersister() as ISagaPersister;

            inMemorySagaPersister.Save(saga1);
            inMemorySagaPersister.Save(saga2);
            inMemorySagaPersister.Save(saga3);
        }
        public void Save_fails_when_data_changes_between_read_and_update()
        {
            var inMemorySagaPersister = new InMemorySagaPersister();
            var saga = new TestSaga {
                Id = Guid.NewGuid()
            };

            inMemorySagaPersister.Save(saga);

            var returnedSaga1 = Task <TestSaga> .Factory.StartNew(() => inMemorySagaPersister.Get <TestSaga>(saga.Id)).Result;

            var returnedSaga2 = inMemorySagaPersister.Get <TestSaga>("Id", saga.Id);

            inMemorySagaPersister.Save(returnedSaga1);
            var exception = Assert.Throws <Exception>(() => inMemorySagaPersister.Save(returnedSaga2));

            Assert.IsTrue(exception.Message.StartsWith(string.Format("InMemorySagaPersister concurrency violation: saga entity Id[{0}] already saved by [Worker.", saga.Id)));
        }
        public void It_should_persist_successfully()
        {
            var saga1 = new SagaWithUniqueProperty {
                Id = Guid.NewGuid(), UniqueString = "whatever"
            };
            var saga2 = new SagaWithUniqueProperty {
                Id = Guid.NewGuid(), UniqueString = "whatever"
            };

            var inMemorySagaPersister = new InMemorySagaPersister();

            inMemorySagaPersister.Save(saga1);
            inMemorySagaPersister.Complete(saga1);
            inMemorySagaPersister.Save(saga2);
            inMemorySagaPersister.Complete(saga2);
            inMemorySagaPersister.Save(saga1);
            inMemorySagaPersister.Complete(saga1);
        }
        public void Save_process_is_repeatable()
        {
            var inMemorySagaPersister = new InMemorySagaPersister();
            var saga = new TestSaga { Id = Guid.NewGuid() };
            inMemorySagaPersister.Save(saga);

            var returnedSaga1 = Task<TestSaga>.Factory.StartNew(() => inMemorySagaPersister.Get<TestSaga>(saga.Id)).Result;
            var returnedSaga2 = inMemorySagaPersister.Get<TestSaga>("Id", saga.Id);

            inMemorySagaPersister.Save(returnedSaga1);
            Assert.Throws<ConcurrencyException>(() => inMemorySagaPersister.Save(returnedSaga2));

            var returnedSaga3 = Task<TestSaga>.Factory.StartNew(() => inMemorySagaPersister.Get<TestSaga>("Id", saga.Id)).Result;
            var returnedSaga4 = inMemorySagaPersister.Get<TestSaga>(saga.Id);

            inMemorySagaPersister.Save(returnedSaga4);
            Assert.Throws<ConcurrencyException>(() => inMemorySagaPersister.Save(returnedSaga3));
        }
        async Task SaveSaga(SagaWithUniquePropertyData saga)
        {
            using (var session = new InMemorySynchronizedStorageSession())
            {
                var ctx = new ContextBag();
                await persister.Save(saga, SagaMetadataHelper.GetMetadata <SagaWithUniqueProperty>(saga), session, ctx);

                await session.CompleteAsync();
            }
        }
        public void Should_delete_the_saga()
        {
            var inMemorySagaPersister = new InMemorySagaPersister();
            var saga = new SagaWithUniqueProperty { Id = Guid.NewGuid(), UniqueString = "whatever" };

            inMemorySagaPersister.Save(saga);
            Assert.NotNull(inMemorySagaPersister.Get<SagaWithUniqueProperty>(saga.Id));
            inMemorySagaPersister.Complete(saga);
            Assert.Null(inMemorySagaPersister.Get<SagaWithUniqueProperty>(saga.Id));
        }
        public void Should_delete_the_saga()
        {
            var inMemorySagaPersister = new InMemorySagaPersister() as ISagaPersister;
            var saga = new TestSaga { Id = Guid.NewGuid() };

            inMemorySagaPersister.Save(saga);
            Assert.NotNull(inMemorySagaPersister.Get<TestSaga>(saga.Id));
            inMemorySagaPersister.Complete(saga);
            Assert.Null(inMemorySagaPersister.Get<TestSaga>(saga.Id));
        }
        public async Task It_should_persist_successfully()
        {
             var saga1 = new SagaWithUniquePropertyData
            {
                Id = Guid.NewGuid(),
                UniqueString = "whatever"
            };
            var saga2 = new AnotherSagaWithUniquePropertyData
            {
                Id = Guid.NewGuid(),
                UniqueString = "whatever"
            };

            var persister = new InMemorySagaPersister();
            var transaction = new InMemorySynchronizedStorageSession();
            await persister.Save(saga1, SagaMetadataHelper.GetMetadata<SagaWithUniqueProperty>(saga1), transaction, new ContextBag());
            await persister.Save(saga2, SagaMetadataHelper.GetMetadata<AnotherSagaTwoUniqueProperty>(saga2), transaction, new ContextBag());
            await transaction.CompleteAsync();
        }
Beispiel #34
0
        public async Task It_should_persist_successfully()
        {
            var saga1 = new SagaWithUniquePropertyData
            {
                Id           = Guid.NewGuid(),
                UniqueString = "whatever"
            };
            var saga2 = new SagaWithUniquePropertyData
            {
                Id           = Guid.NewGuid(),
                UniqueString = "whatever"
            };

            var persister = new InMemorySagaPersister();

            using (var session1 = new InMemorySynchronizedStorageSession())
            {
                await persister.Save(saga1, SagaMetadataHelper.GetMetadata <SagaWithUniqueProperty>(saga1), session1, new ContextBag());

                await persister.Complete(saga1, session1, new ContextBag());

                await session1.CompleteAsync();
            }

            using (var session2 = new InMemorySynchronizedStorageSession())
            {
                await persister.Save(saga2, SagaMetadataHelper.GetMetadata <SagaWithUniqueProperty>(saga2), session2, new ContextBag());

                await persister.Complete(saga2, session2, new ContextBag());

                await session2.CompleteAsync();
            }

            using (var session3 = new InMemorySynchronizedStorageSession())
            {
                await persister.Save(saga1, SagaMetadataHelper.GetMetadata <SagaWithUniqueProperty>(saga1), session3, new ContextBag());

                await persister.Complete(saga1, session3, new ContextBag());

                await session3.CompleteAsync();
            }
        }
        public void It_should_persist_successfully()
        {
            var saga1 = new SagaWithUniqueProperty {
                Id = Guid.NewGuid(), UniqueString = "whatever1"
            };
            var saga2 = new SagaWithUniqueProperty {
                Id = Guid.NewGuid(), UniqueString = "whatever"
            };
            var inMemorySagaPersister = new InMemorySagaPersister() as ISagaPersister;

            inMemorySagaPersister.Save(saga1);
            inMemorySagaPersister.Save(saga2);

            Assert.Throws <InvalidOperationException>(() =>
            {
                var saga          = inMemorySagaPersister.Get <SagaWithUniqueProperty>(saga2.Id);
                saga.UniqueString = "whatever1";
                inMemorySagaPersister.Update(saga);
            });
        }
        public void It_should_persist_successfully_for_two_unique_properties()
        {
            var saga1 = new SagaWithTwoUniqueProperties {
                Id = Guid.NewGuid(), UniqueString = "whatever1", UniqueInt = 5
            };
            var saga2 = new SagaWithTwoUniqueProperties {
                Id = Guid.NewGuid(), UniqueString = "whatever", UniqueInt = 37
            };
            var inMemorySagaPersister = new InMemorySagaPersister() as ISagaPersister;

            inMemorySagaPersister.Save(saga1);
            inMemorySagaPersister.Save(saga2);

            Assert.Throws <InvalidOperationException>(() =>
            {
                var saga       = inMemorySagaPersister.Get <SagaWithTwoUniqueProperties>(saga2.Id);
                saga.UniqueInt = 5;
                inMemorySagaPersister.Update(saga);
            });
        }
Beispiel #37
0
        public async Task It_should_persist_successfully()
        {
            var saga1 = new SagaWithUniquePropertyData
            {
                Id           = Guid.NewGuid(),
                UniqueString = "whatever"
            };
            var saga2 = new AnotherSagaWithUniquePropertyData
            {
                Id           = Guid.NewGuid(),
                UniqueString = "whatever"
            };

            var persister   = new InMemorySagaPersister();
            var transaction = new InMemorySynchronizedStorageSession();
            await persister.Save(saga1, SagaMetadataHelper.GetMetadata <SagaWithUniqueProperty>(saga1), transaction, new ContextBag());

            await persister.Save(saga2, SagaMetadataHelper.GetMetadata <AnotherSagaTwoUniqueProperty>(saga2), transaction, new ContextBag());

            await transaction.CompleteAsync();
        }
        public void Save_process_is_repeatable()
        {
            var inMemorySagaPersister = new InMemorySagaPersister();
            var saga = new TestSaga { Id = Guid.NewGuid() };
            inMemorySagaPersister.Save(saga);

            var returnedSaga1 = Task<TestSaga>.Factory.StartNew(() => inMemorySagaPersister.Get<TestSaga>(saga.Id)).Result;
            var returnedSaga2 = inMemorySagaPersister.Get<TestSaga>("Id", saga.Id);

            inMemorySagaPersister.Save(returnedSaga1);
            var exceptionFromSaga2 = Assert.Throws<Exception>(() => inMemorySagaPersister.Save(returnedSaga2));
            Assert.IsTrue(exceptionFromSaga2.Message.StartsWith(string.Format("InMemorySagaPersister concurrency violation: saga entity Id[{0}] already saved by [Worker.", saga.Id)));

            var returnedSaga3 = Task<TestSaga>.Factory.StartNew(() => inMemorySagaPersister.Get<TestSaga>("Id", saga.Id)).Result;
            var returnedSaga4 = inMemorySagaPersister.Get<TestSaga>(saga.Id);

            inMemorySagaPersister.Save(returnedSaga4);

            var exceptionFromSaga3 = Assert.Throws<Exception>(() => inMemorySagaPersister.Save(returnedSaga3));
            Assert.IsTrue(exceptionFromSaga3.Message.StartsWith(string.Format("InMemorySagaPersister concurrency violation: saga entity Id[{0}] already saved by [Worker.", saga.Id)));
        }
        public void Persister_returns_different_instance_of_saga_data()
        {
            var inMemorySagaPersister = new InMemorySagaPersister();
            var saga = new TestSaga { Id = Guid.NewGuid() };
            inMemorySagaPersister.Save(saga);

            var returnedSaga1 = inMemorySagaPersister.Get<TestSaga>(saga.Id);
            var returnedSaga2 = inMemorySagaPersister.Get<TestSaga>("Id", saga.Id);
            Assert.AreNotSame(returnedSaga2, returnedSaga1);
            Assert.AreNotSame(returnedSaga1, saga);
            Assert.AreNotSame(returnedSaga2, saga);
        }
Beispiel #40
0
        public void Should_delete_the_saga()
        {
            var inMemorySagaPersister = new InMemorySagaPersister();
            var saga = new SagaWithUniqueProperty {
                Id = Guid.NewGuid(), UniqueString = "whatever"
            };

            inMemorySagaPersister.Save(saga);
            Assert.NotNull(inMemorySagaPersister.Get <SagaWithUniqueProperty>(saga.Id));
            inMemorySagaPersister.Complete(saga);
            Assert.Null(inMemorySagaPersister.Get <SagaWithUniqueProperty>(saga.Id));
        }
Beispiel #41
0
        public void Should_delete_the_saga()
        {
            var inMemorySagaPersister = new InMemorySagaPersister() as ISagaPersister;
            var saga = new TestSaga {
                Id = Guid.NewGuid()
            };

            inMemorySagaPersister.Save(saga);
            Assert.NotNull(inMemorySagaPersister.Get <TestSaga>(saga.Id));
            inMemorySagaPersister.Complete(saga);
            Assert.Null(inMemorySagaPersister.Get <TestSaga>(saga.Id));
        }
        public async Task Save_fails_when_data_changes_between_concurrent_instances()
        {
            var saga = new TestSagaData
            {
                Id = Guid.NewGuid()
            };

            var persister      = new InMemorySagaPersister();
            var storageAdapter = new InMemoryTransactionalSynchronizedStorageAdapter();
            var insertSession  = new InMemorySynchronizedStorageSession();

            await persister.Save(saga, SagaMetadataHelper.GetMetadata <TestSaga>(saga), insertSession, new ContextBag());

            await insertSession.CompleteAsync();

            Assert.That(async() =>
            {
                using (var tx = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled))
                {
                    Transaction.Current.EnlistDurable(EnlistmentWhichEnforcesDtcEscalation.Id, new EnlistmentWhichEnforcesDtcEscalation(), EnlistmentOptions.None);

                    var transportTransaction = new TransportTransaction();
                    transportTransaction.Set(Transaction.Current);

                    var unenlistedSession = new InMemorySynchronizedStorageSession();

                    var enlistedSession = await storageAdapter.TryAdapt(transportTransaction, new ContextBag());

                    var unenlistedRecord = await persister.Get <TestSagaData>(saga.Id, unenlistedSession, new ContextBag());
                    var enlistedRecord   = await persister.Get <TestSagaData>("Id", saga.Id, enlistedSession, new ContextBag());

                    await persister.Save(unenlistedRecord, SagaMetadataHelper.GetMetadata <TestSaga>(saga), unenlistedSession, new ContextBag());
                    await persister.Save(enlistedRecord, SagaMetadataHelper.GetMetadata <TestSaga>(saga), enlistedSession, new ContextBag());

                    await unenlistedSession.CompleteAsync();

                    tx.Complete();
                }
            }, Throws.Exception.TypeOf <TransactionAbortedException>());
        }
        public void Should_return_default_when_using_finding_saga_with_id_of_another_type()
        {
            var p = new InMemorySagaPersister() as ISagaPersister;
            var id = Guid.NewGuid();
            var simpleSagaEntity = new SimpleSageEntity()
            {
                Id = id,
                OrderSource = "CA"
            };
            p.Save(simpleSagaEntity);

            var anotherSagaEntity = p.Get<AnotherSimpleSageEntity>(id);
            Assert.AreSame(anotherSagaEntity, default(AnotherSimpleSageEntity));
        }
        public void Save_process_is_repeatable()
        {
            var inMemorySagaPersister = new InMemorySagaPersister();
            var saga = new TestSaga {
                Id = Guid.NewGuid()
            };

            inMemorySagaPersister.Save(saga);

            var returnedSaga1 = Task <TestSaga> .Factory.StartNew(() => inMemorySagaPersister.Get <TestSaga>(saga.Id)).Result;

            var returnedSaga2 = inMemorySagaPersister.Get <TestSaga>("Id", saga.Id);

            inMemorySagaPersister.Save(returnedSaga1);
            Assert.Throws <ConcurrencyException>(() => inMemorySagaPersister.Save(returnedSaga2));

            var returnedSaga3 = Task <TestSaga> .Factory.StartNew(() => inMemorySagaPersister.Get <TestSaga>("Id", saga.Id)).Result;

            var returnedSaga4 = inMemorySagaPersister.Get <TestSaga>(saga.Id);

            inMemorySagaPersister.Save(returnedSaga4);
            Assert.Throws <ConcurrencyException>(() => inMemorySagaPersister.Save(returnedSaga3));
        }
 public async Task Should_return_default_when_using_finding_saga_with_id_of_another_type()
 {
     var id = Guid.NewGuid();
     var simpleSagaEntity = new SimpleSagaEntity
     {
         Id = id,
         OrderSource = "CA"
     };
     var persister = new InMemorySagaPersister();
     var session = new InMemorySynchronizedStorageSession();
     await persister.Save(simpleSagaEntity, SagaMetadataHelper.GetMetadata<SimpleSagaEntitySaga>(simpleSagaEntity), session, new ContextBag());
     await session.CompleteAsync();
     var anotherSagaEntity = await persister.Get<AnotherSimpleSagaEntity>(id, new InMemorySynchronizedStorageSession(),  new ContextBag());
     Assert.IsNull(anotherSagaEntity);
 }
        public async Task It_should_enforce_uniqueness()
        {
            var saga1 = new SagaWithUniquePropertyData
            {
                Id = Guid.NewGuid(),
                UniqueString = "whatever"
            };
            var saga2 = new SagaWithUniquePropertyData
            {
                Id = Guid.NewGuid(),
                UniqueString = "whatever"
            };

            var persister = new InMemorySagaPersister();
            var winningSession = new InMemorySynchronizedStorageSession();
            var losingSession = new InMemorySynchronizedStorageSession();

            await persister.Save(saga1, SagaMetadataHelper.GetMetadata<SagaWithUniqueProperty>(saga1), winningSession, new ContextBag());
            await persister.Save(saga2, SagaMetadataHelper.GetMetadata<SagaWithUniqueProperty>(saga1), losingSession, new ContextBag());

            await winningSession.CompleteAsync();

            Assert.That(async () => await losingSession.CompleteAsync(), Throws.InstanceOf<InvalidOperationException>());
        }
Beispiel #47
0
        public async Task Save_process_is_repeatable()
        {
            var sagaId = Guid.NewGuid();
            var saga   = new TestSagaData
            {
                Id     = sagaId,
                SomeId = sagaId.ToString()
            };
            var persister     = new InMemorySagaPersister();
            var insertSession = new InMemorySynchronizedStorageSession();
            await persister.Save(saga, SagaMetadataHelper.GetMetadata <TestSaga>(saga), insertSession, new ContextBag());

            await insertSession.CompleteAsync();

            var winningSessionContext = new ContextBag();
            var returnedSaga1         = await Task.Run(() => persister.Get <TestSagaData>(saga.Id, new InMemorySynchronizedStorageSession(), winningSessionContext));

            var losingSessionContext = new ContextBag();
            var returnedSaga2        = await persister.Get <TestSagaData>("SomeId", sagaId.ToString(), new InMemorySynchronizedStorageSession(), losingSessionContext);

            var winningSaveSession = new InMemorySynchronizedStorageSession();
            var losingSaveSession  = new InMemorySynchronizedStorageSession();

            await persister.Update(returnedSaga1, winningSaveSession, winningSessionContext);

            await persister.Update(returnedSaga2, losingSaveSession, losingSessionContext);

            await winningSaveSession.CompleteAsync();

            Assert.That(async() => await losingSaveSession.CompleteAsync(), Throws.InstanceOf <Exception>().And.Message.StartsWith($"InMemorySagaPersister concurrency violation: saga entity Id[{saga.Id}] already saved."));

            losingSessionContext = new ContextBag();
            var returnedSaga3 = await Task.Run(() => persister.Get <TestSagaData>("SomeId", sagaId.ToString(), new InMemorySynchronizedStorageSession(), losingSessionContext));

            winningSessionContext = new ContextBag();
            var returnedSaga4 = await persister.Get <TestSagaData>(saga.Id, new InMemorySynchronizedStorageSession(), winningSessionContext);

            winningSaveSession = new InMemorySynchronizedStorageSession();
            losingSaveSession  = new InMemorySynchronizedStorageSession();

            await persister.Update(returnedSaga4, winningSaveSession, winningSessionContext);

            await persister.Update(returnedSaga3, losingSaveSession, losingSessionContext);

            await winningSaveSession.CompleteAsync();

            Assert.That(async() => await losingSaveSession.CompleteAsync(), Throws.InstanceOf <Exception>().And.Message.StartsWith($"InMemorySagaPersister concurrency violation: saga entity Id[{saga.Id}] already saved."));
        }
Beispiel #48
0
        public void Should_return_default_when_using_finding_saga_with_id_of_another_type()
        {
            var p  = new InMemorySagaPersister() as ISagaPersister;
            var id = Guid.NewGuid();
            var simpleSagaEntity = new SimpleSageEntity()
            {
                Id          = id,
                OrderSource = "CA"
            };

            p.Save(simpleSagaEntity);

            var anotherSagaEntity = p.Get <AnotherSimpleSageEntity>(id);

            Assert.AreSame(anotherSagaEntity, default(AnotherSimpleSageEntity));
        }
        public void Persister_returns_different_instance_of_saga_data()
        {
            var inMemorySagaPersister = new InMemorySagaPersister();
            var saga = new TestSaga {
                Id = Guid.NewGuid()
            };

            inMemorySagaPersister.Save(saga);

            var returnedSaga1 = inMemorySagaPersister.Get <TestSaga>(saga.Id);
            var returnedSaga2 = inMemorySagaPersister.Get <TestSaga>("Id", saga.Id);

            Assert.AreNotSame(returnedSaga2, returnedSaga1);
            Assert.AreNotSame(returnedSaga1, saga);
            Assert.AreNotSame(returnedSaga2, saga);
        }
        public async Task Persister_returns_different_instance_of_saga_data()
        {
            var saga = new TestSagaData
            {
                Id = Guid.NewGuid()
            };
            var persister = new InMemorySagaPersister();
            var insertSession = new InMemorySynchronizedStorageSession();
            await persister.Save(saga, SagaMetadataHelper.GetMetadata<TestSaga>(saga), insertSession, new ContextBag());
            await insertSession.CompleteAsync();

            var returnedSaga1 = await persister.Get<TestSagaData>(saga.Id, new InMemorySynchronizedStorageSession(), new ContextBag());
            var returnedSaga2 = await persister.Get<TestSagaData>("Id", saga.Id, new InMemorySynchronizedStorageSession(), new ContextBag());
            Assert.AreNotSame(returnedSaga2, returnedSaga1);
            Assert.AreNotSame(returnedSaga1, saga);
            Assert.AreNotSame(returnedSaga2, saga);
        }
Beispiel #51
0
        public async Task Should_return_default_when_using_finding_saga_with_id_of_another_type()
        {
            var id = Guid.NewGuid();
            var simpleSagaEntity = new SimpleSagaEntity
            {
                Id          = id,
                OrderSource = "CA"
            };
            var persister = new InMemorySagaPersister();
            var session   = new InMemorySynchronizedStorageSession();
            await persister.Save(simpleSagaEntity, SagaMetadataHelper.GetMetadata <SimpleSagaEntitySaga>(simpleSagaEntity), session, new ContextBag());

            await session.CompleteAsync();

            var anotherSagaEntity = await persister.Get <AnotherSimpleSagaEntity>(id, new InMemorySynchronizedStorageSession(), new ContextBag());

            Assert.IsNull(anotherSagaEntity);
        }
Beispiel #52
0
        public async Task Persister_returns_different_instance_of_saga_data()
        {
            var saga = new TestSagaData
            {
                Id = Guid.NewGuid()
            };
            var persister     = new InMemorySagaPersister();
            var insertSession = new InMemorySynchronizedStorageSession();
            await persister.Save(saga, SagaMetadataHelper.GetMetadata <TestSaga>(saga), insertSession, new ContextBag());

            await insertSession.CompleteAsync();

            var returnedSaga1 = await persister.Get <TestSagaData>(saga.Id, new InMemorySynchronizedStorageSession(), new ContextBag());

            var returnedSaga2 = await persister.Get <TestSagaData>("Id", saga.Id, new InMemorySynchronizedStorageSession(), new ContextBag());

            Assert.AreNotSame(returnedSaga2, returnedSaga1);
            Assert.AreNotSame(returnedSaga1, saga);
            Assert.AreNotSame(returnedSaga2, saga);
        }
        public async Task It_should_persist_successfully()
        {
            var saga1 = new SagaWithUniquePropertyData
            {
                Id = Guid.NewGuid(),
                UniqueString = "whatever"
            };
            var persister = new InMemorySagaPersister();

            var insertSession = new InMemorySynchronizedStorageSession();
            await persister.Save(saga1, SagaMetadataHelper.GetMetadata<SagaWithUniqueProperty>(saga1), insertSession, new ContextBag());
            await insertSession.CompleteAsync();

            var updatingContext = new ContextBag();
            saga1 = await persister.Get<SagaWithUniquePropertyData>(saga1.Id, new InMemorySynchronizedStorageSession(), updatingContext);

            var updateSession = new InMemorySynchronizedStorageSession();
            await persister.Update(saga1, updateSession, updatingContext);
            await updateSession.CompleteAsync();
        }
        public async Task Save_fails_when_data_changes_between_concurrent_instances()
        {
            var saga = new TestSagaData
            {
                Id = Guid.NewGuid()
            };

            var persister = new InMemorySagaPersister();
            var storageAdapter = new InMemoryTransactionalSynchronizedStorageAdapter();
            var insertSession = new InMemorySynchronizedStorageSession();

            await persister.Save(saga, SagaMetadataHelper.GetMetadata<TestSaga>(saga), insertSession, new ContextBag());
            await insertSession.CompleteAsync();

            Assert.That(async () =>
            {
                using (var tx = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled))
                {
                    Transaction.Current.EnlistDurable(EnlistmentWhichEnforcesDtcEscalation.Id, new EnlistmentWhichEnforcesDtcEscalation(), EnlistmentOptions.None);

                    var transportTransaction = new TransportTransaction();
                    transportTransaction.Set(Transaction.Current);

                    var unenlistedSession = new InMemorySynchronizedStorageSession();

                    var enlistedSession = await storageAdapter.TryAdapt(transportTransaction, new ContextBag());

                    var unenlistedSessionContext = new ContextBag();
                    var unenlistedRecord = await persister.Get<TestSagaData>(saga.Id, unenlistedSession, unenlistedSessionContext);
                    var enlistedSessionContext = new ContextBag();
                    var enlistedRecord = await persister.Get<TestSagaData>("Id", saga.Id, enlistedSession, enlistedSessionContext);

                    await persister.Update(unenlistedRecord, unenlistedSession, unenlistedSessionContext);
                    await persister.Update(enlistedRecord, enlistedSession, enlistedSessionContext);

                    await unenlistedSession.CompleteAsync();

                    tx.Complete();
                }
            }, Throws.Exception.TypeOf<TransactionAbortedException>());
        }
        public async Task It_should_persist_successfully()
        {
            var sagaData = new SagaWithoutUniquePropertyData
            {
                Id = Guid.NewGuid(),
                NonUniqueString = "whatever"
            };

            var persister = new InMemorySagaPersister();
            using (var session = new InMemorySynchronizedStorageSession())
            {
                await persister.Save(sagaData, null, session, new ContextBag());
                await session.CompleteAsync();
            }

            using (var session = new InMemorySynchronizedStorageSession())
            {
                var retrieved = await persister.Get<SagaWithoutUniquePropertyData>(sagaData.Id, session, new ContextBag());
                Assert.AreEqual(sagaData.NonUniqueString, retrieved.NonUniqueString);
            }
        }
        public async Task Should_delete_the_saga()
        {
            var saga = new SagaWithUniquePropertyData { Id = Guid.NewGuid(), UniqueString = "whatever" };

            var persister = new InMemorySagaPersister();
            var insertSession = new InMemorySynchronizedStorageSession();
            await persister.Save(saga,SagaMetadataHelper.GetMetadata<SagaWithUniqueProperty>(saga), insertSession, new ContextBag());
            await insertSession.CompleteAsync();

            var intentionallySharedContext = new ContextBag();
            var sagaData = await persister.Get<SagaWithUniquePropertyData>(saga.Id, new InMemorySynchronizedStorageSession(), intentionallySharedContext );

            var completeSession = new InMemorySynchronizedStorageSession();
            await persister.Complete(saga, completeSession, intentionallySharedContext );
            await completeSession.CompleteAsync();

            var completedSagaData = await persister.Get<SagaWithUniquePropertyData>(saga.Id, new InMemorySynchronizedStorageSession(), new ContextBag());

            Assert.NotNull(sagaData);
            Assert.Null(completedSagaData);
        }
        public async Task Save_fails_when_writing_same_data_twice()
        {
            var saga = new TestSagaData
            {
                Id = Guid.NewGuid()
            };
            var persister = new InMemorySagaPersister();
            var insertSession = new InMemorySynchronizedStorageSession();
            await persister.Save(saga, SagaMetadataHelper.GetMetadata<TestSaga>(saga), insertSession, new ContextBag());
            await insertSession.CompleteAsync();

            var retrievingContext = new ContextBag();
            var returnedSaga1 = await persister.Get<TestSagaData>(saga.Id, new InMemorySynchronizedStorageSession(), retrievingContext);

            var winningSaveSession = new InMemorySynchronizedStorageSession();
            var losingSaveSession = new InMemorySynchronizedStorageSession();

            await persister.Update(returnedSaga1, winningSaveSession, retrievingContext);
            await persister.Update(returnedSaga1, losingSaveSession, retrievingContext);

            await winningSaveSession.CompleteAsync();

            Assert.That(async () => await losingSaveSession.CompleteAsync(), Throws.InstanceOf<Exception>().And.Message.StartsWith($"InMemorySagaPersister concurrency violation: saga entity Id[{saga.Id}] already saved."));
        }
        public async Task Save_process_is_repeatable()
        {
            var sagaId = Guid.NewGuid();
            var saga = new TestSagaData
            {
                Id = sagaId,
                SomeId = sagaId.ToString()
            };
            var persister = new InMemorySagaPersister();
            var insertSession = new InMemorySynchronizedStorageSession();
            await persister.Save(saga, SagaMetadataHelper.GetMetadata<TestSaga>(saga), insertSession, new ContextBag());
            await insertSession.CompleteAsync();

            var winningSessionContext = new ContextBag();
            var returnedSaga1 = await Task.Run(() => persister.Get<TestSagaData>(saga.Id, new InMemorySynchronizedStorageSession(), winningSessionContext));

            var losingSessionContext = new ContextBag();
            var returnedSaga2 = await persister.Get<TestSagaData>("SomeId", sagaId.ToString(), new InMemorySynchronizedStorageSession(), losingSessionContext);

            var winningSaveSession = new InMemorySynchronizedStorageSession();
            var losingSaveSession = new InMemorySynchronizedStorageSession();

            await persister.Update(returnedSaga1, winningSaveSession, winningSessionContext);
            await persister.Update(returnedSaga2, losingSaveSession, losingSessionContext);

            await winningSaveSession.CompleteAsync();
            Assert.That(async () => await losingSaveSession.CompleteAsync(), Throws.InstanceOf<Exception>().And.Message.StartsWith($"InMemorySagaPersister concurrency violation: saga entity Id[{saga.Id}] already saved."));

            losingSessionContext = new ContextBag();
            var returnedSaga3 = await Task.Run(() => persister.Get<TestSagaData>("SomeId", sagaId.ToString(), new InMemorySynchronizedStorageSession(), losingSessionContext));

            winningSessionContext = new ContextBag();
            var returnedSaga4 = await persister.Get<TestSagaData>(saga.Id, new InMemorySynchronizedStorageSession(), winningSessionContext);

            winningSaveSession = new InMemorySynchronizedStorageSession();
            losingSaveSession = new InMemorySynchronizedStorageSession();

            await persister.Update(returnedSaga4, winningSaveSession, winningSessionContext);
            await persister.Update(returnedSaga3, losingSaveSession, losingSessionContext);

            await winningSaveSession.CompleteAsync();

            Assert.That(async () => await losingSaveSession.CompleteAsync(), Throws.InstanceOf<Exception>().And.Message.StartsWith($"InMemorySagaPersister concurrency violation: saga entity Id[{saga.Id}] already saved."));
        }