Skip to content

Commit 80fb5f3

Browse files
NH-3905 - Replacing more manual async implementation by generation.
1 parent 05bac51 commit 80fb5f3

File tree

10 files changed

+207
-75
lines changed

10 files changed

+207
-75
lines changed

Tools/packages.config

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,5 @@
77
<package id="NUnit.Extension.NUnitV2ResultWriter" version="3.6.0" targetFramework="net461" />
88
<package id="NUnit.Extension.TeamCityEventListener" version="1.0.2" targetFramework="net461" />
99
<package id="NUnit.Extension.VSProjectLoader" version="3.6.0" targetFramework="net461" />
10-
<package id="CSharpAsyncGenerator.CommandLine" version="0.4.0" targetFramework="net461" />
10+
<package id="CSharpAsyncGenerator.CommandLine" version="0.4.3" targetFramework="net461" />
1111
</packages>

src/AsyncGenerator.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,8 @@
132132
- rule: EventListener
133133
- rule: ICache
134134
scanMethodBody: true
135+
scanForMissingAsyncMembers:
136+
- all: true
135137
transformation:
136138
configureAwaitArgument: false
137139
localFunctions: true
@@ -207,7 +209,7 @@
207209
- hasAttributeName: TheoryAttribute
208210
scanMethodBody: true
209211
scanForMissingAsyncMembers:
210-
- anyInterfaceRule: NHibernateAssembly
212+
- all: true
211213
registerPlugin:
212214
- type: AsyncGenerator.Core.Plugins.NUnitAsyncCounterpartsFinder
213215
assemblyName: AsyncGenerator.Core
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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;
12+
using System.Collections.Concurrent;
13+
using System.Data;
14+
using System.Data.Common;
15+
using System.Linq;
16+
using NHibernate.Connection;
17+
18+
namespace NHibernate.Test
19+
{
20+
using System.Threading.Tasks;
21+
using System.Threading;
22+
/// <content>
23+
/// Contains generated async methods
24+
/// </content>
25+
public partial class DebugConnectionProvider : DriverConnectionProvider
26+
{
27+
28+
public override async Task<DbConnection> GetConnectionAsync(CancellationToken cancellationToken)
29+
{
30+
try
31+
{
32+
var connection = await (base.GetConnectionAsync(cancellationToken));
33+
connections.TryAdd(connection, 0);
34+
return connection;
35+
}
36+
catch (Exception e)
37+
{
38+
throw new HibernateException("Could not open connection to: " + ConnectionString, e);
39+
}
40+
}
41+
}
42+
}

src/NHibernate.Test/Async/Events/Collections/CollectionListeners.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,26 @@ public partial class CollectionListeners
2929
#region Nested type: IListener
3030

3131
#endregion
32+
3233
#region Nested type: InitializeCollectionListener
3334

35+
/// <content>
36+
/// Contains generated async methods
37+
/// </content>
38+
public partial class InitializeCollectionListener : DefaultInitializeCollectionEventListener, IListener
39+
{
40+
41+
#region IListener Members
42+
43+
#endregion
44+
45+
public override async Task OnInitializeCollectionAsync(InitializeCollectionEvent @event, CancellationToken cancellationToken)
46+
{
47+
await (base.OnInitializeCollectionAsync(@event, cancellationToken));
48+
AddEvent(@event, this);
49+
}
50+
}
51+
3452
#endregion
3553

3654
#region Nested type: PostCollectionRecreateListener

src/NHibernate.Test/Async/Insertordering/InsertOrderingFixture.cs

Lines changed: 93 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,10 @@
2525
using NHibernate.SqlTypes;
2626
using NUnit.Framework;
2727

28-
using AsyncTask = System.Threading.Tasks.Task;
29-
3028
namespace NHibernate.Test.Insertordering
3129
{
3230
[TestFixture]
33-
public class InsertOrderingFixtureAsync : TestCase
31+
public partial class InsertOrderingFixtureAsync : TestCase
3432
{
3533
const int batchSize = 10;
3634
const int instancesPerEach = 12;
@@ -546,7 +544,7 @@ public async System.Threading.Tasks.Task WithUnidiOneToOneAsync()
546544

547545
#region Nested type: StatsBatcher
548546

549-
public class StatsBatcher : SqlClientBatchingBatcher
547+
public partial class StatsBatcher : SqlClientBatchingBatcher
550548
{
551549
private static string batchSQL;
552550
private static IList<int> batchSizes = new List<int>();
@@ -569,18 +567,18 @@ public static void Reset()
569567
batchSQL = null;
570568
}
571569

572-
public override DbCommand PrepareBatchCommand(CommandType type, SqlString sql, SqlType[] parameterTypes)
570+
public override async System.Threading.Tasks.Task<DbCommand> PrepareBatchCommandAsync(CommandType type, SqlString sql, SqlType[] parameterTypes, CancellationToken cancellationToken)
573571
{
574-
var result = base.PrepareBatchCommand(type, sql, parameterTypes);
572+
var result = await (base.PrepareBatchCommandAsync(type, sql, parameterTypes, cancellationToken));
575573

576574
PrepareStats(sql);
577575

578576
return result;
579577
}
580578

581-
public override Task<DbCommand> PrepareBatchCommandAsync(CommandType type, SqlString sql, SqlType[] parameterTypes, CancellationToken cancellationToken)
579+
public override DbCommand PrepareBatchCommand(CommandType type, SqlString sql, SqlType[] parameterTypes)
582580
{
583-
var result = base.PrepareBatchCommandAsync(type, sql, parameterTypes, cancellationToken);
581+
var result = base.PrepareBatchCommand(type, sql, parameterTypes);
584582

585583
PrepareStats(sql);
586584

@@ -603,16 +601,23 @@ private static void PrepareStats(SqlString sql)
603601
}
604602
}
605603

606-
public override void AddToBatch(IExpectation expectation)
604+
public override System.Threading.Tasks.Task AddToBatchAsync(IExpectation expectation, CancellationToken cancellationToken)
607605
{
608-
AddStats();
609-
base.AddToBatch(expectation);
606+
try
607+
{
608+
AddStats();
609+
return base.AddToBatchAsync(expectation, cancellationToken);
610+
}
611+
catch (Exception ex)
612+
{
613+
return System.Threading.Tasks.Task.FromException<object>(ex);
614+
}
610615
}
611616

612-
public override AsyncTask AddToBatchAsync(IExpectation expectation, CancellationToken cancellationToken)
617+
public override void AddToBatch(IExpectation expectation)
613618
{
614619
AddStats();
615-
return base.AddToBatchAsync(expectation, cancellationToken);
620+
base.AddToBatch(expectation);
616621
}
617622

618623
private static void AddStats()
@@ -624,16 +629,23 @@ private static void AddStats()
624629
Console.WriteLine("Adding to batch [" + batchSQL + "]");
625630
}
626631

627-
protected override void DoExecuteBatch(DbCommand ps)
632+
protected override System.Threading.Tasks.Task DoExecuteBatchAsync(DbCommand ps, CancellationToken cancellationToken)
628633
{
629-
ExecuteStats();
630-
base.DoExecuteBatch(ps);
634+
try
635+
{
636+
ExecuteStats();
637+
return base.DoExecuteBatchAsync(ps, cancellationToken);
638+
}
639+
catch (Exception ex)
640+
{
641+
return System.Threading.Tasks.Task.FromException<object>(ex);
642+
}
631643
}
632644

633-
protected override AsyncTask DoExecuteBatchAsync(DbCommand ps, CancellationToken cancellationToken)
645+
protected override void DoExecuteBatch(DbCommand ps)
634646
{
635647
ExecuteStats();
636-
return base.DoExecuteBatchAsync(ps, cancellationToken);
648+
base.DoExecuteBatch(ps);
637649
}
638650

639651
private static void ExecuteStats()
@@ -651,7 +663,7 @@ private static void ExecuteStats()
651663

652664
#region Nested type: StatsBatcherFactory
653665

654-
public class StatsBatcherFactory : IBatcherFactory
666+
public partial class StatsBatcherFactory : IBatcherFactory
655667
{
656668
#region IBatcherFactory Members
657669

@@ -665,4 +677,66 @@ public IBatcher CreateBatcher(ConnectionManager connectionManager, IInterceptor
665677

666678
#endregion
667679
}
680+
/// <content>
681+
/// Contains generated async methods
682+
/// </content>
683+
public partial class InsertOrderingFixture : TestCase
684+
{
685+
686+
687+
688+
689+
690+
691+
692+
693+
694+
695+
696+
697+
698+
/// <content>
699+
/// Contains generated async methods
700+
/// </content>
701+
public partial class StatsBatcher : SqlClientBatchingBatcher
702+
{
703+
704+
public override async System.Threading.Tasks.Task<DbCommand> PrepareBatchCommandAsync(CommandType type, SqlString sql, SqlType[] parameterTypes, CancellationToken cancellationToken)
705+
{
706+
var result = await (base.PrepareBatchCommandAsync(type, sql, parameterTypes, cancellationToken));
707+
708+
PrepareStats(sql);
709+
710+
return result;
711+
}
712+
713+
public override System.Threading.Tasks.Task AddToBatchAsync(IExpectation expectation, CancellationToken cancellationToken)
714+
{
715+
try
716+
{
717+
AddStats();
718+
return base.AddToBatchAsync(expectation, cancellationToken);
719+
}
720+
catch (Exception ex)
721+
{
722+
return System.Threading.Tasks.Task.FromException<object>(ex);
723+
}
724+
}
725+
726+
protected override System.Threading.Tasks.Task DoExecuteBatchAsync(DbCommand ps, CancellationToken cancellationToken)
727+
{
728+
try
729+
{
730+
ExecuteStats();
731+
return base.DoExecuteBatchAsync(ps, cancellationToken);
732+
}
733+
catch (Exception ex)
734+
{
735+
return System.Threading.Tasks.Task.FromException<object>(ex);
736+
}
737+
}
738+
}
739+
740+
741+
}
668742
}

src/NHibernate.Test/Async/Linq/QueryTimeoutTests.cs

Lines changed: 42 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
namespace NHibernate.Test.Linq
2323
{
2424
[TestFixture]
25-
public class QueryTimeoutTestsAsync : LinqTestCase
25+
public partial class QueryTimeoutTestsAsync : LinqTestCase
2626
{
2727
protected override bool AppliesTo(Dialect.Dialect dialect)
2828
{
@@ -99,7 +99,7 @@ into g
9999
}
100100

101101

102-
public class TimeoutCatchingNonBatchingBatcher : NonBatchingBatcher
102+
public partial class TimeoutCatchingNonBatchingBatcher : NonBatchingBatcher
103103
{
104104
// Is there an easier way to inspect the DbCommand instead of
105105
// creating a custom batcher?
@@ -112,26 +112,60 @@ public TimeoutCatchingNonBatchingBatcher(ConnectionManager connectionManager, II
112112
{
113113
}
114114

115-
public override DbDataReader ExecuteReader(DbCommand cmd)
115+
public override Task<DbDataReader> ExecuteReaderAsync(DbCommand cmd, CancellationToken cancellationToken)
116116
{
117-
LastCommandTimeout = cmd.CommandTimeout;
118-
return base.ExecuteReader(cmd);
117+
try
118+
{
119+
LastCommandTimeout = cmd.CommandTimeout;
120+
return base.ExecuteReaderAsync(cmd, cancellationToken);
121+
}
122+
catch (System.Exception ex)
123+
{
124+
return Task.FromException<DbDataReader>(ex);
125+
}
119126
}
120127

121-
public override Task<DbDataReader> ExecuteReaderAsync(DbCommand cmd, CancellationToken cancellationToken)
128+
public override DbDataReader ExecuteReader(DbCommand cmd)
122129
{
123130
LastCommandTimeout = cmd.CommandTimeout;
124-
return base.ExecuteReaderAsync(cmd, cancellationToken);
131+
return base.ExecuteReader(cmd);
125132
}
126133
}
127134

128135

129-
public class TimeoutCatchingNonBatchingBatcherFactory : IBatcherFactory
136+
public partial class TimeoutCatchingNonBatchingBatcherFactory : IBatcherFactory
130137
{
131138
public IBatcher CreateBatcher(ConnectionManager connectionManager, IInterceptor interceptor)
132139
{
133140
return new TimeoutCatchingNonBatchingBatcher(connectionManager, interceptor);
134141
}
135142
}
136143
}
144+
/// <content>
145+
/// Contains generated async methods
146+
/// </content>
147+
public partial class QueryTimeoutTests : LinqTestCase
148+
{
149+
150+
151+
/// <content>
152+
/// Contains generated async methods
153+
/// </content>
154+
public partial class TimeoutCatchingNonBatchingBatcher : NonBatchingBatcher
155+
{
156+
157+
public override Task<DbDataReader> ExecuteReaderAsync(DbCommand cmd, CancellationToken cancellationToken)
158+
{
159+
try
160+
{
161+
LastCommandTimeout = cmd.CommandTimeout;
162+
return base.ExecuteReaderAsync(cmd, cancellationToken);
163+
}
164+
catch (System.Exception ex)
165+
{
166+
return Task.FromException<DbDataReader>(ex);
167+
}
168+
}
169+
}
170+
}
137171
}

src/NHibernate.Test/DebugConnectionProvider.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ namespace NHibernate.Test
1111
/// This connection provider keeps a list of all open connections,
1212
/// it is used when testing to check that tests clean up after themselves.
1313
/// </summary>
14-
public class DebugConnectionProvider : DriverConnectionProvider
14+
public partial class DebugConnectionProvider : DriverConnectionProvider
1515
{
1616
private ConcurrentDictionary<DbConnection, byte> connections = new ConcurrentDictionary<DbConnection, byte>();
1717

src/NHibernate.Test/Events/Collections/CollectionListeners.cs

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ public interface IListener
137137

138138
#region Nested type: InitializeCollectionListener
139139

140-
public class InitializeCollectionListener : DefaultInitializeCollectionEventListener, IListener
140+
public partial class InitializeCollectionListener : DefaultInitializeCollectionEventListener, IListener
141141
{
142142
private readonly CollectionListeners listeners;
143143

@@ -160,15 +160,6 @@ public override void OnInitializeCollection(InitializeCollectionEvent @event)
160160
base.OnInitializeCollection(@event);
161161
AddEvent(@event, this);
162162
}
163-
164-
public override Task OnInitializeCollectionAsync(InitializeCollectionEvent @event, CancellationToken cancellationToken)
165-
{
166-
var result = base.OnInitializeCollectionAsync(@event, cancellationToken);
167-
168-
AddEvent(@event, this);
169-
170-
return result;
171-
}
172163
}
173164

174165
#endregion

0 commit comments

Comments
 (0)