File tree 2 files changed +26
-1
lines changed
2 files changed +26
-1
lines changed Original file line number Diff line number Diff line change @@ -97,8 +97,11 @@ def unused_tcp_port_factory():
97
97
98
98
def factory ():
99
99
port = unused_tcp_port ()
100
+
100
101
while port in produced :
101
- port = unused_tcp_port
102
+ port = unused_tcp_port ()
103
+
104
+ produced .add (port )
102
105
103
106
return port
104
107
return factory
Original file line number Diff line number Diff line change 3
3
import os
4
4
import pytest
5
5
6
+ import pytest_asyncio .plugin
7
+
6
8
7
9
@asyncio .coroutine
8
10
def async_coro (loop ):
@@ -106,6 +108,26 @@ def closer(_, writer):
106
108
yield from server3 .wait_closed ()
107
109
108
110
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
+
109
131
class Test :
110
132
"""Test that asyncio marked functions work in test methods."""
111
133
You can’t perform that action at this time.
0 commit comments