
This patch consists of the following changes: * Splitting eventlet.greenio into base, py2 and py3 parts (eventlet.greenio should be exporing the same public objects). This change is motivated by the size and the number of conditions present in the current greenio code * Connected to the first point: implementing almost completely new GreenPipe callable utilizing parts of old GreenPipe code but dropping _fileobject/SocketIO inheritance in favour of io.FileIO and making use of patched _pyio.open function which wraps raw file-like object in various readers and writers (they take care of the buffering, encoding/decoding etc.) * Implementing (from scratch or updating existing versions) green versions of the following modules: * http.* (needed by Python 3's urllib) * selectors (Python >= 3.4, used in subprocess module) * urllib.* (needed by various tests and we were already exposing green urllib) * Modifying some tests to make tests pass, which includes: * unicode/bytestring issues * modifying wsgi_test_conntimeout.py to not pass bufsize and close arguments to ExplodingSocketFile - on Python 3 it inherits from SocketIO, which doesn't deal with buffering at all as far as I can see * Random cleaning up and reorganizing * Requiring Python 3.x tests to pass for the whole build to pass Known issues: * code repetition * naming inconsistencies * possibly breaking some external code using private eventlet.greenio attributes Closes https://github.com/eventlet/eventlet/issues/108 Affects https://github.com/eventlet/eventlet/issues/6 (I'd call it an experimental support) Should help for https://github.com/eventlet/eventlet/issues/145 Should help for https://github.com/eventlet/eventlet/issues/157
61 lines
1.8 KiB
Python
61 lines
1.8 KiB
Python
"""Test than modules in eventlet.green package are indeed green.
|
|
To do that spawn a green server and then access it using a green socket.
|
|
If either operation blocked the whole script would block and timeout.
|
|
"""
|
|
import unittest
|
|
|
|
from eventlet.green import BaseHTTPServer
|
|
from eventlet import spawn, kill
|
|
from eventlet.support import six
|
|
|
|
if six.PY2:
|
|
from eventlet.green.urllib2 import HTTPError, urlopen
|
|
else:
|
|
from eventlet.green.urllib.request import urlopen
|
|
from eventlet.green.urllib.error import HTTPError
|
|
|
|
|
|
class QuietHandler(BaseHTTPServer.BaseHTTPRequestHandler):
|
|
protocol_version = "HTTP/1.0"
|
|
|
|
def log_message(self, *args, **kw):
|
|
pass
|
|
|
|
|
|
def start_http_server():
|
|
server_address = ('localhost', 0)
|
|
httpd = BaseHTTPServer.HTTPServer(server_address, QuietHandler)
|
|
sa = httpd.socket.getsockname()
|
|
# print("Serving HTTP on", sa[0], "port", sa[1], "...")
|
|
httpd.request_count = 0
|
|
|
|
def serve():
|
|
# increment the request_count before handling the request because
|
|
# the send() for the response blocks (or at least appeared to be)
|
|
httpd.request_count += 1
|
|
httpd.handle_request()
|
|
return spawn(serve), httpd, sa[1]
|
|
|
|
|
|
class TestGreenness(unittest.TestCase):
|
|
|
|
def setUp(self):
|
|
self.gthread, self.server, self.port = start_http_server()
|
|
# print('Spawned the server')
|
|
|
|
def tearDown(self):
|
|
self.server.server_close()
|
|
kill(self.gthread)
|
|
|
|
def test_urllib(self):
|
|
self.assertEqual(self.server.request_count, 0)
|
|
try:
|
|
urlopen('http://127.0.0.1:%s' % self.port)
|
|
assert False, 'should not get there'
|
|
except HTTPError as ex:
|
|
assert ex.code == 501, repr(ex)
|
|
self.assertEqual(self.server.request_count, 1)
|
|
|
|
if __name__ == '__main__':
|
|
unittest.main()
|