32
32
from bson .objectid import ObjectId
33
33
34
34
from pymongo import (MongoClient ,
35
- monitoring )
35
+ monitoring , read_preferences )
36
36
from pymongo .errors import ConfigurationError , OperationFailure
37
37
from pymongo .monitoring import _SENSITIVE_COMMANDS , ConnectionPoolListener
38
+ from pymongo .pool import PoolOptions
38
39
from pymongo .read_concern import ReadConcern
39
40
from pymongo .read_preferences import ReadPreference
40
41
from pymongo .server_selectors import (any_server_selector ,
44
45
from test import (client_context ,
45
46
db_user ,
46
47
db_pwd )
47
- from test .utils_selection_tests import parse_read_preference
48
-
49
48
50
49
IMPOSSIBLE_WRITE_CONCERN = WriteConcern (w = 1000 )
51
50
@@ -185,6 +184,46 @@ def failed(self, event):
185
184
self .results .append (event )
186
185
187
186
187
+ class MockSocketInfo (object ):
188
+ def close (self ):
189
+ pass
190
+
191
+ def __enter__ (self ):
192
+ return self
193
+
194
+ def __exit__ (self , exc_type , exc_val , exc_tb ):
195
+ pass
196
+
197
+
198
+ class MockPool (object ):
199
+ def __init__ (self , * args , ** kwargs ):
200
+ self .pool_id = 0
201
+ self ._lock = threading .Lock ()
202
+ self .opts = PoolOptions ()
203
+
204
+ def get_socket (self , all_credentials ):
205
+ return MockSocketInfo ()
206
+
207
+ def return_socket (self , * args , ** kwargs ):
208
+ pass
209
+
210
+ def _reset (self ):
211
+ with self ._lock :
212
+ self .pool_id += 1
213
+
214
+ def reset (self ):
215
+ self ._reset ()
216
+
217
+ def close (self ):
218
+ self ._reset ()
219
+
220
+ def update_is_writable (self , is_writable ):
221
+ pass
222
+
223
+ def remove_stale_sockets (self , reference_pool_id ):
224
+ pass
225
+
226
+
188
227
class ScenarioDict (dict ):
189
228
"""Dict that returns {} for any unknown key, recursively."""
190
229
def __init__ (self , data ):
@@ -811,3 +850,14 @@ def run(self):
811
850
except BaseException as exc :
812
851
self .exc = exc
813
852
raise
853
+
854
+
855
+ def parse_read_preference (pref ):
856
+ # Make first letter lowercase to match read_pref's modes.
857
+ mode_string = pref .get ('mode' , 'primary' )
858
+ mode_string = mode_string [:1 ].lower () + mode_string [1 :]
859
+ mode = read_preferences .read_pref_mode_from_name (mode_string )
860
+ max_staleness = pref .get ('maxStalenessSeconds' , - 1 )
861
+ tag_sets = pref .get ('tag_sets' )
862
+ return read_preferences .make_read_preference (
863
+ mode , tag_sets = tag_sets , max_staleness = max_staleness )
0 commit comments