|
| 1 | +using EntityFrameworkCore.Sqlite.Concurrency; |
| 2 | +using Microsoft.EntityFrameworkCore; |
| 3 | + |
| 4 | +namespace EFCore.Sqlite.Concurrency.Test; |
| 5 | + |
| 6 | +/// <summary> |
| 7 | +/// Stress tests that verify correctness under concurrent write load. |
| 8 | +/// Each test owns an isolated temp SQLite file and runs 50–100 concurrent writers. |
| 9 | +/// Assertions verify that no rows are lost, duplicated, or corrupted. |
| 10 | +/// </summary> |
| 11 | +public class ConcurrencyStressTests |
| 12 | +{ |
| 13 | + // ── Test 1: SaveChangesSerializedAsync ──────────────────────────────────── |
| 14 | + |
| 15 | + [Fact] |
| 16 | + public async Task SaveChangesSerializedAsync_WritesAllRows() |
| 17 | + { |
| 18 | + const int writers = 50; |
| 19 | + const int rowsPerWriter = 10; |
| 20 | + const int expected = writers * rowsPerWriter; |
| 21 | + |
| 22 | + using var db = new TempDatabase(); |
| 23 | + |
| 24 | + var tasks = Enumerable.Range(0, writers).Select(async writerIdx => |
| 25 | + { |
| 26 | + await using var ctx = db.CreateContext(); |
| 27 | + |
| 28 | + var entities = Enumerable.Range(0, rowsPerWriter) |
| 29 | + .Select(i => new StressEntity |
| 30 | + { |
| 31 | + Id = Guid.NewGuid(), |
| 32 | + WriterIndex = writerIdx, |
| 33 | + Payload = $"w{writerIdx}-r{i}" |
| 34 | + }) |
| 35 | + .ToList(); |
| 36 | + |
| 37 | + ctx.Entities.AddRange(entities); |
| 38 | + await ctx.SaveChangesSerializedAsync(); |
| 39 | + }); |
| 40 | + |
| 41 | + await Task.WhenAll(tasks); |
| 42 | + |
| 43 | + await using var verify = db.CreateContext(); |
| 44 | + var all = await verify.Entities.ToListAsync(); |
| 45 | + var ids = all.Select(e => e.Id).ToHashSet(); |
| 46 | + |
| 47 | + Assert.Equal(expected, all.Count); |
| 48 | + Assert.Equal(expected, ids.Count); // no duplicate IDs |
| 49 | + } |
| 50 | + |
| 51 | + // ── Test 2: BulkInsertOptimizedAsync ───────────────────────────────────── |
| 52 | + |
| 53 | + [Fact] |
| 54 | + public async Task BulkInsertOptimizedAsync_WritesAllRows() |
| 55 | + { |
| 56 | + const int writers = 10; |
| 57 | + const int entitiesPerWriter = 50; |
| 58 | + const int expected = writers * entitiesPerWriter; |
| 59 | + |
| 60 | + using var db = new TempDatabase(); |
| 61 | + |
| 62 | + var tasks = Enumerable.Range(0, writers).Select(async writerIdx => |
| 63 | + { |
| 64 | + await using var ctx = db.CreateContext(); |
| 65 | + |
| 66 | + var entities = Enumerable.Range(0, entitiesPerWriter) |
| 67 | + .Select(i => new StressEntity |
| 68 | + { |
| 69 | + Id = Guid.NewGuid(), |
| 70 | + WriterIndex = writerIdx, |
| 71 | + Payload = $"bulk-w{writerIdx}-r{i}" |
| 72 | + }) |
| 73 | + .ToList(); |
| 74 | + |
| 75 | + await ctx.BulkInsertOptimizedAsync(entities); |
| 76 | + }); |
| 77 | + |
| 78 | + await Task.WhenAll(tasks); |
| 79 | + |
| 80 | + await using var verify = db.CreateContext(); |
| 81 | + var all = await verify.Entities.ToListAsync(); |
| 82 | + var ids = all.Select(e => e.Id).ToHashSet(); |
| 83 | + |
| 84 | + Assert.Equal(expected, all.Count); |
| 85 | + Assert.Equal(expected, ids.Count); |
| 86 | + } |
| 87 | + |
| 88 | + // ── Test 3: ThreadSafeSqliteContext.ExecuteWriteAsync ──────────────────── |
| 89 | + |
| 90 | + [Fact] |
| 91 | + public async Task ExecuteWriteAsync_ThreadSafeSqliteContext_WritesAllRows() |
| 92 | + { |
| 93 | + const int writers = 50; |
| 94 | + const int rowsPerWriter = 10; |
| 95 | + const int expected = writers * rowsPerWriter; |
| 96 | + |
| 97 | + using var db = new TempDatabase(); |
| 98 | + |
| 99 | + var tasks = Enumerable.Range(0, writers).Select(async writerIdx => |
| 100 | + { |
| 101 | + // ThreadSafeSqliteContext must be constructed with the connection string |
| 102 | + // so it can find the shared write queue for this database file. |
| 103 | + var ctx = new ThreadSafeSqliteStressContext(db.ConnectionString); |
| 104 | + await using (ctx) |
| 105 | + { |
| 106 | + await ctx.ExecuteWriteAsync(async c => |
| 107 | + { |
| 108 | + var entities = Enumerable.Range(0, rowsPerWriter) |
| 109 | + .Select(i => new StressEntity |
| 110 | + { |
| 111 | + Id = Guid.NewGuid(), |
| 112 | + WriterIndex = writerIdx, |
| 113 | + Payload = $"tssc-w{writerIdx}-r{i}" |
| 114 | + }); |
| 115 | + |
| 116 | + c.Entities.AddRange(entities); |
| 117 | + }); |
| 118 | + } |
| 119 | + }); |
| 120 | + |
| 121 | + await Task.WhenAll(tasks); |
| 122 | + |
| 123 | + await using var verify = db.CreateContext(); |
| 124 | + var all = await verify.Entities.ToListAsync(); |
| 125 | + var ids = all.Select(e => e.Id).ToHashSet(); |
| 126 | + |
| 127 | + Assert.Equal(expected, all.Count); |
| 128 | + Assert.Equal(expected, ids.Count); |
| 129 | + } |
| 130 | + |
| 131 | + // ── Test 4: Mixed concurrent reads + writes ─────────────────────────────── |
| 132 | + |
| 133 | + [Fact] |
| 134 | + public async Task MixedReadsAndWrites_NoExceptionsOrDataLoss() |
| 135 | + { |
| 136 | + const int writers = 50; |
| 137 | + const int readers = 20; |
| 138 | + const int rowsPerWriter = 10; |
| 139 | + const int expected = writers * rowsPerWriter; |
| 140 | + |
| 141 | + using var db = new TempDatabase(); |
| 142 | + |
| 143 | + // Pre-insert one seed row so readers have something to count from the start. |
| 144 | + await using (var seed = db.CreateContext()) |
| 145 | + { |
| 146 | + seed.Entities.Add(new StressEntity { Payload = "seed" }); |
| 147 | + await seed.SaveChangesSerializedAsync(); |
| 148 | + } |
| 149 | + |
| 150 | + var writerTasks = Enumerable.Range(0, writers).Select(async writerIdx => |
| 151 | + { |
| 152 | + await using var ctx = db.CreateContext(); |
| 153 | + var entities = Enumerable.Range(0, rowsPerWriter) |
| 154 | + .Select(i => new StressEntity |
| 155 | + { |
| 156 | + Id = Guid.NewGuid(), |
| 157 | + WriterIndex = writerIdx, |
| 158 | + Payload = $"mixed-w{writerIdx}-r{i}" |
| 159 | + }) |
| 160 | + .ToList(); |
| 161 | + |
| 162 | + ctx.Entities.AddRange(entities); |
| 163 | + await ctx.SaveChangesSerializedAsync(); |
| 164 | + }); |
| 165 | + |
| 166 | + var readResults = new System.Collections.Concurrent.ConcurrentBag<int>(); |
| 167 | + var readerTasks = Enumerable.Range(0, readers).Select(async _ => |
| 168 | + { |
| 169 | + await using var ctx = db.CreateContext(); |
| 170 | + // WAL mode allows reads to run concurrently with writes. |
| 171 | + var count = await ctx.Entities.CountAsync(); |
| 172 | + readResults.Add(count); |
| 173 | + }); |
| 174 | + |
| 175 | + await Task.WhenAll(writerTasks.Concat(readerTasks)); |
| 176 | + |
| 177 | + // Verify all writes landed |
| 178 | + await using var verify = db.CreateContext(); |
| 179 | + var all = await verify.Entities.ToListAsync(); |
| 180 | + |
| 181 | + // +1 for the seed row |
| 182 | + Assert.Equal(expected + 1, all.Count); |
| 183 | + Assert.Equal(expected + 1, all.Select(e => e.Id).Distinct().Count()); |
| 184 | + |
| 185 | + // All reader results must be non-negative (reads always saw valid data) |
| 186 | + Assert.All(readResults, count => Assert.True(count >= 0)); |
| 187 | + } |
| 188 | +} |
0 commit comments