Skip to content

Commit cc99eee

Browse files
committed
Fixed unused_tcp_port_factory edge condition. Added a test for it.
1 parent 98eaff3 commit cc99eee

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

pytest_asyncio/plugin.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,11 @@ def unused_tcp_port_factory():
9797

9898
def factory():
9999
port = unused_tcp_port()
100+
100101
while port in produced:
101-
port = unused_tcp_port
102+
port = unused_tcp_port()
103+
104+
produced.add(port)
102105

103106
return port
104107
return factory

tests/test_simple.py

+22
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import os
44
import pytest
55

6+
import pytest_asyncio.plugin
7+
68

79
@asyncio.coroutine
810
def async_coro(loop):
@@ -106,6 +108,26 @@ def closer(_, writer):
106108
yield from server3.wait_closed()
107109

108110

111+
def test_unused_port_factory_duplicate(unused_tcp_port_factory, monkeypatch):
112+
"""Test correct avoidance of duplicate ports."""
113+
counter = 0
114+
115+
def mock_unused_tcp_port():
116+
"""Force some duplicate ports."""
117+
nonlocal counter
118+
counter += 1
119+
if counter < 5:
120+
return 10000
121+
else:
122+
return 10000 + counter
123+
124+
monkeypatch.setattr(pytest_asyncio.plugin, 'unused_tcp_port',
125+
mock_unused_tcp_port)
126+
127+
assert unused_tcp_port_factory() == 10000
128+
assert unused_tcp_port_factory() > 10000
129+
130+
109131
class Test:
110132
"""Test that asyncio marked functions work in test methods."""
111133

0 commit comments

Comments
 (0)