From 252c9e0b5158e6bcbfe4c0ab2cfa4ba2f8bda772 Mon Sep 17 00:00:00 2001 From: Tomasz Masternak Date: Thu, 6 Apr 2023 10:41:40 +0000 Subject: [PATCH] updating transactional session package and adding test (#972) --- ...ransactionalSession.AcceptanceTests.csproj | 6 +- ...sactional_session_with_transactionscope.cs | 129 ++++++++++++++++++ ...ibernate.TransactionalSession.Tests.csproj | 2 +- ...Bus.NHibernate.TransactionalSession.csproj | 6 +- 4 files changed, 136 insertions(+), 7 deletions(-) create mode 100644 src/NServiceBus.NHibernate.TransactionalSession.AcceptanceTests/When_using_transactional_session_with_transactionscope.cs diff --git a/src/NServiceBus.NHibernate.TransactionalSession.AcceptanceTests/NServiceBus.NHibernate.TransactionalSession.AcceptanceTests.csproj b/src/NServiceBus.NHibernate.TransactionalSession.AcceptanceTests/NServiceBus.NHibernate.TransactionalSession.AcceptanceTests.csproj index 2645fa2cb..948a42655 100644 --- a/src/NServiceBus.NHibernate.TransactionalSession.AcceptanceTests/NServiceBus.NHibernate.TransactionalSession.AcceptanceTests.csproj +++ b/src/NServiceBus.NHibernate.TransactionalSession.AcceptanceTests/NServiceBus.NHibernate.TransactionalSession.AcceptanceTests.csproj @@ -1,4 +1,4 @@ - + net472;net6.0;net7.0 @@ -15,8 +15,8 @@ - - + + diff --git a/src/NServiceBus.NHibernate.TransactionalSession.AcceptanceTests/When_using_transactional_session_with_transactionscope.cs b/src/NServiceBus.NHibernate.TransactionalSession.AcceptanceTests/When_using_transactional_session_with_transactionscope.cs new file mode 100644 index 000000000..957a65834 --- /dev/null +++ b/src/NServiceBus.NHibernate.TransactionalSession.AcceptanceTests/When_using_transactional_session_with_transactionscope.cs @@ -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() + .WithEndpoint(s => s.When(async (_, ctx) => + { + using IServiceScope scope = ctx.ServiceProvider.CreateScope(); + using var transactionalSession = scope.ServiceProvider.GetRequiredService(); + 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(c => + { + c.EnableOutbox().UseTransactionScope(); + }); + + class SampleHandler : IHandleMessages + { + 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 + { + 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 + { + } + } +} \ No newline at end of file diff --git a/src/NServiceBus.NHibernate.TransactionalSession.Tests/NServiceBus.NHibernate.TransactionalSession.Tests.csproj b/src/NServiceBus.NHibernate.TransactionalSession.Tests/NServiceBus.NHibernate.TransactionalSession.Tests.csproj index 2cc6e9266..90b3ab61b 100644 --- a/src/NServiceBus.NHibernate.TransactionalSession.Tests/NServiceBus.NHibernate.TransactionalSession.Tests.csproj +++ b/src/NServiceBus.NHibernate.TransactionalSession.Tests/NServiceBus.NHibernate.TransactionalSession.Tests.csproj @@ -14,7 +14,7 @@ - + diff --git a/src/NServiceBus.NHibernate.TransactionalSession/NServiceBus.NHibernate.TransactionalSession.csproj b/src/NServiceBus.NHibernate.TransactionalSession/NServiceBus.NHibernate.TransactionalSession.csproj index 4783b75b0..18a386bf5 100644 --- a/src/NServiceBus.NHibernate.TransactionalSession/NServiceBus.NHibernate.TransactionalSession.csproj +++ b/src/NServiceBus.NHibernate.TransactionalSession/NServiceBus.NHibernate.TransactionalSession.csproj @@ -1,4 +1,4 @@ - + net472;net6.0 @@ -11,8 +11,8 @@ - - + +