receiver = gevent.spawn(sock.recv, 25) try: gevent.sleep(0.001) sock.close() receiver.join(timeout=0.1) self.assertTrue(receiver.ready(), receiver) self.assertEqual(receiver.value, None) self.assertIsInstance(receiver.exception, socket.error) self.assertEqual(receiver.exception.errno, socket.EBADF) finally: receiver.kill() # XXX: This is possibly due to the bad behaviour of small sleeps? # The timeout is the global test timeout, 10s @greentest.skipOnLibuvOnCI("Sometimes randomly times out") def test_recv_twice(self): sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock.connect((greentest.DEFAULT_CONNECT_HOST, self.server.server_port)) receiver = gevent.spawn(sock.recv, 25) try: gevent.sleep(0.001) self.assertRaises(AssertionError, sock.recv, 25) self.assertRaises(AssertionError, sock.recv, 25) finally: receiver.kill() sock.close() if __name__ == '__main__': greentest.main()
import unittest import subprocess import os from gevent import testing as greentest @unittest.skipUnless(greentest.resolver_dnspython_available(), "dnspython not available") class TestDnsPython(unittest.TestCase): def _run_one(self, mod_name): cmd = [sys.executable, '-m', 'gevent.tests.monkey_package.' + mod_name] env = dict(os.environ) env['GEVENT_RESOLVER'] = 'dnspython' output = subprocess.check_output(cmd, env=env) self.assertIn(b'_g_patched_module_dns', output) self.assertNotIn(b'_g_patched_module_dns.rdtypes', output) return output def test_import_dns_no_monkey_patch(self): self._run_one('issue1526_no_monkey') def test_import_dns_with_monkey_patch(self): self._run_one('issue1526_with_monkey') if __name__ == '__main__': greentest.main()
class TestExec(unittest.TestCase): pass def make_exec_test(path, module): def test(_): with open(path, 'rb') as f: src = f.read() with warnings.catch_warnings(): warnings.simplefilter('ignore', DeprecationWarning) six.exec_(src, {'__file__': path}) name = "test_" + module.replace(".", "_") test.__name__ = name setattr(TestExec, name, test) def make_all_tests(): for path, module in walk_modules(recursive=True): if module.endswith(NON_APPLICABLE_SUFFIXES): continue make_exec_test(path, module) make_all_tests() if __name__ == '__main__': main()
src = f.read() with warnings.catch_warnings(): warnings.simplefilter('ignore', DeprecationWarning) try: six.exec_(src, {'__file__': path}) except ImportError: if module in modules.OPTIONAL_MODULES: raise unittest.SkipTest("Unable to import optional module %s" % module) raise name = "test_" + module.replace(".", "_") test.__name__ = name return test def make_all_tests(cls): for path, module in modules.walk_modules(recursive=True, check_optional=False): if module.endswith(NON_APPLICABLE_SUFFIXES): continue test = make_exec_test(path, module) setattr(cls, test.__name__, test) return cls @make_all_tests class Test(unittest.TestCase): pass if __name__ == '__main__': main()
from gevent.testing import util from gevent.testing import main class Test_udp_client(util.TestServer): start_kwargs = {'timeout': 10} example = 'udp_client.py' example_args = ['Test_udp_client'] def test(self): log = [] def handle(message, address): log.append(message) server.sendto(b'reply-from-server', address) server = DatagramServer('127.0.0.1:9001', handle) server.start() try: self.run_example() finally: server.close() self.assertEqual(log, [b'Test_udp_client']) if __name__ == '__main__': # Running this following test__example_portforwarder on Appveyor # doesn't work in the same process for some reason. main() # pragma: testrunner-no-combine
msg="locals() unusable: %s..." % response) def test_switch_exc(self): from gevent.queue import Queue, Empty def bad(): q = Queue() print('switching out, then throwing in') try: q.get(block=True, timeout=0.1) except Empty: print("Got Empty") print('switching out') gevent.sleep(0.1) print('switched in') with self._make_and_start_server(locals={'bad': bad}) as server: with self._create_connection(server) as conn: conn.sendall(b'bad()\r\n') response = self._wait_for_prompt(conn) self._close(conn) response = response.replace('\r\n', '\n') self.assertEqual( 'switching out, then throwing in\nGot Empty\nswitching out\nswitched in\n>>> ', response) if __name__ == '__main__': greentest.main() # pragma: testrunner-no-combine
# -*- coding: utf-8 -*- """ Tests for ``gevent.threading`` that DO NOT monkey patch. This allows easy comparison with the standard module. """ from __future__ import absolute_import from __future__ import division from __future__ import print_function import threading from gevent import threading as gthreading from gevent import testing class TestDummyThread(testing.TestCase): def test_name(self): # Matches the stdlib. # https://github.com/gevent/gevent/issues/1659 std_dummy = threading._DummyThread() gvt_dummy = gthreading._DummyThread() self.assertIsNot(type(std_dummy), type(gvt_dummy)) self.assertStartsWith(std_dummy.name, 'Dummy-') self.assertStartsWith(gvt_dummy.name, 'Dummy-') if __name__ == '__main__': testing.main()