Exemplo n.º 1
0
        public async Task TestBasicSaga()
        {
            var testableSaga = new TestableSaga <ShippingPolicy, ShippingPolicyData>();

            var placeResult = await testableSaga.Handle(new OrderPlaced { OrderId = "abc" });

            var billResult = await testableSaga.Handle(new OrderBilled { OrderId = "abc" });

            Assert.That(placeResult.Completed, Is.False);
            Assert.That(billResult.Completed, Is.False);

            // Snapshots of data should still be assertable even after multiple operations have occurred.
            Assert.That(placeResult.SagaDataSnapshot.OrderId, Is.EqualTo("abc"));
            Assert.That(placeResult.SagaDataSnapshot.Placed, Is.True);
            Assert.That(placeResult.SagaDataSnapshot.Billed, Is.False);

            var noResults = await testableSaga.AdvanceTime(TimeSpan.FromMinutes(10));

            Assert.That(noResults.Length, Is.EqualTo(0));

            var timeoutResults = await testableSaga.AdvanceTime(TimeSpan.FromHours(1));

            Assert.That(timeoutResults.Length, Is.EqualTo(1));

            var shipped = timeoutResults.First().FindPublishedMessage <OrderShipped>();

            Assert.That(shipped.OrderId == "abc");
        }
        public async Task TestDealyedMessages()
        {
            var testableSaga = new TestableSaga <DelaySaga, Data>();

            var corrId = Guid.NewGuid().ToString().Substring(0, 8);

            var startResult = await testableSaga.Handle(new Start { CorrId = corrId });

            Assert.That(startResult.Context.TimeoutMessages.Count, Is.EqualTo(1)); // Just timeout
            Assert.That(startResult.Context.SentMessages.Count, Is.EqualTo(2));    // Both
            Assert.That(startResult.SagaDataSnapshot.RegularTimeoutReceived, Is.False);
            Assert.That(startResult.SagaDataSnapshot.DelayedCommandReceived, Is.False);

            var timeout = startResult.FindTimeoutMessage <RegularTimeout>();
            var delayed = startResult.FindSentMessage <DelayedCmd>();

            Assert.That(timeout, Is.Not.Null);
            Assert.That(delayed, Is.Not.Null);
            Assert.That(delayed.CorrId, Is.EqualTo(corrId));

            var timeoutResults = await testableSaga.AdvanceTime(TimeSpan.FromMinutes(30));

            var timeoutResult = timeoutResults.Single();

            Assert.That(timeoutResult.SagaDataSnapshot.RegularTimeoutReceived, Is.True);
            Assert.That(timeoutResult.SagaDataSnapshot.DelayedCommandReceived, Is.False);

            var delayedCmdResults = await testableSaga.AdvanceTime(TimeSpan.FromMinutes(30));

            var delayedCmdResult = delayedCmdResults.Single();

            Assert.That(delayedCmdResult.SagaDataSnapshot.RegularTimeoutReceived, Is.True);
            Assert.That(delayedCmdResult.SagaDataSnapshot.DelayedCommandReceived, Is.True);
        }