Skip to content

Commit 1c06b52

Browse files
committed
Added tests for all ReadBinding Operations
1 parent 55b1e25 commit 1c06b52

13 files changed

+984
-135
lines changed

driver-core/src/test/functional/com/mongodb/operation/AggregateOperationSpecification.groovy

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,20 @@ import category.Async
2020
import com.mongodb.Block
2121
import com.mongodb.ExplainVerbosity
2222
import com.mongodb.MongoExecutionTimeoutException
23+
import com.mongodb.MongoNamespace
2324
import com.mongodb.OperationFunctionalSpecification
25+
import com.mongodb.ReadPreference
26+
import com.mongodb.async.SingleResultCallback
27+
import com.mongodb.binding.AsyncConnectionSource
28+
import com.mongodb.binding.AsyncReadBinding
29+
import com.mongodb.binding.ConnectionSource
30+
import com.mongodb.binding.ReadBinding
31+
import com.mongodb.connection.AsyncConnection
32+
import com.mongodb.connection.Connection
33+
import com.mongodb.connection.ConnectionDescription
34+
import com.mongodb.connection.ServerVersion
2435
import org.bson.BsonDocument
36+
import org.bson.BsonInt64
2537
import org.bson.BsonString
2638
import org.bson.Document
2739
import org.bson.codecs.BsonDocumentCodec
@@ -240,6 +252,84 @@ class AggregateOperationSpecification extends OperationFunctionalSpecification {
240252
result.containsKey('stages')
241253
}
242254

255+
def 'should use the ReadBindings readPreference to set slaveOK'() {
256+
given:
257+
def connection = Mock(Connection)
258+
def connectionSource = Stub(ConnectionSource) {
259+
getConnection() >> connection
260+
}
261+
def readBinding = Stub(ReadBinding) {
262+
getReadConnectionSource() >> connectionSource
263+
getReadPreference() >> readPreference
264+
}
265+
def operation = new AggregateOperation(helper.namespace, [], new BsonDocumentCodec())
266+
267+
when:
268+
operation.execute(readBinding)
269+
270+
then:
271+
_ * connection.getDescription() >> helper.twoFourConnectionDescription
272+
1 * connection.command(helper.dbName, _, readPreference.isSlaveOk(), _, _) >> helper.inlineResult
273+
1 * connection.release()
274+
275+
when: '2.6.0'
276+
operation.execute(readBinding)
277+
278+
then:
279+
_ * connection.getDescription() >> helper.twoSixConnectionDescription
280+
1 * connection.command(helper.dbName, _, readPreference.isSlaveOk(), _, _) >> helper.cursorResult
281+
1 * connection.release()
282+
283+
where:
284+
readPreference << [ReadPreference.primary(), ReadPreference.secondary()]
285+
}
286+
287+
def 'should use the AsyncReadBindings readPreference to set slaveOK'() {
288+
given:
289+
def connection = Mock(AsyncConnection)
290+
def connectionSource = Stub(AsyncConnectionSource) {
291+
getConnection(_) >> { it[0].onResult(connection, null) }
292+
}
293+
def readBinding = Stub(AsyncReadBinding) {
294+
getReadPreference() >> readPreference
295+
getReadConnectionSource(_) >> { it[0].onResult(connectionSource, null) }
296+
}
297+
def operation = new AggregateOperation(helper.namespace, [], new BsonDocumentCodec())
298+
299+
when:
300+
operation.executeAsync(readBinding, Stub(SingleResultCallback))
301+
302+
then:
303+
_ * connection.getDescription() >> helper.twoFourConnectionDescription
304+
1 * connection.commandAsync(helper.dbName, _, readPreference.isSlaveOk(), _, _, _) >> { it[5].onResult(helper.inlineResult, null) }
305+
1 * connection.release()
306+
307+
when: '2.6.0'
308+
operation.executeAsync(readBinding, Stub(SingleResultCallback))
309+
310+
then:
311+
_ * connection.getDescription() >> helper.twoSixConnectionDescription
312+
1 * connection.commandAsync(helper.dbName, _, readPreference.isSlaveOk(), _, _, _) >> { it[5].onResult(helper.cursorResult, null) }
313+
1 * connection.release()
314+
315+
where:
316+
readPreference << [ReadPreference.primary(), ReadPreference.secondary()]
317+
}
318+
319+
def helper = [
320+
dbName: 'db',
321+
namespace: new MongoNamespace('db', 'coll'),
322+
twoFourConnectionDescription: Stub(ConnectionDescription) {
323+
getServerVersion() >> new ServerVersion([2, 4, 0])
324+
},
325+
twoSixConnectionDescription : Stub(ConnectionDescription) {
326+
getServerVersion() >> new ServerVersion([2, 6, 0])
327+
},
328+
inlineResult: BsonDocument.parse('{ok: 1.0}').append('result', new BsonArrayWrapper([])),
329+
cursorResult: BsonDocument.parse('{ok: 1.0}')
330+
.append('cursor', new BsonDocument('id', new BsonInt64(0)).append('ns', new BsonString('db.coll'))
331+
.append('firstBatch', new BsonArrayWrapper([])))
332+
]
243333

244334
private static List<Boolean> useCursorOptions() {
245335
[null, true, false]

driver-core/src/test/functional/com/mongodb/operation/CountOperationSpecification.groovy

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,18 @@ import category.Async
2020
import com.mongodb.ExplainVerbosity
2121
import com.mongodb.MongoException
2222
import com.mongodb.MongoExecutionTimeoutException
23+
import com.mongodb.MongoNamespace
2324
import com.mongodb.OperationFunctionalSpecification
25+
import com.mongodb.ReadPreference
26+
import com.mongodb.async.SingleResultCallback
27+
import com.mongodb.binding.AsyncConnectionSource
28+
import com.mongodb.binding.AsyncReadBinding
29+
import com.mongodb.binding.ConnectionSource
30+
import com.mongodb.binding.ReadBinding
2431
import com.mongodb.bulk.IndexRequest
32+
import com.mongodb.connection.AsyncConnection
33+
import com.mongodb.connection.Connection
34+
import com.mongodb.connection.ConnectionDescription
2535
import org.bson.BsonDocument
2636
import org.bson.BsonInt32
2737
import org.bson.BsonString
@@ -248,4 +258,58 @@ class CountOperationSpecification extends OperationFunctionalSpecification {
248258
result.getNumber('ok').intValue() == 1
249259
}
250260

261+
def 'should use the ReadBindings readPreference to set slaveOK'() {
262+
given:
263+
def connection = Mock(Connection)
264+
def connectionSource = Stub(ConnectionSource) {
265+
getConnection() >> connection
266+
}
267+
def readBinding = Stub(ReadBinding) {
268+
getReadConnectionSource() >> connectionSource
269+
getReadPreference() >> readPreference
270+
}
271+
def operation = new CountOperation(helper.namespace).filter(BsonDocument.parse('{a: 1}'))
272+
273+
when:
274+
operation.execute(readBinding)
275+
276+
then:
277+
_ * connection.getDescription() >> helper.connectionDescription
278+
1 * connection.command(helper.dbName, _, readPreference.isSlaveOk(), _, _) >> helper.commandResult
279+
1 * connection.release()
280+
281+
where:
282+
readPreference << [ReadPreference.primary(), ReadPreference.secondary()]
283+
}
284+
285+
def 'should use the AsyncReadBindings readPreference to set slaveOK'() {
286+
given:
287+
def connection = Mock(AsyncConnection)
288+
def connectionSource = Stub(AsyncConnectionSource) {
289+
getConnection(_) >> { it[0].onResult(connection, null) }
290+
}
291+
def readBinding = Stub(AsyncReadBinding) {
292+
getReadPreference() >> readPreference
293+
getReadConnectionSource(_) >> { it[0].onResult(connectionSource, null) }
294+
}
295+
def operation = new CountOperation(helper.namespace).filter(BsonDocument.parse('{a: 1}'))
296+
297+
when:
298+
operation.executeAsync(readBinding, Stub(SingleResultCallback))
299+
300+
then:
301+
_ * connection.getDescription() >> helper.connectionDescription
302+
1 * connection.commandAsync(helper.dbName, _, readPreference.isSlaveOk(), _, _, _) >> { it[5].onResult(helper.commandResult, null) }
303+
1 * connection.release()
304+
305+
where:
306+
readPreference << [ReadPreference.primary(), ReadPreference.secondary()]
307+
}
308+
309+
def helper = [
310+
dbName: 'db',
311+
namespace: new MongoNamespace('db', 'coll'),
312+
commandResult: BsonDocument.parse('{ok: 1.0, n: 10}'),
313+
connectionDescription: Stub(ConnectionDescription)
314+
]
251315
}

driver-core/src/test/functional/com/mongodb/operation/DistinctOperationSpecification.groovy

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,26 @@ package com.mongodb.operation
1919
import category.Async
2020
import com.mongodb.MongoException
2121
import com.mongodb.MongoExecutionTimeoutException
22+
import com.mongodb.MongoNamespace
2223
import com.mongodb.OperationFunctionalSpecification
24+
import com.mongodb.ReadPreference
2325
import com.mongodb.async.FutureResultCallback
26+
import com.mongodb.async.SingleResultCallback
27+
import com.mongodb.binding.AsyncConnectionSource
28+
import com.mongodb.binding.AsyncReadBinding
29+
import com.mongodb.binding.ConnectionSource
30+
import com.mongodb.binding.ReadBinding
2431
import com.mongodb.client.test.Worker
2532
import com.mongodb.client.test.WorkerCodec
33+
import com.mongodb.connection.AsyncConnection
34+
import com.mongodb.connection.Connection
35+
import com.mongodb.connection.ConnectionDescription
2636
import org.bson.BsonDocument
2737
import org.bson.BsonInt32
2838
import org.bson.BsonInvalidOperationException
2939
import org.bson.Document
3040
import org.bson.codecs.BsonValueCodecProvider
41+
import org.bson.codecs.Decoder
3142
import org.bson.codecs.DocumentCodec
3243
import org.bson.codecs.DocumentCodecProvider
3344
import org.bson.codecs.ValueCodecProvider
@@ -247,4 +258,60 @@ class DistinctOperationSpecification extends OperationFunctionalSpecification {
247258
cleanup:
248259
disableMaxTimeFailPoint()
249260
}
261+
262+
def 'should use the ReadBindings readPreference to set slaveOK'() {
263+
given:
264+
def connection = Mock(Connection)
265+
def connectionSource = Stub(ConnectionSource) {
266+
getConnection() >> connection
267+
}
268+
def readBinding = Stub(ReadBinding) {
269+
getReadConnectionSource() >> connectionSource
270+
getReadPreference() >> readPreference
271+
}
272+
def operation = new DistinctOperation(helper.namespace, 'name', helper.decoder)
273+
274+
when:
275+
operation.execute(readBinding)
276+
277+
then:
278+
_ * connection.getDescription() >> helper.connectionDescription
279+
1 * connection.command(helper.dbName, _, readPreference.isSlaveOk(), _, _) >> helper.commandResult
280+
1 * connection.release()
281+
282+
where:
283+
readPreference << [ReadPreference.primary(), ReadPreference.secondary()]
284+
}
285+
286+
def 'should use the AsyncReadBindings readPreference to set slaveOK'() {
287+
given:
288+
def connection = Mock(AsyncConnection)
289+
def connectionSource = Stub(AsyncConnectionSource) {
290+
getConnection(_) >> { it[0].onResult(connection, null) }
291+
}
292+
def readBinding = Stub(AsyncReadBinding) {
293+
getReadPreference() >> readPreference
294+
getReadConnectionSource(_) >> { it[0].onResult(connectionSource, null) }
295+
}
296+
def operation = new DistinctOperation(helper.namespace, 'name', helper.decoder)
297+
298+
when:
299+
operation.executeAsync(readBinding, Stub(SingleResultCallback))
300+
301+
then:
302+
_ * connection.getDescription() >> helper.connectionDescription
303+
1 * connection.commandAsync(helper.dbName, _, readPreference.isSlaveOk(), _, _, _) >> { it[5].onResult(helper.commandResult, null) }
304+
1 * connection.release()
305+
306+
where:
307+
readPreference << [ReadPreference.primary(), ReadPreference.secondary()]
308+
}
309+
310+
def helper = [
311+
dbName: 'db',
312+
namespace: new MongoNamespace('db', 'coll'),
313+
decoder: Stub(Decoder),
314+
commandResult: BsonDocument.parse('{ok: 1.0}').append('values', new BsonArrayWrapper([])),
315+
connectionDescription: Stub(ConnectionDescription)
316+
]
250317
}

driver-core/src/test/functional/com/mongodb/operation/FindOperationSpecification.groovy

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,17 @@ import com.mongodb.Block
2222
import com.mongodb.ClusterFixture
2323
import com.mongodb.ExplainVerbosity
2424
import com.mongodb.MongoExecutionTimeoutException
25+
import com.mongodb.MongoNamespace
2526
import com.mongodb.OperationFunctionalSpecification
2627
import com.mongodb.ReadPreference
2728
import com.mongodb.ServerAddress
29+
import com.mongodb.async.SingleResultCallback
30+
import com.mongodb.binding.AsyncConnectionSource
31+
import com.mongodb.binding.AsyncReadBinding
2832
import com.mongodb.binding.ClusterBinding
2933
import com.mongodb.binding.ConnectionSource
3034
import com.mongodb.binding.ReadBinding
35+
import com.mongodb.connection.AsyncConnection
3136
import com.mongodb.connection.ClusterId
3237
import com.mongodb.connection.Connection
3338
import com.mongodb.connection.ConnectionDescription
@@ -41,6 +46,7 @@ import org.bson.BsonDocument
4146
import org.bson.BsonInt32
4247
import org.bson.BsonString
4348
import org.bson.Document
49+
import org.bson.codecs.Decoder
4450
import org.bson.codecs.DocumentCodec
4551
import org.junit.experimental.categories.Category
4652
import spock.lang.IgnoreIf
@@ -374,4 +380,77 @@ class FindOperationSpecification extends OperationFunctionalSpecification {
374380
then:
375381
result
376382
}
383+
384+
385+
def 'should use the ReadBindings readPreference to set slaveOK'() {
386+
given:
387+
def dbName = 'db'
388+
def collectionName = 'coll'
389+
def namespace = new MongoNamespace(dbName, collectionName)
390+
def decoder = Stub(Decoder)
391+
def readBinding = Mock(ReadBinding)
392+
def readPreference = Mock(ReadPreference)
393+
def connectionSource = Mock(ConnectionSource)
394+
def connection = Mock(Connection)
395+
def connectionDescription = Mock(ConnectionDescription)
396+
def queryResult = Mock(QueryResult)
397+
def operation = new FindOperation<BsonDocument>(namespace, decoder)
398+
399+
when:
400+
operation.execute(readBinding)
401+
402+
then:
403+
1 * readBinding.getReadConnectionSource() >> connectionSource
404+
2 * readBinding.getReadPreference() >> readPreference
405+
1 * connectionSource.getConnection() >> connection
406+
1 * connection.getDescription() >> connectionDescription
407+
1 * readPreference.slaveOk >> slaveOk
408+
1 * connection.query(namespace, _, _, _, _, slaveOk, _, _, _, _, _, _) >> queryResult
409+
1 * queryResult.getNamespace() >> namespace
410+
2 * queryResult.getResults() >> []
411+
1 * connection.release()
412+
1 * connectionSource.release()
413+
414+
where:
415+
slaveOk << [true, false]
416+
}
417+
418+
def 'should use the AsyncReadBindings readPreference to set slaveOK'() {
419+
given:
420+
def dbName = 'db'
421+
def collectionName = 'coll'
422+
def namespace = new MongoNamespace(dbName, collectionName)
423+
def decoder = Stub(Decoder)
424+
def readBinding = Mock(AsyncReadBinding)
425+
def readPreference = Mock(ReadPreference)
426+
def connectionSource = Mock(AsyncConnectionSource)
427+
def connection = Mock(AsyncConnection)
428+
def connectionDescription = Mock(ConnectionDescription)
429+
def queryResult = Mock(QueryResult)
430+
def operation = new FindOperation<BsonDocument>(namespace, decoder)
431+
432+
when:
433+
operation.executeAsync(readBinding, Stub(SingleResultCallback))
434+
435+
then:
436+
2 * readBinding.getReadPreference() >> readPreference
437+
1 * readBinding.getReadConnectionSource(_) >> { it[0].onResult(connectionSource, null) }
438+
1 * connectionSource.getConnection(_) >> { it[0].onResult(connection, null) }
439+
1 * connection.getDescription() >> connectionDescription
440+
1 * readPreference.slaveOk >> slaveOk
441+
1 * connection.queryAsync(namespace, _, _, _, _, slaveOk, _, _, _, _, _, _, _) >> { it[12].onResult(queryResult, null) }
442+
1 * queryResult.getNamespace() >> namespace
443+
1 * queryResult.getResults() >> []
444+
1 * connection.release()
445+
1 * connectionSource.release()
446+
447+
where:
448+
slaveOk << [true, false]
449+
}
450+
451+
def helper = [
452+
namespace: new MongoNamespace('db', 'coll'),
453+
queryResult: Stub(QueryResult),
454+
connectionDescription: Stub(ConnectionDescription)
455+
]
377456
}

0 commit comments

Comments
 (0)