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?