Then() vs ThenAsync() methods. MassTransit unit testing.

488 views
Skip to first unread message

Andrii Kuznietsov

unread,
Apr 26, 2016, 10:20:53 AM4/26/16
to masstransit-discuss
Hello, guys.
I have a question about Then() and ThenAsync() methods.
In my current project I use MassTransit with  Automatonymous sagas.
Currently I'm working on unit testing of saga's workflow.
I wrote a short test based on examples from MassTransit source code. I spent a lot of time debugging it because my test didn't work.
While I was debugging it I mentioned that saga's CurrentState changed only after test failed (After disposing test environment I suppose).
I have the following workflow:
 
 Initially(
               
When(CalculateRequestReceived)
                   
.Then(InitSagaInstance)
                   
.Then(x => _log.InfoEvent(context, "START: Calculation request received " + RequestEventToLogString(context.Data)))
                   
.Respond(SendCalculationCommand)
                   
.TransitionTo(PendingCalculation)...

After a few days of debugging I commented line with logger call and test passed.
  .Then(x => _log.InfoEvent(context, "START: Calculation request received " + RequestEventToLogString(context.Data)))

My test method is:
       
        [Fact]
       
public async Task Should_handle_the_initial_state()
       
{
           
Guid sagaId = Guid.NewGuid();
           
var message = new CalculateRequestEvent(sagaId);


            await
InputQueueSendEndpoint.Send(message);
         
           
Guid? saga = await _repository.ShouldContainSaga(
                        x
=> x.CorrelationId == sagaId && Equals(x.CurrentState, _machine.PendingCalculation.Name), TimeSpan.FromSeconds(20));


            saga
.Should().HaveValue();
         
}


I was surprised with this.
After that I replaced this line by following and test also passed
                   
.ThenAsync(context => Task.Run(() => _log.InfoEvent(context, "START: Calculation request received " + RequestEventToLogString(context.Data))))

I use Common.Logging in my project and InMemoryTestFixture and InMemorySagaRepository for testing purpose.

So, I want to ask you what is the difference in MassTransit(Automatonymous) behavior when I use these methods?
Why the saga doesn't change its state in first case?
Should I worry about the order of log messages? 

Chris Patterson

unread,
May 12, 2016, 9:40:02 AM5/12/16
to masstrans...@googlegroups.com
The saga doesn't change state until after the TransitionTo activity is completed, so anything before that is not going to have the new state. So if your log message prior to the transition will show the Initial state.

--
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/6c735559-3ca7-4239-8af3-bd30a81f64d3%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages