1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
|
import greentest
import gevent
from gevent.hub import get_hub
DELAY = 0.01
class TestDirectRaise(greentest.TestCase):
switch_expected = False
def test_direct_raise_class(self):
try:
raise gevent.Timeout
except gevent.Timeout, t:
assert not t.pending, repr(t)
def test_direct_raise_instance(self):
timeout = gevent.Timeout()
try:
raise timeout
except gevent.Timeout, t:
assert timeout is t, (timeout, t)
assert not t.pending, repr(t)
class Test(greentest.TestCase):
def _test(self, timeout):
try:
get_hub().switch()
raise AssertionError('Must raise Timeout')
except gevent.Timeout, ex:
if ex is not timeout:
raise
def test(self):
timeout = gevent.Timeout(0.01)
timeout.start()
self._test(timeout)
timeout.start()
self._test(timeout)
def test_false(self):
timeout = gevent.Timeout(0.01, False)
timeout.start()
self._test(timeout)
timeout.start()
self._test(timeout)
def test_cancel(self):
timeout = gevent.Timeout(0.01)
timeout.start()
timeout.cancel()
gevent.sleep(0.02)
assert not timeout.pending, timeout
def test_with_timeout(self):
self.assertRaises(gevent.Timeout, gevent.with_timeout, DELAY, gevent.sleep, DELAY * 2)
X = object()
r = gevent.with_timeout(DELAY, gevent.sleep, DELAY * 2, timeout_value=X)
assert r is X, (r, X)
r = gevent.with_timeout(DELAY * 2, gevent.sleep, DELAY, timeout_value=X)
assert r is None, r
if __name__ == '__main__':
greentest.main()
|