-
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.
updating transactional session package and adding test (#973)
- Loading branch information
1 parent
00af00c
commit b60a863
Showing
4 changed files
with
129 additions
and
5 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
124 changes: 124 additions & 0 deletions
124
...actionalSession.AcceptanceTests/When_using_transactional_session_with_transactionscope.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,124 @@ | ||
namespace NServiceBus.TransactionalSession.AcceptanceTests | ||
{ | ||
using System; | ||
using System.Data.SqlClient; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using System.Transactions; | ||
using AcceptanceTesting; | ||
using Infrastructure; | ||
using NUnit.Framework; | ||
using ObjectBuilder; | ||
|
||
public class When_using_transactional_session_with_transactionscope : NServiceBusAcceptanceTest | ||
{ | ||
[Test] | ||
public async Task Should_provide_ambient_transactionscope() | ||
{ | ||
string rowId = Guid.NewGuid().ToString(); | ||
|
||
await Scenario.Define<Context>() | ||
.WithEndpoint<AnEndpoint>(s => s.When(async (_, ctx) => | ||
{ | ||
using var scope = ctx.Builder.CreateChildBuilder(); | ||
using var transactionalSession = scope.Build<ITransactionalSession>(); | ||
await transactionalSession.Open(); | ||
|
||
await transactionalSession.SendLocal(new SampleMessage()); | ||
|
||
var storageSession = transactionalSession.SynchronizedStorageSession.Session(); | ||
|
||
string insertText = | ||
$@"IF NOT EXISTS (SELECT * FROM sysobjects WHERE name='SomeTable' and xtype='U') | ||
BEGIN | ||
CREATE TABLE [dbo].[SomeTable]([Id] [nvarchar](50) NOT NULL) | ||
END; | ||
INSERT INTO [dbo].[SomeTable] VALUES ('{rowId}')"; | ||
|
||
await storageSession.CreateSQLQuery(insertText).ExecuteUpdateAsync(CancellationToken.None); | ||
|
||
using (var __ = new TransactionScope(TransactionScopeOption.Suppress, | ||
TransactionScopeAsyncFlowOption.Enabled)) | ||
{ | ||
using var connection = new SqlConnection(TransactionSessionDefaultServer.ConnectionString); | ||
|
||
await connection.OpenAsync(); | ||
|
||
using var queryCommand = | ||
new SqlCommand( | ||
$"SELECT TOP 1 [Id] FROM [dbo].[SomeTable] WITH (READPAST) WHERE [Id]='{rowId}' ", | ||
connection); | ||
object result = await queryCommand.ExecuteScalarAsync(); | ||
|
||
Assert.AreEqual(null, result); | ||
} | ||
|
||
await transactionalSession.Commit().ConfigureAwait(false); | ||
})) | ||
.Done(c => c.MessageReceived) | ||
.Run(); | ||
|
||
using var connection = new SqlConnection(TransactionSessionDefaultServer.ConnectionString); | ||
|
||
await connection.OpenAsync(); | ||
|
||
using var queryCommand = | ||
new SqlCommand($"SELECT TOP 1 [Id] FROM [dbo].[SomeTable] WHERE [Id]='{rowId}'", connection); | ||
object result = await queryCommand.ExecuteScalarAsync(); | ||
|
||
Assert.AreEqual(rowId, result); | ||
} | ||
|
||
class Context : ScenarioContext, IInjectBuilder | ||
{ | ||
public bool MessageReceived { get; set; } | ||
public bool CompleteMessageReceived { get; set; } | ||
public IBuilder Builder { get; set; } | ||
} | ||
|
||
class AnEndpoint : EndpointConfigurationBuilder | ||
{ | ||
public AnEndpoint() => | ||
EndpointSetup<TransactionSessionWithOutboxEndpoint>(c => | ||
{ | ||
c.EnableOutbox().UseTransactionScope(); | ||
}); | ||
|
||
class SampleHandler : IHandleMessages<SampleMessage> | ||
{ | ||
public SampleHandler(Context testContext) => this.testContext = testContext; | ||
|
||
public Task Handle(SampleMessage message, IMessageHandlerContext context) | ||
{ | ||
testContext.MessageReceived = true; | ||
|
||
return Task.CompletedTask; | ||
} | ||
|
||
readonly Context testContext; | ||
} | ||
|
||
class CompleteTestMessageHandler : IHandleMessages<CompleteTestMessage> | ||
{ | ||
public CompleteTestMessageHandler(Context context) => testContext = context; | ||
|
||
public Task Handle(CompleteTestMessage message, IMessageHandlerContext context) | ||
{ | ||
testContext.CompleteMessageReceived = true; | ||
|
||
return Task.CompletedTask; | ||
} | ||
|
||
readonly Context testContext; | ||
} | ||
} | ||
|
||
class SampleMessage : ICommand | ||
{ | ||
} | ||
|
||
class CompleteTestMessage : ICommand | ||
{ | ||
} | ||
} | ||
} |
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