Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Apply SO_REUSEADDR to test server's socket #4393

Merged
merged 4 commits into from
Nov 27, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGES/4393.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Apply SO_REUSEADDR to test server's socket.
8 changes: 4 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -60,21 +60,21 @@ check_changes:
@touch .develop

test: .develop
@pytest -c pytest.ci.ini -q
@pytest -q

vtest: .develop
@pytest -c pytest.ci.ini -s -v
@pytest -s -v

cov cover coverage:
tox

cov-dev: .develop
@pytest -c pytest.ci.ini --cov-report=html
@pytest --cov-report=html
@echo "open file://`pwd`/htmlcov/index.html"

cov-ci-run: .develop
@echo "Regular run"
@pytest -c pytest.ci.ini --cov-report=html
@pytest --cov-report=html

cov-dev-full: cov-ci-run
@echo "open file://`pwd`/htmlcov/index.html"
Expand Down
9 changes: 9 additions & 0 deletions aiohttp/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import functools
import gc
import inspect
import os
import socket
import sys
import unittest
Expand Down Expand Up @@ -57,12 +58,20 @@
SSLContext = None


REUSE_ADDRESS = os.name == 'posix' and sys.platform != 'cygwin'


def get_unused_port_socket(host: str) -> socket.socket:
return get_port_socket(host, 0)


def get_port_socket(host: str, port: int) -> socket.socket:
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
if REUSE_ADDRESS:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It will be nice if you add a comment describing why it is set here.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1

not everyone knows that it has different semantics under Windows.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, I mean another thing ;). I don't understand why it ever needs. Even in Linux.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because if you retry binding to the same socket fast enough, it'll fail. Even from the same process (after unbinding). There's some sort of timeout in place.

OTOH if you were to always use an ephemeral port 0, the kernel would allocate you a different port each time and there wouldn't be such a problem.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I confused. I misread as SO_REUSEPORT.

asvetlov marked this conversation as resolved.
Show resolved Hide resolved
# Windows has different semantics for SO_REUSEADDR,
# so don't set it. Ref:
# https://docs.microsoft.com/en-us/windows/win32/winsock/using-so-reuseaddr-and-so-exclusiveaddruse
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind((host, port))
return s

Expand Down
9 changes: 0 additions & 9 deletions pytest.ci.ini

This file was deleted.

9 changes: 0 additions & 9 deletions pytest.ini

This file was deleted.

11 changes: 11 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,17 @@ branch = True
source = aiohttp, tests
omit = site-packages

[tool:pytest]
addopts = --cov=aiohttp -v -rxXs --durations 10
filterwarnings =
error
ignore:module 'ssl' has no attribute 'OP_NO_COMPRESSION'. The Python interpreter is compiled against OpenSSL < 1.0.0. Ref. https.//docs.python.org/3/library/ssl.html#ssl.OP_NO_COMPRESSION:UserWarning
junit_suite_name = aiohttp_test_suite
norecursedirs = dist docs build .tox .eggs
minversion = 3.8.2
testpaths = tests/
junit_family=xunit2

[mypy]
follow_imports = silent
strict_optional = True
Expand Down
2 changes: 1 addition & 1 deletion tests/test_web_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ def test_ctor() -> None:
assert 'GET' == req.method
assert HttpVersion(1, 1) == req.version
# MacOS may return CamelCased host name, need .lower()
assert req.host == socket.getfqdn().lower()
assert req.host.lower() == socket.getfqdn().lower()
assert '/path/to?a=1&b=2' == req.path_qs
assert '/path/to' == req.path
assert 'a=1&b=2' == req.query_string
Expand Down