diff options
| author | Sergey Shepelev <temotor@gmail.com> | 2015-03-05 16:32:40 +0300 |
|---|---|---|
| committer | Sergey Shepelev <temotor@gmail.com> | 2015-03-05 16:32:40 +0300 |
| commit | 8ca2b0d5ff27bf963db78c01bab77e679baa0abc (patch) | |
| tree | 9ff77b27986f9ac31a07bef14caa44ed6b1804af | |
| parent | 84b535becafcdc932ab905061f5fa2fbea35dc74 (diff) | |
| download | eventlet-tm4.tar.gz | |
tests: move some into isolatedtm4
21 files changed, 456 insertions, 323 deletions
diff --git a/tests/__init__.py b/tests/__init__.py index 26c0c2e..ba8011c 100644 --- a/tests/__init__.py +++ b/tests/__init__.py @@ -23,6 +23,9 @@ import eventlet from eventlet import tpool +DEFAULT_TIMEOUT = 10 + + # convenience for importers main = unittest.main @@ -144,7 +147,7 @@ class LimitedTestCase(unittest.TestCase): timeout is 1 second, change it by setting TEST_TIMEOUT to the desired quantity.""" - TEST_TIMEOUT = 1 + TEST_TIMEOUT = DEFAULT_TIMEOUT def setUp(self): self.previous_alarm = None @@ -205,6 +208,7 @@ def check_idle_cpu_usage(duration, allowed_part): raise SkipTest('CPU usage testing not supported (`import resource` failed)') r1 = resource.getrusage(resource.RUSAGE_SELF) + # Must use green sleep here eventlet.sleep(duration) r2 = resource.getrusage(resource.RUSAGE_SELF) utime = r2.ru_utime - r1.ru_utime @@ -292,13 +296,30 @@ def get_database_auth(): return retval -def run_python(path): +def thread_call_timeout(timeout, fun, *args, **kwargs): + # ok, fun result + state = [False, None] + + def waiter_fun(): + state[:] = (True, fun(*args, **kwargs)) + + threading_original = eventlet.patcher.original('threading') + waiter = threading_original.Thread(target=waiter_fun) + waiter.daemon = True + waiter.start() + waiter.join(timeout=timeout) + return state + + +def run_python(path, env=None, timeout=DEFAULT_TIMEOUT): if not path.endswith('.py'): path += '.py' path = os.path.abspath(path) src_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) new_env = os.environ.copy() new_env['PYTHONPATH'] = os.pathsep.join(sys.path + [src_dir]) + if env: + new_env.update(env) p = subprocess.Popen( [sys.executable, path], env=new_env, @@ -306,12 +327,21 @@ def run_python(path): stdin=subprocess.PIPE, stdout=subprocess.PIPE, ) - output, _ = p.communicate() - return output + # Popen.communicate(timeout) is not available in CPython2.6 + ok, result = thread_call_timeout(timeout, p.communicate, input=None) + if not ok: + try: + p.kill() + except subprocess.ProcessLookupError: + # process finished between timeout and kill -- count as timeout still + pass + assert False, 'run_python timeout={0} path="{1}"'.format(timeout, path) + + return result[0] -def run_isolated(path, prefix='tests/isolated/'): - output = run_python(prefix + path).rstrip() +def run_isolated(path, prefix='tests/isolated/', env=None, timeout=DEFAULT_TIMEOUT): + output = run_python(path=prefix + path, timeout=timeout).rstrip() if output.startswith(b'skip'): parts = output.split(b':', 1) skip_args = [] diff --git a/tests/db_pool_test.py b/tests/db_pool_test.py index 9fc9ebc..76f7108 100644 --- a/tests/db_pool_test.py +++ b/tests/db_pool_test.py @@ -645,7 +645,7 @@ class Test02MysqlRaw(MysqlConnectionPool, RawConnectionPool, TestCase): __test__ = True -def postgres_requirement(_f): +def postgres_requirement(_f, debug=True): try: import psycopg2 try: @@ -653,10 +653,12 @@ def postgres_requirement(_f): psycopg2.connect(**auth) return True except psycopg2.OperationalError: - print("Skipping postgres tests, error when connecting") + if debug: + print("Skipping postgres tests, error when connecting") return False except ImportError: - print("Skipping postgres tests, psycopg2 not importable") + if debug: + print("Skipping postgres tests, psycopg2 not importable") return False diff --git a/tests/env_test.py b/tests/env_test.py index f8931c1..4fcc4cc 100644 --- a/tests/env_test.py +++ b/tests/env_test.py @@ -1,26 +1,15 @@ import os -from eventlet.support import six + +import tests from tests.patcher_test import ProcessBase -from tests import skip_with_pyevent -class Socket(ProcessBase): - def test_patched_thread(self): - new_mod = """from eventlet.green import socket -socket.gethostbyname('localhost') -socket.getaddrinfo('localhost', 80) -""" - os.environ['EVENTLET_TPOOL_DNS'] = 'yes' - try: - self.write_to_tempfile("newmod", new_mod) - output, lines = self.launch_subprocess('newmod.py') - self.assertEqual(len(lines), 1, lines) - finally: - del os.environ['EVENTLET_TPOOL_DNS'] +def test_tpool_dns(): + tests.run_isolated('env_tpool_dns.py') class Tpool(ProcessBase): - @skip_with_pyevent + @tests.skip_with_pyevent def test_tpool_size(self): expected = "40" normal = "20" diff --git a/tests/hub_test.py b/tests/hub_test.py index 07e502a..759e5b8 100644 --- a/tests/hub_test.py +++ b/tests/hub_test.py @@ -1,15 +1,14 @@ from __future__ import with_statement import sys - -import tests -from tests import LimitedTestCase, main, skip_with_pyevent, skip_if_no_itimer, skip_unless -from tests.patcher_test import ProcessBase import time + import eventlet from eventlet import hubs from eventlet.event import Event from eventlet.semaphore import Semaphore from eventlet.support import greenlets, six +import tests +from tests import LimitedTestCase, skip_with_pyevent, skip_if_no_itimer, skip_unless DELAY = 0.001 @@ -206,6 +205,10 @@ class TestExceptionInGreenthread(LimitedTestCase): g.kill() +class Foo(object): + pass + + class TestHubSelection(LimitedTestCase): def test_explicit_hub(self): @@ -357,47 +360,5 @@ class TestDeadRunLoop(LimitedTestCase): assert g.dead # sanity check that dummyproc has completed -class Foo(object): - pass - - -class TestDefaultHub(ProcessBase): - - def test_kqueue_unsupported(self): - # https://github.com/eventlet/eventlet/issues/38 - # get_hub on windows broken by kqueue - module_source = r''' -from __future__ import print_function - -# Simulate absence of kqueue even on platforms that support it. -import select -try: - del select.kqueue -except AttributeError: - pass - -from eventlet.support.six.moves import builtins - -original_import = builtins.__import__ - -def fail_import(name, *args, **kwargs): - if 'epoll' in name: - raise ImportError('disabled for test') - if 'kqueue' in name: - print('kqueue tried') - return original_import(name, *args, **kwargs) - -builtins.__import__ = fail_import - - -import eventlet.hubs -eventlet.hubs.get_default_hub() -print('ok') -''' - self.write_to_tempfile('newmod', module_source) - output, _ = self.launch_subprocess('newmod.py') - self.assertEqual(output, 'kqueue tried\nok\n') - - -if __name__ == '__main__': - main() +def test_kqueue_unsupported(): + tests.run_isolated('hub_kqueue_unsupported.py') diff --git a/tests/isolated/env_tpool_dns.py b/tests/isolated/env_tpool_dns.py new file mode 100644 index 0000000..cbdbb1d --- /dev/null +++ b/tests/isolated/env_tpool_dns.py @@ -0,0 +1,12 @@ +from __future__ import print_function + +# no standard tests in this file, ignore +__test__ = False + +if __name__ == '__main__': + import os + os.environ['EVENTLET_TPOOL_DNS'] = 'yes' + from eventlet.green import socket + socket.gethostbyname('localhost') + socket.getaddrinfo('localhost', 80) + print('pass') diff --git a/tests/isolated/hub_kqueue_unsupported.py b/tests/isolated/hub_kqueue_unsupported.py new file mode 100644 index 0000000..481b3e5 --- /dev/null +++ b/tests/isolated/hub_kqueue_unsupported.py @@ -0,0 +1,38 @@ +# https://github.com/eventlet/eventlet/issues/38 +# get_hub on windows broken by kqueue +from __future__ import print_function + +# no standard tests in this file, ignore +__test__ = False + + +def main(): + # Simulate absence of kqueue even on platforms that support it. + import select + try: + del select.kqueue + except AttributeError: + pass + + from eventlet.support.six.moves import builtins + + original_import = builtins.__import__ + state = [False] + + def fail_import(name, *args, **kwargs): + if 'epoll' in name: + raise ImportError('disabled for test') + if 'kqueue' in name: + state[0] = True + return original_import(name, *args, **kwargs) + + builtins.__import__ = fail_import + + import eventlet.hubs + eventlet.hubs.get_default_hub() + assert state[0], 'did not try to import kqueue' + print('pass') + + +if __name__ == '__main__': + main() diff --git a/tests/isolated/os_waitpid.py b/tests/isolated/os_waitpid.py new file mode 100644 index 0000000..677b3cc --- /dev/null +++ b/tests/isolated/os_waitpid.py @@ -0,0 +1,13 @@ +from __future__ import print_function + +# no standard tests in this file, ignore +__test__ = False + +if __name__ == '__main__': + import subprocess + import eventlet + eventlet.monkey_patch(all=False, os=True) + process = subprocess.Popen("sleep 0.1 && false", shell=True) + rc = process.wait() + assert rc == 1, rc + print('pass') diff --git a/tests/isolated/patcher_monkey_patch_ok.py b/tests/isolated/patcher_monkey_patch_ok.py new file mode 100644 index 0000000..718cae4 --- /dev/null +++ b/tests/isolated/patcher_monkey_patch_ok.py @@ -0,0 +1,9 @@ +from __future__ import print_function + +# no standard tests in this file, ignore +__test__ = False + +if __name__ == '__main__': + import eventlet + eventlet.monkey_patch() + print('pass') diff --git a/tests/isolated/patcher_monkey_patch_unknown_module.py b/tests/isolated/patcher_monkey_patch_unknown_module.py new file mode 100644 index 0000000..8b83200 --- /dev/null +++ b/tests/isolated/patcher_monkey_patch_unknown_module.py @@ -0,0 +1,14 @@ +from __future__ import print_function + +# no standard tests in this file, ignore +__test__ = False + +if __name__ == '__main__': + import eventlet + err = '' + try: + eventlet.monkey_patch(finagle=True) + except TypeError as e: + err = str(e) + assert 'finagle' in err, err + print('pass') diff --git a/tests/isolated/patcher_psycopg.py b/tests/isolated/patcher_psycopg.py new file mode 100644 index 0000000..491a63e --- /dev/null +++ b/tests/isolated/patcher_psycopg.py @@ -0,0 +1,51 @@ +from __future__ import print_function + +# no standard tests in this file, ignore +__test__ = False + + +def main(): + from tests.db_pool_test import postgres_requirement + if not postgres_requirement(None, debug=False): + print('skip:postgres_requirement') + return + + import sys + import eventlet + eventlet.monkey_patch() + if not eventlet.patcher.is_monkey_patched('psycopg'): + print('psycopg not monkeypatched') + sys.exit(1) + + from eventlet.support import six + import tests + # construct a non-json dsn for the subprocess + psycopg_auth = tests.get_database_auth()['psycopg2'] + if isinstance(psycopg_auth, six.string_types): + dsn = psycopg_auth + else: + dsn = " ".join(["%s=%s" % (k, v) for k, v in six.iteritems(psycopg_auth)]) + + count = [0] + + def tick(totalseconds, persecond): + for i in range(totalseconds * persecond): + count[0] += 1 + eventlet.sleep(1.0 / persecond) + + import psycopg2 + + def fetch(num, secs): + conn = psycopg2.connect(dsn) + cur = conn.cursor() + for i in range(num): + cur.execute("select pg_sleep(%s)", [secs]) + + f = eventlet.spawn(fetch, 2, 1) + eventlet.spawn(tick, 2, 100) + f.wait() + assert count[0] > 100, count[0] + print('pass') + +if __name__ == '__main__': + main() diff --git a/tests/isolated/patcher_subprocess.py b/tests/isolated/patcher_subprocess.py new file mode 100644 index 0000000..2b359a8 --- /dev/null +++ b/tests/isolated/patcher_subprocess.py @@ -0,0 +1,18 @@ +from __future__ import print_function + +# no standard tests in this file, ignore +__test__ = False + + +def main(): + import sys + import eventlet + eventlet.monkey_patch() + from eventlet.green import subprocess + + subprocess.Popen([sys.executable, '-c', ''], stdin=subprocess.PIPE) + + print('pass') + +if __name__ == '__main__': + main() diff --git a/tests/isolated/patcher_threading_greenlet.py b/tests/isolated/patcher_threading_greenlet.py new file mode 100644 index 0000000..d192268 --- /dev/null +++ b/tests/isolated/patcher_threading_greenlet.py @@ -0,0 +1,28 @@ +from __future__ import print_function + +# no standard tests in this file, ignore +__test__ = False + + +def main(): + import eventlet + eventlet.monkey_patch() + from eventlet import event + import threading + evt = event.Event() + state = [''] + + def fun(): + state[0] = repr(threading.currentThread()) + evt.send() + + eventlet.spawn_n(fun) + evt.wait() + + assert state[0].startswith('<_MainThread'), state[0] + active_count = len(threading._active) + assert active_count == 1, active_count + print('pass') + +if __name__ == '__main__': + main() diff --git a/tests/isolated/patcher_threading_greenthread.py b/tests/isolated/patcher_threading_greenthread.py new file mode 100644 index 0000000..b5fc363 --- /dev/null +++ b/tests/isolated/patcher_threading_greenthread.py @@ -0,0 +1,23 @@ +from __future__ import print_function + +# no standard tests in this file, ignore +__test__ = False + + +def main(): + import eventlet + eventlet.monkey_patch() + import threading + state = [''] + + def fun(): + state[0] = repr(threading.currentThread()) + + eventlet.spawn(fun).wait() + assert state[0].startswith('<_GreenThread'), state[0] + active_count = len(threading._active) + assert active_count == 1, active_count + print('pass') + +if __name__ == '__main__': + main() diff --git a/tests/isolated/patcher_threading_original_thread.py b/tests/isolated/patcher_threading_original_thread.py new file mode 100644 index 0000000..5009d69 --- /dev/null +++ b/tests/isolated/patcher_threading_original_thread.py @@ -0,0 +1,29 @@ +from __future__ import print_function + +# no standard tests in this file, ignore +__test__ = False + + +def main(): + import eventlet + eventlet.monkey_patch() + import threading + threading_original = eventlet.patcher.original('threading') + state = [''] + + def fun(): + state[0] = repr(threading.currentThread()) + + t = threading_original.Thread(target=fun) + t.start() + t.join() + + assert state[0].startswith('<Thread'), state[0] + active_patched = len(threading._active) + active_original = len(threading_original._active) + assert active_patched == 1, active_patched + assert active_original == 1, active_original + print('pass') + +if __name__ == '__main__': + main() diff --git a/tests/isolated/patcher_threading_patched_thread.py b/tests/isolated/patcher_threading_patched_thread.py new file mode 100644 index 0000000..f9b7681 --- /dev/null +++ b/tests/isolated/patcher_threading_patched_thread.py @@ -0,0 +1,26 @@ +from __future__ import print_function + +# no standard tests in this file, ignore +__test__ = False + + +def main(): + import eventlet + eventlet.monkey_patch() + import threading + state = [''] + + def fun(): + state[0] = repr(threading.currentThread()) + + t = threading.Thread(target=fun) + t.start() + t.join() + + assert state[0].startswith('<_MainThread'), state[0] + active_count = len(threading._active) + assert active_count == 1, active_count + print('pass') + +if __name__ == '__main__': + main() diff --git a/tests/isolated/patcher_threading_tpool.py b/tests/isolated/patcher_threading_tpool.py new file mode 100644 index 0000000..a74ec0b --- /dev/null +++ b/tests/isolated/patcher_threading_tpool.py @@ -0,0 +1,25 @@ +from __future__ import print_function + +# no standard tests in this file, ignore +__test__ = False + + +def main(): + import eventlet + eventlet.monkey_patch() + from eventlet import tpool + import threading + state = [''] + + def fun(): + state[0] = repr(threading.currentThread()) + + tpool.execute(fun) + + assert state[0].startswith('<Thread'), state[0] + active_count = len(threading._active) + assert active_count == 1, active_count + print('pass') + +if __name__ == '__main__': + main() diff --git a/tests/isolated/patcher_tpool_original_thread.py b/tests/isolated/patcher_tpool_original_thread.py new file mode 100644 index 0000000..aa2b32d --- /dev/null +++ b/tests/isolated/patcher_tpool_original_thread.py @@ -0,0 +1,32 @@ +from __future__ import print_function + +# no standard tests in this file, ignore +__test__ = False + + +def main(): + import eventlet + eventlet.monkey_patch(time=False, thread=False) + from eventlet import tpool + import time + + tickcount = [0] + + def tick(): + from eventlet.support import six + for i in six.moves.range(1000): + tickcount[0] += 1 + eventlet.sleep() + + def do_sleep(): + tpool.execute(time.sleep, 0.5) + + eventlet.spawn(tick) + w1 = eventlet.spawn(do_sleep) + w1.wait() + assert tickcount[0] > 900 + tpool.killall() + print('pass') + +if __name__ == '__main__': + main() diff --git a/tests/isolated/patcher_tpool_patched_thread.py b/tests/isolated/patcher_tpool_patched_thread.py new file mode 100644 index 0000000..5bb8f8b --- /dev/null +++ b/tests/isolated/patcher_tpool_patched_thread.py @@ -0,0 +1,32 @@ +from __future__ import print_function + +# no standard tests in this file, ignore +__test__ = False + + +def main(): + import eventlet + eventlet.monkey_patch(time=False, thread=True) + from eventlet import tpool + import time + + tickcount = [0] + + def tick(): + from eventlet.support import six + for i in six.moves.range(1000): + tickcount[0] += 1 + eventlet.sleep() + + def do_sleep(): + tpool.execute(time.sleep, 0.5) + + eventlet.spawn(tick) + w1 = eventlet.spawn(do_sleep) + w1.wait() + assert tickcount[0] > 900 + tpool.killall() + print('pass') + +if __name__ == '__main__': + main() diff --git a/tests/isolated/patcher_tpool_simple.py b/tests/isolated/patcher_tpool_simple.py new file mode 100644 index 0000000..871b690 --- /dev/null +++ b/tests/isolated/patcher_tpool_simple.py @@ -0,0 +1,19 @@ +from __future__ import print_function + +# no standard tests in this file, ignore +__test__ = False + + +def main(): + from eventlet import patcher + patcher.monkey_patch() + from eventlet import tpool + state = [] + tpool.execute(state.append, 1) + tpool.execute(state.append, 2) + tpool.killall() + assert set(state) == set([1, 2]) + print('pass') + +if __name__ == '__main__': + main() diff --git a/tests/patcher_psycopg_test.py b/tests/patcher_psycopg_test.py deleted file mode 100644 index 83ffdfa..0000000 --- a/tests/patcher_psycopg_test.py +++ /dev/null @@ -1,58 +0,0 @@ -import os - -from eventlet.support import six - -from tests import patcher_test, skip_unless -from tests import get_database_auth -from tests.db_pool_test import postgres_requirement - -psycopg_test_file = """ -import os -import sys -import eventlet -eventlet.monkey_patch() -from eventlet import patcher -if not patcher.is_monkey_patched('psycopg'): - print("Psycopg not monkeypatched") - sys.exit(0) - -count = [0] -def tick(totalseconds, persecond): - for i in range(totalseconds*persecond): - count[0] += 1 - eventlet.sleep(1.0/persecond) - -dsn = os.environ['PSYCOPG_TEST_DSN'] -import psycopg2 -def fetch(num, secs): - conn = psycopg2.connect(dsn) - cur = conn.cursor() - for i in range(num): - cur.execute("select pg_sleep(%s)", (secs,)) - -f = eventlet.spawn(fetch, 2, 1) -t = eventlet.spawn(tick, 2, 100) -f.wait() -assert count[0] > 100, count[0] -print("done") -""" - - -class PatchingPsycopg(patcher_test.ProcessBase): - @skip_unless(postgres_requirement) - def test_psycopg_patched(self): - if 'PSYCOPG_TEST_DSN' not in os.environ: - # construct a non-json dsn for the subprocess - psycopg_auth = get_database_auth()['psycopg2'] - if isinstance(psycopg_auth, str): - dsn = psycopg_auth - else: - dsn = " ".join(["%s=%s" % (k, v) for k, v in six.iteritems(psycopg_auth)]) - os.environ['PSYCOPG_TEST_DSN'] = dsn - self.write_to_tempfile("psycopg_patcher", psycopg_test_file) - output, lines = self.launch_subprocess('psycopg_patcher.py') - if lines[0].startswith('Psycopg not monkeypatched'): - print("Can't test psycopg2 patching; it's not installed.") - return - # if there's anything wrong with the test program it'll have a stack trace - assert lines[0].startswith('done'), output diff --git a/tests/patcher_test.py b/tests/patcher_test.py index cd6296f..f514589 100644 --- a/tests/patcher_test.py +++ b/tests/patcher_test.py @@ -60,12 +60,6 @@ class ProcessBase(tests.LimitedTestCase): lines = output.split(separator) return output, lines - def run_script(self, contents, modname=None): - if modname is None: - modname = "testmod" - self.write_to_tempfile(modname, contents) - return self.launch_subprocess(modname) - class ImportPatched(ProcessBase): def test_patch_a_module(self): @@ -148,16 +142,6 @@ print("newmod") self.assertEqual(len(lines), 2, repr(output)) assert lines[0].startswith('newmod'), repr(output) - def test_typeerror(self): - new_mod = """ -from eventlet import patcher -patcher.monkey_patch(finagle=True) -""" - self.write_to_tempfile("newmod", new_mod) - output, lines = self.launch_subprocess('newmod.py') - assert lines[-2].startswith('TypeError'), repr(output) - assert 'finagle' in lines[-2], repr(output) - def assert_boolean_logic(self, call, expected, not_expected=''): expected_list = ", ".join(['"%s"' % x for x in expected.split(',') if len(x)]) not_expected_list = ", ".join(['"%s"' % x for x in not_expected.split(',') if len(x)]) @@ -221,200 +205,52 @@ print("already_patched {0}".format(",".join(sorted(patcher.already_patched.keys( 'select') -test_monkey_patch_threading = """ -def test_monkey_patch_threading(): - tickcount = [0] +def test_tpool_original_thread(): + tests.run_isolated('patcher_tpool_original_thread.py') - def tick(): - from eventlet.support import six - for i in six.moves.range(1000): - tickcount[0] += 1 - eventlet.sleep() - def do_sleep(): - tpool.execute(time.sleep, 0.5) +def test_tpool_patched_thread(): + tests.run_isolated('patcher_tpool_patched_thread.py') - eventlet.spawn(tick) - w1 = eventlet.spawn(do_sleep) - w1.wait() - print(tickcount[0]) - assert tickcount[0] > 900 - tpool.killall() -""" +def test_tpool_simple(): + tests.run_isolated('patcher_tpool_simple.py') -class Tpool(ProcessBase): - TEST_TIMEOUT = 3 - @tests.skip_with_pyevent - def test_simple(self): - new_mod = """ -import eventlet -from eventlet import patcher -patcher.monkey_patch() -from eventlet import tpool -print("newmod {0}".format(tpool.execute(len, "hi"))) -print("newmod {0}".format(tpool.execute(len, "hi2"))) -tpool.killall() -""" - self.write_to_tempfile("newmod", new_mod) - output, lines = self.launch_subprocess('newmod.py') - self.assertEqual(len(lines), 3, output) - assert lines[0].startswith('newmod'), repr(output) - assert '2' in lines[0], repr(output) - assert '3' in lines[1], repr(output) - - @tests.skip_with_pyevent - def test_unpatched_thread(self): - new_mod = """import eventlet -eventlet.monkey_patch(time=False, thread=False) -from eventlet import tpool -import time -""" - new_mod += test_monkey_patch_threading - new_mod += "\ntest_monkey_patch_threading()\n" - self.write_to_tempfile("newmod", new_mod) - output, lines = self.launch_subprocess('newmod.py') - self.assertEqual(len(lines), 2, lines) - - @tests.skip_with_pyevent - def test_patched_thread(self): - new_mod = """import eventlet -eventlet.monkey_patch(time=False, thread=True) -from eventlet import tpool -import time -""" - new_mod += test_monkey_patch_threading - new_mod += "\ntest_monkey_patch_threading()\n" - self.write_to_tempfile("newmod", new_mod) - output, lines = self.launch_subprocess('newmod.py') - self.assertEqual(len(lines), 2, "\n".join(lines)) +def test_subprocess(): + tests.run_isolated('patcher_subprocess.py') -class Subprocess(ProcessBase): - def test_monkeypatched_subprocess(self): - new_mod = """import eventlet -eventlet.monkey_patch() -from eventlet.green import subprocess +def test_threading_original_thread(): + tests.run_isolated('patcher_threading_original_thread.py') -subprocess.Popen(['true'], stdin=subprocess.PIPE) -print("done") -""" - self.write_to_tempfile("newmod", new_mod) - output, lines = self.launch_subprocess('newmod') - self.assertEqual(output, "done\n", output) +def test_threading_patched_thread(): + tests.run_isolated('patcher_threading_patched_thread.py') -class Threading(ProcessBase): - def test_orig_thread(self): - new_mod = """import eventlet -eventlet.monkey_patch() -from eventlet import patcher -import threading -_threading = patcher.original('threading') -def test(): - print(repr(threading.currentThread())) -t = _threading.Thread(target=test) -t.start() -t.join() -print(len(threading._active)) -print(len(_threading._active)) -""" - self.write_to_tempfile("newmod", new_mod) - output, lines = self.launch_subprocess('newmod') - self.assertEqual(len(lines), 4, "\n".join(lines)) - assert lines[0].startswith('<Thread'), lines[0] - self.assertEqual(lines[1], "1", lines[1]) - self.assertEqual(lines[2], "1", lines[2]) - def test_threading(self): - new_mod = """import eventlet -eventlet.monkey_patch() -import threading -def test(): - print(repr(threading.currentThread())) -t = threading.Thread(target=test) -t.start() -t.join() -print(len(threading._active)) -""" - self.write_to_tempfile("newmod", new_mod) - output, lines = self.launch_subprocess('newmod') - self.assertEqual(len(lines), 3, "\n".join(lines)) - assert lines[0].startswith('<_MainThread'), lines[0] - self.assertEqual(lines[1], "1", lines[1]) +def test_threading_tpool(): + tests.run_isolated('patcher_threading_tpool.py') - def test_tpool(self): - new_mod = """import eventlet -eventlet.monkey_patch() -from eventlet import tpool -import threading -def test(): - print(repr(threading.currentThread())) -tpool.execute(test) -print(len(threading._active)) -""" - self.write_to_tempfile("newmod", new_mod) - output, lines = self.launch_subprocess('newmod') - self.assertEqual(len(lines), 3, "\n".join(lines)) - assert lines[0].startswith('<Thread'), lines[0] - self.assertEqual(lines[1], "1", lines[1]) - def test_greenlet(self): - new_mod = """import eventlet -eventlet.monkey_patch() -from eventlet import event -import threading -evt = event.Event() -def test(): - print(repr(threading.currentThread())) - evt.send() -eventlet.spawn_n(test) -evt.wait() -print(len(threading._active)) -""" - self.write_to_tempfile("newmod", new_mod) - output, lines = self.launch_subprocess('newmod') - self.assertEqual(len(lines), 3, "\n".join(lines)) - assert lines[0].startswith('<_MainThread'), lines[0] - self.assertEqual(lines[1], "1", lines[1]) +def test_threading_greenlet(): + tests.run_isolated('patcher_threading_greenlet.py') - def test_greenthread(self): - new_mod = """import eventlet -eventlet.monkey_patch() -import threading -def test(): - print(repr(threading.currentThread())) -t = eventlet.spawn(test) -t.wait() -print(len(threading._active)) -""" - self.write_to_tempfile("newmod", new_mod) - output, lines = self.launch_subprocess('newmod') - self.assertEqual(len(lines), 3, "\n".join(lines)) - assert lines[0].startswith('<_GreenThread'), lines[0] - self.assertEqual(lines[1], "1", lines[1]) - def test_keyerror(self): - new_mod = """import eventlet -eventlet.monkey_patch() -""" - self.write_to_tempfile("newmod", new_mod) - output, lines = self.launch_subprocess('newmod') - self.assertEqual(len(lines), 1, "\n".join(lines)) +def test_threading_greenthread(): + tests.run_isolated('patcher_threading_greenthread.py') -class Os(ProcessBase): - def test_waitpid(self): - new_mod = """import subprocess -import eventlet -eventlet.monkey_patch(all=False, os=True) -process = subprocess.Popen("sleep 0.1 && false", shell=True) -print(process.wait())""" - self.write_to_tempfile("newmod", new_mod) - output, lines = self.launch_subprocess('newmod') - self.assertEqual(len(lines), 2, "\n".join(lines)) - self.assertEqual('1', lines[0], repr(output)) +def test_monkey_patch_ok(): + tests.run_isolated('patcher_monkey_patch_ok.py') + + +def test_monkey_patch_unknown_module(): + tests.run_isolated('patcher_monkey_patch_unknown_module.py') + + +def test_os_waitpid(): + tests.run_isolated('os_waitpid.py') class GreenThreadWrapper(ProcessBase): @@ -498,3 +334,7 @@ t2.join() def test_importlib_lock(): tests.run_isolated('patcher_importlib_lock.py') + + +def test_psycopg_patched(): + tests.run_isolated('patcher_psycopg.py') |
