@@ -91,17 +91,57 @@ class TransactionsTestContext {
91
91
constructor ( ) {
92
92
this . url = null ;
93
93
this . sharedClient = null ;
94
+ this . failPointClients = [ ] ;
95
+ }
96
+
97
+ runForAllClients ( fn ) {
98
+ const allClients = [ this . sharedClient ] . concat ( this . failPointClients ) ;
99
+ return Promise . all ( allClients . map ( fn ) ) ;
100
+ }
101
+
102
+ runFailPointCmd ( fn ) {
103
+ return this . failPointClients . length
104
+ ? Promise . all ( this . failPointClients . map ( fn ) )
105
+ : fn ( this . sharedClient ) ;
94
106
}
95
107
96
108
setup ( config ) {
97
- this . url = `mongodb://${ config . host } :${ config . port } /?replicaSet=${ config . replicasetName } ` ;
109
+ if ( config . options . proxies ) {
110
+ const mainProxy = config . options . proxies [ 0 ] ;
111
+ this . url = `mongodb://${ mainProxy . host } :${ mainProxy . port } /` ;
112
+
113
+ this . failPointClients = config . options . proxies . map ( proxy =>
114
+ config . newClient ( `mongodb://${ proxy . host } :${ proxy . port } /` )
115
+ ) ;
116
+ } else {
117
+ this . url = config . url ( ) ;
118
+ }
98
119
99
- this . sharedClient = config . newClient ( this . url ) ;
100
- return this . sharedClient . connect ( ) ;
120
+ this . sharedClient = this . newClient ( config ) ;
121
+ return this . runForAllClients ( client => client . connect ( ) ) ;
122
+ }
123
+
124
+ newClient ( config , clientOptions ) {
125
+ return config . newClient ( this . url , clientOptions ) ;
101
126
}
102
127
103
128
teardown ( ) {
104
- return this . sharedClient . close ( ) ;
129
+ return this . runForAllClients ( client => client . close ( ) ) ;
130
+ }
131
+
132
+ enableFailPoint ( failPoint ) {
133
+ return this . runFailPointCmd ( client => {
134
+ return client . db ( this . dbName ) . executeDbAdminCommand ( failPoint ) ;
135
+ } ) ;
136
+ }
137
+
138
+ disableFailPoint ( failPoint ) {
139
+ return this . runFailPointCmd ( client => {
140
+ return client . db ( this . dbName ) . executeDbAdminCommand ( {
141
+ configureFailPoint : failPoint . configureFailPoint ,
142
+ mode : 'off'
143
+ } ) ;
144
+ } ) ;
105
145
}
106
146
}
107
147
@@ -123,7 +163,7 @@ describe('Transactions', function() {
123
163
return testContext . setup ( this . configuration ) ;
124
164
} ) ;
125
165
126
- generateTestSuiteTests ( testSuites , testContext ) ;
166
+ generateTopologyTests ( testSuites , testContext ) ;
127
167
} ) ;
128
168
} ) ;
129
169
@@ -140,7 +180,7 @@ describe('Transactions', function() {
140
180
} ) ;
141
181
142
182
it ( 'should provide a useful error if a Promise is not returned' , {
143
- metadata : { requires : { topology : [ 'replicaset' , 'mongos ' ] , mongodb : '>=4.0.x ' } } ,
183
+ metadata : { requires : { topology : [ 'replicaset' , 'sharded ' ] , mongodb : '>=4.1.5 ' } } ,
144
184
test : function ( done ) {
145
185
function fnThatDoesntReturnPromise ( ) {
146
186
return false ;
@@ -156,27 +196,43 @@ describe('Transactions', function() {
156
196
} ) ;
157
197
} ) ;
158
198
159
- function generateTestSuiteTests ( testSuites , testContext ) {
199
+ function generateTopologyTests ( testSuites , testContext ) {
200
+ [
201
+ { topology : 'replicaset' , minServerVersion : '>=3.7.x' } ,
202
+ { topology : 'sharded' , minServerVersion : '>=4.1.5' }
203
+ ] . forEach ( config => {
204
+ describe ( config . topology , function ( ) {
205
+ generateTestSuiteTests ( testSuites , testContext , config ) ;
206
+ } ) ;
207
+ } ) ;
208
+ }
209
+
210
+ function generateTestSuiteTests ( testSuites , testContext , config ) {
160
211
testSuites . forEach ( testSuite => {
161
212
const minServerVersion = testSuite . minServerVersion
162
213
? `>=${ testSuite . minServerVersion } `
163
- : '>=3.7.x' ;
214
+ : config . minServerVersion ;
215
+
216
+ const metadata = {
217
+ requires : {
218
+ topology : [ config . topology ] ,
219
+ mongodb : minServerVersion
220
+ }
221
+ } ;
164
222
165
223
describe ( testSuite . name , {
166
- metadata : { requires : { topology : [ 'replicaset' , 'mongos' ] , mongodb : minServerVersion } } ,
224
+ metadata,
167
225
test : function ( ) {
168
226
beforeEach ( ( ) => prepareDatabaseForSuite ( testSuite , testContext ) ) ;
169
227
afterEach ( ( ) => cleanupAfterSuite ( testContext ) ) ;
170
228
171
229
testSuite . tests . forEach ( testData => {
172
- const maybeSkipIt = testData . skipReason || testSuite . name === 'pin-mongos' ? it . skip : it ;
230
+ const maybeSkipIt = testData . skipReason ? it . skip : it ;
173
231
maybeSkipIt ( testData . description , function ( ) {
174
232
let testPromise = Promise . resolve ( ) ;
175
233
176
234
if ( testData . failPoint ) {
177
- testPromise = testPromise . then ( ( ) =>
178
- enableFailPoint ( testData . failPoint , testContext )
179
- ) ;
235
+ testPromise = testPromise . then ( ( ) => testContext . enableFailPoint ( testData . failPoint ) ) ;
180
236
}
181
237
182
238
// run the actual test
@@ -186,11 +242,11 @@ function generateTestSuiteTests(testSuites, testContext) {
186
242
187
243
if ( testData . failPoint ) {
188
244
testPromise = testPromise . then ( ( ) =>
189
- disableFailPoint ( testData . failPoint , testContext )
245
+ testContext . disableFailPoint ( testData . failPoint )
190
246
) ;
191
247
}
192
248
193
- return testPromise ;
249
+ return testPromise . then ( ( ) => validateOutcome ( testData , testContext ) ) ;
194
250
} ) ;
195
251
} ) ;
196
252
}
@@ -230,17 +286,6 @@ function cleanupAfterSuite(context) {
230
286
}
231
287
}
232
288
233
- function enableFailPoint ( failPoint , testContext ) {
234
- return testContext . sharedClient . db ( testContext . dbName ) . executeDbAdminCommand ( failPoint ) ;
235
- }
236
-
237
- function disableFailPoint ( failPoint , testContext ) {
238
- return testContext . sharedClient . db ( testContext . dbName ) . executeDbAdminCommand ( {
239
- configureFailPoint : failPoint . configureFailPoint ,
240
- mode : 'off'
241
- } ) ;
242
- }
243
-
244
289
function parseSessionOptions ( options ) {
245
290
const result = Object . assign ( { } , options ) ;
246
291
if ( result . defaultTransactionOptions && result . defaultTransactionOptions . readPreference ) {
@@ -263,7 +308,7 @@ function runTestSuiteTest(configuration, testData, context) {
263
308
clientOptions . autoReconnect = false ;
264
309
clientOptions . haInterval = 100 ;
265
310
266
- const client = configuration . newClient ( context . url , clientOptions ) ;
311
+ const client = context . newClient ( configuration , clientOptions ) ;
267
312
return client . connect ( ) . then ( client => {
268
313
context . testClient = client ;
269
314
client . on ( 'commandStarted' , event => {
@@ -310,6 +355,23 @@ function runTestSuiteTest(configuration, testData, context) {
310
355
} ) ;
311
356
}
312
357
358
+ function validateOutcome ( testData , testContext ) {
359
+ if ( testData . outcome ) {
360
+ if ( testData . outcome . collection ) {
361
+ // use the client without transactions to verify
362
+ return testContext . sharedClient
363
+ . db ( testContext . dbName )
364
+ . collection ( testContext . collectionName )
365
+ . find ( { } , { readPreference : 'primary' , readConcern : { level : 'local' } } )
366
+ . toArray ( )
367
+ . then ( docs => {
368
+ expect ( docs ) . to . eql ( testData . outcome . collection . data ) ;
369
+ } ) ;
370
+ }
371
+ }
372
+ return Promise . resolve ( ) ;
373
+ }
374
+
313
375
function validateExpectations ( commandEvents , testData , testContext , operationContext ) {
314
376
const session0 = operationContext . session0 ;
315
377
const session1 = operationContext . session1 ;
@@ -366,20 +428,6 @@ function validateExpectations(commandEvents, testData, testContext, operationCon
366
428
expect ( actualCommand ) . to . containSubset ( expectedCommand ) ;
367
429
} ) ;
368
430
}
369
-
370
- if ( testData . outcome ) {
371
- if ( testData . outcome . collection ) {
372
- // use the client without transactions to verify
373
- return testContext . sharedClient
374
- . db ( testContext . dbName )
375
- . collection ( testContext . collectionName )
376
- . find ( { } , { readPreference : 'primary' , readConcern : { level : 'local' } } )
377
- . toArray ( )
378
- . then ( docs => {
379
- expect ( docs ) . to . eql ( testData . outcome . collection . data ) ;
380
- } ) ;
381
- }
382
- }
383
431
}
384
432
385
433
function linkSessionData ( command , context ) {
0 commit comments