generated from mauroservienti/code-repo-template
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #64 from mauroservienti/timeouts-reschedule-support
Timeouts reschedule support
- Loading branch information
Showing
8 changed files
with
180 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
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 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 = Guid.NewGuid() }))) | ||
.Done(c => c.MessageWasProcessedBySaga<ASaga.MyTimeout, ASaga>() || c.HasFailedMessages()) | ||
.Run(); | ||
|
||
Assert.True(context.MessageWasProcessedBySaga<ASaga.MyTimeout, ASaga>()); | ||
Assert.False(context.HasFailedMessages()); | ||
Assert.False(context.HasHandlingErrors()); | ||
} | ||
|
||
class MyServiceEndpoint : EndpointConfigurationBuilder | ||
{ | ||
public MyServiceEndpoint() | ||
{ | ||
EndpointSetup<EndpointTemplate<MyServiceConfiguration>>(); | ||
} | ||
} | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
src/NServiceBus.IntegrationTesting.Tests/Reschedule_Timeout_Behavior.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
using MyMessages.Messages; | ||
using NServiceBus.DelayedDelivery; | ||
using NServiceBus.DeliveryConstraints; | ||
using NServiceBus.Pipeline; | ||
using NServiceBus.Testing; | ||
using NUnit.Framework; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
|
||
namespace NServiceBus.IntegrationTesting.Tests | ||
{ | ||
public class Reschedule_Timeout_Behavior | ||
{ | ||
[Test] | ||
public async Task Should_Reschedule_Timeouts() | ||
{ | ||
var expectedDeliveryAt = new DateTime(2020, 1, 1); | ||
|
||
var scenarioContext = new IntegrationScenarioContext(); | ||
scenarioContext.RegisterTimeoutRescheduleRule<AMessage>(currentDelay => | ||
{ | ||
return new DoNotDeliverBefore(expectedDeliveryAt); | ||
}); | ||
|
||
var context = new TestableOutgoingSendContext | ||
{ | ||
Message = new OutgoingLogicalMessage(typeof(AMessage), new AMessage()) | ||
}; | ||
context.Headers.Add(Headers.SagaId, "a-saga-id"); | ||
context.Headers.Add(Headers.SagaType, "a-saga-type"); | ||
context.Headers.Add(Headers.IsSagaTimeoutMessage, bool.TrueString); | ||
|
||
var constraints = new List<DeliveryConstraint> | ||
{ | ||
new DoNotDeliverBefore(new DateTime(2030, 1, 1)) | ||
}; | ||
context.Extensions.Set(constraints); | ||
|
||
var sut = new RescheduleTimeoutsBehavior(scenarioContext); ; | ||
await sut.Invoke(context, () => Task.CompletedTask).ConfigureAwait(false); | ||
|
||
var rescheduledDoNotDeliverBefore = constraints.OfType<DoNotDeliverBefore>().Single(); | ||
Assert.AreEqual(expectedDeliveryAt, rescheduledDoNotDeliverBefore.At); | ||
} | ||
} | ||
} |
3 changes: 3 additions & 0 deletions
3
src/NServiceBus.IntegrationTesting.Tests/Send_Operation_Interceptor.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
src/NServiceBus.IntegrationTesting/InterceptPhysicalDispatch.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
using NServiceBus.DelayedDelivery; | ||
using NServiceBus.DeliveryConstraints; | ||
using NServiceBus.Pipeline; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
|
||
namespace NServiceBus.IntegrationTesting | ||
{ | ||
class RescheduleTimeoutsBehavior : Behavior<IOutgoingSendContext> | ||
{ | ||
readonly IntegrationScenarioContext integrationContext; | ||
|
||
public RescheduleTimeoutsBehavior(IntegrationScenarioContext integrationContext) | ||
{ | ||
this.integrationContext = integrationContext; | ||
} | ||
|
||
public override Task Invoke(IOutgoingSendContext context, Func<Task> next) | ||
{ | ||
if (integrationContext.TryGetTimeoutRescheduleRule(context.Message.MessageType, out Func<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); | ||
} | ||
|
||
return next(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters