3232from bson .objectid import ObjectId
3333
3434from pymongo import (MongoClient ,
35- monitoring )
35+ monitoring , read_preferences )
3636from pymongo .errors import ConfigurationError , OperationFailure
3737from pymongo .monitoring import _SENSITIVE_COMMANDS , ConnectionPoolListener
38+ from pymongo .pool import PoolOptions
3839from pymongo .read_concern import ReadConcern
3940from pymongo .read_preferences import ReadPreference
4041from pymongo .server_selectors import (any_server_selector ,
4445from test import (client_context ,
4546 db_user ,
4647 db_pwd )
47- from test .utils_selection_tests import parse_read_preference
48-
4948
5049IMPOSSIBLE_WRITE_CONCERN = WriteConcern (w = 1000 )
5150
@@ -185,6 +184,46 @@ def failed(self, event):
185184 self .results .append (event )
186185
187186
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+
188227class ScenarioDict (dict ):
189228 """Dict that returns {} for any unknown key, recursively."""
190229 def __init__ (self , data ):
@@ -811,3 +850,14 @@ def run(self):
811850 except BaseException as exc :
812851 self .exc = exc
813852 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