forked from npgsql/efcore.pg
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBatchingTest.cs
More file actions
268 lines (222 loc) · 9.29 KB
/
BatchingTest.cs
File metadata and controls
268 lines (222 loc) · 9.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
using Microsoft.EntityFrameworkCore.Diagnostics.Internal;
using Npgsql.EntityFrameworkCore.PostgreSQL.Diagnostics.Internal;
using Npgsql.EntityFrameworkCore.PostgreSQL.Infrastructure;
// ReSharper disable UnusedAutoPropertyAccessor.Local
// ReSharper disable InconsistentNaming
namespace Microsoft.EntityFrameworkCore;
#nullable disable
public class BatchingTest(BatchingTest.BatchingTestFixture fixture) : IClassFixture<BatchingTest.BatchingTestFixture>
{
protected BatchingTestFixture Fixture { get; } = fixture;
[Theory]
[InlineData(true, true, true)]
[InlineData(false, true, true)]
[InlineData(true, false, true)]
[InlineData(false, false, true)]
[InlineData(true, true, false)]
[InlineData(false, true, false)]
[InlineData(true, false, false)]
[InlineData(false, false, false)]
public async Task Inserts_are_batched_correctly(bool clientPk, bool clientFk, bool clientOrder)
{
var expectedBlogs = new List<Blog>();
await ExecuteWithStrategyInTransactionAsync(
async context =>
{
var owner1 = new Owner();
var owner2 = new Owner();
context.Owners.Add(owner1);
context.Owners.Add(owner2);
for (var i = 1; i < 500; i++)
{
var blog = new Blog();
if (clientPk)
{
blog.Id = Guid.NewGuid();
}
if (clientFk)
{
blog.Owner = i % 2 == 0 ? owner1 : owner2;
}
if (clientOrder)
{
blog.Order = i;
}
context.Set<Blog>().Add(blog);
expectedBlogs.Add(blog);
}
await context.SaveChangesAsync();
},
context => AssertDatabaseState(context, clientOrder, expectedBlogs));
}
[Fact]
public async Task Inserts_and_updates_are_batched_correctly()
{
var expectedBlogs = new List<Blog>();
await ExecuteWithStrategyInTransactionAsync(
async context =>
{
var owner1 = new Owner { Name = "0" };
var owner2 = new Owner { Name = "1" };
context.Owners.Add(owner1);
context.Owners.Add(owner2);
var blog1 = new Blog
{
Id = Guid.NewGuid(),
Owner = owner1,
Order = 1
};
context.Set<Blog>().Add(blog1);
expectedBlogs.Add(blog1);
context.SaveChanges();
owner2.Name = "2";
blog1.Order = 0;
var blog2 = new Blog
{
Id = Guid.NewGuid(),
Owner = owner1,
Order = 1
};
context.Set<Blog>().Add(blog2);
expectedBlogs.Add(blog2);
var blog3 = new Blog
{
Id = Guid.NewGuid(),
Owner = owner2,
Order = 2
};
context.Set<Blog>().Add(blog3);
expectedBlogs.Add(blog3);
await context.SaveChangesAsync();
},
context => AssertDatabaseState(context, true, expectedBlogs));
}
[Fact]
public Task Inserts_when_database_type_is_different()
=> ExecuteWithStrategyInTransactionAsync(
async context =>
{
var owner1 = new Owner { Id = "0", Name = "Zero" };
var owner2 = new Owner { Id = "A", Name = string.Join("", Enumerable.Repeat('A', 900)) };
context.Owners.Add(owner1);
context.Owners.Add(owner2);
await context.SaveChangesAsync();
},
async context => Assert.Equal(2, await context.Owners.CountAsync()));
[ConditionalTheory]
[InlineData(3)]
[InlineData(4)]
public Task Inserts_are_batched_only_when_necessary(int minBatchSize)
{
var expectedBlogs = new List<Blog>();
return TestHelpers.ExecuteWithStrategyInTransactionAsync(
() => (BloggingContext)Fixture.CreateContext(minBatchSize),
UseTransaction,
async context =>
{
var owner = new Owner();
context.Owners.Add(owner);
for (var i = 1; i < 3; i++)
{
var blog = new Blog { Id = Guid.NewGuid(), Owner = owner };
context.Set<Blog>().Add(blog);
expectedBlogs.Add(blog);
}
Fixture.TestSqlLoggerFactory.Clear();
await context.SaveChangesAsync();
Assert.Contains(
minBatchSize == 3
? RelationalResources.LogBatchReadyForExecution(new TestLogger<NpgsqlLoggingDefinitions>())
.GenerateMessage(3)
: RelationalResources.LogBatchSmallerThanMinBatchSize(new TestLogger<NpgsqlLoggingDefinitions>())
.GenerateMessage(3, 4),
Fixture.TestSqlLoggerFactory.Log.Select(l => l.Message));
Assert.Equal(minBatchSize <= 3 ? 1 : 3, Fixture.TestSqlLoggerFactory.SqlStatements.Count);
}, context => AssertDatabaseState(context, false, expectedBlogs));
}
private async Task AssertDatabaseState(DbContext context, bool clientOrder, List<Blog> expectedBlogs)
{
expectedBlogs = clientOrder
? expectedBlogs.OrderBy(b => b.Order).ToList()
: expectedBlogs.OrderBy(b => b.Id).ToList();
var actualBlogs = clientOrder
? await context.Set<Blog>().OrderBy(b => b.Order).ToListAsync()
: expectedBlogs.OrderBy(b => b.Id).ToList();
Assert.Equal(expectedBlogs.Count, actualBlogs.Count);
for (var i = 0; i < actualBlogs.Count; i++)
{
var expected = expectedBlogs[i];
var actual = actualBlogs[i];
Assert.Equal(expected.Id, actual.Id);
Assert.Equal(expected.Order, actual.Order);
Assert.Equal(expected.OwnerId, actual.OwnerId);
Assert.Equal(expected.Version, actual.Version);
}
}
private BloggingContext CreateContext()
=> (BloggingContext)Fixture.CreateContext();
private Task ExecuteWithStrategyInTransactionAsync(
Func<BloggingContext, Task> testOperation,
Func<BloggingContext, Task> nestedTestOperation)
=> TestHelpers.ExecuteWithStrategyInTransactionAsync(
CreateContext, UseTransaction, testOperation, nestedTestOperation);
protected void UseTransaction(DatabaseFacade facade, IDbContextTransaction transaction)
=> facade.UseTransaction(transaction.GetDbTransaction());
private class BloggingContext(DbContextOptions options) : PoolableDbContext(options)
{
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Owner>(
b =>
{
b.Property(e => e.Id).ValueGeneratedOnAdd();
b.Property(e => e.Version)
.HasColumnName("xmin")
.HasColumnType("xid")
.ValueGeneratedOnAddOrUpdate()
.IsConcurrencyToken();
});
modelBuilder.Entity<Blog>().Property(b => b.Version)
.HasColumnName("xmin")
.HasColumnType("xid")
.ValueGeneratedOnAddOrUpdate()
.IsConcurrencyToken();
}
// ReSharper disable once UnusedMember.Local
public DbSet<Blog> Blogs { get; set; }
public DbSet<Owner> Owners { get; set; }
}
private class Blog
{
public Guid Id { get; set; }
public int Order { get; set; }
public string OwnerId { get; set; }
public Owner Owner { get; set; }
public uint Version { get; set; }
}
private class Owner
{
public string Id { get; set; }
public string Name { get; set; }
public uint Version { get; set; }
}
public class BatchingTestFixture : SharedStoreFixtureBase<PoolableDbContext>
{
protected override string StoreName { get; } = "BatchingTest";
public TestSqlLoggerFactory TestSqlLoggerFactory
=> (TestSqlLoggerFactory)ListLoggerFactory;
protected override ITestStoreFactory TestStoreFactory
=> NpgsqlTestStoreFactory.Instance;
protected override Type ContextType { get; } = typeof(BloggingContext);
protected override bool ShouldLogCategory(string logCategory)
=> logCategory == DbLoggerCategory.Update.Name;
protected override Task SeedAsync(PoolableDbContext context)
=> context.Database.EnsureCreatedResilientlyAsync();
public DbContext CreateContext(int minBatchSize)
{
var optionsBuilder = new DbContextOptionsBuilder(CreateOptions());
new NpgsqlDbContextOptionsBuilder(optionsBuilder).MinBatchSize(minBatchSize);
return new BloggingContext(optionsBuilder.Options);
}
}
}