Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow snapshot isolation to be used for the outbox #1643

Merged
merged 16 commits into from
Dec 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ jobs:
with:
connection-string-env-var: SQLServerConnectionString
catalog: nservicebus
extra-params: "Encrypt=False;"
extra-params: "Encrypt=False;"
- name: Setup PostgreSql 15.x
if: matrix.engine == 'PostgreSql'
uses: Particular/setup-postgres-action@v1.0.0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,47 +9,42 @@
[TestFixture]
public class When_using_outbox_with_transaction_scope : NServiceBusAcceptanceTest
{
[Test]
public async Task Should_float_transaction_scope_into_handler()
[TestCase(IsolationLevel.ReadCommitted)]
[TestCase(IsolationLevel.Serializable)]
Comment on lines +12 to +13
Copy link
Member Author

@andreasohlund andreasohlund Dec 5, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ReadCommited and Serializable are the isolation levels that we allow that all databases can handle

public async Task Should_float_transaction_scope_into_handler(IsolationLevel isolationLevel)
{
var context = await Scenario.Define<Context>()
.WithEndpoint<Endpoint>(b => b.When(s => s.SendLocal(new MyMessage())))
.WithEndpoint<Endpoint>(b => b.When(s => s.SendLocal(new MyMessage()))
.CustomConfig(c => c.EnableOutbox().UseTransactionScope(isolationLevel)))
.Done(c => c.Done)
.Run();

Assert.That(context.Transaction, Is.Not.Null);
Assert.That(context.Transaction, Is.Not.Null, "Ambient transaction should be available in handler");
Assert.That(context.IsolationLevel, Is.EqualTo(isolationLevel), "IsolationLevel should be honored");
}

public class Context : ScenarioContext
{
public bool Done { get; set; }
public Transaction Transaction { get; set; }
public IsolationLevel IsolationLevel { get; set; }
}

public class Endpoint : EndpointConfigurationBuilder
{
public Endpoint()
{
EndpointSetup<DefaultServer>(c =>
{
c.ConfigureTransport().TransportTransactionMode = TransportTransactionMode.ReceiveOnly;
c.EnableOutbox().UseTransactionScope();
});
}
public Endpoint() =>
EndpointSetup<DefaultServer>(c => c.ConfigureTransport().TransportTransactionMode = TransportTransactionMode.ReceiveOnly);

public class MyMessageHandler : IHandleMessages<MyMessage>
public class MyMessageHandler(Context context) : IHandleMessages<MyMessage>
{
Context context;

public MyMessageHandler(Context context)
{
this.context = context;
}


public Task Handle(MyMessage message, IMessageHandlerContext handlerContext)
{
context.Transaction = Transaction.Current;
if (Transaction.Current != null)
{
context.IsolationLevel = Transaction.Current.IsolationLevel;
}

context.Done = true;
return Task.CompletedTask;
}
Expand All @@ -58,7 +53,5 @@ public Task Handle(MyMessage message, IMessageHandlerContext handlerContext)

public class MyMessage : IMessage
{
public string Property { get; set; }
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ public void SetUp()
{
MsSqlMicrosoftDataClientConnectionBuilder.DropDbIfCollationIncorrect();
MsSqlMicrosoftDataClientConnectionBuilder.CreateDbIfNotExists();
MsSqlMicrosoftDataClientConnectionBuilder.EnableSnapshotIsolation();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
namespace NServiceBus.AcceptanceTests;

using System.Data.Common;
using System.Threading.Tasks;
using System.Transactions;
using Microsoft.Data.SqlClient;
using NServiceBus;
using NServiceBus.AcceptanceTesting;
using NServiceBus.AcceptanceTests.EndpointTemplates;
using NUnit.Framework;
using Persistence.Sql;
using IsolationLevel = System.Data.IsolationLevel;

[TestFixture]
public class When_outbox_in_ado_mode : NServiceBusAcceptanceTest
andreasohlund marked this conversation as resolved.
Show resolved Hide resolved
{
[Test]
public async Task Should_work_with_snapshot_isolation()
{
var context = await Scenario.Define<Context>()
.WithEndpoint<Endpoint>(b => b.When(s => s.SendLocal(new MyMessage()))
.CustomConfig(c => c.EnableOutbox().TransactionIsolationLevel(IsolationLevel.Snapshot)))
.Done(c => c.Done)
.Run();

Assert.That(context.Transaction, Is.Not.Null, "Transaction should be available in handler");
Assert.That(context.IsolationLevel, Is.EqualTo(IsolationLevel.Snapshot), "IsolationLevel should be honored");
}

public class Context : ScenarioContext
{
public bool Done { get; set; }
public DbTransaction Transaction { get; set; }
public IsolationLevel IsolationLevel { get; set; }
}

public class Endpoint : EndpointConfigurationBuilder
{
public Endpoint() =>
EndpointSetup<DefaultServer>(c => c.ConfigureTransport().TransportTransactionMode = TransportTransactionMode.ReceiveOnly);

public class MyMessageHandler(Context context, ISqlStorageSession storageSession) : IHandleMessages<MyMessage>
{
public Task Handle(MyMessage message, IMessageHandlerContext handlerContext)
{
context.Transaction = storageSession.Transaction;
context.IsolationLevel = storageSession.Transaction.IsolationLevel;
context.Done = true;
return Task.CompletedTask;
}
}
}

public class MyMessage : IMessage
{
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
namespace NServiceBus.AcceptanceTests;

using System.Threading.Tasks;
using System.Transactions;
using NServiceBus;
using NServiceBus.AcceptanceTesting;
using NServiceBus.AcceptanceTests.EndpointTemplates;
using NUnit.Framework;

[TestFixture]
public class When_outbox_in_transaction_scope_mode : NServiceBusAcceptanceTest
{
[Test]
public async Task Should_work_with_snapshot_isolation()
{
var context = await Scenario.Define<Context>()
.WithEndpoint<Endpoint>(b => b.When(s => s.SendLocal(new MyMessage()))
.CustomConfig(c => c.EnableOutbox().UseTransactionScope(IsolationLevel.Snapshot)))
.Done(c => c.Done)
.Run();

Assert.That(context.Transaction, Is.Not.Null, "Ambient transaction should be available in handler");
Assert.That(context.IsolationLevel, Is.EqualTo(IsolationLevel.Snapshot), "IsolationLevel should be honored");
}

public class Context : ScenarioContext
{
public bool Done { get; set; }
public Transaction Transaction { get; set; }
public IsolationLevel IsolationLevel { get; set; }
}

public class Endpoint : EndpointConfigurationBuilder
{
public Endpoint() =>
EndpointSetup<DefaultServer>(c => c.ConfigureTransport().TransportTransactionMode = TransportTransactionMode.ReceiveOnly);

public class MyMessageHandler(Context context) : IHandleMessages<MyMessage>
{
public Task Handle(MyMessage message, IMessageHandlerContext handlerContext)
{
context.Transaction = Transaction.Current;
if (Transaction.Current != null)
{
context.IsolationLevel = Transaction.Current.IsolationLevel;
}

context.Done = true;
return Task.CompletedTask;
}
}
}

public class MyMessage : IMessage
{
}
}
Loading