@@ -118,53 +118,47 @@ test('test error in COMMIT', assert => {
118
118
}
119
119
}
120
120
} )
121
- return
122
- test ( 'test error in ROLLBACK' , assert => {
121
+
122
+ test ( 'test error in ROLLBACK: does not reuse connection ' , assert => {
123
123
const domain1 = domain . create ( )
124
+ class RollbackError extends Error { }
124
125
125
- db . install ( domain1 , ( ) => new Promise ( ( _ , reject ) => {
126
- reject ( new TestError ( 'cannot connect' ) )
127
- } ) , { maxConcurrency : 0 } )
128
-
126
+ db . install ( domain1 , getConnection , { maxConcurrency : 1 } )
127
+
128
+ var connectionPair = null
129
129
domain1 . run ( ( ) => {
130
- return db . transaction ( ( ) => {
131
- assert . fail ( 'should not reach here.' )
132
- } ) ( )
133
- } )
134
- . catch ( err => assert . fail ( err ) )
135
- . finally ( ( ) => domain1 . exit ( ) )
136
- . finally ( assert . end )
137
- } )
130
+ const first = db . transaction ( ( ) => {
131
+ return db . getConnection ( ) . then ( pair => {
132
+ connectionPair = pair . pair
133
+ pair . release ( )
134
+ throw new Error ( 'any kind of error, really' )
135
+ } )
136
+ } ) ( ) . reflect ( )
138
137
139
- // what happens when getConnection fails before BEGIN
140
- test ( 'test getConnection error' , assert => {
141
- const domain1 = domain . create ( )
138
+ const second = db . getConnection ( ) . then ( pair => {
139
+ // with concurrency=1, we will try to re-use
140
+ // the connection if we can. since we had an error,
141
+ // it's best not to use the connection!
142
+ assert . notEqual ( connectionPair , pair )
143
+ pair . release ( )
144
+ } )
142
145
143
- db . install ( domain1 , ( ) => new Promise ( ( _ , reject ) => {
144
- reject ( new TestError ( 'cannot connect' ) )
145
- } ) , { maxConcurrency : 0 } )
146
-
147
- domain1 . run ( ( ) => {
148
- return db . transaction ( ( ) => {
149
- assert . fail ( 'should not reach here.' )
150
- } ) ( )
146
+ return Promise . join ( first , second )
151
147
} )
152
148
. catch ( err => assert . fail ( err ) )
153
149
. finally ( ( ) => domain1 . exit ( ) )
154
150
. finally ( assert . end )
155
- } )
156
151
157
- function innerGetConnection ( ) {
158
- return {
159
- connection : { query ( sql , ready ) {
160
- if ( shouldErrorToggle ) {
161
- var err = shouldErrorToggle
162
- shouldErrorToggle = false
163
- return ready ( err )
152
+ function getConnection ( ) {
153
+ return {
154
+ connection : { query ( sql , ready ) {
155
+ if ( sql === 'ROLLBACK' ) {
156
+ return ready ( new RollbackError ( 'failed ROLLBACK' ) )
157
+ }
158
+ return ready ( )
159
+ } } ,
160
+ release ( err ) {
164
161
}
165
- return ready ( )
166
- } } ,
167
- release ( err ) {
168
162
}
169
163
}
170
- }
164
+ } )
0 commit comments