Skip to content

Commit cd7bae7

Browse files
committed
Generate Async tests.
- Update code to new rules about cancellation tokens
1 parent 9e8e631 commit cd7bae7

File tree

1,028 files changed

+112900
-1316
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

1,028 files changed

+112900
-1316
lines changed

src/NHibernate.DomainModel/Async/CustomPersister.cs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public partial class CustomPersister : IEntityPersister
3535
#region IEntityPersister Members
3636
#region IOptimisticCacheSource Members
3737

38-
public Task<object[]> GetNaturalIdentifierSnapshotAsync(object id, ISessionImplementor session, CancellationToken cancellationToken = default(CancellationToken))
38+
public Task<object[]> GetNaturalIdentifierSnapshotAsync(object id, ISessionImplementor session, CancellationToken cancellationToken)
3939
{
4040
try
4141
{
@@ -47,7 +47,7 @@ public partial class CustomPersister : IEntityPersister
4747
}
4848
}
4949

50-
public async Task<object> LoadAsync(object id, object optionalObject, LockMode lockMode, ISessionImplementor session, CancellationToken cancellationToken = default(CancellationToken))
50+
public async Task<object> LoadAsync(object id, object optionalObject, LockMode lockMode, ISessionImplementor session, CancellationToken cancellationToken)
5151
{
5252
// fails when optional object is supplied
5353
Custom clone = null;
@@ -64,12 +64,12 @@ public partial class CustomPersister : IEntityPersister
6464
return clone;
6565
}
6666

67-
public Task LockAsync(object id, object version, object obj, LockMode lockMode, ISessionImplementor session, CancellationToken cancellationToken = default(CancellationToken))
67+
public Task LockAsync(object id, object version, object obj, LockMode lockMode, ISessionImplementor session, CancellationToken cancellationToken)
6868
{
6969
throw new NotSupportedException();
7070
}
7171

72-
public Task InsertAsync(object id, object[] fields, object obj, ISessionImplementor session, CancellationToken cancellationToken = default(CancellationToken))
72+
public Task InsertAsync(object id, object[] fields, object obj, ISessionImplementor session, CancellationToken cancellationToken)
7373
{
7474
try
7575
{
@@ -82,12 +82,12 @@ public partial class CustomPersister : IEntityPersister
8282
}
8383
}
8484

85-
public Task<object> InsertAsync(object[] fields, object obj, ISessionImplementor session, CancellationToken cancellationToken = default(CancellationToken))
85+
public Task<object> InsertAsync(object[] fields, object obj, ISessionImplementor session, CancellationToken cancellationToken)
8686
{
8787
throw new NotSupportedException();
8888
}
8989

90-
public Task DeleteAsync(object id, object version, object obj, ISessionImplementor session, CancellationToken cancellationToken = default(CancellationToken))
90+
public Task DeleteAsync(object id, object version, object obj, ISessionImplementor session, CancellationToken cancellationToken)
9191
{
9292
try
9393
{
@@ -100,7 +100,7 @@ public partial class CustomPersister : IEntityPersister
100100
}
101101
}
102102

103-
public Task UpdateAsync(object id, object[] fields, int[] dirtyFields, bool hasDirtyCollection, object[] oldFields, object oldVersion, object obj, object rowId, ISessionImplementor session, CancellationToken cancellationToken = default(CancellationToken))
103+
public Task UpdateAsync(object id, object[] fields, int[] dirtyFields, bool hasDirtyCollection, object[] oldFields, object oldVersion, object obj, object rowId, ISessionImplementor session, CancellationToken cancellationToken)
104104
{
105105
try
106106
{
@@ -113,7 +113,7 @@ public partial class CustomPersister : IEntityPersister
113113
}
114114
}
115115

116-
public Task<object[]> GetDatabaseSnapshotAsync(object id, ISessionImplementor session, CancellationToken cancellationToken = default(CancellationToken))
116+
public Task<object[]> GetDatabaseSnapshotAsync(object id, ISessionImplementor session, CancellationToken cancellationToken)
117117
{
118118
try
119119
{
@@ -125,7 +125,7 @@ public partial class CustomPersister : IEntityPersister
125125
}
126126
}
127127

128-
public Task<object> GetCurrentVersionAsync(object id, ISessionImplementor session, CancellationToken cancellationToken = default(CancellationToken))
128+
public Task<object> GetCurrentVersionAsync(object id, ISessionImplementor session, CancellationToken cancellationToken)
129129
{
130130
try
131131
{
@@ -137,7 +137,7 @@ public partial class CustomPersister : IEntityPersister
137137
}
138138
}
139139

140-
public Task<object> ForceVersionIncrementAsync(object id, object currentVersion, ISessionImplementor session, CancellationToken cancellationToken = default(CancellationToken))
140+
public Task<object> ForceVersionIncrementAsync(object id, object currentVersion, ISessionImplementor session, CancellationToken cancellationToken)
141141
{
142142
try
143143
{
@@ -149,12 +149,12 @@ public partial class CustomPersister : IEntityPersister
149149
}
150150
}
151151

152-
public Task ProcessInsertGeneratedPropertiesAsync(object id, object entity, object[] state, ISessionImplementor session, CancellationToken cancellationToken = default(CancellationToken))
152+
public Task ProcessInsertGeneratedPropertiesAsync(object id, object entity, object[] state, ISessionImplementor session, CancellationToken cancellationToken)
153153
{
154154
return Task.CompletedTask;
155155
}
156156

157-
public Task ProcessUpdateGeneratedPropertiesAsync(object id, object entity, object[] state, ISessionImplementor session, CancellationToken cancellationToken = default(CancellationToken))
157+
public Task ProcessUpdateGeneratedPropertiesAsync(object id, object entity, object[] state, ISessionImplementor session, CancellationToken cancellationToken)
158158
{
159159
return Task.CompletedTask;
160160
}
Lines changed: 285 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,285 @@
1+
//------------------------------------------------------------------------------
2+
// <auto-generated>
3+
// This code was generated by AsyncGenerator.
4+
//
5+
// Changes to this file may cause incorrect behavior and will be lost if
6+
// the code is regenerated.
7+
// </auto-generated>
8+
//------------------------------------------------------------------------------
9+
10+
11+
using System.Collections;
12+
using NHibernate.AdoNet;
13+
using NHibernate.Cfg;
14+
using NUnit.Framework;
15+
16+
namespace NHibernate.Test.Ado
17+
{
18+
using System.Threading.Tasks;
19+
using System.Threading;
20+
[TestFixture]
21+
public class BatcherFixtureAsync: TestCase
22+
{
23+
protected override string MappingsAssembly
24+
{
25+
get { return "NHibernate.Test"; }
26+
}
27+
28+
protected override IList Mappings
29+
{
30+
get { return new[] { "Ado.VerySimple.hbm.xml", "Ado.AlmostSimple.hbm.xml" }; }
31+
}
32+
33+
protected override void Configure(Configuration configuration)
34+
{
35+
configuration.SetProperty(Environment.FormatSql, "true");
36+
configuration.SetProperty(Environment.GenerateStatistics, "true");
37+
configuration.SetProperty(Environment.BatchSize, "10");
38+
}
39+
40+
protected override bool AppliesTo(Engine.ISessionFactoryImplementor factory)
41+
{
42+
return !(factory.Settings.BatcherFactory is NonBatchingBatcherFactory);
43+
}
44+
45+
[Test]
46+
[Description("The batcher should run all INSERT queries in only one roundtrip.")]
47+
public async Task OneRoundTripInsertsAsync()
48+
{
49+
sessions.Statistics.Clear();
50+
await (FillDbAsync(CancellationToken.None));
51+
52+
Assert.That(sessions.Statistics.PrepareStatementCount, Is.EqualTo(1));
53+
await (CleanupAsync(CancellationToken.None));
54+
}
55+
56+
private async Task CleanupAsync(CancellationToken cancellationToken = default(CancellationToken))
57+
{
58+
using (ISession s = sessions.OpenSession())
59+
using (s.BeginTransaction())
60+
{
61+
await (s.CreateQuery("delete from VerySimple").ExecuteUpdateAsync(cancellationToken));
62+
await (s.CreateQuery("delete from AlmostSimple").ExecuteUpdateAsync(cancellationToken));
63+
await (s.Transaction.CommitAsync(cancellationToken));
64+
}
65+
}
66+
67+
private async Task FillDbAsync(CancellationToken cancellationToken = default(CancellationToken))
68+
{
69+
using (ISession s = sessions.OpenSession())
70+
using (ITransaction tx = s.BeginTransaction())
71+
{
72+
await (s.SaveAsync(new VerySimple {Id = 1, Name = "Fabio", Weight = 119.5}, cancellationToken));
73+
await (s.SaveAsync(new VerySimple {Id = 2, Name = "Fiamma", Weight = 9.8}, cancellationToken));
74+
await (tx.CommitAsync(cancellationToken));
75+
}
76+
}
77+
78+
[Test]
79+
[Description("The batcher should run all UPDATE queries in only one roundtrip.")]
80+
public async Task OneRoundTripUpdateAsync()
81+
{
82+
await (FillDbAsync(CancellationToken.None));
83+
84+
using (ISession s = sessions.OpenSession())
85+
using (ITransaction tx = s.BeginTransaction())
86+
{
87+
var vs1 = await (s.GetAsync<VerySimple>(1, CancellationToken.None));
88+
var vs2 = await (s.GetAsync<VerySimple>(2, CancellationToken.None));
89+
vs1.Weight -= 10;
90+
vs2.Weight -= 1;
91+
sessions.Statistics.Clear();
92+
await (s.UpdateAsync(vs1, CancellationToken.None));
93+
await (s.UpdateAsync(vs2, CancellationToken.None));
94+
await (tx.CommitAsync(CancellationToken.None));
95+
}
96+
97+
Assert.That(sessions.Statistics.PrepareStatementCount, Is.EqualTo(1));
98+
await (CleanupAsync(CancellationToken.None));
99+
}
100+
101+
[Test, Ignore("Not fixed yet.")]
102+
[Description("SqlClient: The batcher should run all different INSERT queries in only one roundtrip.")]
103+
public async Task SqlClientOneRoundTripForUpdateAndInsertAsync()
104+
{
105+
if (sessions.Settings.BatcherFactory is SqlClientBatchingBatcherFactory == false)
106+
Assert.Ignore("This test is for SqlClientBatchingBatcher only");
107+
108+
await (FillDbAsync(CancellationToken.None));
109+
110+
using(var sqlLog = new SqlLogSpy())
111+
using (ISession s = sessions.OpenSession())
112+
using (ITransaction tx = s.BeginTransaction())
113+
{
114+
await (s.SaveAsync(new VerySimple
115+
{
116+
Name = "test441",
117+
Weight = 894
118+
}, CancellationToken.None));
119+
120+
await (s.SaveAsync(new AlmostSimple
121+
{
122+
Name = "test441",
123+
Weight = 894
124+
}, CancellationToken.None));
125+
126+
await (tx.CommitAsync(CancellationToken.None));
127+
128+
var log = sqlLog.GetWholeLog();
129+
//log should only contain NHibernate.SQL once, because that means
130+
//that we ony generated a single batch (NHibernate.SQL log will output
131+
//once per batch)
132+
Assert.AreEqual(0, log.IndexOf("NHibernate.SQL"), "log should start with NHibernate.SQL");
133+
Assert.AreEqual(-1, log.IndexOf("NHibernate.SQL", "NHibernate.SQL".Length), "NHibernate.SQL should only appear once in the log");
134+
}
135+
136+
await (CleanupAsync(CancellationToken.None));
137+
}
138+
139+
[Test]
140+
[Description("SqlClient: The batcher log output should be formatted")]
141+
public async Task BatchedoutputShouldBeFormattedAsync()
142+
{
143+
if (sessions.Settings.BatcherFactory is SqlClientBatchingBatcherFactory == false)
144+
Assert.Ignore("This test is for SqlClientBatchingBatcher only");
145+
146+
using (var sqlLog = new SqlLogSpy())
147+
{
148+
await (FillDbAsync(CancellationToken.None));
149+
var log = sqlLog.GetWholeLog();
150+
Assert.IsTrue(log.Contains("INSERT \n INTO"));
151+
}
152+
153+
await (CleanupAsync(CancellationToken.None));
154+
}
155+
156+
157+
[Test]
158+
[Description("The batcher should run all DELETE queries in only one roundtrip.")]
159+
public async Task OneRoundTripDeleteAsync()
160+
{
161+
await (FillDbAsync(CancellationToken.None));
162+
163+
using (ISession s = sessions.OpenSession())
164+
using (ITransaction tx = s.BeginTransaction())
165+
{
166+
var vs1 = await (s.GetAsync<VerySimple>(1, CancellationToken.None));
167+
var vs2 = await (s.GetAsync<VerySimple>(2, CancellationToken.None));
168+
sessions.Statistics.Clear();
169+
await (s.DeleteAsync(vs1, CancellationToken.None));
170+
await (s.DeleteAsync(vs2, CancellationToken.None));
171+
await (tx.CommitAsync(CancellationToken.None));
172+
}
173+
174+
Assert.That(sessions.Statistics.PrepareStatementCount, Is.EqualTo(1));
175+
await (CleanupAsync(CancellationToken.None));
176+
}
177+
178+
[Test]
179+
[Description(@"Activating the SQL and turning off the batcher's log the log stream:
180+
-should not contains adding to batch
181+
-should contain batch command
182+
-the batcher should work.")]
183+
public async Task SqlLogAsync()
184+
{
185+
using (new LogSpy(typeof(AbstractBatcher), true))
186+
{
187+
using (var sl = new SqlLogSpy())
188+
{
189+
sessions.Statistics.Clear();
190+
await (FillDbAsync(CancellationToken.None));
191+
string logs = sl.GetWholeLog();
192+
Assert.That(logs, Does.Not.Contain("Adding to batch").IgnoreCase);
193+
Assert.That(logs, Does.Contain("Batch command").IgnoreCase);
194+
Assert.That(logs, Does.Contain("INSERT").IgnoreCase);
195+
}
196+
}
197+
198+
Assert.That(sessions.Statistics.PrepareStatementCount, Is.EqualTo(1));
199+
await (CleanupAsync(CancellationToken.None));
200+
}
201+
202+
[Test]
203+
[Description(@"Activating the AbstractBatcher's log the log stream:
204+
-should not contains batch info
205+
-should contain SQL log info only regarding batcher (SQL log should not be duplicated)
206+
-the batcher should work.")]
207+
public async Task AbstractBatcherLogAsync()
208+
{
209+
using (new LogSpy(typeof(AbstractBatcher)))
210+
{
211+
using (var sl = new SqlLogSpy())
212+
{
213+
sessions.Statistics.Clear();
214+
await (FillDbAsync(CancellationToken.None));
215+
string logs = sl.GetWholeLog();
216+
Assert.That(logs, Does.Contain("batch").IgnoreCase);
217+
foreach (var loggingEvent in sl.Appender.GetEvents())
218+
{
219+
string message = loggingEvent.RenderedMessage;
220+
if(message.ToLowerInvariant().Contains("insert"))
221+
{
222+
Assert.That(message, Does.Contain("batch").IgnoreCase);
223+
}
224+
}
225+
}
226+
}
227+
228+
Assert.That(sessions.Statistics.PrepareStatementCount, Is.EqualTo(1));
229+
await (CleanupAsync(CancellationToken.None));
230+
}
231+
232+
[Test]
233+
public async Task SqlLogShouldGetBatchCommandNotificationAsync()
234+
{
235+
using (new LogSpy(typeof(AbstractBatcher)))
236+
{
237+
using (var sl = new SqlLogSpy())
238+
{
239+
sessions.Statistics.Clear();
240+
await (FillDbAsync(CancellationToken.None));
241+
string logs = sl.GetWholeLog();
242+
Assert.That(logs, Does.Contain("Batch commands:").IgnoreCase);
243+
}
244+
}
245+
246+
Assert.That(sessions.Statistics.PrepareStatementCount, Is.EqualTo(1));
247+
await (CleanupAsync(CancellationToken.None));
248+
}
249+
250+
[Test]
251+
[Description(@"Activating the AbstractBatcher's log the log stream:
252+
-should contain well formatted SQL log info")]
253+
public async Task AbstractBatcherLogFormattedSqlAsync()
254+
{
255+
using (new LogSpy(typeof(AbstractBatcher)))
256+
{
257+
using (var sl = new SqlLogSpy())
258+
{
259+
sessions.Statistics.Clear();
260+
await (FillDbAsync(CancellationToken.None));
261+
foreach (var loggingEvent in sl.Appender.GetEvents())
262+
{
263+
string message = loggingEvent.RenderedMessage;
264+
if(message.StartsWith("Adding"))
265+
{
266+
// should be the line with the formatted SQL
267+
var strings = message.Split(System.Environment.NewLine.ToCharArray());
268+
foreach (var sqlLine in strings)
269+
{
270+
if(sqlLine.Contains("p0"))
271+
{
272+
Assert.That(sqlLine, Does.Contain("p1"));
273+
Assert.That(sqlLine, Does.Contain("p2"));
274+
}
275+
}
276+
}
277+
}
278+
}
279+
}
280+
281+
Assert.That(sessions.Statistics.PrepareStatementCount, Is.EqualTo(1));
282+
await (CleanupAsync(CancellationToken.None));
283+
}
284+
}
285+
}

0 commit comments

Comments
 (0)