Skip to content

Commit

Permalink
updating transactional session package and adding test (#972)
Browse files Browse the repository at this point in the history
  • Loading branch information
tmasternak authored Apr 6, 2023
1 parent 5bb5ce0 commit 252c9e0
Show file tree
Hide file tree
Showing 4 changed files with 136 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net472;net6.0;net7.0</TargetFrameworks>
Expand All @@ -15,8 +15,8 @@
<PackageReference Include="System.Data.SqlClient" Version="4.8.3" />
<PackageReference Include="NUnit3TestAdapter" Version="4.2.1" />
<PackageReference Include="Nunit" Version="3.13.3" />
<PackageReference Include="NServiceBus.AcceptanceTesting" Version="8.0.0" />
<PackageReference Include="NServiceBus.TransactionalSession" Version="2.0.0" />
<PackageReference Include="NServiceBus.AcceptanceTesting" Version="8.0.3" />
<PackageReference Include="NServiceBus.TransactionalSession" Version="2.0.1" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
namespace NServiceBus.TransactionalSession.AcceptanceTests
{
using System;
using System.Data.SqlClient;
using System.Threading;
using System.Threading.Tasks;
using System.Transactions;
using AcceptanceTesting;
using AcceptanceTesting.Customization;
using Configuration.AdvancedExtensibility;
using Microsoft.Extensions.DependencyInjection;
using NServiceBus.Outbox;
using NServiceBus.Transport;
using NUnit.Framework;
using Persistence;
using IsolationLevel = System.Data.IsolationLevel;

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 IServiceScope scope = ctx.ServiceProvider.CreateScope();
using var transactionalSession = scope.ServiceProvider.GetRequiredService<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, IInjectServiceProvider
{
public bool MessageReceived { get; set; }
public bool CompleteMessageReceived { get; set; }
public IServiceProvider ServiceProvider { 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
{
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.3.1" />
<PackageReference Include="NUnit3TestAdapter" Version="4.2.1" />
<PackageReference Include="Nunit" Version="3.13.3" />
<PackageReference Include="NServiceBus.TransactionalSession" Version="2.0.0" />
<PackageReference Include="NServiceBus.TransactionalSession" Version="2.0.1" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net472;net6.0</TargetFrameworks>
Expand All @@ -11,8 +11,8 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Particular.Packaging" Version="2.2.0" PrivateAssets="All" />
<PackageReference Include="NServiceBus.TransactionalSession" Version="[2.0.0, 3.0.0)" />
<PackageReference Include="Particular.Packaging" Version="2.3.0" PrivateAssets="All" />
<PackageReference Include="NServiceBus.TransactionalSession" Version="[2.0.1, 3.0.0)" />
</ItemGroup>

<ItemGroup>
Expand Down

0 comments on commit 252c9e0

Please sign in to comment.