Exemplo n.º 1
0
        public async Task It_should_persist_successfully()
        {
            var correlationPropertyData = Guid.NewGuid().ToString();
            var saga1 = new SagaWithCorrelationPropertyData
            {
                CorrelatedProperty = correlationPropertyData,
                DateTimeProperty   = DateTime.UtcNow
            };
            var saga2 = new AnotherSagaWithCorrelatedPropertyData
            {
                CorrelatedProperty = correlationPropertyData,
            };

            var persister        = configuration.SagaStorage;
            var savingContextBag = configuration.GetContextBagForSagaStorage();

            using (var session = await configuration.SynchronizedStorage.OpenSession(savingContextBag))
            {
                var correlationPropertySaga1 = SetActiveSagaInstanceForSave(savingContextBag, new SagaWithCorrelationProperty(), saga1);
                await persister.Save(saga1, correlationPropertySaga1, session, savingContextBag);

                var correlationPropertySaga2 = SetActiveSagaInstanceForSave(savingContextBag, new AnotherSagaWithCorrelatedProperty(), saga2);
                await persister.Save(saga2, correlationPropertySaga2, session, savingContextBag);

                await session.CompleteAsync();
            }

            var readContextBag = configuration.GetContextBagForSagaStorage();

            using (var readSession = await configuration.SynchronizedStorage.OpenSession(readContextBag))
            {
                SetActiveSagaInstanceForGet <SagaWithCorrelationProperty, SagaWithCorrelationPropertyData>(readContextBag, saga1);
                var saga1Result = await persister.Get <SagaWithCorrelationPropertyData>(nameof(SagaWithCorrelationPropertyData.CorrelatedProperty), saga1.CorrelatedProperty, readSession, readContextBag);

                SetActiveSagaInstanceForGet <AnotherSagaWithCorrelatedProperty, AnotherSagaWithCorrelatedPropertyData>(readContextBag, saga2);
                var saga2Result = await persister.Get <AnotherSagaWithCorrelatedPropertyData>(nameof(AnotherSagaWithCorrelatedPropertyData.CorrelatedProperty), saga2.CorrelatedProperty, readSession, readContextBag);

                Assert.AreEqual(saga1.CorrelatedProperty, saga1Result.CorrelatedProperty);
                Assert.AreEqual(saga2.CorrelatedProperty, saga2Result.CorrelatedProperty);
            }
        }
Exemplo n.º 2
0
        public async Task It_should_enforce_uniqueness()
        {
            var correlationPropertyData = Guid.NewGuid().ToString();
            var saga1 = new SagaWithCorrelationPropertyData
            {
                CorrelatedProperty = correlationPropertyData,
                DateTimeProperty   = DateTime.UtcNow
            };
            var saga2 = new SagaWithCorrelationPropertyData
            {
                CorrelatedProperty = correlationPropertyData,
                DateTimeProperty   = DateTime.UtcNow
            };

            var persister = configuration.SagaStorage;

            var winningContextBag = configuration.GetContextBagForSagaStorage();

            using (var winningSession = await configuration.SynchronizedStorage.OpenSession(winningContextBag))
            {
                var correlationPropertySaga1 = SetActiveSagaInstanceForSave(winningContextBag, new SagaWithCorrelationProperty(), saga1);
                await persister.Save(saga1, correlationPropertySaga1, winningSession, winningContextBag);

                await winningSession.CompleteAsync();
            }

            var losingContextBag = configuration.GetContextBagForSagaStorage();

            using (var losingSession = await configuration.SynchronizedStorage.OpenSession(losingContextBag))
            {
                var correlationPropertySaga2 = SetActiveSagaInstanceForSave(losingContextBag, new SagaWithCorrelationProperty(), saga2);
                await persister.Save(saga2, correlationPropertySaga2, losingSession, losingContextBag);

                // ReSharper disable once AccessToDisposedClosure
                Assert.That(async() => await losingSession.CompleteAsync(), Throws.InstanceOf <Exception>().And.Message.EndsWith($"The saga with the correlation id 'Name: {nameof(SagaWithCorrelationPropertyData.CorrelatedProperty)} Value: {correlationPropertyData}' already exists."));
            }
        }