diff options
| author | Ryan Williams <rdw@lindenlab.com> | 2009-12-17 10:37:59 -0800 |
|---|---|---|
| committer | Ryan Williams <rdw@lindenlab.com> | 2009-12-17 10:37:59 -0800 |
| commit | 441f72f4081c8a3ede26d0fc06d6c2ca2eff81fd (patch) | |
| tree | dd00600b0651fb231c1dadf8d568c9bd008e4e7e /tests | |
| parent | c7d3432b298f3e05e6775ffd999418006be14d77 (diff) | |
| download | eventlet-441f72f4081c8a3ede26d0fc06d6c2ca2eff81fd.tar.gz | |
Moved get_hub, use_hub, get_default_hub to eventlet.hubs. This is a step in the direction of better factoring and fewer circular-ish dependencies.
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/__init__.py | 15 | ||||
| -rw-r--r-- | tests/api_test.py | 22 | ||||
| -rw-r--r-- | tests/eventlethub.py | 8 | ||||
| -rw-r--r-- | tests/stdlib/test_thread.py | 4 | ||||
| -rw-r--r-- | tests/stdlib/test_threading_local.py | 4 | ||||
| -rw-r--r-- | tests/test__hub.py | 57 | ||||
| -rw-r--r-- | tests/test__pool.py | 4 | ||||
| -rw-r--r-- | tests/test__socket_errors.py | 9 | ||||
| -rw-r--r-- | tests/timer_test.py | 10 |
9 files changed, 70 insertions, 63 deletions
diff --git a/tests/__init__.py b/tests/__init__.py index 617e179..d99f0fa 100644 --- a/tests/__init__.py +++ b/tests/__init__.py @@ -4,7 +4,7 @@ import os import errno import unittest -# convenience +# convenience for importers main = unittest.main def skipped(func): @@ -63,7 +63,7 @@ def skip_unless(condition): def requires_twisted(func): """ Decorator that skips a test if Twisted is not present.""" def requirement(_f): - from eventlet.api import get_hub + from eventlet.hubs import get_hub try: return 'Twisted' in type(get_hub()).__name__ except Exception: @@ -74,12 +74,13 @@ def requires_twisted(func): def skip_with_pyevent(func): """ Decorator that skips a test if we're using the pyevent hub.""" def using_pyevent(_f): - from eventlet.api import get_hub + from eventlet.hubs import get_hub return 'pyevent' in type(get_hub()).__module__ return skip_if(using_pyevent)(func) def skip_on_windows(func): + """ Decorator that skips a test on Windows.""" import sys return skip_if(sys.platform.startswith('win'))(func) @@ -109,14 +110,14 @@ class SilencedTestCase(LimitedTestCase): """ Subclass of LimitedTestCase that also silences the printing of timer exceptions.""" def setUp(self): - from eventlet import api + from eventlet import hubs super(SilencedTestCase, self).setUp() - api.get_hub().silent_timer_exceptions = True + hubs.get_hub().silent_timer_exceptions = True def tearDown(self): - from eventlet import api + from eventlet import hubs super(SilencedTestCase, self).tearDown() - api.get_hub().silent_timer_exceptions = False + hubs.get_hub().silent_timer_exceptions = False def find_command(command): diff --git a/tests/api_test.py b/tests/api_test.py index 1ca9306..3e29cfe 100644 --- a/tests/api_test.py +++ b/tests/api_test.py @@ -6,21 +6,21 @@ from unittest import TestCase, main from eventlet import api from eventlet import greenio from eventlet import util - +from eventlet import hubs def check_hub(): # Clear through the descriptor queue api.sleep(0) api.sleep(0) - hub = api.get_hub() + hub = hubs.get_hub() for nm in 'get_readers', 'get_writers': dct = getattr(hub, nm)() assert not dct, "hub.%s not empty: %s" % (nm, dct) # Stop the runloop (unless it's twistedhub which does not support that) - if not getattr(api.get_hub(), 'uses_twisted_reactor', None): - api.get_hub().abort() + if not getattr(hub, 'uses_twisted_reactor', None): + hub.abort() api.sleep(0) - ### ??? assert not api.get_hub().running + ### ??? assert not hubs.get_hub().running class TestApi(TestCase): @@ -145,16 +145,6 @@ class TestApi(TestCase): check_hub() - if not getattr(api.get_hub(), 'uses_twisted_reactor', None): - def test_explicit_hub(self): - oldhub = api.get_hub() - try: - api.use_hub(Foo) - assert isinstance(api.get_hub(), Foo), api.get_hub() - finally: - api._threadlocal.hub = oldhub - check_hub() - def test_named(self): named_foo = api.named('tests.api_test.Foo') self.assertEquals( @@ -233,8 +223,6 @@ class TestApi(TestCase): self.assertRaises(api.TimeoutError, api.with_timeout, 0.1, func) - - class Foo(object): pass diff --git a/tests/eventlethub.py b/tests/eventlethub.py index 58e69a0..0da3235 100644 --- a/tests/eventlethub.py +++ b/tests/eventlethub.py @@ -1,7 +1,7 @@ import logging from nose.plugins.base import Plugin -from eventlet import api +from eventlet import hubs log = logging.getLogger('nose.plugins.eventlethub') @@ -56,13 +56,13 @@ class EventletHub(Plugin): if self.hub_name is None: log.warn('Using default eventlet hub: %s, did you mean '\ 'to supply --hub command line argument?', - api.get_hub().__module__) + hubs.get_hub().__module__) else: if self.hub_name == 'twistedr': if self.twisted_already_used: return else: self.twisted_already_used = True - api.use_hub(self.hub_name) - log.info('using hub %s', api.get_hub()) + hubs.use_hub(self.hub_name) + log.info('using hub %s', hubs.get_hub())
\ No newline at end of file diff --git a/tests/stdlib/test_thread.py b/tests/stdlib/test_thread.py index d0e40cf..0c4f8f3 100644 --- a/tests/stdlib/test_thread.py +++ b/tests/stdlib/test_thread.py @@ -3,8 +3,8 @@ from eventlet.green import thread from eventlet.green import time # necessary to initialize the hub before running on 2.5 -from eventlet import api -api.get_hub() +from eventlet import hubs +hubs.get_hub() patcher.inject('test.test_thread', globals(), diff --git a/tests/stdlib/test_threading_local.py b/tests/stdlib/test_threading_local.py index 04ab5db..85e4a0f 100644 --- a/tests/stdlib/test_threading_local.py +++ b/tests/stdlib/test_threading_local.py @@ -4,8 +4,8 @@ from eventlet.green import threading from eventlet.green import time # hub requires initialization before test can run -from eventlet import api -api.get_hub() +from eventlet import hubs +hubs.get_hub() patcher.inject('test.test_threading_local', globals(), diff --git a/tests/test__hub.py b/tests/test__hub.py index 007ce55..e00a960 100644 --- a/tests/test__hub.py +++ b/tests/test__hub.py @@ -1,37 +1,45 @@ -import unittest -from tests import SilencedTestCase +from tests import LimitedTestCase, SilencedTestCase, main import time from eventlet import api +from eventlet import hubs from eventlet.green import socket -DELAY = 0.1 - - -class TestScheduleCall(unittest.TestCase): +DELAY = 0.001 +class TestScheduleCall(LimitedTestCase): def test_local(self): lst = [1] - api.spawn(api.get_hub().schedule_call_local, DELAY, lst.pop) + api.spawn(hubs.get_hub().schedule_call_local, DELAY, lst.pop) + api.sleep(0) api.sleep(DELAY*2) assert lst == [1], lst def test_global(self): lst = [1] - api.spawn(api.get_hub().schedule_call_global, DELAY, lst.pop) + api.spawn(hubs.get_hub().schedule_call_global, DELAY, lst.pop) + api.sleep(0) api.sleep(DELAY*2) assert lst == [], lst + + def test_ordering(self): + lst = [] + hubs.get_hub().schedule_call_global(DELAY*2, lst.append, 3) + hubs.get_hub().schedule_call_global(DELAY, lst.append, 1) + hubs.get_hub().schedule_call_global(DELAY, lst.append, 2) + while len(lst) < 3: + api.sleep(DELAY) + self.assertEquals(lst, [1,2,3]) -class TestDebug(unittest.TestCase): +class TestDebug(LimitedTestCase): def test_debug(self): - api.get_hub().debug = True - self.assert_(api.get_hub().debug) - api.get_hub().debug = False - self.assert_(not api.get_hub().debug) + hubs.get_hub().debug = True + self.assert_(hubs.get_hub().debug) + hubs.get_hub().debug = False + self.assert_(not hubs.get_hub().debug) class TestExceptionInMainloop(SilencedTestCase): - def test_sleep(self): # even if there was an error in the mainloop, the hub should continue to work start = time.time() @@ -43,7 +51,7 @@ class TestExceptionInMainloop(SilencedTestCase): def fail(): 1/0 - api.get_hub().schedule_call_global(0, fail) + hubs.get_hub().schedule_call_global(0, fail) start = time.time() api.sleep(DELAY) @@ -52,6 +60,23 @@ class TestExceptionInMainloop(SilencedTestCase): assert delay >= DELAY*0.9, 'sleep returned after %s seconds (was scheduled for %s)' % (delay, DELAY) +class TestHubSelection(LimitedTestCase): + def test_explicit_hub(self): + if getattr(hubs.get_hub(), 'uses_twisted_reactor', None): + # doesn't work with twisted + return + oldhub = hubs.get_hub() + try: + hubs.use_hub(Foo) + self.assert_(isinstance(hubs.get_hub(), Foo), hubs.get_hub()) + finally: + hubs._threadlocal.hub = oldhub + + + +class Foo(object): + pass + if __name__=='__main__': - unittest.main() + main() diff --git a/tests/test__pool.py b/tests/test__pool.py index d9ff4a8..2278080 100644 --- a/tests/test__pool.py +++ b/tests/test__pool.py @@ -1,4 +1,4 @@ -from eventlet import pool, coros, api +from eventlet import pool, coros, api, hubs from tests import LimitedTestCase from unittest import main @@ -70,7 +70,7 @@ class TestCoroutinePool(LimitedTestCase): def fire_timer(): timer_fired.append(True) def some_work(): - api.get_hub().schedule_call_local(0, fire_timer) + hubs.get_hub().schedule_call_local(0, fire_timer) pool = self.klass(0, 2) worker = pool.execute(some_work) worker.wait() diff --git a/tests/test__socket_errors.py b/tests/test__socket_errors.py index 573568b..91fe3b0 100644 --- a/tests/test__socket_errors.py +++ b/tests/test__socket_errors.py @@ -1,13 +1,8 @@ import unittest from eventlet import api +from eventlet.green import socket -if hasattr(api._threadlocal, 'hub'): - from eventlet.green import socket -else: - import socket - -class TestSocketErrors(unittest.TestCase): - +class TestSocketErrors(unittest.TestCase): def test_connection_refused(self): # open and close a dummy server to find an unused port server = socket.socket(socket.AF_INET, socket.SOCK_STREAM) diff --git a/tests/timer_test.py b/tests/timer_test.py index cf6a39b..0510e4b 100644 --- a/tests/timer_test.py +++ b/tests/timer_test.py @@ -1,10 +1,8 @@ from unittest import TestCase, main -from eventlet import api, timer +from eventlet import api, timer, hubs class TestTimer(TestCase): - mode = 'static' - def test_copy(self): t = timer.Timer(0, lambda: None) t2 = t.copy() @@ -24,7 +22,7 @@ class TestTimer(TestCase): ## assert not r.running def test_schedule(self): - hub = api.get_hub() + hub = hubs.get_hub() # clean up the runloop, preventing side effects from previous tests # on this thread if hub.running: @@ -36,8 +34,8 @@ class TestTimer(TestCase): # let's have a timer somewhere in the future; make sure abort() still works # (for pyevent, its dispatcher() does not exit if there is something scheduled) # XXX pyevent handles this, other hubs do not - #api.get_hub().schedule_call_global(10000, lambda: (called.append(True), hub.abort())) - api.get_hub().schedule_call_global(0, lambda: (called.append(True), hub.abort())) + #hubs.get_hub().schedule_call_global(10000, lambda: (called.append(True), hub.abort())) + hubs.get_hub().schedule_call_global(0, lambda: (called.append(True), hub.abort())) hub.default_sleep = lambda: 0.0 hub.switch() assert called |
