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 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 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_persist_successfully()
        {
            var saga1 = new SagaWithUniqueProperty { Id = Guid.NewGuid(), UniqueString = "whatever"};

            var inMemorySagaPersister = new InMemorySagaPersister() as ISagaPersister;
            inMemorySagaPersister.Save(saga1);
            saga1 = inMemorySagaPersister.Get<SagaWithUniqueProperty>(saga1.Id);
            inMemorySagaPersister.Update(saga1);
        }
        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 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 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 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 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_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));
        }
示例#14
0
        public void It_should_persist_successfully()
        {
            var saga1 = new SagaWithUniqueProperty
            {
                Id           = Guid.NewGuid(),
                UniqueString = "whatever"
            };

            var inMemorySagaPersister = new InMemorySagaPersister();

            inMemorySagaPersister.Save(saga1);
            saga1 = inMemorySagaPersister.Get <SagaWithUniqueProperty>(saga1.Id);
            inMemorySagaPersister.Update(saga1);
        }
        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 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 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);
 }
示例#18
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."));
        }
        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);
            });
        }
示例#21
0
    public async Task Try()
    {
        var persister  = new InMemorySagaPersister();
        var inbox      = new InMemoryInbox();
        var dispatcher = new FakeDispatcher();
        var manager    = new SagaManager(persister, inbox, dispatcher);

        await manager.Process <SagaData>("messageId", "correlationId", new ContextBag(),
                                         HandlerCallback);

        var dataContainer = await persister.LoadByCorrelationId("correlationId");

        var sagaData = (SagaData)dataContainer.SagaData;

        Assert.AreEqual(1, sagaData.Counter);
    }
        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);
        }
示例#23
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 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));
        }
示例#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));
        }
示例#26
0
        protected override void DoSetUp()
        {
            // this is the bus hosting the saga
            sagaHandlerActivator = new HandlerActivatorForTesting();
            sagaPersister        = new InMemorySagaPersister();
            sagaBus = CreateBus(SagaBusInputQueueName, sagaHandlerActivator, new InMemorySubscriptionStorage(), sagaPersister, "error").Start(1);

            // this is the "service bus", i.e. just some request/reply-enabled service somewhere
            serviceHandlerActivator = new HandlerActivatorForTesting();
            serviceBus = CreateBus(ServiceBusInputQueueName, serviceHandlerActivator).Start(1);

            // this is just a bus from the outside that can initiate everything
            initiatorBus = CreateBus("test.autocorrelation.initiator", new HandlerActivatorForTesting()).Start(1);

            timeoutService = new TimeoutService(new InMemoryTimeoutStorage());
            timeoutService.Start();
        }
        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 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)));
        }
示例#29
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);
            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));
        }
        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);
        }
示例#32
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);
        }
        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();
        }
示例#34
0
        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>());
        }
示例#35
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();
            }
        }
示例#36
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 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);
            });
        }
        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);
        }
示例#43
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)));
        }
示例#45
0
        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();

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

            var updateSession = new InMemorySynchronizedStorageSession();
            await persister.Update(saga1, updateSession, new ContextBag());

            await updateSession.CompleteAsync();
        }
示例#46
0
        public void UsesFallbackPersisterWhenNoCustomOnesAreConfigured()
        {
            // arrange
            var stringPath = Reflect.Path <ChunkOfData>(c => c.SomeString);
            var indexPaths = new[] { stringPath };

            var inMemorySagaPersister = new InMemorySagaPersister();
            var persister             = new HybridSagaPersister(inMemorySagaPersister)
                                        .Add(new ThrowingSagaPersister());

            // act
            // assert
            var data = new ChunkOfData {
                SomeString = "Hello"
            };

            persister.Insert(data, indexPaths);

            var sagaCountAfterFirstInsert = inMemorySagaPersister.Count();

            var loadedData = persister.Find <ChunkOfData>(stringPath, "Hello");

            loadedData.ShouldNotBe(null);
            loadedData.SomeString.ShouldBe("Hello");

            loadedData.SomeString = "Hello again!";
            persister.Update(loadedData, indexPaths);

            var reloadedData = persister.Find <ChunkOfData>(stringPath, "Hello again!");

            reloadedData.ShouldNotBe(null);
            reloadedData.SomeString.ShouldBe("Hello again!");

            persister.Delete(reloadedData);
            var sagaCountAfterDelete = inMemorySagaPersister.Count();

            sagaCountAfterDelete.ShouldBe(0);
            sagaCountAfterFirstInsert.ShouldBe(1);
        }
        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_delete_the_saga()
        {
            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 sagaData = await persister.Get <TestSagaData>(saga.Id, new InMemorySynchronizedStorageSession(), new ContextBag());

            var deleteSession = new InMemorySynchronizedStorageSession();
            await persister.Complete(saga, deleteSession, new ContextBag());

            await deleteSession.CompleteAsync();

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

            Assert.NotNull(sagaData);
            Assert.Null(completedSaga);
        }
示例#49
0
    public async Task PerformScenario(string scenario)
    {
        var controller = new TestController(scenario);
        var persister  = new InMemorySagaPersister();
        var inbox      = new InMemoryInbox();
        var dispatcher = new FakeDispatcher();
        var persisterA = new TestingSagaDataPersister(persister, inbox, 'A', controller.GetBarrier);
        var persisterB = new TestingSagaDataPersister(persister, inbox, 'B', controller.GetBarrier);

        var managerA = new SagaManager(persisterA, persisterA, dispatcher);
        var managerB = new SagaManager(persisterB, persisterB, dispatcher);

        var processA = Task.Run(() => ProcessMessage(managerA, controller));
        var processB = Task.Run(() => ProcessMessage(managerB, controller));

        var done = Task.WhenAll(processA, processB);
        await done.ConfigureAwait(false);

        var dataContainer = await persister.LoadByCorrelationId("correlationId");

        var sagaData = (SagaData)dataContainer.SagaData;

        Assert.AreEqual(1, sagaData.Counter);
    }
        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>());
        }
示例#51
0
        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 sagaData = await persister.Get <SagaWithUniquePropertyData>(saga.Id, new InMemorySynchronizedStorageSession(), new ContextBag());

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

            await completeSession.CompleteAsync();

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

            Assert.NotNull(sagaData);
            Assert.Null(completedSagaData);
        }
        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);
            }
        }
示例#53
0
 public void SetUp()
 {
     persister = new InMemorySagaPersister(new SagaMetaModel(TypeBasedSagaMetaModel.Create(sagaTypes, new Conventions())));
 }
 protected override void DoSetUp()
 {
     persister = new InMemorySagaPersister();
 }
 public void Should_return_default_when_using_finding_saga_with_id()
 {
     var p = new InMemorySagaPersister() as ISagaPersister;
     var simpleSageEntity = p.Get<SimpleSageEntity>(Guid.Empty);
     Assert.AreSame(simpleSageEntity, default(SimpleSageEntity));
 }
 public void Should_return_default_when_using_finding_saga_with_property()
 {
     var p = new InMemorySagaPersister() as ISagaPersister;
     var simpleSageEntity = p.Get<SimpleSageEntity>("propertyNotFound", null);
     Assert.AreEqual(simpleSageEntity, default(SimpleSageEntity));
 }
 public async Task Should_return_default_when_using_finding_saga_with_id()
 {
     var persister = new InMemorySagaPersister();
     var simpleSageEntity = await persister.Get<SimpleSagaEntity>(Guid.Empty, new InMemorySynchronizedStorageSession(), new ContextBag());
     Assert.IsNull(simpleSageEntity);
 }
示例#58
0
            public SagaFixtureSagaPersister(IList <TSagaDataToStore> deletedSagaData)
            {
                innerPersister = new InMemorySagaPersister();

                this.deletedSagaData = deletedSagaData;
            }