1
1
"""Quick'n'dirty unit tests for provided fixtures and markers."""
2
2
import asyncio
3
3
import os
4
- import urllib
5
4
import pytest
6
5
7
6
8
7
@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
8
+ def async_coro (loop ):
9
+ yield from asyncio .sleep (0 , loop = loop )
10
+ return 'ok'
23
11
24
12
25
13
def test_event_loop_fixture (event_loop ):
26
14
"""Test the injection of the event_loop fixture."""
27
15
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
16
+ ret = event_loop .run_until_complete (async_coro (event_loop ))
17
+ assert ret == 'ok'
31
18
32
19
33
20
def test_event_loop_processpool_fixture (event_loop_process_pool ):
34
21
"""Test the injection of the event_loop with a process pool fixture."""
35
22
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
23
+
24
+ ret = event_loop_process_pool .run_until_complete (
25
+ async_coro (event_loop_process_pool ))
26
+ assert ret == 'ok'
39
27
40
28
this_pid = os .getpid ()
41
29
future = event_loop_process_pool .run_in_executor (None , os .getpid )
@@ -46,45 +34,38 @@ def test_event_loop_processpool_fixture(event_loop_process_pool):
46
34
@pytest .mark .asyncio
47
35
def test_asyncio_marker ():
48
36
"""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
37
+ yield # sleep(0)
52
38
53
39
54
40
@pytest .mark .asyncio
55
41
def test_asyncio_marker_with_default_param (a_param = None ):
56
42
"""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
43
+ yield # sleep(0)
60
44
61
45
62
46
@pytest .mark .asyncio_process_pool
63
47
def test_asyncio_process_pool_marker (event_loop ):
64
48
"""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
49
+ ret = yield from async_coro (event_loop )
50
+ assert ret == 'ok'
72
51
73
52
74
53
@pytest .mark .asyncio
75
- def test_unused_port_fixture (unused_tcp_port ):
54
+ def test_unused_port_fixture (unused_tcp_port , event_loop ):
76
55
"""Test the unused TCP port fixture."""
77
56
78
57
@asyncio .coroutine
79
58
def closer (_ , writer ):
80
59
writer .close ()
81
60
82
61
server1 = yield from asyncio .start_server (closer , host = 'localhost' ,
83
- port = unused_tcp_port )
62
+ port = unused_tcp_port ,
63
+ loop = event_loop )
84
64
85
65
with pytest .raises (IOError ):
86
66
yield from asyncio .start_server (closer , host = 'localhost' ,
87
- port = unused_tcp_port )
67
+ port = unused_tcp_port ,
68
+ loop = event_loop )
88
69
89
70
server1 .close ()
90
71
yield from server1 .wait_closed ()
@@ -94,8 +75,7 @@ class Test:
94
75
"""Test that asyncio marked functions work in test methods."""
95
76
96
77
@pytest .mark .asyncio
97
- def test_asyncio_marker_method (self ):
78
+ def test_asyncio_marker_method (self , event_loop ):
98
79
"""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
80
+ ret = yield from async_coro (event_loop )
81
+ assert ret == 'ok'
0 commit comments