Pytest failed with - ConnectionRefusedError: [Errno 61] Connection refused

Environment:

  • pytest ==5.3.5
  • sanic ==19.12.2
  • python 3.7.6
  • macos 10.15.3

I have created a dummy sanic project with a simple pytest file test-server.py:

from sanic import Sanic
from sanic.response import empty

app = Sanic(__name__)


@app.route('/')
async def dummy_get(request):
    return empty()


def test_dummy_get():
    _, response = app.test_client.get('/')
    assert response.status == 204

Running pytest gives me:

[2020-02-24 21:47:22 +0800] [81113] [INFO] Goin' Fast @ http://127.0.0.1:42101
[2020-02-24 21:47:22 +0800] [81113] [INFO] http://127.0.0.1:42101/
[2020-02-24 21:47:22 +0800] [81113] [ERROR] Exception
Traceback (most recent call last):
  File "/Users/david/.local/share/virtualenvs/sanic-test-3RCHb05m/lib/python3.7/site-packages/sanic/testing.py", line 120, in _collect_response
    method, url, *request_args, **request_kwargs
  File "/Users/david/.local/share/virtualenvs/sanic-test-3RCHb05m/lib/python3.7/site-packages/sanic/testing.py", line 41, in _local_request
    url, verify=False, *args, **kwargs
  File "/Users/david/.local/share/virtualenvs/sanic-test-3RCHb05m/lib/python3.7/site-packages/httpx/client.py", line 671, in get
    trust_env=trust_env,
  File "/Users/david/.local/share/virtualenvs/sanic-test-3RCHb05m/lib/python3.7/site-packages/httpx/client.py", line 268, in request
    trust_env=trust_env,
  File "/Users/david/.local/share/virtualenvs/sanic-test-3RCHb05m/lib/python3.7/site-packages/httpx/client.py", line 410, in send
    allow_redirects=allow_redirects,
  File "/Users/david/.local/share/virtualenvs/sanic-test-3RCHb05m/lib/python3.7/site-packages/httpx/client.py", line 470, in send_handling_redirects
    request, verify=verify, cert=cert, timeout=timeout
  File "/Users/david/.local/share/virtualenvs/sanic-test-3RCHb05m/lib/python3.7/site-packages/httpx/client.py", line 599, in send_single_request
    request, verify=verify, cert=cert, timeout=timeout
  File "/Users/david/.local/share/virtualenvs/sanic-test-3RCHb05m/lib/python3.7/site-packages/httpx/dispatch/proxy_http.py", line 204, in send
    request=request, verify=verify, cert=cert, timeout=timeout
  File "/Users/david/.local/share/virtualenvs/sanic-test-3RCHb05m/lib/python3.7/site-packages/httpx/dispatch/connection_pool.py", line 126, in send
    raise exc
  File "/Users/david/.local/share/virtualenvs/sanic-test-3RCHb05m/lib/python3.7/site-packages/httpx/dispatch/connection_pool.py", line 121, in send
    request, verify=verify, cert=cert, timeout=timeout
  File "/Users/david/.local/share/virtualenvs/sanic-test-3RCHb05m/lib/python3.7/site-packages/httpx/dispatch/connection.py", line 53, in send
    await self.connect(verify=verify, cert=cert, timeout=timeout)
  File "/Users/david/.local/share/virtualenvs/sanic-test-3RCHb05m/lib/python3.7/site-packages/httpx/dispatch/connection.py", line 82, in connect
    host, port, ssl_context, timeout
  File "/Users/david/.local/share/virtualenvs/sanic-test-3RCHb05m/lib/python3.7/site-packages/httpx/concurrency/auto.py", line 33, in open_tcp_stream
    return await self.backend.open_tcp_stream(hostname, port, ssl_context, timeout)
  File "/Users/david/.local/share/virtualenvs/sanic-test-3RCHb05m/lib/python3.7/site-packages/httpx/concurrency/asyncio.py", line 267, in open_tcp_stream
    timeout.connect_timeout,
  File "/usr/local/Cellar/python/3.7.6_1/Frameworks/Python.framework/Versions/3.7/lib/python3.7/asyncio/tasks.py", line 442, in wait_for
    return fut.result()
  File "/usr/local/Cellar/python/3.7.6_1/Frameworks/Python.framework/Versions/3.7/lib/python3.7/asyncio/streams.py", line 77, in open_connection
    lambda: protocol, host, port, **kwds)
  File "uvloop/loop.pyx", line 1974, in create_connection
  File "uvloop/loop.pyx", line 1951, in uvloop.loop.Loop.create_connection
ConnectionRefusedError: [Errno 61] Connection refused
[2020-02-24 21:47:22 +0800] [81113] [INFO] Starting worker [81113]
[2020-02-24 21:47:22 +0800] [81113] [INFO] Stopping worker [81113]
[2020-02-24 21:47:22 +0800] [81113] [INFO] Server Stopped

I have also tried specifying port=None and port=8080 for SanicTestClient but got the same error.

When I run the server directly, everything works as normal.

Any ideas?


Update:

After restarting my Macbook Pro, test started passing.

This is interesting. If it starts happening again, please open an issue. I’m concerned that this is a deeper problem.

After more investigation, I found out what was going on.

My Macbook Pro uses a SOCKS proxy to connect to the internet (being a developer behind the GFW really sucks). The proxy setting (all_proxy=http://127.0.0.1:xxxx to be more specific) gets propagated to the PyCharm run env, which inherits system env by default. I have recently updated the proxy port but apparently PyCharm did not pick up the updated port, hence causing Connection refused error.

Updating the run env by removing all_proxy=http://127.0.0.1:xxxx will resolve this particular problem. But only when I turn off my proxy completely do all the tests pass eventually.

This makes me think that running tests behind a proxy will cause some unexpected issues in general.

Thank you for the update, @davidygs - I appreciate you running it down for us!