-
Notifications
You must be signed in to change notification settings - Fork 934
/
Copy pathBatcherFixture.cs
253 lines (226 loc) · 7.36 KB
/
BatcherFixture.cs
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
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by AsyncGenerator.
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
using NHibernate.AdoNet;
using NHibernate.Cfg;
using NUnit.Framework;
namespace NHibernate.Test.Ado
{
using System.Threading.Tasks;
using System.Threading;
[TestFixture]
public class BatcherFixtureAsync: TestCase
{
protected override string MappingsAssembly
{
get { return "NHibernate.Test"; }
}
protected override string[] Mappings
{
get { return new[] { "Ado.VerySimple.hbm.xml", "Ado.AlmostSimple.hbm.xml" }; }
}
protected override void Configure(Configuration configuration)
{
configuration.SetProperty(Environment.FormatSql, "true");
configuration.SetProperty(Environment.GenerateStatistics, "true");
configuration.SetProperty(Environment.BatchSize, "10");
}
protected override bool AppliesTo(Engine.ISessionFactoryImplementor factory)
{
return !(factory.Settings.BatcherFactory is NonBatchingBatcherFactory);
}
[Test]
[Description("The batcher should run all INSERT queries in only one roundtrip.")]
public async Task OneRoundTripInsertsAsync()
{
Sfi.Statistics.Clear();
await (FillDbAsync());
Assert.That(Sfi.Statistics.PrepareStatementCount, Is.EqualTo(1));
await (CleanupAsync());
}
private async Task CleanupAsync(CancellationToken cancellationToken = default(CancellationToken))
{
using (ISession s = Sfi.OpenSession())
using (var t = s.BeginTransaction())
{
await (s.CreateQuery("delete from VerySimple").ExecuteUpdateAsync(cancellationToken));
await (s.CreateQuery("delete from AlmostSimple").ExecuteUpdateAsync(cancellationToken));
await (t.CommitAsync(cancellationToken));
}
}
private async Task FillDbAsync(CancellationToken cancellationToken = default(CancellationToken))
{
using (ISession s = Sfi.OpenSession())
using (ITransaction tx = s.BeginTransaction())
{
await (s.SaveAsync(new VerySimple {Id = 1, Name = "Fabio", Weight = 119.5}, cancellationToken));
await (s.SaveAsync(new VerySimple {Id = 2, Name = "Fiamma", Weight = 9.8}, cancellationToken));
await (s.SaveAsync(new VerySimple {Id = 3, Name = "Roberto", Weight = 98.8 }, cancellationToken));
await (tx.CommitAsync(cancellationToken));
}
}
[Test]
[Description("The batcher should run all UPDATE queries in only one roundtrip.")]
public async Task OneRoundTripUpdateAsync()
{
await (FillDbAsync());
using (ISession s = Sfi.OpenSession())
using (ITransaction tx = s.BeginTransaction())
{
var vs1 = await (s.GetAsync<VerySimple>(1));
var vs2 = await (s.GetAsync<VerySimple>(2));
var vs3 = await (s.GetAsync<VerySimple>(3));
vs1.Weight -= 10;
vs2.Weight -= 1;
vs3.Weight -= 5;
Sfi.Statistics.Clear();
await (s.UpdateAsync(vs1));
await (s.UpdateAsync(vs2));
await (s.UpdateAsync(vs3));
await (tx.CommitAsync());
}
Assert.That(Sfi.Statistics.PrepareStatementCount, Is.EqualTo(1));
await (CleanupAsync());
}
[Test, NetFxOnly]
[Description("SqlClient: The batcher log output should be formatted")]
public async Task BatchedoutputShouldBeFormattedAsync()
{
#if NETFX
if (Sfi.Settings.BatcherFactory is SqlClientBatchingBatcherFactory == false)
Assert.Ignore("This test is for SqlClientBatchingBatcher only");
#endif
using (var sqlLog = new SqlLogSpy())
{
await (FillDbAsync());
var log = sqlLog.GetWholeLog();
Assert.IsTrue(log.Contains("INSERT \n INTO"));
}
await (CleanupAsync());
}
[Test]
[Description("The batcher should run all DELETE queries in only one roundtrip.")]
public async Task OneRoundTripDeleteAsync()
{
await (FillDbAsync());
using (ISession s = Sfi.OpenSession())
using (ITransaction tx = s.BeginTransaction())
{
var vs1 = await (s.GetAsync<VerySimple>(1));
var vs2 = await (s.GetAsync<VerySimple>(2));
var vs3 = await (s.GetAsync<VerySimple>(3));
Sfi.Statistics.Clear();
await (s.DeleteAsync(vs1));
await (s.DeleteAsync(vs2));
await (s.DeleteAsync(vs3));
await (tx.CommitAsync());
}
Assert.That(Sfi.Statistics.PrepareStatementCount, Is.EqualTo(1));
await (CleanupAsync());
}
[Test]
[Description(@"Activating the SQL and turning off the batcher's log the log stream:
-should not contains adding to batch
-should contain batch command
-the batcher should work.")]
public async Task SqlLogAsync()
{
using (new LogSpy(typeof(AbstractBatcher), true))
{
using (var sl = new SqlLogSpy())
{
Sfi.Statistics.Clear();
await (FillDbAsync());
string logs = sl.GetWholeLog();
Assert.That(logs, Does.Not.Contain("Adding to batch").IgnoreCase);
Assert.That(logs, Does.Contain("Batch command").IgnoreCase);
Assert.That(logs, Does.Contain("INSERT").IgnoreCase);
}
}
Assert.That(Sfi.Statistics.PrepareStatementCount, Is.EqualTo(1));
await (CleanupAsync());
}
[Test]
[Description(@"Activating the AbstractBatcher's log the log stream:
-should not contains batch info
-should contain SQL log info only regarding batcher (SQL log should not be duplicated)
-the batcher should work.")]
public async Task AbstractBatcherLogAsync()
{
using (new LogSpy(typeof(AbstractBatcher)))
{
using (var sl = new SqlLogSpy())
{
Sfi.Statistics.Clear();
await (FillDbAsync());
string logs = sl.GetWholeLog();
Assert.That(logs, Does.Contain("batch").IgnoreCase);
foreach (var loggingEvent in sl.Appender.GetEvents())
{
string message = loggingEvent.RenderedMessage;
if(message.ToLowerInvariant().Contains("insert"))
{
Assert.That(message, Does.Contain("batch").IgnoreCase);
}
}
}
}
Assert.That(Sfi.Statistics.PrepareStatementCount, Is.EqualTo(1));
await (CleanupAsync());
}
[Test]
public async Task SqlLogShouldGetBatchCommandNotificationAsync()
{
using (new LogSpy(typeof(AbstractBatcher)))
{
using (var sl = new SqlLogSpy())
{
Sfi.Statistics.Clear();
await (FillDbAsync());
string logs = sl.GetWholeLog();
Assert.That(logs, Does.Contain("Batch commands:").IgnoreCase);
}
}
Assert.That(Sfi.Statistics.PrepareStatementCount, Is.EqualTo(1));
await (CleanupAsync());
}
[Test]
[Description(@"Activating the AbstractBatcher's log the log stream:
-should contain well formatted SQL log info")]
public async Task AbstractBatcherLogFormattedSqlAsync()
{
using (new LogSpy(typeof(AbstractBatcher)))
{
using (var sl = new SqlLogSpy())
{
Sfi.Statistics.Clear();
await (FillDbAsync());
foreach (var loggingEvent in sl.Appender.GetEvents())
{
string message = loggingEvent.RenderedMessage;
if(message.StartsWith("Adding"))
{
// should be the line with the formatted SQL
var strings = message.Split(System.Environment.NewLine.ToCharArray());
foreach (var sqlLine in strings)
{
if(sqlLine.Contains("p0"))
{
Assert.That(sqlLine, Does.Contain("p1"));
Assert.That(sqlLine, Does.Contain("p2"));
}
}
}
}
}
}
Assert.That(Sfi.Statistics.PrepareStatementCount, Is.EqualTo(1));
await (CleanupAsync());
}
}
}