-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
upgrading to latest transactional session testing Transaction.Current…
… setup (#1176) (#1178) Co-authored-by: Szymon Pobiega <[email protected]> Co-authored-by: Daniel Marbach <[email protected]> # Conflicts: # src/NServiceBus.Persistence.Sql.TransactionalSession/NServiceBus.Persistence.Sql.TransactionalSession.csproj # src/TransactionalSession.MsSqlMicrosoftDataClient.AcceptanceTests/TransactionalSession.MsSqlMicrosoftDataClient.AcceptanceTests.csproj # src/TransactionalSession.MsSqlSystemDataClient.AcceptenceTests/TransactionalSession.MsSqlSystemDataClient.AcceptenceTests.csproj # src/TransactionalSession.MsSqlSystemDataClient.AcceptenceTests/When_using_transactional_session_with_transactionscope.cs Co-authored-by: Tomasz Masternak <[email protected]>
- Loading branch information
1 parent
1706723
commit 13413f3
Showing
6 changed files
with
296 additions
and
16 deletions.
There are no files selected for viewing
26 changes: 13 additions & 13 deletions
26
...sistence.Sql.TransactionalSession/NServiceBus.Persistence.Sql.TransactionalSession.csproj
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 |
---|---|---|
@@ -1,22 +1,22 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFrameworks>net461;netstandard2.0</TargetFrameworks> | ||
<SignAssembly>true</SignAssembly> | ||
<AssemblyOriginatorKeyFile>$(SolutionDir)NServiceBus.snk</AssemblyOriginatorKeyFile> | ||
<LangVersion>10.0</LangVersion> | ||
<!-- We want the root namespace to match the transactional session one --> | ||
<RootNamespace>NServiceBus.TransactionalSession</RootNamespace> | ||
<Description>NServiceBus Transactional Session for SQL persistence.</Description> | ||
</PropertyGroup> | ||
<PropertyGroup> | ||
<TargetFrameworks>net461;netstandard2.0</TargetFrameworks> | ||
<SignAssembly>true</SignAssembly> | ||
<AssemblyOriginatorKeyFile>$(SolutionDir)NServiceBus.snk</AssemblyOriginatorKeyFile> | ||
<LangVersion>10.0</LangVersion> | ||
<!-- We want the root namespace to match the transactional session one --> | ||
<RootNamespace>NServiceBus.TransactionalSession</RootNamespace> | ||
<Description>NServiceBus Transactional Session for SQL persistence.</Description> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Particular.Packaging" Version="2.2.0" PrivateAssets="All" /> | ||
<PackageReference Include="NServiceBus.TransactionalSession" Version="[1.0.0, 2)" /> | ||
<ProjectReference Include="..\SqlPersistence\SqlPersistence.csproj" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\SqlPersistence\SqlPersistence.csproj" /> | ||
</ItemGroup> | ||
<PackageReference Include="Particular.Packaging" Version="2.3.0" PrivateAssets="All" /> | ||
<PackageReference Include="NServiceBus.TransactionalSession" Version="[1.0.1, 2.0.0)" /> | ||
</ItemGroup> | ||
|
||
</Project> |
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
140 changes: 140 additions & 0 deletions
140
...osoftDataClient.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,140 @@ | ||
namespace NServiceBus.TransactionalSession.AcceptanceTests | ||
{ | ||
using System; | ||
using System.Threading.Tasks; | ||
using System.Transactions; | ||
using AcceptanceTesting; | ||
using AcceptanceTesting.Customization; | ||
using Microsoft.Data.SqlClient; | ||
using NUnit.Framework; | ||
using ObjectBuilder; | ||
using Persistence.Sql.ScriptBuilder; | ||
|
||
public class When_using_transactional_session_with_transactionscope : NServiceBusAcceptanceTest | ||
{ | ||
[OneTimeSetUp] | ||
public void OneTimeSetup() | ||
{ | ||
MsSqlMicrosoftDataClientConnectionBuilder.DropDbIfCollationIncorrect(); | ||
MsSqlMicrosoftDataClientConnectionBuilder.CreateDbIfNotExists(); | ||
} | ||
|
||
[Test] | ||
public async Task Should_provide_ambient_transactionscope() | ||
{ | ||
await CreateOutboxTable(Conventions.EndpointNamingConvention(typeof(AnEndpoint))); | ||
|
||
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>(); | ||
var sessionOptions = new SqlPersistenceOpenSessionOptions(); | ||
await transactionalSession.Open(sessionOptions); | ||
|
||
await transactionalSession.SendLocal(new SampleMessage()); | ||
|
||
var storageSession = transactionalSession.SynchronizedStorageSession.SqlPersistenceSession(); | ||
|
||
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}')"; | ||
|
||
using (var insertCommand = new SqlCommand(insertText, (SqlConnection)storageSession.Connection)) | ||
{ | ||
await insertCommand.ExecuteNonQueryAsync(); | ||
} | ||
|
||
using (var __ = new TransactionScope(TransactionScopeOption.Suppress, TransactionScopeAsyncFlowOption.Enabled)) | ||
{ | ||
using var connection = MsSqlMicrosoftDataClientConnectionBuilder.Build(); | ||
|
||
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 = MsSqlMicrosoftDataClientConnectionBuilder.Build(); | ||
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); | ||
} | ||
|
||
|
||
static async Task CreateOutboxTable(string endpointName) | ||
{ | ||
string tablePrefix = endpointName.Replace('.', '_'); | ||
using var connection = MsSqlMicrosoftDataClientConnectionBuilder.Build(); | ||
await connection.OpenAsync().ConfigureAwait(false); | ||
|
||
connection.ExecuteCommand(OutboxScriptBuilder.BuildDropScript(BuildSqlDialect.MsSqlServer), tablePrefix); | ||
connection.ExecuteCommand(OutboxScriptBuilder.BuildCreateScript(BuildSqlDialect.MsSqlServer), tablePrefix); | ||
} | ||
|
||
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
140 changes: 140 additions & 0 deletions
140
...ystemDataClient.AcceptenceTests/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,140 @@ | ||
namespace NServiceBus.TransactionalSession.AcceptanceTests | ||
{ | ||
using System; | ||
using System.Threading.Tasks; | ||
using System.Transactions; | ||
using AcceptanceTesting; | ||
using AcceptanceTesting.Customization; | ||
using System.Data.SqlClient; | ||
using NUnit.Framework; | ||
using ObjectBuilder; | ||
using Persistence.Sql.ScriptBuilder; | ||
|
||
public class When_using_transactional_session_with_transactionscope : NServiceBusAcceptanceTest | ||
{ | ||
[OneTimeSetUp] | ||
public void OneTimeSetup() | ||
{ | ||
MsSqlSystemDataClientConnectionBuilder.DropDbIfCollationIncorrect(); | ||
MsSqlSystemDataClientConnectionBuilder.CreateDbIfNotExists(); | ||
} | ||
|
||
[Test] | ||
public async Task Should_provide_ambient_transactionscope() | ||
{ | ||
await CreateOutboxTable(Conventions.EndpointNamingConvention(typeof(AnEndpoint))); | ||
|
||
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>(); | ||
var sessionOptions = new SqlPersistenceOpenSessionOptions(); | ||
await transactionalSession.Open(sessionOptions); | ||
|
||
await transactionalSession.SendLocal(new SampleMessage()); | ||
|
||
var storageSession = transactionalSession.SynchronizedStorageSession.SqlPersistenceSession(); | ||
|
||
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}')"; | ||
|
||
using (var insertCommand = new SqlCommand(insertText, (SqlConnection)storageSession.Connection)) | ||
{ | ||
await insertCommand.ExecuteNonQueryAsync(); | ||
} | ||
|
||
using (var __ = new TransactionScope(TransactionScopeOption.Suppress, TransactionScopeAsyncFlowOption.Enabled)) | ||
{ | ||
using var connection = MsSqlSystemDataClientConnectionBuilder.Build(); | ||
|
||
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 = MsSqlSystemDataClientConnectionBuilder.Build(); | ||
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); | ||
} | ||
|
||
|
||
static async Task CreateOutboxTable(string endpointName) | ||
{ | ||
string tablePrefix = endpointName.Replace('.', '_'); | ||
using var connection = MsSqlSystemDataClientConnectionBuilder.Build(); | ||
await connection.OpenAsync().ConfigureAwait(false); | ||
|
||
connection.ExecuteCommand(OutboxScriptBuilder.BuildDropScript(BuildSqlDialect.MsSqlServer), tablePrefix); | ||
connection.ExecuteCommand(OutboxScriptBuilder.BuildCreateScript(BuildSqlDialect.MsSqlServer), tablePrefix); | ||
} | ||
|
||
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