Skip to content

Commit 04b2e98

Browse files
committed
Get rid of using external resource in tests
1 parent 2afbebc commit 04b2e98

File tree

1 file changed

+21
-40
lines changed

1 file changed

+21
-40
lines changed

tests/test_simple.py

+21-40
Original file line numberDiff line numberDiff line change
@@ -6,36 +6,25 @@
66

77

88
@asyncio.coroutine
9-
def simple_http_client(url):
10-
"""Just a simple http client, for testing."""
11-
u = urllib.parse.urlparse(url)
12-
port = u.port if u.port else 80
13-
r, w = yield from asyncio.open_connection(host=u.netloc, port=port)
14-
w.write(b'GET ' + u.path.encode() + b' HTTP/1.0\r\n')
15-
w.write(b'Host: ' + u.netloc.encode() + b'\r\n')
16-
w.write(b'\r\n')
17-
yield from w.drain()
18-
19-
resp = yield from r.read()
20-
21-
w.close()
22-
return resp
9+
def async_coro(loop):
10+
yield from asyncio.sleep(0, loop=loop)
11+
return 'ok'
2312

2413

2514
def test_event_loop_fixture(event_loop):
2615
"""Test the injection of the event_loop fixture."""
2716
assert event_loop
28-
url = 'http://httpbin.org/get'
29-
resp = event_loop.run_until_complete(simple_http_client(url))
30-
assert b'HTTP/1.1 200 OK' in resp
17+
ret = event_loop.run_until_complete(async_coro(event_loop))
18+
assert ret == 'ok'
3119

3220

3321
def test_event_loop_processpool_fixture(event_loop_process_pool):
3422
"""Test the injection of the event_loop with a process pool fixture."""
3523
assert event_loop_process_pool
36-
url = 'http://httpbin.org/get'
37-
resp = event_loop_process_pool.run_until_complete(simple_http_client(url))
38-
assert b'HTTP/1.1 200 OK' in resp
24+
25+
ret = event_loop_process_pool.run_until_complete(
26+
async_coro(event_loop_process_pool))
27+
assert ret == 'ok'
3928

4029
this_pid = os.getpid()
4130
future = event_loop_process_pool.run_in_executor(None, os.getpid)
@@ -46,45 +35,38 @@ def test_event_loop_processpool_fixture(event_loop_process_pool):
4635
@pytest.mark.asyncio
4736
def test_asyncio_marker():
4837
"""Test the asyncio pytest marker."""
49-
url = 'http://httpbin.org/get'
50-
resp = yield from simple_http_client(url)
51-
assert b'HTTP/1.1 200 OK' in resp
38+
yield # sleep(0)
5239

5340

5441
@pytest.mark.asyncio
5542
def test_asyncio_marker_with_default_param(a_param=None):
5643
"""Test the asyncio pytest marker."""
57-
url = 'http://httpbin.org/get'
58-
resp = yield from simple_http_client(url)
59-
assert b'HTTP/1.1 200 OK' in resp
44+
yield # sleep(0)
6045

6146

6247
@pytest.mark.asyncio_process_pool
6348
def test_asyncio_process_pool_marker(event_loop):
6449
"""Test the asyncio pytest marker."""
65-
url = 'http://httpbin.org/get'
66-
resp = yield from simple_http_client(url)
67-
assert b'HTTP/1.1 200 OK' in resp
68-
69-
this_pid = os.getpid()
70-
pool_pid = yield from event_loop.run_in_executor(None, os.getpid)
71-
assert this_pid != pool_pid
50+
ret = yield from async_coro(event_loop)
51+
assert ret == 'ok'
7252

7353

7454
@pytest.mark.asyncio
75-
def test_unused_port_fixture(unused_tcp_port):
55+
def test_unused_port_fixture(unused_tcp_port, event_loop):
7656
"""Test the unused TCP port fixture."""
7757

7858
@asyncio.coroutine
7959
def closer(_, writer):
8060
writer.close()
8161

8262
server1 = yield from asyncio.start_server(closer, host='localhost',
83-
port=unused_tcp_port)
63+
port=unused_tcp_port,
64+
loop=event_loop)
8465

8566
with pytest.raises(IOError):
8667
yield from asyncio.start_server(closer, host='localhost',
87-
port=unused_tcp_port)
68+
port=unused_tcp_port,
69+
loop=event_loop)
8870

8971
server1.close()
9072
yield from server1.wait_closed()
@@ -94,8 +76,7 @@ class Test:
9476
"""Test that asyncio marked functions work in test methods."""
9577

9678
@pytest.mark.asyncio
97-
def test_asyncio_marker_method(self):
79+
def test_asyncio_marker_method(self, event_loop):
9880
"""Test the asyncio pytest marker in a Test class."""
99-
url = 'http://httpbin.org/get'
100-
resp = yield from simple_http_client(url)
101-
assert b'HTTP/1.1 200 OK' in resp
81+
ret = yield from async_coro(event_loop)
82+
assert ret == 'ok'

0 commit comments

Comments
 (0)