kaizendorks / pymongo_inmemory

A mongo mocking library with an ephemeral MongoDB running in memory.
MIT License
40 stars 13 forks source link

MongoClient causes pymongo ServerSelectionTimeoutError error #63

Open yCobanoglu opened 1 year ago

yCobanoglu commented 1 year ago

Describe the bug pymongo.errors.ServerSelectionTimeoutError: 127.0.0.1:27017: [Errno 111] Connection refused. This used to work in the past due to (probably ?!) System update suddenly stopped working. For generic linux with version 4 everything works as expected.

To Reproduce Steps to reproduce the behavior: Packages needed: pytest==7.2.0, pymongo==4.3.3 ENV Variables: PYMONGOIMMONGO_VERSION=4.4; PYMONGOIM__OPERATING_SYSTEM=ubuntu; PYMONGOIMOS_VERSION=20

import uuid
import pytest
from pymongo_inmemory import MongoClient
from pymongo.database import Database

COLLECTION = "test"

class MongoCollection:
    def __init__(self, database: Database, collection: str):
        self.__database = database
        self.__collection = collection

    def __get_collection(self):
        return self.__database[self.__collection]

    def insert_one(self, document):
        return self.__get_collection().insert_one(document).inserted_id

    def find_one(self, query, *args, **kwargs):
        return self.__get_collection().find_one(query, *args, **kwargs)

class MongoRepository:
    def __init__(self, database: Database) -> None:
        self._collection = MongoCollection(database, COLLECTION)

    def create(self, test) -> None:
        self._collection.insert_one(test)

    def find(self, query):
        return self._collection.find_one(query)

@pytest.fixture
def mongo_client() -> MongoClient:
    return MongoClient()

@pytest.fixture
def database(mongo_client: MongoClient):
    database = str(uuid.uuid4())
    yield mongo_client[database]
    mongo_client.drop_database(database)

@pytest.fixture
def repository(database):
    return MongoRepository(database)

def test_create(database, repository):
    repository.create({"_id":"id-0", "test": "test"})
    assert database[COLLECTION].find_one({"_id": "id-0"}) == {"_id":"id-0", "test": "test"}
test setup failed
@pytest.fixture
    def mongo_client() -> MongoClient:
>       return MongoClient()

test.py:12: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 
../../.venv/lib/pypy3.9/site-packages/pymongo_inmemory/_pim.py:9: in __init__
    self._mongod.start()
../../.venv/lib/pypy3.9/site-packages/pymongo_inmemory/mongod.py:127: in start
    while not self.is_healthy:
../../.venv/lib/pypy3.9/site-packages/pymongo_inmemory/mongod.py:165: in is_healthy
    status = db.command("serverStatus")
../../.venv/lib/pypy3.9/site-packages/pymongo/_csot.py:105: in csot_wrapper
    return func(self, *args, **kwargs)
../../.venv/lib/pypy3.9/site-packages/pymongo/database.py:805: in command
    with self.__client._socket_for_reads(read_preference, session) as (
../../.venv/lib/pypy3.9/site-packages/pymongo/mongo_client.py:1296: in _socket_for_reads
    server = self._select_server(read_preference, session)
../../.venv/lib/pypy3.9/site-packages/pymongo/mongo_client.py:1257: in _select_server
    server = topology.select_server(server_selector)
../../.venv/lib/pypy3.9/site-packages/pymongo/topology.py:272: in select_server
    server = self._select_server(selector, server_selection_timeout, address)
../../.venv/lib/pypy3.9/site-packages/pymongo/topology.py:261: in _select_server
    servers = self.select_servers(selector, server_selection_timeout, address)
../../.venv/lib/pypy3.9/site-packages/pymongo/topology.py:223: in select_servers
    server_descriptions = self._select_servers_loop(selector, server_timeout, address)
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

self = <Topology <TopologyDescription id: 63a05700067dbc0bfabfdd47, topology_type: Unknown, servers: [<ServerDescription ('12....1', 27017) server_type: Unknown, rtt: None, error=AutoReconnect('127.0.0.1:27017: [Errno 111] Connection refused')>]>>
selector = Primary(), timeout = 30, address = None

    def _select_servers_loop(self, selector, timeout, address):
        """select_servers() guts. Hold the lock when calling this."""
        now = time.monotonic()
        end_time = now + timeout
        server_descriptions = self._description.apply_selector(
            selector, address, custom_selector=self._settings.server_selector
        )

        while not server_descriptions:
            # No suitable servers.
            if timeout == 0 or now > end_time:
>               raise ServerSelectionTimeoutError(
                    "%s, Timeout: %ss, Topology Description: %r"
                    % (self._error_message(selector), timeout, self.description)
                )
E               pymongo.errors.ServerSelectionTimeoutError: 127.0.0.1:27017: [Errno 111] Connection refused, Timeout: 30s, Topology Description: <TopologyDescription id: 63a05700067dbc0bfabfdd47, topology_type: Unknown, servers: [<ServerDescription ('127.0.0.1', 27017) server_type: Unknown, rtt: None, error=AutoReconnect('127.0.0.1:27017: [Errno 111] Connection refused')>]>

../../.venv/lib/pypy3.9/site-packages/pymongo/topology.py:238: ServerSelectionTimeoutError

Context:

yCobanoglu commented 1 year ago

I could fix it by installing mongo for ARCH. You can close the issue.

ekarademir commented 1 year ago

It could be something Ubuntu version not playing nice with Arch. I have no idea unfortunately. It's good to hear that you have a workaround.

sonishivam10 commented 1 year ago

Hey @ekarademir @yCobanoglu I am also getting the same error since yesterday, earlier it was running without an issue.

I have the following env variables:

os.environ["PYMONGOIM__OPERATING_SYSTEM"] = "ubuntu"
os.environ["PYMONGOIM__OS_VERSION"] = "20"
os.environ["PYMONGOIM__MONGO_VERSION"] = "5.0"

with
pymongo 4.3.2 pymongo-inmemory 0.2.11 pytest 6.2.5

Here SS image

any thoughts on this? why it could have popped up.

Context:

OS: Ubuntu 20.04.5 Version of pymongo_inmemory 0.2.11 Version of mongo you are downloading 45.0

mmebsout commented 9 months ago

Same here since pymongo-inmemory 0.3.1

from pymongo_inmemory import MongoClient
client = MongoClient() 
db = client['testdb']
collection = db['test-collection']
client.close()

I get

File "../python3.10/site-packages/pymongo/topology.py", line 259, in _select_servers_loop
    raise ServerSelectionTimeoutError(
pymongo.errors.ServerSelectionTimeoutError: 127.0.0.1:27018: [Errno 61] Connection refused (configured timeouts: socketTimeoutMS: 20000.0ms, connectTimeoutMS: 20000.0ms), Timeout: 30s, Topology Description: <TopologyDescription id: 6555081ddd1de2a12212380f, topology_type: Unknown, servers: [<ServerDescription ('127.0.0.1', 27018) server_type: Unknown, rtt: None, error=AutoReconnect('127.0.0.1:27018: [Errno 61] Connection refused (configured timeouts: socketTimeoutMS: 20000.0ms, connectTimeoutMS: 20000.0ms)')>]>

Works fine with pymongo-inmemory 0.3.0

OS : Apple M1 - macOSVentura 13.4 pymongo version 4.6.0