9
9
10
10
11
11
using System ;
12
- using System . Collections ;
12
+ using System . Data ;
13
13
using System . Data . Common ;
14
+ using NHibernate . Transaction ;
14
15
using NUnit . Framework ;
15
16
16
17
namespace NHibernate . Test . TransactionTest
@@ -20,22 +21,23 @@ namespace NHibernate.Test.TransactionTest
20
21
[ TestFixture ]
21
22
public class TransactionNotificationFixtureAsync : TestCase
22
23
{
23
- protected override string [ ] Mappings
24
- {
25
- get { return Array . Empty < string > ( ) ; }
26
- }
24
+ protected override string [ ] Mappings => Array . Empty < string > ( ) ;
27
25
28
26
[ Test ]
29
27
public async Task CommitAsync ( )
30
28
{
31
29
var interceptor = new RecordingInterceptor ( ) ;
32
30
using ( var session = Sfi . WithOptions ( ) . Interceptor ( interceptor ) . OpenSession ( ) )
31
+ using ( var tx = session . BeginTransaction ( ) )
33
32
{
34
- ITransaction tx = session . BeginTransaction ( ) ;
33
+ var synchronisation = new Synchronization ( ) ;
34
+ tx . RegisterSynchronization ( synchronisation ) ;
35
35
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" ) ;
39
41
}
40
42
}
41
43
@@ -44,26 +46,31 @@ public async Task RollbackAsync()
44
46
{
45
47
var interceptor = new RecordingInterceptor ( ) ;
46
48
using ( var session = Sfi . WithOptions ( ) . Interceptor ( interceptor ) . OpenSession ( ) )
49
+ using ( var tx = session . BeginTransaction ( ) )
47
50
{
48
- ITransaction tx = session . BeginTransaction ( ) ;
51
+ var synchronisation = new Synchronization ( ) ;
52
+ tx . RegisterSynchronization ( synchronisation ) ;
49
53
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" ) ;
53
59
}
54
60
}
55
61
56
-
57
62
[ Theory ]
58
63
[ Description ( "NH2128" ) ]
59
64
public async Task ShouldNotifyAfterTransactionAsync ( bool usePrematureClose )
60
65
{
61
66
var interceptor = new RecordingInterceptor ( ) ;
67
+ var synchronisation = new Synchronization ( ) ;
62
68
ISession s ;
63
69
64
70
using ( s = OpenSession ( interceptor ) )
65
- using ( s . BeginTransaction ( ) )
71
+ using ( var t = s . BeginTransaction ( ) )
66
72
{
73
+ t . RegisterSynchronization ( synchronisation ) ;
67
74
await ( s . CreateCriteria < object > ( ) . ListAsync ( ) ) ;
68
75
69
76
// Call session close while still inside transaction?
@@ -72,22 +79,24 @@ public async Task ShouldNotifyAfterTransactionAsync(bool usePrematureClose)
72
79
}
73
80
74
81
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" ) ;
76
84
}
77
85
78
-
79
86
[ Description ( "NH2128" ) ]
80
87
[ Theory ]
81
88
public async Task ShouldNotifyAfterTransactionWithOwnConnectionAsync ( bool usePrematureClose )
82
89
{
83
90
var interceptor = new RecordingInterceptor ( ) ;
91
+ var synchronisation = new Synchronization ( ) ;
84
92
ISession s ;
85
93
86
94
using ( var ownConnection = await ( Sfi . ConnectionProvider . GetConnectionAsync ( CancellationToken . None ) ) )
87
95
{
88
96
using ( s = Sfi . WithOptions ( ) . Connection ( ownConnection ) . Interceptor ( interceptor ) . OpenSession ( ) )
89
- using ( s . BeginTransaction ( ) )
97
+ using ( var t = s . BeginTransaction ( ) )
90
98
{
99
+ t . RegisterSynchronization ( synchronisation ) ;
91
100
await ( s . CreateCriteria < object > ( ) . ListAsync ( ) ) ;
92
101
93
102
// Call session close while still inside transaction?
@@ -97,7 +106,56 @@ public async Task ShouldNotifyAfterTransactionWithOwnConnectionAsync(bool usePre
97
106
}
98
107
99
108
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
+ }
101
157
}
102
158
}
159
+
160
+ #endregion
103
161
}
0 commit comments