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 1 commit
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'
14 changes: 14 additions & 0 deletions pytest_asyncio/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,17 @@ 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
Copy link
Member

Choose a reason for hiding this comment

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

I think this should be port = unused_tcp_port().

Copy link
Member Author

Choose a reason for hiding this comment

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

Good catch! Of course the bug is in the section not tested. I'll add a test and get coverage up to 100%.


return port
return factory
35 changes: 35 additions & 0 deletions tests/test_simple.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,41 @@ 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()


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

Expand Down