6
6
7
7
8
8
@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'
23
12
24
13
25
14
def test_event_loop_fixture (event_loop ):
26
15
"""Test the injection of the event_loop fixture."""
27
16
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'
31
19
32
20
33
21
def test_event_loop_processpool_fixture (event_loop_process_pool ):
34
22
"""Test the injection of the event_loop with a process pool fixture."""
35
23
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'
39
28
40
29
this_pid = os .getpid ()
41
30
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):
46
35
@pytest .mark .asyncio
47
36
def test_asyncio_marker ():
48
37
"""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)
52
39
53
40
54
41
@pytest .mark .asyncio
55
42
def test_asyncio_marker_with_default_param (a_param = None ):
56
43
"""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)
60
45
61
46
62
47
@pytest .mark .asyncio_process_pool
63
48
def test_asyncio_process_pool_marker (event_loop ):
64
49
"""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'
72
52
73
53
74
54
@pytest .mark .asyncio
75
- def test_unused_port_fixture (unused_tcp_port ):
55
+ def test_unused_port_fixture (unused_tcp_port , event_loop ):
76
56
"""Test the unused TCP port fixture."""
77
57
78
58
@asyncio .coroutine
79
59
def closer (_ , writer ):
80
60
writer .close ()
81
61
82
62
server1 = yield from asyncio .start_server (closer , host = 'localhost' ,
83
- port = unused_tcp_port )
63
+ port = unused_tcp_port ,
64
+ loop = event_loop )
84
65
85
66
with pytest .raises (IOError ):
86
67
yield from asyncio .start_server (closer , host = 'localhost' ,
87
- port = unused_tcp_port )
68
+ port = unused_tcp_port ,
69
+ loop = event_loop )
88
70
89
71
server1 .close ()
90
72
yield from server1 .wait_closed ()
@@ -94,8 +76,7 @@ class Test:
94
76
"""Test that asyncio marked functions work in test methods."""
95
77
96
78
@pytest .mark .asyncio
97
- def test_asyncio_marker_method (self ):
79
+ def test_asyncio_marker_method (self , event_loop ):
98
80
"""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