1
+ /*
2
+ * Copyright 2014-2015 MongoDB, Inc.
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
16
+
1
17
package com.mongodb
2
18
3
19
import com.mongodb.operation.CreateCollectionOperation
4
20
import org.bson.BsonDocument
21
+ import org.bson.BsonDouble
5
22
import spock.lang.Specification
6
23
7
24
import static com.mongodb.CustomMatchers.isTheSameAs
@@ -35,4 +52,53 @@ class DBSpecification extends Specification {
35
52
.autoIndex(true )
36
53
.storageEngineOptions(new BsonDocument (' wiredTiger' , new BsonDocument ())))
37
54
}
38
- }
55
+
56
+
57
+ def ' should use provided read preference for obedient commands' () {
58
+ given :
59
+ def mongo = Stub (Mongo )
60
+ mongo. mongoClientOptions >> MongoClientOptions . builder(). build()
61
+ def executor = new TestOperationExecutor ([new BsonDocument (' ok' , new BsonDouble (1.0 ))])
62
+ def database = new DB(mongo, ' test' , executor)
63
+ database. setReadPreference(ReadPreference . secondary())
64
+
65
+ when :
66
+ database. command(cmd)
67
+
68
+ then :
69
+ executor. getReadPreference() == expectedReadPreference
70
+
71
+ where :
72
+ expectedReadPreference | cmd
73
+ ReadPreference . secondary() | new BasicDBObject (' listCollections' , 1 )
74
+ ReadPreference . secondary() | new BasicDBObject (' collStats' , 1 )
75
+ ReadPreference . secondary() | new BasicDBObject (' dbStats' , 1 )
76
+ ReadPreference . secondary() | new BasicDBObject (' distinct' , 1 )
77
+ ReadPreference . secondary() | new BasicDBObject (' geoNear' , 1 )
78
+ ReadPreference . secondary() | new BasicDBObject (' geoSearch' , 1 )
79
+ ReadPreference . secondary() | new BasicDBObject (' group' , 1 )
80
+ ReadPreference . secondary() | new BasicDBObject (' listCollections' , 1 )
81
+ ReadPreference . secondary() | new BasicDBObject (' listIndexes' , 1 )
82
+ ReadPreference . secondary() | new BasicDBObject (' parallelCollectionScan' , 1 )
83
+ ReadPreference . secondary() | new BasicDBObject (' text' , 1 )
84
+ }
85
+
86
+ def ' should use primary read preference for non obedient commands' () {
87
+ given :
88
+ def mongo = Stub (Mongo )
89
+ mongo. mongoClientOptions >> MongoClientOptions . builder(). build()
90
+ def executor = new TestOperationExecutor ([new BsonDocument (' ok' , new BsonDouble (1.0 ))])
91
+ def database = new DB(mongo, ' test' , executor)
92
+ database. setReadPreference(ReadPreference . secondary())
93
+
94
+ when :
95
+ database. command(cmd)
96
+
97
+ then :
98
+ executor. getReadPreference() == expectedReadPreference
99
+
100
+ where :
101
+ expectedReadPreference | cmd
102
+ ReadPreference . primary() | new BasicDBObject (' command' , 1 )
103
+ }
104
+ }
0 commit comments