99
1010
1111using System ;
12- using System . Collections ;
12+ using System . Data ;
1313using System . Data . Common ;
14+ using NHibernate . Transaction ;
1415using NUnit . Framework ;
1516
1617namespace NHibernate . Test . TransactionTest
@@ -20,22 +21,23 @@ namespace NHibernate.Test.TransactionTest
2021 [ TestFixture ]
2122 public class TransactionNotificationFixtureAsync : TestCase
2223 {
23- protected override string [ ] Mappings
24- {
25- get { return Array . Empty < string > ( ) ; }
26- }
24+ protected override string [ ] Mappings => Array . Empty < string > ( ) ;
2725
2826 [ Test ]
2927 public async Task CommitAsync ( )
3028 {
3129 var interceptor = new RecordingInterceptor ( ) ;
3230 using ( var session = Sfi . WithOptions ( ) . Interceptor ( interceptor ) . OpenSession ( ) )
31+ using ( var tx = session . BeginTransaction ( ) )
3332 {
34- ITransaction tx = session . BeginTransaction ( ) ;
33+ var synchronisation = new Synchronization ( ) ;
34+ tx . RegisterSynchronization ( synchronisation ) ;
3535 await ( tx . CommitAsync ( ) ) ;
36- Assert . That ( interceptor . afterTransactionBeginCalled , Is . EqualTo ( 1 ) ) ;
37- Assert . That ( interceptor . beforeTransactionCompletionCalled , Is . EqualTo ( 1 ) ) ;
38- Assert . That ( interceptor . afterTransactionCompletionCalled , Is . EqualTo ( 1 ) ) ;
36+ Assert . That ( interceptor . afterTransactionBeginCalled , Is . EqualTo ( 1 ) , "interceptor begin" ) ;
37+ Assert . That ( interceptor . beforeTransactionCompletionCalled , Is . EqualTo ( 1 ) , "interceptor before" ) ;
38+ Assert . That ( interceptor . afterTransactionCompletionCalled , Is . EqualTo ( 1 ) , "interceptor after" ) ;
39+ Assert . That ( synchronisation . BeforeExecutions , Is . EqualTo ( 1 ) , "sync before" ) ;
40+ Assert . That ( synchronisation . AfterExecutions , Is . EqualTo ( 1 ) , "sync after" ) ;
3941 }
4042 }
4143
@@ -44,26 +46,31 @@ public async Task RollbackAsync()
4446 {
4547 var interceptor = new RecordingInterceptor ( ) ;
4648 using ( var session = Sfi . WithOptions ( ) . Interceptor ( interceptor ) . OpenSession ( ) )
49+ using ( var tx = session . BeginTransaction ( ) )
4750 {
48- ITransaction tx = session . BeginTransaction ( ) ;
51+ var synchronisation = new Synchronization ( ) ;
52+ tx . RegisterSynchronization ( synchronisation ) ;
4953 await ( tx . RollbackAsync ( ) ) ;
50- Assert . That ( interceptor . afterTransactionBeginCalled , Is . EqualTo ( 1 ) ) ;
51- Assert . That ( interceptor . beforeTransactionCompletionCalled , Is . EqualTo ( 0 ) ) ;
52- Assert . That ( interceptor . afterTransactionCompletionCalled , Is . EqualTo ( 1 ) ) ;
54+ Assert . That ( interceptor . afterTransactionBeginCalled , Is . EqualTo ( 1 ) , "interceptor begin" ) ;
55+ Assert . That ( interceptor . beforeTransactionCompletionCalled , Is . EqualTo ( 0 ) , "interceptor before" ) ;
56+ Assert . That ( interceptor . afterTransactionCompletionCalled , Is . EqualTo ( 1 ) , "interceptor after" ) ;
57+ Assert . That ( synchronisation . BeforeExecutions , Is . EqualTo ( 0 ) , "sync before" ) ;
58+ Assert . That ( synchronisation . AfterExecutions , Is . EqualTo ( 1 ) , "sync after" ) ;
5359 }
5460 }
5561
56-
5762 [ Theory ]
5863 [ Description ( "NH2128" ) ]
5964 public async Task ShouldNotifyAfterTransactionAsync ( bool usePrematureClose )
6065 {
6166 var interceptor = new RecordingInterceptor ( ) ;
67+ var synchronisation = new Synchronization ( ) ;
6268 ISession s ;
6369
6470 using ( s = OpenSession ( interceptor ) )
65- using ( s . BeginTransaction ( ) )
71+ using ( var t = s . BeginTransaction ( ) )
6672 {
73+ t . RegisterSynchronization ( synchronisation ) ;
6774 await ( s . CreateCriteria < object > ( ) . ListAsync ( ) ) ;
6875
6976 // Call session close while still inside transaction?
@@ -72,22 +79,24 @@ public async Task ShouldNotifyAfterTransactionAsync(bool usePrematureClose)
7279 }
7380
7481 Assert . That ( s . IsOpen , Is . False ) ;
75- Assert . That ( interceptor . afterTransactionCompletionCalled , Is . EqualTo ( 1 ) ) ;
82+ Assert . That ( interceptor . afterTransactionCompletionCalled , Is . EqualTo ( 1 ) , "interceptor" ) ;
83+ Assert . That ( synchronisation . AfterExecutions , Is . EqualTo ( 1 ) , "sync" ) ;
7684 }
7785
78-
7986 [ Description ( "NH2128" ) ]
8087 [ Theory ]
8188 public async Task ShouldNotifyAfterTransactionWithOwnConnectionAsync ( bool usePrematureClose )
8289 {
8390 var interceptor = new RecordingInterceptor ( ) ;
91+ var synchronisation = new Synchronization ( ) ;
8492 ISession s ;
8593
8694 using ( var ownConnection = await ( Sfi . ConnectionProvider . GetConnectionAsync ( CancellationToken . None ) ) )
8795 {
8896 using ( s = Sfi . WithOptions ( ) . Connection ( ownConnection ) . Interceptor ( interceptor ) . OpenSession ( ) )
89- using ( s . BeginTransaction ( ) )
97+ using ( var t = s . BeginTransaction ( ) )
9098 {
99+ t . RegisterSynchronization ( synchronisation ) ;
91100 await ( s . CreateCriteria < object > ( ) . ListAsync ( ) ) ;
92101
93102 // Call session close while still inside transaction?
@@ -97,7 +106,56 @@ public async Task ShouldNotifyAfterTransactionWithOwnConnectionAsync(bool usePre
97106 }
98107
99108 Assert . That ( s . IsOpen , Is . False ) ;
100- Assert . That ( interceptor . afterTransactionCompletionCalled , Is . EqualTo ( 1 ) ) ;
109+ Assert . That ( interceptor . afterTransactionCompletionCalled , Is . EqualTo ( 1 ) , "interceptor" ) ;
110+ Assert . That ( synchronisation . AfterExecutions , Is . EqualTo ( 1 ) , "sync" ) ;
111+ }
112+ }
113+
114+ #region Synchronization classes
115+
116+ public partial class CustomTransaction : ITransaction
117+ {
118+
119+ public Task CommitAsync ( CancellationToken cancellationToken = default ( CancellationToken ) )
120+ {
121+ throw new NotImplementedException ( ) ;
122+ }
123+
124+ public Task RollbackAsync ( CancellationToken cancellationToken = default ( CancellationToken ) )
125+ {
126+ throw new NotImplementedException ( ) ;
127+ }
128+ }
129+
130+ public partial class Synchronization : ITransactionCompletionSynchronization
131+ {
132+
133+ public Task ExecuteBeforeTransactionCompletionAsync ( CancellationToken cancellationToken )
134+ {
135+ try
136+ {
137+ BeforeExecutions += 1 ;
138+ return Task . CompletedTask ;
139+ }
140+ catch ( Exception ex )
141+ {
142+ return Task . FromException < object > ( ex ) ;
143+ }
144+ }
145+
146+ public Task ExecuteAfterTransactionCompletionAsync ( bool success , CancellationToken cancellationToken )
147+ {
148+ try
149+ {
150+ AfterExecutions += 1 ;
151+ return Task . CompletedTask ;
152+ }
153+ catch ( Exception ex )
154+ {
155+ return Task . FromException < object > ( ex ) ;
156+ }
101157 }
102158 }
159+
160+ #endregion
103161}
0 commit comments