-
Notifications
You must be signed in to change notification settings - Fork 648
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 #5810 from Particular/7.3-pipeline-uow-ordering
NServiceBus 7.3 Pipeline UOW ordering fix
- Loading branch information
Showing
5 changed files
with
268 additions
and
1 deletion.
There are no files selected for viewing
83 changes: 83 additions & 0 deletions
83
...Bus.AcceptanceTests/Core/UnitOfWork/When_using_custom_unit_of_work_with_failed_message.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,83 @@ | ||
namespace NServiceBus.AcceptanceTests.UnitOfWork | ||
{ | ||
using System; | ||
using System.Threading.Tasks; | ||
using AcceptanceTesting; | ||
using EndpointTemplates; | ||
using NServiceBus.UnitOfWork; | ||
using NUnit.Framework; | ||
|
||
public class When_using_custom_unit_of_work_with_failed_message : NServiceBusAcceptanceTest | ||
{ | ||
[Test] | ||
public async Task Should_execute_uow_and_provide_exception_details() | ||
{ | ||
var context = await Scenario.Define<Context>() | ||
.WithEndpoint<EndpointWithCustomUnitOfWork>(g => | ||
{ | ||
g.DoNotFailOnErrorMessages(); | ||
g.When(b => b.SendLocal(new MyMessage())); | ||
}) | ||
.Done(c => c.BeginCalled && c.EndCalled) | ||
.Run(); | ||
|
||
Assert.True(context.BeginCalled, "Unit of work should have been executed"); | ||
Assert.True(context.EndCalled, "Unit of work should have been executed"); | ||
Assert.That(context.EndException, Is.InstanceOf<SimulatedException>().And.Message.Contain("Something went wrong"), "Exception was not provided but should have been"); | ||
} | ||
|
||
public class Context : ScenarioContext | ||
{ | ||
public bool Done { get; set; } | ||
public bool BeginCalled { get; set; } | ||
public bool EndCalled { get; set; } | ||
public Exception EndException { get; set; } | ||
} | ||
|
||
public class EndpointWithCustomUnitOfWork : EndpointConfigurationBuilder | ||
{ | ||
public EndpointWithCustomUnitOfWork() | ||
{ | ||
EndpointSetup<DefaultServer>((c, r) => | ||
{ | ||
c.RegisterComponents(container => container.ConfigureComponent<CustomUnitOfWork>(DependencyLifecycle.InstancePerCall)); | ||
}); | ||
} | ||
|
||
class CustomUnitOfWork : IManageUnitsOfWork | ||
{ | ||
public CustomUnitOfWork(Context testContext) | ||
{ | ||
this.testContext = testContext; | ||
} | ||
|
||
public Task Begin() | ||
{ | ||
testContext.BeginCalled = true; | ||
return Task.FromResult(0); | ||
} | ||
|
||
public Task End(Exception ex = null) | ||
{ | ||
testContext.EndCalled = true; | ||
testContext.EndException = ex; | ||
return Task.FromResult(0); | ||
} | ||
|
||
Context testContext; | ||
} | ||
|
||
class MyMessageHandler : IHandleMessages<MyMessage> | ||
{ | ||
public Task Handle(MyMessage message, IMessageHandlerContext context) | ||
{ | ||
throw new SimulatedException("Something went wrong"); | ||
} | ||
} | ||
} | ||
|
||
public class MyMessage : IMessage | ||
{ | ||
} | ||
} | ||
} |
88 changes: 88 additions & 0 deletions
88
...AcceptanceTests/Core/UnitOfWork/When_using_custom_unit_of_work_with_successful_message.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,88 @@ | ||
namespace NServiceBus.AcceptanceTests.UnitOfWork | ||
{ | ||
using System; | ||
using System.Threading.Tasks; | ||
using AcceptanceTesting; | ||
using EndpointTemplates; | ||
using NServiceBus.UnitOfWork; | ||
using NUnit.Framework; | ||
|
||
public class When_using_custom_unit_of_work_with_successful_message : NServiceBusAcceptanceTest | ||
{ | ||
[Test] | ||
public async Task Should_execute_uow() | ||
{ | ||
var context = await Scenario.Define<Context>() | ||
.WithEndpoint<EndpointWithCustomUnitOfWork>(g => g.When(b => b.SendLocal(new MyMessage()))) | ||
.Done(c => c.BeginCalled && c.EndCalled) | ||
.Run(); | ||
|
||
Assert.True(context.BeginCalled, "Unit of work should have been executed"); | ||
Assert.True(context.EndCalled, "Unit of work should have been executed"); | ||
Assert.IsNull(context.EndException, "Exception was provided to unit of work but should not have been"); | ||
} | ||
|
||
public class Context : ScenarioContext | ||
{ | ||
public bool Done { get; set; } | ||
public bool BeginCalled { get; set; } | ||
public bool EndCalled { get; set; } | ||
public Exception EndException { get; set; } | ||
} | ||
|
||
public class EndpointWithCustomUnitOfWork : EndpointConfigurationBuilder | ||
{ | ||
public EndpointWithCustomUnitOfWork() | ||
{ | ||
EndpointSetup<DefaultServer>((c, r) => | ||
{ | ||
c.RegisterComponents(container => container.ConfigureComponent<CustomUnitOfWork>(DependencyLifecycle.InstancePerCall)); | ||
}); | ||
} | ||
|
||
class CustomUnitOfWork : IManageUnitsOfWork | ||
{ | ||
public CustomUnitOfWork(Context testContext) | ||
{ | ||
this.testContext = testContext; | ||
} | ||
|
||
public Task Begin() | ||
{ | ||
testContext.BeginCalled = true; | ||
return Task.FromResult(0); | ||
} | ||
|
||
public Task End(Exception ex = null) | ||
{ | ||
testContext.EndCalled = true; | ||
testContext.EndException = ex; | ||
return Task.FromResult(0); | ||
} | ||
|
||
Context testContext; | ||
} | ||
|
||
class MyMessageHandler : IHandleMessages<MyMessage> | ||
{ | ||
public MyMessageHandler(Context testContext) | ||
{ | ||
this.testContext = testContext; | ||
} | ||
|
||
public Task Handle(MyMessage message, IMessageHandlerContext context) | ||
{ | ||
testContext.Done = true; | ||
|
||
return Task.FromResult(0); | ||
} | ||
|
||
Context testContext; | ||
} | ||
} | ||
|
||
public class MyMessage : IMessage | ||
{ | ||
} | ||
} | ||
} |
85 changes: 85 additions & 0 deletions
85
...ptanceTests/Core/UnitOfWork/When_using_custom_unit_of_work_with_wrap_handlers_in_scope.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,85 @@ | ||
namespace NServiceBus.AcceptanceTests.UnitOfWork | ||
{ | ||
using System; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using System.Transactions; | ||
using AcceptanceTesting; | ||
using EndpointTemplates; | ||
using NServiceBus.UnitOfWork; | ||
using NUnit.Framework; | ||
|
||
public class When_using_custom_unit_of_work_with_wrap_handlers_in_scope : NServiceBusAcceptanceTest | ||
{ | ||
[Test] | ||
public async Task Should_fail() | ||
{ | ||
var context = await Scenario.Define<Context>() | ||
.WithEndpoint<EndpointWithCustomUnitOfWork>(g => | ||
{ | ||
g.DoNotFailOnErrorMessages(); | ||
g.When(b => b.SendLocal(new MyMessage())); | ||
}) | ||
.Done(c => c.FailedMessages.Any()) | ||
.Run(); | ||
|
||
Assert.False(context.ShouldNeverBeCalled, "Unit of work should have been executed"); | ||
} | ||
|
||
public class Context : ScenarioContext | ||
{ | ||
public bool Done { get; set; } | ||
public bool ShouldNeverBeCalled { get; set; } | ||
} | ||
|
||
public class EndpointWithCustomUnitOfWork : EndpointConfigurationBuilder | ||
{ | ||
public EndpointWithCustomUnitOfWork() | ||
{ | ||
EndpointSetup<DefaultServer>((c, r) => | ||
{ | ||
c.UnitOfWork().WrapHandlersInATransactionScope(); | ||
c.RegisterComponents(container => container.ConfigureComponent<CustomUnitOfWork>(DependencyLifecycle.InstancePerCall)); | ||
}); | ||
} | ||
|
||
class CustomUnitOfWork : IManageUnitsOfWork | ||
{ | ||
TransactionScope transactionScope; | ||
public Task Begin() | ||
{ | ||
// this only works because we are not using the async state machine | ||
transactionScope = new TransactionScope(TransactionScopeOption.Required, TransactionScopeAsyncFlowOption.Enabled); | ||
return Task.FromResult(0); | ||
} | ||
|
||
public Task End(Exception ex = null) | ||
{ | ||
transactionScope.Complete(); | ||
return Task.FromResult(0); | ||
} | ||
} | ||
|
||
class MyMessageHandler : IHandleMessages<MyMessage> | ||
{ | ||
readonly Context testContext; | ||
|
||
public MyMessageHandler(Context testContext) | ||
{ | ||
this.testContext = testContext; | ||
} | ||
|
||
public Task Handle(MyMessage message, IMessageHandlerContext context) | ||
{ | ||
testContext.ShouldNeverBeCalled = true; | ||
return Task.FromResult(0); | ||
} | ||
} | ||
} | ||
|
||
public class MyMessage : IMessage | ||
{ | ||
} | ||
} | ||
} |
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