Skip to content

Commit

Permalink
Fixed unused_tcp_port_factory edge condition. Added a test for it.
Browse files Browse the repository at this point in the history
  • Loading branch information
Tinche committed Aug 2, 2015
1 parent 98eaff3 commit cc99eee
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
5 changes: 4 additions & 1 deletion pytest_asyncio/plugin.py
Expand Up @@ -97,8 +97,11 @@ def unused_tcp_port_factory():

def factory():
port = unused_tcp_port()

while port in produced:
port = unused_tcp_port
port = unused_tcp_port()

produced.add(port)

return port
return factory
22 changes: 22 additions & 0 deletions tests/test_simple.py
Expand Up @@ -3,6 +3,8 @@
import os
import pytest

import pytest_asyncio.plugin


@asyncio.coroutine
def async_coro(loop):
Expand Down Expand Up @@ -106,6 +108,26 @@ def closer(_, writer):
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

0 comments on commit cc99eee

Please sign in to comment.