Skip to content

Commit 415916e

Browse files
committed
SQLite deadlock test
1 parent 7b7ca3d commit 415916e

File tree

4 files changed

+21
-7
lines changed

4 files changed

+21
-7
lines changed

DbExceptionClassifier/Common/IDbExceptionClassifier.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@ public interface IDbExceptionClassifier
99
public bool IsNumericOverflowError(DbException exception);
1010
public bool IsUniqueConstraintError(DbException exception);
1111
public bool IsMaxLengthExceededError(DbException exception);
12-
public bool IsDeadlockError(DbException exception) => false;
12+
public bool IsDeadlockError(DbException exception);
1313
}
1414
}

DbExceptionClassifier/Sqlite/SqliteExceptionClassifier.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,6 @@ public bool IsUniqueConstraintError(DbException exception) => exception is Sqlit
1919
};
2020

2121
public bool IsMaxLengthExceededError(DbException exception) => exception is SqliteException { SqliteExtendedErrorCode: SQLITE_TOOBIG };
22+
23+
public bool IsDeadlockError(DbException exception) => exception is SqliteException { SqliteExtendedErrorCode: SQLITE_LOCKED_SHAREDCACHE};
2224
}

EntityFramework.Exceptions/Tests/DatabaseTests.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -359,9 +359,9 @@ public virtual async Task Deadlock()
359359
var id1 = p1.Entity.Id;
360360
var id2 = p2.Entity.Id;
361361

362-
using var controlContext = new DemoContext(DemoContext.Options);
363-
using var transaction1 = await DemoContext.Database.BeginTransactionAsync();
364-
using var transaction2 = await controlContext.Database.BeginTransactionAsync();
362+
await using var controlContext = new DemoContext(DemoContext.Options);
363+
await using var transaction1 = await DemoContext.Database.BeginTransactionAsync();
364+
await using var transaction2 = await controlContext.Database.BeginTransactionAsync();
365365

366366
await DemoContext.Products.Where(c => c.Id == id1)
367367
.ExecuteUpdateAsync(c => c.SetProperty(p => p.Name, "Test11"));

EntityFramework.Exceptions/Tests/SqliteTests.cs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
using DotNet.Testcontainers.Containers;
2+
using EntityFramework.Exceptions.Common;
23
using EntityFramework.Exceptions.Sqlite;
34
using Microsoft.Data.Sqlite;
45
using Microsoft.EntityFrameworkCore;
56
using SQLitePCL;
7+
using System.Linq;
68
using System.Runtime.InteropServices;
79
using System.Threading.Tasks;
810
using Xunit;
@@ -53,10 +55,20 @@ public override Task NumericOverflowViolationThrowsNumericOverflowExceptionThrou
5355
return Task.CompletedTask;
5456
}
5557

56-
[Fact(Skip = "Skipping as SQLite no deadlock.")]
57-
public override Task Deadlock()
58+
[Fact]
59+
public override async Task Deadlock()
5860
{
59-
return Task.CompletedTask;
61+
var product = new Product { Name = "Test1" };
62+
DemoContext.Products.Add(product);
63+
64+
await DemoContext.SaveChangesAsync();
65+
66+
await using var controlContext = new DemoContext(DemoContext.Options);
67+
await using var transaction1 = await DemoContext.Database.BeginTransactionAsync();
68+
69+
await Assert.ThrowsAsync<DeadlockException>(() => controlContext.Products
70+
.Where(c => c.Id == product.Id)
71+
.ExecuteUpdateAsync(c => c.SetProperty(p => p.Name, "Test12")));
6072
}
6173
}
6274

0 commit comments

Comments
 (0)