-
Notifications
You must be signed in to change notification settings - Fork 652
/
When_using_custom_unit_of_work_with_wrap_handlers_in_scope.cs
85 lines (74 loc) · 2.75 KB
/
When_using_custom_unit_of_work_with_wrap_handlers_in_scope.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
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
{
}
}
}