Commit 5b9c9249 authored by Denis Bilenko's avatar Denis Bilenko

test__queue.py: use "with Timeout"

parent 9d7e6847
from __future__ import with_statement
from greentest import TestCase, main, GenericGetTestCase from greentest import TestCase, main, GenericGetTestCase
import gevent import gevent
from gevent.hub import get_hub from gevent.hub import get_hub
...@@ -19,11 +20,8 @@ class TestQueue(TestCase): ...@@ -19,11 +20,8 @@ class TestQueue(TestCase):
q = queue.Queue() q = queue.Queue()
def waiter(q): def waiter(q):
timer = gevent.Timeout.start_new(0.1) with gevent.Timeout(0.1):
try:
self.assertEquals(q.get(), 'hi2') self.assertEquals(q.get(), 'hi2')
finally:
timer.cancel()
return "OK" return "OK"
p = gevent.spawn(waiter, q) p = gevent.spawn(waiter, q)
...@@ -75,11 +73,8 @@ class TestQueue(TestCase): ...@@ -75,11 +73,8 @@ class TestQueue(TestCase):
p2 = gevent.spawn(receiver, e2, q) p2 = gevent.spawn(receiver, e2, q)
self.assertEquals(e2.get(), 'hi') self.assertEquals(e2.get(), 'hi')
self.assertEquals(e1.get(), 'done') self.assertEquals(e1.get(), 'done')
timeout = gevent.Timeout.start_new(0) with gevent.Timeout(0):
try:
gevent.joinall([p1, p2]) gevent.joinall([p1, p2])
finally:
timeout.cancel()
def test_multiple_waiters(self): def test_multiple_waiters(self):
# tests that multiple waiters get their results back # tests that multiple waiters get their results back
...@@ -91,22 +86,19 @@ class TestQueue(TestCase): ...@@ -91,22 +86,19 @@ class TestQueue(TestCase):
sendings = ['1', '2', '3', '4'] sendings = ['1', '2', '3', '4']
evts = [AsyncResult() for x in sendings] evts = [AsyncResult() for x in sendings]
for i, x in enumerate(sendings): for i, x in enumerate(sendings):
gevent.spawn(waiter, q, evts[i]) # use waitall for them gevent.spawn(waiter, q, evts[i]) # XXX use waitall for them
gevent.sleep(0.01) # get 'em all waiting gevent.sleep(0.01) # get 'em all waiting
results = set() results = set()
def collect_pending_results(): def collect_pending_results():
for i, e in enumerate(evts): for e in evts:
timer = gevent.Timeout.start_new(0.001) with gevent.Timeout(0.001, False):
try:
x = e.get() x = e.get()
results.add(x) results.add(x)
timer.cancel()
except gevent.Timeout:
pass # no pending result at that event
return len(results) return len(results)
q.put(sendings[0]) q.put(sendings[0])
self.assertEquals(collect_pending_results(), 1) self.assertEquals(collect_pending_results(), 1)
q.put(sendings[1]) q.put(sendings[1])
...@@ -119,7 +111,7 @@ class TestQueue(TestCase): ...@@ -119,7 +111,7 @@ class TestQueue(TestCase):
q = queue.Queue() q = queue.Queue()
def do_receive(q, evt): def do_receive(q, evt):
gevent.Timeout.start_new(0, RuntimeError()) with gevent.Timeout(0, RuntimeError()):
try: try:
result = q.get() result = q.get()
evt.set(result) evt.set(result)
...@@ -148,15 +140,12 @@ class TestQueue(TestCase): ...@@ -148,15 +140,12 @@ class TestQueue(TestCase):
evt.set(q.get()) evt.set(q.get())
def do_receive(q, evt): def do_receive(q, evt):
timeout = gevent.Timeout.start_new(0, RuntimeError()) with gevent.Timeout(0, RuntimeError()):
try:
try: try:
result = q.get() result = q.get()
evt.set(result) evt.set(result)
except RuntimeError: except RuntimeError:
evt.set('timed out') evt.set('timed out')
finally:
timeout.cancel()
q = queue.Queue() q = queue.Queue()
dying_evt = AsyncResult() dying_evt = AsyncResult()
...@@ -170,13 +159,12 @@ class TestQueue(TestCase): ...@@ -170,13 +159,12 @@ class TestQueue(TestCase):
def test_two_bogus_waiters(self): def test_two_bogus_waiters(self):
def do_receive(q, evt): def do_receive(q, evt):
gevent.Timeout.start_new(0, RuntimeError()) with gevent.Timeout(0, RuntimeError()):
try: try:
result = q.get() result = q.get()
evt.set(result) evt.set(result)
except RuntimeError: except RuntimeError:
evt.set('timed out') evt.set('timed out')
# XXX finally = timeout
q = queue.Queue() q = queue.Queue()
e1 = AsyncResult() e1 = AsyncResult()
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment