Unit Testing Statemachine Saga

647 views
Skip to first unread message

Rupesh Kumar Tiwari

unread,
Jul 20, 2015, 10:44:33 AM7/20/15
to masstrans...@googlegroups.com
Hi I have been trying to find some sample to write unit test and never got working solution.

I created one AuctionSaga State machine and its test case, First test is passing next one is failing. 
I mean the first message which initiates the saga is good another test where I happen to publish next message to change the saga state is not working. 
I am not sure whats going wrong why next publish messages are not hitting saga. 
I am using  MassTransit.Testing.

Does anyone has one full example of unit testing statemachine saga ?
I tried Masstransit solution from Github (https://github.com/MassTransit/MassTransit/tree/develop/src/MassTransit.Tests ) saga/statemachine
Nowhere I found this kind of testing. Please suggest how to test saga ?

[TestFixture]
    public class AuctionSagaTests
    {
        private Guid _correlationId;
        private SagaTest<BusTestScenario, AuctionSaga> _saga;
        private Guid _auctionId;
        [SetUp]
        public void TestSetup()
        {
            _correlationId = NewId.NextGuid();
            _auctionId = NewId.NextGuid();

            _saga = TestFactory.ForSaga<AuctionSaga>()
                .InSingleBusScenario()
                .New(x =>
                {
                    x.UseScenarioBuilder(() => new CustomBusScenarioBuilder());
                    x.Send(new CreateAuction()
                    {
                        CorrelationId = _correlationId,
                        AuctionId = _auctionId
                    });
                });

            _saga.Execute();
        }

        [Test]
        public void When_CreateAuction_Message_IsReceived_Then_Saga_TransitionedTo_Open_State()
        {
            var sagaShouldPresent = _saga.Saga.Created.Any(x => x.CorrelationId == _correlationId);
            var saga = _saga.Saga.Contains(_correlationId);
            Assert.True(sagaShouldPresent);

            Assert.AreEqual(AuctionSaga.Open.Name, saga.CurrentState.Name);
        }



//This test case is failing it always stays in OPEN state.
        [Test]
        public void When_Saga_Created_AuctionFailed_Received_Saga_TransitionedTo_Faulted_State()
        {
            var sagaShouldPresent = _saga.Saga.Created.Any(x => x.CorrelationId == _correlationId);
            _saga.Scenario.Bus.Publish(new AuctionFailed() { AuctionId = _auctionId });
            
            var saga = _saga.Saga.Contains(_correlationId);
            Assert.True(sagaShouldPresent);

            Assert.AreEqual(AuctionSaga.Faulted.Name, saga.CurrentState.Name); //sagastate remains OPEN never changes to FAULTEd.
        }


        [TearDown]
        public void TearDown()
        {
            _saga.Dispose();
            _saga = null;
        }
    }



 public class AuctionSaga : SagaStateMachine<AuctionSaga>, ISaga
    {
        public   Guid CorrelationId { get; private set; }
        public   Guid AuctionId { get; set; }
        public IServiceBus Bus { get; set; }
        public   string Title { get; set; }
        public static Event<PlaceBid> Bid { get; set; }
        public static Event<CreateAuction> CreateAuction { get; set; }
        public static Event<AuctionFailed> AuctionFailed { get; set; }
        public static Event<RetryBid> RetryBid { get; set; }
        public static Event<FinalBid> FinalBid { get; set; }

        public decimal OpenBid { get; set; }
        public decimal MaximumBid{ get; set; }
        public static State Initial { get; set; }
        public static State Completed { get; set; }
        public static State Open { get; set; }
        public static State BidPlaced { get; set; }
        public static State Faulted { get; set; }
        public static State Closed { get; set; }

        public AuctionSaga()
        {
            
        }
        public AuctionSaga(IServiceBus bus)
        {
            Bus = bus;
        }
        static AuctionSaga()
        {
            try
            {
                Define(() =>
                {
                    Correlate(Bid).By((saga, message) => saga.AuctionId == message.AuctionId);
                    Correlate(AuctionFailed).By((saga, message) => saga.AuctionId == message.AuctionId);
                    Correlate(RetryBid).By((saga, message) => saga.AuctionId == message.AuctionId);
                    Correlate(FinalBid).By((saga, message) => saga.AuctionId == message.AuctionId);

                    Initially(When(CreateAuction).Then((saga, message) => saga.OnCreate(message)).TransitionTo(Open));

                    Anytime(When(AuctionFailed).Then((saga, message) => saga.OnAuctionFailed(message)).TransitionTo(Faulted));

                    During(Open,
                        When(Bid)
                        .Then((saga, message) => saga.OnBid(message)),
                        When(FinalBid)
                        .Then((saga,message)=>saga.OnFinalBid(message)));

                    During(Faulted,
                        When(RetryBid)
                            .Then((saga, message) => saga.OnRetryBid(message))
                            .TransitionTo(Open));

                    Combine(Bid, FinalBid).Into(AuctionCompleted, saga => saga.AuctionComplete);

                    Anytime(When(AuctionCompleted).Then((saga,message)=>saga.OnAuctionCompleted(message)).TransitionTo(Completed));
                });
            }
            catch (Exception exception)
            {

               Console.WriteLine(exception);
            }
        }

        private void OnAuctionCompleted(BasicEvent<AuctionSaga> message)
        {
            Console.WriteLine("Auction Completed " + message.Name + " for Auction: " + AuctionId);
        }

        public int AuctionComplete { get; set; }

        public static Event AuctionCompleted { get; set; }

        private void OnFinalBid(FinalBid message)
        {
            Console.WriteLine("Received FinalBid of $"+message.MaximumBid+" message for auction " + message.AuctionId);  

        }

        private void OnRetryBid(RetryBid message)
        {
            Console.WriteLine("Received RetryBid message for auction "+message.AuctionId);
        }

        private void OnAuctionFailed(AuctionFailed message)
        {
            Console.WriteLine("Received AuctionFailed message for auction " + message.AuctionId + " Marked as Faulted");
        }

        private void OnBid(PlaceBid message)
        {
            MaximumBid = message.MaximumBid;
            Console.WriteLine("Current State " + CurrentState.Name);
            Console.WriteLine("Received maximum bid " + message.MaximumBid);
        }

        private void OnCreate(CreateAuction message)
        {
            AuctionId = message.AuctionId;
            Title = message.Title;
            OpenBid = message.OpeningBid;
            Console.WriteLine(Title +" Auction Created with opening bid of $" + message.OpeningBid+" for Auction " +message.AuctionId);
            Console.WriteLine("Current State " + CurrentState.Name);
        }

        public AuctionSaga(Guid correlationId)
        {
            CorrelationId = correlationId;
        }

        
    }




Chris Patterson

unread,
Jul 20, 2015, 10:51:31 AM7/20/15
to masstrans...@googlegroups.com
It's highly recommend using Automatonymous, and not Magnum, for building a state machine.


--
You received this message because you are subscribed to the Google Groups "masstransit-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to masstransit-dis...@googlegroups.com.
To post to this group, send email to masstrans...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/masstransit-discuss/e2abc1ee-ffdf-4d9a-9c5f-66af2dfd4712%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Rupesh Kumar Tiwari

unread,
Jul 20, 2015, 3:15:09 PM7/20/15
to masstrans...@googlegroups.com

Jearsears-mee kalouré

unread,
Oct 31, 2015, 2:09:48 PM10/31/15
to masstransit-discuss
KALU.... 123 cosx² koné karimou 
×

Welcome to GlobalMarket.com [Join Free]or[Sign In]Buyer Protection|My GlobalMarket|Like|Language
GlobalMarket.com
ProductsGMC OrderManufacturersBuying Leads

Search Products
SearchPost Buying Lead Popular Searches: led ceiling light ,  Ice Cream Maker ,  led high bay ,  Ceiling Fan ,  Watch Phone ,  Desktop Computers ,  LED Panel Light , 
Home
 
Categories
GMC Center
Information
Service & Tools
About Us
GlobalMarket > GMC Center > Auditing Process
Global Manufacturer Certificate
The 8 GMC Benchmarks
Global GMC events, wherever you can join!
How to Get GMC Certificate?

Application
Manufacturers submit application form and GlobalMarket makes audit arrangement with TÜV, the leading provider of technical services worldwide.

On-site Auditing

Statement Document Inspection
Statement Document Inspection
Circumstance Qualification Testing
Circumstance Qualification Testing
Group Discussion
Group Discussion
Conclusion
TÜV Rheinland concludes and issues GMC Auditing Report according to applicant's actual condition.

Certificate Granting
GMC Certificate is granted by GlobalMarket to manufacturer who is conformed to the GMC criteria.

Click to check GMC events worldwide
Categories
Product Categories
Manufacturers Categories
Buying Leads
Featured Products
Featured Manufacturers
GMC Center
GMC Center
GMC Introduction
GMC Benchmarks
Auditing Process
Innovation China
Information
Promotional Products
Sourcing Tips
Exhibition Tour
Success Stories
Companies Directory
Service & Tools
GMC Order
GMC Like
Product Express
Sourcing Consultants
Sourcing Service
About Us
Company Introduction
Global Partners
Investor Relations
Press Center
Contact Us
Language Option: Português | Русский | 日本語 | 한국어 | Français | Español | Deutsch | اللغة العربية Mobile Site
Email Service | Career | Hot Searches | China Manufacturers | Site Map | Investor Relations | Nichemarket | GMC 中国站 GlobalMarket Group: GlobalMarket.com | Tradeeasy.com | B2S.com
Privacy Policy | Terms of Use | 粤ICP备09182444 粤B2-20080111 Copyright © GlobalMarket Group All Rights Reserved | View More 
dreams deals cuntreolds likeds sectreds dgrds segrezeards lifeds selts dealds .... cosx² 
Reply all
Reply to author
Forward
0 new messages