Skip to content

Commit

Permalink
Merge pull request #75 from mauroservienti/enhance-timeout-reschedule…
Browse files Browse the repository at this point in the history
…-rules

Add current message to the timeout reschedule rule
  • Loading branch information
mauroservienti authored Jun 4, 2020
2 parents 040fe36 + 7d3e149 commit 0ac54d3
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/MySystem.AcceptanceTests/When_requesting_a_timeout.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public async Task It_should_be_rescheduled_and_handled()
{
var context = await Scenario.Define<IntegrationScenarioContext>(ctx =>
{
ctx.RegisterTimeoutRescheduleRule<ASaga.MyTimeout>(currentDelay =>
ctx.RegisterTimeoutRescheduleRule<ASaga.MyTimeout>((message, currentDelay) =>
{
return new DoNotDeliverBefore(DateTime.UtcNow.AddSeconds(5));
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public async Task Should_Reschedule_Timeouts()
var expectedDeliveryAt = new DateTime(2020, 1, 1);

var scenarioContext = new IntegrationScenarioContext();
scenarioContext.RegisterTimeoutRescheduleRule<AMessage>(currentDelay =>
scenarioContext.RegisterTimeoutRescheduleRule<AMessage>((msg, currentDelay) =>
{
return new DoNotDeliverBefore(expectedDeliveryAt);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class IntegrationScenarioContext : ScenarioContext
readonly ConcurrentBag<HandlerInvocation> invokedHandlers = new ConcurrentBag<HandlerInvocation>();
readonly ConcurrentBag<SagaInvocation> invokedSagas = new ConcurrentBag<SagaInvocation>();
readonly ConcurrentBag<OutgoingMessageOperation> outgoingMessageOperations = new ConcurrentBag<OutgoingMessageOperation>();
readonly Dictionary<Type, Func<DoNotDeliverBefore, DoNotDeliverBefore>> timeoutRescheduleRules = new Dictionary<Type, Func<DoNotDeliverBefore, DoNotDeliverBefore>>();
readonly Dictionary<Type, Func<object, DoNotDeliverBefore, DoNotDeliverBefore>> timeoutRescheduleRules = new Dictionary<Type, Func<object, DoNotDeliverBefore, DoNotDeliverBefore>>();

public IEnumerable<HandlerInvocation> InvokedHandlers { get { return invokedHandlers; } }
public IEnumerable<SagaInvocation> InvokedSagas { get { return invokedSagas; } }
Expand All @@ -26,7 +26,7 @@ internal HandlerInvocation CaptureInvokedHandler(HandlerInvocation invocation)
return invocation;
}

public void RegisterTimeoutRescheduleRule<TTimeout>(Func<DoNotDeliverBefore, DoNotDeliverBefore> rule)
public void RegisterTimeoutRescheduleRule<TTimeout>(Func<object, DoNotDeliverBefore, DoNotDeliverBefore> rule)
{
if (timeoutRescheduleRules.ContainsKey(typeof(TTimeout)))
{
Expand All @@ -36,7 +36,7 @@ public void RegisterTimeoutRescheduleRule<TTimeout>(Func<DoNotDeliverBefore, DoN
timeoutRescheduleRules.Add(typeof(TTimeout), rule);
}

internal bool TryGetTimeoutRescheduleRule(Type timeoutMessageType, out Func<DoNotDeliverBefore, DoNotDeliverBefore> rule)
internal bool TryGetTimeoutRescheduleRule(Type timeoutMessageType, out Func<object, DoNotDeliverBefore, DoNotDeliverBefore> rule)
{
return timeoutRescheduleRules.TryGetValue(timeoutMessageType, out rule);
}
Expand Down
11 changes: 7 additions & 4 deletions src/NServiceBus.IntegrationTesting/RescheduleTimeoutsBehavior.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,17 @@ public RescheduleTimeoutsBehavior(IntegrationScenarioContext integrationContext)

public override Task Invoke(IOutgoingSendContext context, Func<Task> next)
{
if (integrationContext.TryGetTimeoutRescheduleRule(context.Message.MessageType, out Func<DoNotDeliverBefore, DoNotDeliverBefore> rule))
if (integrationContext.TryGetTimeoutRescheduleRule(context.Message.MessageType, out Func<object, DoNotDeliverBefore, DoNotDeliverBefore> rule))
{
var constraints = context.Extensions.Get<List<DeliveryConstraint>>();
var doNotDeliverBefore = constraints.OfType<DoNotDeliverBefore>().SingleOrDefault();

var newDoNotDeliverBefore = rule(doNotDeliverBefore);
constraints.Remove(doNotDeliverBefore);
constraints.Add(newDoNotDeliverBefore);
var newDoNotDeliverBefore = rule(context.Message, doNotDeliverBefore);
if(newDoNotDeliverBefore != doNotDeliverBefore)
{
constraints.Remove(doNotDeliverBefore);
constraints.Add(newDoNotDeliverBefore);
}
}

return next();
Expand Down

0 comments on commit 0ac54d3

Please sign in to comment.