Skip to content

Commit

Permalink
new done condition
Browse files Browse the repository at this point in the history
  • Loading branch information
mauroservienti committed Jun 2, 2020
1 parent ac11491 commit c09d282
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 2 deletions.
12 changes: 10 additions & 2 deletions src/MyService/ASaga.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@ namespace MyService
{
public class ASaga : Saga<ASagaData>,
IAmStartedByMessages<StartASaga>,
IHandleMessages<CompleteASaga>
IHandleMessages<CompleteASaga>,
IHandleTimeouts<ASaga.MyTimeout>
{
public Task Handle(StartASaga message, IMessageHandlerContext context)
{
return Task.CompletedTask;
return RequestTimeout<MyTimeout>(context, DateTime.Now.AddDays(10));
}

public Task Handle(CompleteASaga message, IMessageHandlerContext context)
Expand All @@ -20,11 +21,18 @@ public Task Handle(CompleteASaga message, IMessageHandlerContext context)
return Task.CompletedTask;
}

public Task Timeout(MyTimeout state, IMessageHandlerContext context)
{
return Task.CompletedTask;
}

protected override void ConfigureHowToFindSaga(SagaPropertyMapper<ASagaData> mapper)
{
mapper.ConfigureMapping<StartASaga>(m => m.SomeId).ToSaga(s => s.SomeId);
mapper.ConfigureMapping<CompleteASaga>(m => m.SomeId).ToSaga(s => s.SomeId);
}

public class MyTimeout { }
}

public class ASagaData : ContainSagaData
Expand Down
59 changes: 59 additions & 0 deletions src/MySystem.AcceptanceTests/When_requesting_a_timeout.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
using MyMessages.Messages;
using MyService;
using NServiceBus;
using NServiceBus.AcceptanceTesting;
using NServiceBus.DelayedDelivery;
using NServiceBus.IntegrationTesting;
using NUnit.Framework;
using System;
using System.Linq;
using System.Threading.Tasks;

namespace MySystem.AcceptanceTests
{
public class When_requesting_a_timeout
{
[Test]
public async Task It_should_be_rescheduled_and_handled()
{
var theExpectedSagaId = Guid.NewGuid();
var context = await Scenario.Define<IntegrationScenarioContext>(ctx=>
{
ctx.RegisterTimeoutRescheduleRule<ASaga.MyTimeout>(currentDelay =>
{
return new DoNotDeliverBefore(DateTime.Now.AddSeconds(5));
});
})
.WithEndpoint<MyServiceEndpoint>(g =>
{
g.When(session => session.Send("MyService", new StartASaga() { SomeId = theExpectedSagaId }));
})
.Done(c =>
{
return
(
c.SagaHandledMessage<ASaga, ASaga.MyTimeout>()
)
|| c.HasFailedMessages();
})
.Run();

var invokedSagas = context.InvokedSagas.Where(s => s.SagaType == typeof(ASaga));
var newSaga = invokedSagas.SingleOrDefault(s => s.IsNew);
var completedSaga = invokedSagas.SingleOrDefault(s => s.IsCompleted);

Assert.IsNotNull(newSaga);
Assert.IsNotNull(completedSaga);
Assert.False(context.HasFailedMessages());
Assert.False(context.HasHandlingErrors());
}

class MyServiceEndpoint : EndpointConfigurationBuilder
{
public MyServiceEndpoint()
{
EndpointSetup<EndpointTemplate<MyServiceConfiguration>>();
}
}
}
}

0 comments on commit c09d282

Please sign in to comment.