-
Notifications
You must be signed in to change notification settings - Fork 32
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 #468 from Particular/outbox-txscope-fix
Moving Outbox TransactionScope fix to the release branch
- Loading branch information
Showing
8 changed files
with
191 additions
and
13 deletions.
There are no files selected for viewing
62 changes: 62 additions & 0 deletions
62
src/NServiceBus.NHibernate.AcceptanceTests/When_using_outbox_with_transaction_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,62 @@ | ||
namespace NServiceBus.AcceptanceTests.Outbox | ||
{ | ||
using System.Threading.Tasks; | ||
using System.Transactions; | ||
using AcceptanceTesting; | ||
using EndpointTemplates; | ||
using NUnit.Framework; | ||
|
||
[TestFixture] | ||
public class When_using_outbox_with_transaction_scope : NServiceBusAcceptanceTest | ||
{ | ||
[Test] | ||
public async Task Should_float_transaction_scope_into_handler() | ||
{ | ||
var context = await Scenario.Define<Context>() | ||
.WithEndpoint<Endpoint>(b => b.When(s => s.SendLocal(new MyMessage()))) | ||
.Done(c => c.Done) | ||
.Run() | ||
.ConfigureAwait(false); | ||
|
||
Assert.NotNull(context.Transaction); | ||
} | ||
|
||
public class Context : ScenarioContext | ||
{ | ||
public bool Done { get; set; } | ||
public Transaction Transaction { get; set; } | ||
} | ||
|
||
public class Endpoint : EndpointConfigurationBuilder | ||
{ | ||
public Endpoint() | ||
{ | ||
EndpointSetup<DefaultServer>(c => { c.EnableOutbox().UseTransactionScope(); }); | ||
} | ||
|
||
public class MyMessageHandler : IHandleMessages<MyMessage> | ||
{ | ||
Context context; | ||
|
||
public MyMessageHandler(Context context) | ||
{ | ||
this.context = context; | ||
} | ||
|
||
|
||
public Task Handle(MyMessage message, IMessageHandlerContext handlerContext) | ||
{ | ||
context.Transaction = Transaction.Current; | ||
context.Done = true; | ||
return Task.CompletedTask; | ||
} | ||
} | ||
} | ||
|
||
public class MyMessage : IMessage | ||
{ | ||
public string Property { get; set; } | ||
} | ||
|
||
} | ||
} |
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
79 changes: 79 additions & 0 deletions
79
src/NServiceBus.NHibernate.Tests/Outbox/When_using_transaction_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,79 @@ | ||
namespace NServiceBus.NHibernate.Tests.Outbox | ||
{ | ||
using System; | ||
using System.Threading.Tasks; | ||
using global::NHibernate; | ||
using global::NHibernate.Cfg; | ||
using global::NHibernate.Mapping.ByCode; | ||
using global::NHibernate.Tool.hbm2ddl; | ||
using Extensibility; | ||
using global::NHibernate.Dialect; | ||
using NHibernate.Outbox; | ||
using NServiceBus.Outbox.NHibernate; | ||
using NUnit.Framework; | ||
|
||
[TestFixture(false)] | ||
[TestFixture(true)] | ||
class When_using_transaction_scope | ||
{ | ||
bool pessimistic; | ||
INHibernateOutboxStorage persister; | ||
ISessionFactory sessionFactory; | ||
SchemaExport schema; | ||
OutboxPersisterFactory<OutboxRecord> outboxPersisterFactory; | ||
|
||
public When_using_transaction_scope(bool pessimistic) | ||
{ | ||
this.pessimistic = pessimistic; | ||
} | ||
|
||
[SetUp] | ||
public async Task Setup() | ||
{ | ||
var mapper = new ModelMapper(); | ||
mapper.AddMapping(typeof(OutboxRecordMapping)); | ||
|
||
var cfg = new Configuration() | ||
.DataBaseIntegration(x => | ||
{ | ||
x.Dialect<MsSql2012Dialect>(); | ||
x.ConnectionString = Consts.SqlConnectionString; | ||
}); | ||
|
||
cfg.AddMapping(mapper.CompileMappingForAllExplicitlyAddedEntities()); | ||
|
||
schema = new SchemaExport(cfg); | ||
await schema.DropAsync(false, true); | ||
await schema.CreateAsync(false, true); | ||
|
||
sessionFactory = cfg.BuildSessionFactory(); | ||
outboxPersisterFactory = new OutboxPersisterFactory<OutboxRecord>(); | ||
persister = outboxPersisterFactory.Create(sessionFactory, "TestEndpoint", pessimistic, true); | ||
} | ||
|
||
[TearDown] | ||
public async Task TearDown() | ||
{ | ||
await sessionFactory.CloseAsync(); | ||
await schema.DropAsync(false, true); | ||
} | ||
|
||
[Test] | ||
public async Task Should_allow_the_transaction_to_flow_to_handling_code() | ||
{ | ||
var messageId = Guid.NewGuid().ToString("N"); | ||
|
||
var contextBag = new ContextBag(); | ||
await persister.Get(messageId, contextBag); | ||
|
||
using (var transaction = await persister.BeginTransaction(contextBag)) | ||
{ | ||
var ambientTransaction = System.Transactions.Transaction.Current; | ||
|
||
Assert.IsNotNull(ambientTransaction); | ||
|
||
await transaction.Commit(); | ||
} | ||
} | ||
} | ||
} |
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
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