Debian Codegraph @

tests/test__greenness.py

raw
1 """Test than modules in eventlet.green package are indeed green.
2 To do that spawn a green server and then access it using a green socket.
3 If either operation blocked the whole script would block and timeout.
4 """
5 import eventlet
6 from eventlet.green import BaseHTTPServer
7
8 from eventlet.green.urllib.request import urlopen
9 from eventlet.green.urllib.error import HTTPError
10
11
12 class QuietHandler(BaseHTTPServer.BaseHTTPRequestHandler):
13 protocol_version = "HTTP/1.0"
14
15 def log_message(self, *args, **kw):
16 pass
17
18
19 def start_http_server():
20 server_address = ('localhost', 0)
21 httpd = BaseHTTPServer.HTTPServer(server_address, QuietHandler)
22 sa = httpd.socket.getsockname()
23 # print("Serving HTTP on", sa[0], "port", sa[1], "...")
24 httpd.request_count = 0
25
26 def serve():
27 # increment the request_count before handling the request because
28 # the send() for the response blocks (or at least appeared to be)
29 httpd.request_count += 1
30 httpd.handle_request()
31 return eventlet.spawn(serve), httpd, sa[1]
32
33
34 def test_urllib():
35 gthread, server, port = start_http_server()
36
37 try:
38 assert server.request_count == 0
39 try:
40 urlopen('http://127.0.0.1:{}'.format(port))
41 assert False, 'should not get there'
42 except HTTPError as ex:
43 assert ex.code == 501, repr(ex)
44 assert server.request_count == 1
45 finally:
46 server.server_close()
47 eventlet.kill(gthread)