Commit 71b5c5cf authored by Jason Madden's avatar Jason Madden

Move our tests to src/gevent/tests; not actually run yet.

parent a5f821d5
# testrunner timeout: 300
import sys
import glob
import subprocess
import time
TIMEOUT = 30
def kill(popen):
if popen.poll() is not None:
return
try:
popen.kill()
except OSError as ex:
if ex.errno == 3: # No such process
return
if ex.errno == 13: # Permission denied (translated from windows error 5: "Access is denied")
return
raise
def wait(popen):
end = time.time() + TIMEOUT
while popen.poll() is None:
if time.time() > end:
kill(popen)
popen.wait()
return 'TIMEOUT'
time.sleep(0.1)
return popen.poll()
def system(command):
popen = subprocess.Popen(command, shell=False)
try:
return wait(popen)
finally:
kill(popen)
modules = set()
for path in glob.glob('bench_*.py'):
modules.add(path)
if __name__ == '__main__':
assert modules
errors = []
for path in modules:
sys.stderr.write(path + '\n')
sys.stdout.flush()
command = [sys.executable, '-u', path, 'all']
res = system(command)
if res:
error = '%r failed with %s' % (' '.join(command), res)
sys.stderr.write(error + '\n')
errors.append(error)
sys.stderr.write('-----\n\n')
if errors:
sys.exit('\n'.join(errors))
from gevent.select import select
from gevent.server import StreamServer
from gevent import socket
def handler(socket, address):
while True:
if not socket.recv(1000):
break
server = StreamServer(('127.0.0.1', 0), handler)
server.start()
s = socket.create_connection(('127.0.0.1', server.server_port))
while True:
select([], [s.fileno()] * 10, [])
import unittest
from gevent import socket
import gevent
import errno
import os
from test__server import SimpleStreamServer
class Test(unittest.TestCase):
ServerSubClass = SimpleStreamServer
def makefile(self, timeout=0.1, bufsize=1):
sock = socket.create_connection((self.server.server_host, self.server.server_port))
sock.settimeout(timeout)
return sock.makefile(bufsize=bufsize)
def assertConnectionRefused(self):
try:
conn = self.makefile()
raise AssertionError('Connection was not refused: %r' % (conn._sock, ))
except socket.error as ex:
if ex.args[0] != errno.ECONNREFUSED:
raise
def assertRequestSucceeded(self):
conn = self.makefile()
conn.write('GET /ping HTTP/1.0\r\n\r\n')
result = conn.read()
assert result.endswith('\r\n\r\nPONG'), repr(result)
def init_server(self):
self.server = self.ServerSubClass(('127.0.0.1', 0))
self.server.start()
gevent.sleep(0.01)
def test_socket_shutdown(self):
self.init_server()
self.server.socket.shutdown(socket.SHUT_RDWR)
self.assertConnectionRefused()
assert not self.server.started, self.server
def test_socket_close(self):
self.server = self.ServerSubClass(('127.0.0.1', 0))
self.server.start()
self.server.socket.close()
self.assertConnectionRefused()
#assert not self.server.started
def test_socket_close_fileno(self):
self.server = self.ServerSubClass(('127.0.0.1', 0))
self.server.start()
os.close(self.server.socket.fileno())
self.assertConnectionRefused()
#assert not self.server.started
def test_socket_file(self):
self.server = self.ServerSubClass(('127.0.0.1', 0))
self.server.start()
os.close(self.server.socket.fileno())
f = open("/dev/zero", "r")
self.assertConnectionRefused()
del f
if __name__ == '__main__':
unittest.main()
"""This is the extract from test_signal.py that runs forever until it fails.
It reproduces the bug where SIGCHLD either not delivered or somehow lost and thus
if libev does not poll waitpid() periodically, popen.wait() blocks forever.
The patch that fixes it: https://bitbucket.org/denis/gevent/changeset/adb8b5ac698c
Comment out the lines in ev.c that start the timer if you want to see for yourself.
Reproduced on my machine (Linux 3.0.0-16-generic) with backend epoll and select.
With signalfd enabled (GEVENT_BACKEND=signalfd) it seems to work.
"""
from __future__ import print_function
import gevent
from contextlib import closing
import gc
import pickle
from gevent import select
from gevent import subprocess
import traceback
import sys
import os
gc.disable()
MAX_DURATION = 10
def run_test():
child = subprocess.Popen(['/bin/true'])
child.wait() # << this is where it blocks
def test_main():
# This function spawns a child process to insulate the main
# test-running process from all the signals. It then
# communicates with that child process over a pipe and
# re-raises information about any exceptions the child
# throws. The real work happens in self.run_test().
os_done_r, os_done_w = os.pipe()
with closing(os.fdopen(os_done_r)) as done_r:
with closing(os.fdopen(os_done_w, 'w')) as done_w:
child = gevent.fork()
if not child:
# In the child process; run the test and report results
# through the pipe.
try:
done_r.close()
# Have to close done_w again here because
# exit_subprocess() will skip the enclosing with block.
with closing(done_w):
try:
run_test()
except:
pickle.dump(traceback.format_exc(), done_w)
else:
pickle.dump(None, done_w)
except:
print('Uh oh, raised from pickle.')
traceback.print_exc()
finally:
os._exit(0)
done_w.close()
# Block for up to MAX_DURATION seconds for the test to finish.
r, w, x = select.select([done_r], [], [], MAX_DURATION)
if done_r in r:
tb = pickle.load(done_r)
assert not tb, tb
else:
os.kill(child, 9)
assert False, 'Test deadlocked after %d seconds.' % MAX_DURATION
if __name__ == "__main__":
print(gevent.get_hub())
while True:
test_main()
sys.stderr.write('.')
import sys
import os
if os.system("ack 'from test import (?!test_support)|from test\.(?!test_support)' 2.5 2.6 2.7") != 256:
sys.exit('FAILED: Some tests in stdlib were not updated to not reference "test".')
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment