Httpretty causes socket to lose connection?

315 Views Asked by At

I'm using Httpretty to simulate a web API.

If I use httpretty.enable(allow_net_connect=False) (When allow_net_connect is False any connection to an unregistered uri will throw httpretty.errors.UnmockedError), I get an error since pymysql can't connect, via sockets, to a local DB.

However, when I use httpretty.enable(allow_net_connect=True), I get an error from a lost connection.

../../.virtualenvs/3.6espiga/lib/python3.6/site-packages/pymysql/__init__.py:90: in Connect
    return Connection(*args, **kwargs)
../../.virtualenvs/3.6espiga/lib/python3.6/site-packages/pymysql/connections.py:699: in __init__
    self.connect()
../../.virtualenvs/3.6espiga/lib/python3.6/site-packages/pymysql/connections.py:935: in connect
    self._get_server_information()
../../.virtualenvs/3.6espiga/lib/python3.6/site-packages/pymysql/connections.py:1249: in _get_server_information
    packet = self._read_packet()
../../.virtualenvs/3.6espiga/lib/python3.6/site-packages/pymysql/connections.py:991: in _read_packet
    packet_header = self._read_bytes(4)
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

self = <pymysql.connections.Connection object at 0x7fefe1ad40f0>, num_bytes = 4

    def _read_bytes(self, num_bytes):
        self._sock.settimeout(self._read_timeout)
        while True:
            try:
                data = self._rfile.read(num_bytes)
                break
            except (IOError, OSError) as e:
                if e.errno == errno.EINTR:
                    continue
                self._force_close()
                raise err.OperationalError(
                    CR.CR_SERVER_LOST,
                    "Lost connection to MySQL server during query (%s)" % (e,))
        if len(data) < num_bytes:
            self._force_close()
            raise err.OperationalError(
>               CR.CR_SERVER_LOST, "Lost connection to MySQL server during query")
E           pymysql.err.OperationalError: (2013, 'Lost connection to MySQL server during query')

../../.virtualenvs/3.6espiga/lib/python3.6/site-packages/pymysql/connections.py:1037: OperationalError

How can I have Httpretty mocking the web API and leave the DB connection to run as usually?

1

There are 1 best solutions below

0
On

To anyone who finds this years later, I got the a UnmockedError from pytest-mock-resources that led me to realise that httpretty in other tests was causing my db tests to fail. The fix I found was to add a teardown function in pytest that calls httpretty.disable() after each test. E.g

@pytest.fixture(autouse=True)
def stop_http_pretty():
    yield
    # Everything after the yield is run after each test as a "teardown".
    # This is required to stop httpretty from intercepting calls to the
    # mock db used by pytest-mock-resources
    httpretty.disable()

This only worked for me because I was not using httpretty and db connections in the same tests, but I'm not sure how it would work otherwise. The pytest devs are apparently generally against teardowns so I'm not sure if this is best practice, but it works.