fixed bug with nested api.with_timeout interfering with each other; added a testcase
This commit is contained in:
@@ -395,12 +395,13 @@ def with_timeout(seconds, func, *args, **kwds):
|
|||||||
# pass timeout_value through to func().
|
# pass timeout_value through to func().
|
||||||
has_timeout_value = "timeout_value" in kwds
|
has_timeout_value = "timeout_value" in kwds
|
||||||
timeout_value = kwds.pop("timeout_value", None)
|
timeout_value = kwds.pop("timeout_value", None)
|
||||||
timeout = exc_after(seconds, TimeoutError())
|
error = TimeoutError()
|
||||||
|
timeout = exc_after(seconds, error)
|
||||||
try:
|
try:
|
||||||
try:
|
try:
|
||||||
return func(*args, **kwds)
|
return func(*args, **kwds)
|
||||||
except TimeoutError:
|
except TimeoutError, ex:
|
||||||
if has_timeout_value:
|
if ex is error and has_timeout_value:
|
||||||
return timeout_value
|
return timeout_value
|
||||||
raise
|
raise
|
||||||
finally:
|
finally:
|
||||||
|
@@ -1,5 +1,5 @@
|
|||||||
import unittest
|
import unittest
|
||||||
from eventlet.api import sleep, spawn, kill
|
from eventlet.api import sleep, spawn, kill, with_timeout, TimeoutError
|
||||||
|
|
||||||
DELAY = 0.1
|
DELAY = 0.1
|
||||||
|
|
||||||
@@ -29,6 +29,11 @@ class Test(unittest.TestCase):
|
|||||||
sleep(DELAY)
|
sleep(DELAY)
|
||||||
assert state == ['start', 'except', 'finished'], state
|
assert state == ['start', 'except', 'finished'], state
|
||||||
|
|
||||||
|
def test_nested_with_timeout(self):
|
||||||
|
def func():
|
||||||
|
return with_timeout(0.2, sleep, 2, timeout_value=1)
|
||||||
|
self.assertRaises(TimeoutError, with_timeout, 0.1, func)
|
||||||
|
|
||||||
if __name__=='__main__':
|
if __name__=='__main__':
|
||||||
unittest.main()
|
unittest.main()
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user