4
4
// Created by: Denis Krjuchkov
5
5
// Created: 2009.11.26
6
6
7
+ using System . Collections . Generic ;
8
+ using System . Linq ;
7
9
using NUnit . Framework ;
8
10
using Xtensive . Core ;
9
11
using Xtensive . Orm . Providers ;
@@ -13,25 +15,28 @@ namespace Xtensive.Orm.Tests.Storage
13
15
{
14
16
public class NestedTransactionsTest : TransactionsTestBase
15
17
{
18
+ private StorageProviderInfo storageProviderInfo ;
19
+ private Session globalSession ;
20
+
16
21
public override void TestFixtureSetUp ( )
17
22
{
18
23
base . TestFixtureSetUp ( ) ;
19
- Domain . OpenSession ( ) ;
24
+ globalSession = Domain . OpenSession ( ) ;
25
+ storageProviderInfo = StorageProviderInfo . Instance ;
20
26
}
21
27
22
- public override void TestFixtureTearDown ( )
23
- {
24
- Session . Current . DisposeSafely ( ) ;
25
- }
28
+ protected override void CheckRequirements ( )
29
+ => Require . AllFeaturesSupported ( ProviderFeatures . Savepoints ) ;
30
+
31
+ public override void TestFixtureTearDown ( ) => globalSession . DisposeSafely ( ) ;
26
32
27
33
[ Test ]
28
34
public void UnmodifiedStateIsValidInInnerTransactionTest ( )
29
35
{
30
- Require . AllFeaturesSupported ( ProviderFeatures . Savepoints ) ;
31
- using ( var outerScope = Session . Demand ( ) . OpenTransaction ( ) ) {
36
+ using ( var outerScope = globalSession . OpenTransaction ( ) ) {
32
37
var outerTransaction = Transaction . Current ;
33
38
var theHexagon = new Hexagon ( ) ;
34
- using ( var innerScope = Session . Demand ( ) . OpenTransaction ( TransactionOpenMode . New ) ) {
39
+ using ( var innerScope = globalSession . OpenTransaction ( TransactionOpenMode . New ) ) {
35
40
AssertStateIsValid ( theHexagon ) ;
36
41
Assert . AreEqual ( theHexagon . Kwanza , 0 ) ;
37
42
AssertStateIsValid ( theHexagon ) ;
@@ -43,11 +48,10 @@ public void UnmodifiedStateIsValidInInnerTransactionTest()
43
48
[ Test ]
44
49
public void ModifiedStateIsValidInInnerTransactionTest ( )
45
50
{
46
- Require . AllFeaturesSupported ( ProviderFeatures . Savepoints ) ;
47
- using ( var outerScope = Session . Demand ( ) . OpenTransaction ( ) ) {
51
+ using ( var outerScope = globalSession . OpenTransaction ( ) ) {
48
52
var outerTransaction = Transaction . Current ;
49
53
var theHexagon = new Hexagon ( ) ;
50
- using ( var innerScope = Session . Demand ( ) . OpenTransaction ( TransactionOpenMode . New ) ) {
54
+ using ( var innerScope = globalSession . OpenTransaction ( TransactionOpenMode . New ) ) {
51
55
theHexagon . IncreaseKwanza ( ) ;
52
56
AssertStateIsValid ( theHexagon ) ;
53
57
Assert . AreEqual ( theHexagon . Kwanza , 1 ) ;
@@ -59,11 +63,10 @@ public void ModifiedStateIsValidInInnerTransactionTest()
59
63
[ Test ]
60
64
public void UnmodifiedStateIsValidInOuterTransactionAfterInnerTransactionRolledBackTest ( )
61
65
{
62
- Require . AllFeaturesSupported ( ProviderFeatures . Savepoints ) ;
63
- using ( var outerScope = Session . Demand ( ) . OpenTransaction ( ) ) {
66
+ using ( var outerScope = globalSession . OpenTransaction ( ) ) {
64
67
var outerTransaction = Transaction . Current ;
65
68
var theHexagon = new Hexagon ( ) ;
66
- using ( var innerScope = Session . Demand ( ) . OpenTransaction ( TransactionOpenMode . New ) ) {
69
+ using ( var innerScope = globalSession . OpenTransaction ( TransactionOpenMode . New ) ) {
67
70
// rollback
68
71
}
69
72
AssertStateIsValid ( theHexagon ) ;
@@ -74,11 +77,10 @@ public void UnmodifiedStateIsValidInOuterTransactionAfterInnerTransactionRolledB
74
77
[ Test ]
75
78
public void ModifiedStateIsInvalidInOuterTransactionAfterInnerTransactionRolledBackTest ( )
76
79
{
77
- Require . AllFeaturesSupported ( ProviderFeatures . Savepoints ) ;
78
- using ( var outerScope = Session . Demand ( ) . OpenTransaction ( ) ) {
80
+ using ( var outerScope = globalSession . OpenTransaction ( ) ) {
79
81
var outerTransaction = Transaction . Current ;
80
82
var theHexagon = new Hexagon ( ) ;
81
- using ( var innerScope = Session . Demand ( ) . OpenTransaction ( TransactionOpenMode . New ) ) {
83
+ using ( var innerScope = globalSession . OpenTransaction ( TransactionOpenMode . New ) ) {
82
84
theHexagon . IncreaseKwanza ( ) ;
83
85
// rollback
84
86
}
@@ -90,11 +92,10 @@ public void ModifiedStateIsInvalidInOuterTransactionAfterInnerTransactionRolledB
90
92
[ Test ]
91
93
public void UnmodifiedStateIsValidInOuterTransactionAfterInnerTransactionCommitedTest ( )
92
94
{
93
- Require . AllFeaturesSupported ( ProviderFeatures . Savepoints ) ;
94
- using ( var outerScope = Session . Demand ( ) . OpenTransaction ( ) ) {
95
+ using ( var outerScope = globalSession . OpenTransaction ( ) ) {
95
96
var outerTransaction = Transaction . Current ;
96
97
var theHexagon = new Hexagon ( ) ;
97
- using ( var innerScope = Session . Demand ( ) . OpenTransaction ( TransactionOpenMode . New ) ) {
98
+ using ( var innerScope = globalSession . OpenTransaction ( TransactionOpenMode . New ) ) {
98
99
innerScope . Complete ( ) ;
99
100
}
100
101
AssertStateIsValid ( theHexagon ) ;
@@ -105,11 +106,10 @@ public void UnmodifiedStateIsValidInOuterTransactionAfterInnerTransactionCommite
105
106
[ Test ]
106
107
public void ModifiedStateIsValidInOuterTransactionAfterInnerTransactionCommitedTest ( )
107
108
{
108
- Require . AllFeaturesSupported ( ProviderFeatures . Savepoints ) ;
109
- using ( var outerScope = Session . Demand ( ) . OpenTransaction ( ) ) {
109
+ using ( var outerScope = globalSession . OpenTransaction ( ) ) {
110
110
var outerTransaction = Transaction . Current ;
111
111
var theHexagon = new Hexagon ( ) ;
112
- using ( var innerScope = Session . Demand ( ) . OpenTransaction ( TransactionOpenMode . New ) ) {
112
+ using ( var innerScope = globalSession . OpenTransaction ( TransactionOpenMode . New ) ) {
113
113
theHexagon . IncreaseKwanza ( ) ;
114
114
innerScope . Complete ( ) ;
115
115
}
@@ -121,14 +121,114 @@ public void ModifiedStateIsValidInOuterTransactionAfterInnerTransactionCommitedT
121
121
[ Test ]
122
122
public void WrongNestedTransactionUsageTest ( )
123
123
{
124
- Require . AllFeaturesSupported ( ProviderFeatures . Savepoints ) ;
125
- using ( var outerScope = Session . Demand ( ) . OpenTransaction ( ) )
126
- using ( var innerScope = Session . Demand ( ) . OpenTransaction ( TransactionOpenMode . New ) ) {
124
+ using ( var outerScope = globalSession . OpenTransaction ( ) )
125
+ using ( var innerScope = globalSession . OpenTransaction ( TransactionOpenMode . New ) ) {
127
126
outerScope . Complete ( ) ;
128
127
AssertEx . ThrowsInvalidOperationException ( outerScope . Dispose ) ;
129
128
}
130
129
Assert . IsNull ( Session . Current . Transaction ) ;
131
130
Assert . IsNull ( StorageTestHelper . GetNativeTransaction ( ) ) ;
132
131
}
132
+
133
+ [ Test ]
134
+ public void RollbackNestedTransactionWithActiveEnumeratorTest ( )
135
+ {
136
+ var session = Session . Demand ( ) ;
137
+ using ( var outerTx = session . OpenTransaction ( ) ) {
138
+ _ = new Hexagon ( ) ;
139
+ _ = new Hexagon ( ) ;
140
+ _ = new Hexagon ( ) ;
141
+
142
+ IEnumerator < int > enumerator = null ;
143
+ var innerTx = session . OpenTransaction ( TransactionOpenMode . New ) ;
144
+
145
+ enumerator = session . Query . All < Hexagon > ( )
146
+ . Select ( item => item . Id ) . AsEnumerable ( ) . GetEnumerator ( ) ;
147
+ _ = enumerator . MoveNext ( ) ;
148
+
149
+ if ( storageProviderInfo . CheckProviderIs ( StorageProvider . SqlServer ) ) {
150
+ _ = Assert . Throws < StorageException > ( ( ) => innerTx . Dispose ( ) ) ;
151
+ }
152
+ else {
153
+ Assert . DoesNotThrow ( ( ) => innerTx . Dispose ( ) ) ;
154
+ }
155
+ }
156
+ }
157
+
158
+ [ Test ]
159
+ public void RollbackNestedTransactionWithActiveEnumeratorAndThenCompleteOutermostTest ( )
160
+ {
161
+ var session = Session . Demand ( ) ;
162
+ using ( var outerTx = session . OpenTransaction ( ) ) {
163
+ _ = new Hexagon ( ) ;
164
+ _ = new Hexagon ( ) ;
165
+ _ = new Hexagon ( ) ;
166
+
167
+ IEnumerator < int > enumerator = null ;
168
+ var innerTx = session . OpenTransaction ( TransactionOpenMode . New ) ;
169
+
170
+ enumerator = session . Query . All < Hexagon > ( )
171
+ . Select ( item => item . Id ) . AsEnumerable ( ) . GetEnumerator ( ) ;
172
+ _ = enumerator . MoveNext ( ) ;
173
+
174
+ if ( storageProviderInfo . CheckProviderIs ( StorageProvider . SqlServer ) ) {
175
+ _ = Assert . Throws < StorageException > ( ( ) => innerTx . Dispose ( ) ) ;
176
+ }
177
+ else {
178
+ Assert . DoesNotThrow ( ( ) => innerTx . Dispose ( ) ) ;
179
+ }
180
+ outerTx . Complete ( ) ;
181
+ }
182
+ }
183
+
184
+ [ Test ]
185
+ public void CommitNestedTransactionWithActiveEnumeratorAndRollbackOutermostTest ( )
186
+ {
187
+ var session = Session . Demand ( ) ;
188
+ using ( var outerTx = session . OpenTransaction ( ) ) {
189
+ _ = new Hexagon ( ) ;
190
+ _ = new Hexagon ( ) ;
191
+ _ = new Hexagon ( ) ;
192
+
193
+ IEnumerator < int > enumerator = null ;
194
+ var innerTx = session . OpenTransaction ( TransactionOpenMode . New ) ;
195
+
196
+ enumerator = session . Query . All < Hexagon > ( )
197
+ . Select ( item => item . Id ) . AsEnumerable ( ) . GetEnumerator ( ) ;
198
+ _ = enumerator . MoveNext ( ) ;
199
+
200
+ innerTx . Complete ( ) ;
201
+ innerTx . Dispose ( ) ;
202
+ }
203
+ }
204
+
205
+ [ Test ]
206
+ public void CommitNestedTransactionWithActiveEnumeratorAndCommitOutermostTest ( )
207
+ {
208
+ var session = Session . Demand ( ) ;
209
+ var outerTx = session . OpenTransaction ( ) ;
210
+ _ = new Hexagon ( ) ;
211
+ _ = new Hexagon ( ) ;
212
+ _ = new Hexagon ( ) ;
213
+
214
+ IEnumerator < int > enumerator = null ;
215
+ var innerTx = session . OpenTransaction ( TransactionOpenMode . New ) ;
216
+
217
+ enumerator = session . Query . All < Hexagon > ( )
218
+ . Select ( item => item . Id ) . AsEnumerable ( ) . GetEnumerator ( ) ;
219
+ _ = enumerator . MoveNext ( ) ;
220
+
221
+ innerTx . Complete ( ) ;
222
+ innerTx . Dispose ( ) ;
223
+
224
+ outerTx . Complete ( ) ;
225
+ if ( storageProviderInfo . CheckProviderIs ( StorageProvider . SqlServer ) ) {
226
+ var exception = Assert . Throws < StorageException > ( ( ) => outerTx . Dispose ( ) ) ;
227
+ Assert . That ( exception . InnerException , Is . InstanceOf < System . Data . SqlClient . SqlException > ( ) ) ;
228
+ }
229
+ else {
230
+ Assert . DoesNotThrow ( ( ) => outerTx . Dispose ( ) ) ;
231
+ }
232
+ }
133
233
}
134
234
}
0 commit comments