Skip to content

Commit 557049d

Browse files
committed
Execute test LogsAreNotAffectedByTransactionsByDefault in a separate run from all other tests to avoid side effects with other DB tests. Ugly but the only feasible way to get this stable.
1 parent 2b0c9c8 commit 557049d

File tree

3 files changed

+60
-1
lines changed

3 files changed

+60
-1
lines changed

Build.ps1

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,18 @@ if ($SkipTests -eq $false) {
5454

5555
echo "build: Testing project in $test"
5656

57-
& dotnet test -c Release
57+
& dotnet test -c Release --filter "Category!=Isolated"
58+
if($LASTEXITCODE -ne 0) { exit 3 }
59+
60+
Pop-Location
61+
}
62+
63+
foreach ($test in ls test/*.Tests) {
64+
Push-Location $test
65+
66+
echo "build: Running isolated tests in $test"
67+
68+
& dotnet test -c Release --filter "Category=Isolated"
5869
if($LASTEXITCODE -ne 0) { exit 3 }
5970

6071
Pop-Location

test/Serilog.Sinks.MSSqlServer.Tests/TestUtils/TestCategory.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ public static class TestCategory
55
public const string TraitName = "Category";
66

77
public const string Integration = nameof(Integration);
8+
public const string Isolated = nameof(Isolated);
89
public const string Unit = nameof(Unit);
910
}
1011
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
using System.Transactions;
2+
using FluentAssertions;
3+
using Serilog.Sinks.MSSqlServer.Tests.TestUtils;
4+
using Xunit;
5+
using Xunit.Abstractions;
6+
7+
namespace Serilog.Sinks.MSSqlServer.Tests
8+
{
9+
[Trait(TestCategory.TraitName, TestCategory.Isolated)]
10+
public class TransactionTests : DatabaseTestsBase
11+
{
12+
public TransactionTests(ITestOutputHelper output) : base(output)
13+
{
14+
}
15+
16+
[Fact]
17+
public void LogsAreNotAffectedByTransactionsByDefault()
18+
{
19+
// Arrange
20+
Log.Logger = new LoggerConfiguration()
21+
.WriteTo.MSSqlServer
22+
(
23+
connectionString: DatabaseFixture.LogEventsConnectionString,
24+
new MSSqlServerSinkOptions
25+
{
26+
TableName = DatabaseFixture.LogTableName,
27+
AutoCreateSqlTable = true
28+
}
29+
)
30+
.CreateLogger();
31+
32+
using (var scope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled))
33+
{
34+
// Act
35+
Log.Logger.Information("Logging message");
36+
37+
// Flush message so it is written on foreground thread instead of timer
38+
// So we can test if it is affected by transaction
39+
Log.CloseAndFlush();
40+
}
41+
42+
// Assert after rollback, the message should still be persisted
43+
VerifyCustomQuery<LogEventColumn>($"SELECT Id from {DatabaseFixture.LogTableName}",
44+
e => e.Should().HaveCount(1));
45+
}
46+
}
47+
}

0 commit comments

Comments
 (0)