def _select_servers_loop(self, selector, timeout, address): """select_servers() guts. Hold the lock when calling this.""" now = _time() end_time = now + timeout server_descriptions = self._description.apply_selector( selector, address) while not server_descriptions: # No suitable servers. if timeout == 0 or now > end_time: raise ServerSelectionTimeoutError( self._error_message(selector)) self._ensure_opened() self._request_check_all() # Release the lock and wait for the topology description to # change, or for a timeout. We won't miss any changes that # came after our most recent apply_selector call, since we've # held the lock until now. self._condition.wait(common.MIN_HEARTBEAT_INTERVAL) self._description.check_compatible() now = _time() server_descriptions = self._description.apply_selector( selector, address) self._description.check_compatible() return server_descriptions
def __init__(self, db, host='localhost', port=27017): self.client = MongoClient(host, port) # Informations de connexion au serveur MongoDB self.db_socket = self.client[db] # Information de connexion à "la bdd" # Vérifie que la connexion fonctionne try: self.db_socket.collection_names() print(f'Connexion à la base de données : {host}:{port} réussie!') except ServerSelectionTimeoutError: raise ServerSelectionTimeoutError(f'Echec de la connexion à la base de données : {host}:{port}!')
def test_ServerSelectionTimeoutError_no_retry(): error = ServerSelectionTimeoutError('some error') with patch('arctic.decorators._log_exception', autospec=True) as le: @mongo_retry def foo(): raise error with pytest.raises(ServerSelectionTimeoutError) as e: foo() assert 'some error' in str(e.value) assert le.call_count == 1
def test_health_exception(client, db, mocker): """ Mongo query raises exception. """ do_health = mocker.patch('api.v0.views.do_health') do_health.side_effect = ServerSelectionTimeoutError() result = client.get(HEALTH_URL) assert result.status_code == 503
def select_servers(self, selector, server_selection_timeout=None, address=None): """Return a list of Servers matching selector, or time out. :Parameters: - `selector`: function that takes a list of Servers and returns a subset of them. - `server_selection_timeout` (optional): maximum seconds to wait. If not provided, the default value common.SERVER_SELECTION_TIMEOUT is used. - `address`: optional server address to select. Calls self.open() if needed. Raises exc:`ServerSelectionTimeoutError` after `server_selection_timeout` if no matching servers are found. """ if server_selection_timeout is None: server_timeout = self._settings.server_selection_timeout else: server_timeout = server_selection_timeout with self._lock: self._description.check_compatible() now = _time() end_time = now + server_timeout server_descriptions = self._description.apply_selector( selector, address) while not server_descriptions: # No suitable servers. if server_timeout == 0 or now > end_time: raise ServerSelectionTimeoutError( self._error_message(selector)) self._ensure_opened() self._request_check_all() # Release the lock and wait for the topology description to # change, or for a timeout. We won't miss any changes that # came after our most recent apply_selector call, since we've # held the lock until now. self._condition.wait(common.MIN_HEARTBEAT_INTERVAL) self._description.check_compatible() now = _time() server_descriptions = self._description.apply_selector( selector, address) return [ self.get_server_by_address(sd.address) for sd in server_descriptions ]
def save(self, data: dict) -> User: try: if User.objects.raw({"email": data["email"]}).count() > 0: raise Conflict("Email already in use.") return User(email=data["email"], name=data["name"], password=data["password"]).save() except ServerSelectionTimeoutError as err: raise ServerSelectionTimeoutError( f"DataBasse connection error: {str(err)}")
def db_connect( mongo_config: dict, is_arctic: bool = True, lib_name: str = None ) -> Union[object, Arctic, VersionStore, ChunkStore, Collection]: """ Connect to the MongoDB Atlas instance using the mongo_user, mongo_password and url_cluster Args: mongo_config: Dict-like object with the keys ["mongo_user", "mongo_pwd", "url_cluster"] is_arctic: If searching the Arctic database on MongoDB (for time-series data) lib_name: Name of library within database, default to None Returns: object: lib_store: arctic store lib_collection: pymongo collection db_connection: arctic.arctic.Arctic, pymongo.database.Database """ user = mongo_config["mongo_user"] password = mongo_config["mongo_pwd"] mongo_url = mongo_config["url_cluster"] host_url = "".join(["mongodb+srv://", user, ":", password, "@", mongo_url]) client = MongoClient(host=host_url) # check the connection to the database try: client.PORT # print(f"Listing existing database names: {client.list_database_names()}") except ServerSelectionTimeoutError: raise ServerSelectionTimeoutError('MongoDB is not hosted.') if is_arctic: db_connection = Arctic(client) library_list = db_connection.list_libraries() if lib_name is not None: assert lib_name in library_list, \ f"\n Library: '{lib_name}' does not exist in Arctic database" lib_store = db_connection[lib_name] return lib_store else: print(f"List of libraries in 'Arctic' database: \n {library_list}") return db_connection # non-arctic collection else: print("Have not yet configured non-Arctic database on MongoDB Atlas")
def getRegisteredCommands(self, worker_name): """Return the commands list registered by the given worker name Args: worker_name: the wworker shortname. """ try: if self.client is None: self.connect() if self.client is None: raise ServerSelectionTimeoutError() worker_res = self.findInDb("pollenisator", "workers", { "name": worker_name}, False) if worker_res is not None: return worker_res["registeredCommands"] except ServerSelectionTimeoutError as e: print("Failed to connect." + str(e)) print("Please verify that the mongod service is running on host " + self.host + " and has a user mongAdmin with the correct password.") self.client = None
def raise_error(): raise ServerSelectionTimeoutError('mock error')
def raise_error(*args, **kwargs): raise ServerSelectionTimeoutError( 'No primary available for writes')
assert resp.status == '201 CREATED' assert resp_data['thumbnails']['preview'] is not None assert test_app.fs.get(resp_data['thumbnails']['preview'] ['storage_id']).__class__ is bytes assert len(resp_data['thumbnails']['timeline']) == amount for thumbn_data in resp_data['thumbnails']['timeline']: assert test_app.fs.get( thumbn_data['storage_id']).__class__ is bytes @pytest.mark.parametrize('projects', [({ 'file': 'sample_0.mp4', 'duplicate': False }, )], indirect=True) @mock.patch('pymongo.collection.Collection.find_one_and_update', side_effect=ServerSelectionTimeoutError('Timeout error')) def test_duplicate_project_cant_set_storage_id(mock_find_one_and_update, test_app, client, projects): project = projects[0] with test_app.test_request_context(): # duplicate url = url_for('projects.duplicate_project', project_id=project['_id']) resp = client.post(url) resp_data = json.loads(resp.data) assert resp.status == '500 INTERNAL SERVER ERROR' assert resp_data == {'error': 'Timeout error'} assert len(list(test_app.mongo.db.projects.find())) == len(projects)
def _mock_db_timeout(*args, **kwargs): raise ServerSelectionTimeoutError('Failed to connect')
def test_setup_database_connect_error(self, monkeypatch, db_config): connect_mock = Mock(side_effect=ServerSelectionTimeoutError()) monkeypatch.setattr(beer_garden.db.mongo.api, "connect", connect_mock) assert beer_garden.db.mongo.api.check_connection(db_config) is False