Skip to content

Unused TCP port factory fixture. Bump to 0.2.0. #14

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

Merged
merged 2 commits into from
Aug 2, 2015
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
14 changes: 13 additions & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ Features
--------

- fixtures for creating and injecting versions of the asyncio event loop
- fixtures for injecting unused tcp ports
- pytest markers for treating tests as asyncio coroutines


Expand Down Expand Up @@ -72,9 +73,20 @@ The ``event_loop_process_pool`` fixture is almost identical to the

``unused_tcp_port``
~~~~~~~~~~~~~~~~~~~
Finds and yields an unused TCP port on the localhost interface. Useful for
Finds and yields a single unused TCP port on the localhost interface. Useful for
binding temporary test servers.

``unused_tcp_port_factory``
~~~~~~~~~~~~~~~~~~~~~~~~~~~
A callable which returns a different unused TCP port each invocation. Useful
when several unused TCP ports are required in a test.

.. code-block:: python

def a_test(unused_tcp_port_factory):
port1, port2 = unused_tcp_port_factory(), unused_tcp_port_factory()
...

Markers
-------

Expand Down
2 changes: 1 addition & 1 deletion pytest_asyncio/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = '0.1.3'
__version__ = '0.2.0'
17 changes: 17 additions & 0 deletions pytest_asyncio/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,20 @@ def unused_tcp_port():
with closing(socket.socket()) as sock:
sock.bind(('127.0.0.1', 0))
return sock.getsockname()[1]


@pytest.fixture
def unused_tcp_port_factory():
"""A factory function, producing different unused TCP ports."""
produced = set()

def factory():
port = unused_tcp_port()

while port in produced:
port = unused_tcp_port()

produced.add(port)

return port
return factory
57 changes: 57 additions & 0 deletions tests/test_simple.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import os
import pytest

import pytest_asyncio.plugin


@asyncio.coroutine
def async_coro(loop):
Expand Down Expand Up @@ -71,6 +73,61 @@ def closer(_, writer):
yield from server1.wait_closed()


@pytest.mark.asyncio
def test_unused_port_factory_fixture(unused_tcp_port_factory, event_loop):
"""Test the unused TCP port factory fixture."""

@asyncio.coroutine
def closer(_, writer):
writer.close()

port1, port2, port3 = (unused_tcp_port_factory(), unused_tcp_port_factory(),
unused_tcp_port_factory())

server1 = yield from asyncio.start_server(closer, host='localhost',
port=port1,
loop=event_loop)
server2 = yield from asyncio.start_server(closer, host='localhost',
port=port2,
loop=event_loop)
server3 = yield from asyncio.start_server(closer, host='localhost',
port=port3,
loop=event_loop)

for port in port1, port2, port3:
with pytest.raises(IOError):
yield from asyncio.start_server(closer, host='localhost',
port=port,
loop=event_loop)

server1.close()
yield from server1.wait_closed()
server2.close()
yield from server2.wait_closed()
server3.close()
yield from server3.wait_closed()


def test_unused_port_factory_duplicate(unused_tcp_port_factory, monkeypatch):
"""Test correct avoidance of duplicate ports."""
counter = 0

def mock_unused_tcp_port():
"""Force some duplicate ports."""
nonlocal counter
counter += 1
if counter < 5:
return 10000
else:
return 10000 + counter

monkeypatch.setattr(pytest_asyncio.plugin, 'unused_tcp_port',
mock_unused_tcp_port)

assert unused_tcp_port_factory() == 10000
assert unused_tcp_port_factory() > 10000


class Test:
"""Test that asyncio marked functions work in test methods."""

Expand Down