class LimitedTestCase(unittest.TestCase): """ Unittest subclass that adds a timeout to all tests. Subclasses must be sure to call the LimitedTestCase setUp and tearDown methods. The default timeout is 1 second, change it by setting self.TEST_TIMEOUT to the desired quantity. """ TEST_TIMEOUT = 1 def setUp(self): self.timer = Timeout(self.TEST_TIMEOUT, TestIsTakingTooLong(self.TEST_TIMEOUT)) def reset_timeout(self, new_timeout): """ Changes the timeout duration; only has effect during one test case """ self.timer.cancel() self.timer = Timeout(new_timeout, TestIsTakingTooLong(new_timeout)) def tearDown(self): self.timer.cancel() try: hub = hubs.get_hub() num_readers = len(hub.get_readers()) num_writers = len(hub.get_writers()) assert num_readers == num_writers == 0 except AssertionError, e: print "ERROR: Hub not empty" print debug.format_hub_timers() print debug.format_hub_listeners()
class LimitedTestCase(unittest.TestCase): """ Unittest subclass that adds a timeout to all tests. Subclasses must be sure to call the LimitedTestCase setUp and tearDown methods. The default timeout is 1 second, change it by setting self.TEST_TIMEOUT to the desired quantity. """ TEST_TIMEOUT = 1 def setUp (self): self.timer = Timeout(self.TEST_TIMEOUT, TestIsTakingTooLong(self.TEST_TIMEOUT)) def reset_timeout (self, new_timeout): """ Changes the timeout duration; only has effect during one test case """ self.timer.cancel() self.timer = Timeout(new_timeout, TestIsTakingTooLong(new_timeout)) def tearDown (self): self.timer.cancel() try: hub = hubs.get_hub() num_readers = len(hub.get_readers()) num_writers = len(hub.get_writers()) assert num_readers == num_writers == 0 except AssertionError, e: print "ERROR: Hub not empty" print debug.format_hub_timers() print debug.format_hub_listeners()
def do_receive(tp): _timer = Timeout(0, RuntimeError()) try: t = tp.get() self.fail("Shouldn't have recieved anything from the pool") except RuntimeError: return "timed out" else: _timer.cancel()
def do_receive(tp): _timer = Timeout(0, RuntimeError()) try: t = tp.get() self.fail("Shouldn't have recieved anything from the pool") except RuntimeError: return 'timed out' else: _timer.cancel()
def assert_pool_has_free(self, pool, num_free): self.assertEquals(pool.free(), num_free) def wait_long_time(e): e.wait() timer = Timeout(1) try: evt = Event() for x in xrange(num_free): pool.spawn(wait_long_time, evt) # if the pool has fewer free than we expect, # then we'll hit the timeout error finally: timer.cancel() # if the runtime error is not raised it means the pool had # some unexpected free items timer = Timeout(0, RuntimeError) try: self.assertRaises(RuntimeError, pool.spawn, wait_long_time, evt) finally: timer.cancel() # clean up by causing all the wait_long_time functions to return evt.send(None) sleep(0) sleep(0)
def do_receive(q, evt): Timeout(0, RuntimeError()) try: result = q.join() evt.send(result) except RuntimeError: evt.send('timed out')
def do_bail(q): Timeout(0, RuntimeError()) try: result = q.get() return result except RuntimeError: return 'timed out'
def put(self, item, block=True, timeout=None): """ Put an item into the queue. If optional arg *block* is true and *timeout* is ``None`` (the default), block if necessary until a free slot is available. If *timeout* is a positive number, it blocks at most *timeout* seconds and raises the :class:`Full` exception if no free slot was available within that time. Otherwise (*block* is false), put an item on the queue if a free slot is immediately available, else raise the :class:`Full` exception (*timeout* is ignored in that case). """ if self.maxsize is None or self.qsize() < self.maxsize: # there's a free slot, put an item right away self._put(item) if self.getters: self._schedule_unlock() elif not block and get_hub().greenlet is getcurrent(): # we're in the mainloop, so we cannot wait; we can switch() to other greenlets though # find a getter and deliver an item to it while self.getters: getter = self.getters.pop() if getter: self._put(item) item = self._get() getter.switch(item) return raise Full elif block: waiter = ItemWaiter(item) self.putters.add(waiter) timeout = Timeout(timeout, Full) try: if self.getters: self._schedule_unlock() result = waiter.wait() assert result is waiter, "Invalid switch into Queue.put: %r" % ( result, ) if waiter.item is not _NONE: self._put(item) finally: timeout.cancel() self.putters.discard(waiter) else: raise Full
def put (self, item, block = True, timeout = None): """ Put an item into the queue. If optional arg *block* is true and *timeout* is ``None`` (the default), block if necessary until a free slot is available. If *timeout* is a positive number, it blocks at most *timeout* seconds and raises the :class:`Full` exception if no free slot was available within that time. Otherwise (*block* is false), put an item on the queue if a free slot is immediately available, else raise the :class:`Full` exception (*timeout* is ignored in that case). """ if self.maxsize is None or self.qsize() < self.maxsize: # there's a free slot, put an item right away self._put(item) if self.getters: self._schedule_unlock() elif not block and get_hub().greenlet is getcurrent(): # we're in the mainloop, so we cannot wait; we can switch() to other greenlets though # find a getter and deliver an item to it while self.getters: getter = self.getters.pop() if getter: self._put(item) item = self._get() getter.switch(item) return raise Full elif block: waiter = ItemWaiter(item) self.putters.add(waiter) timeout = Timeout(timeout, Full) try: if self.getters: self._schedule_unlock() result = waiter.wait() assert result is waiter, "Invalid switch into Queue.put: %r" % (result, ) if waiter.item is not _NONE: self._put(item) finally: timeout.cancel() self.putters.discard(waiter) else: raise Full
def test_double_exception (self): evt = Event() # send an exception through the event evt.send(exc = RuntimeError('from test_double_exception')) self.assertRaises(RuntimeError, evt.wait) evt.reset() # shouldn't see the RuntimeError again Timeout(0.001) self.assertRaises(Timeout, evt.wait)
def test_pile_spawn_times_out(self): p = GreenPile(4) for i in xrange(4): p.spawn(passthru, i) # now it should be full and this should time out Timeout(0) self.assertRaises(Timeout, p.spawn, passthru, "time out") # verify that the spawn breakage didn't interrupt the sequence # and terminates properly for i in xrange(4, 10): p.spawn(passthru, i) self.assertEquals(list(p), list(xrange(10)))
def test_send (self): event1 = Event() event2 = Event() spawn(event1.send, 'hello event1') Timeout(0, ValueError('interrupted')) try: result = event1.wait() except ValueError: X = object() result = with_timeout(DELAY, event2.wait, timeout_value = X) assert result is X, 'Nobody sent anything to event2 yet it received %r' % (result, )
def test_putting_to_queue (self): timer = Timeout(0.1) try: size = 2 self.pool = IntPool(min_size = 0, max_size = size) queue = Queue() results = [] def just_put (pool_item, index): self.pool.put(pool_item) queue.put(index) for index in xrange(size + 1): pool_item = self.pool.get() spawn(just_put, pool_item, index) for _ in range(size + 1): x = queue.get() results.append(x) self.assertEqual(sorted(results), range(size + 1)) finally: timer.cancel()
def test_putting_to_queue(self): timer = Timeout(0.1) try: size = 2 self.pool = IntPool(min_size=0, max_size=size) queue = Queue() results = [] def just_put(pool_item, index): self.pool.put(pool_item) queue.put(index) for index in xrange(size + 1): pool_item = self.pool.get() spawn(just_put, pool_item, index) for _ in range(size + 1): x = queue.get() results.append(x) self.assertEqual(sorted(results), range(size + 1)) finally: timer.cancel()
def get (self, block = True, timeout = None): """ Remove and return an item from the queue. If optional args *block* is true and *timeout* is ``None`` (the default), block if necessary until an item is available. If *timeout* is a positive number, it blocks at most *timeout* seconds and raises the :class:`Empty` exception if no item was available within that time. Otherwise (*block* is false), return an item if one is immediately available, else raise the :class:`Empty` exception (*timeout* is ignored in that case). """ if self.qsize(): if self.putters: self._schedule_unlock() return self._get() elif not block and get_hub().greenlet is getcurrent(): # special case to make get_nowait() runnable in the mainloop greenlet # there are no items in the queue; try to fix the situation by unlocking putters while self.putters: putter = self.putters.pop() if putter: putter.switch(putter) if self.qsize(): return self._get() raise Empty elif block: waiter = Waiter() timeout = Timeout(timeout, Empty) try: self.getters.add(waiter) if self.putters: self._schedule_unlock() return waiter.wait() finally: self.getters.discard(waiter) timeout.cancel() else: raise Empty
def get(self, block=True, timeout=None): """ Remove and return an item from the queue. If optional args *block* is true and *timeout* is ``None`` (the default), block if necessary until an item is available. If *timeout* is a positive number, it blocks at most *timeout* seconds and raises the :class:`Empty` exception if no item was available within that time. Otherwise (*block* is false), return an item if one is immediately available, else raise the :class:`Empty` exception (*timeout* is ignored in that case). """ if self.qsize(): if self.putters: self._schedule_unlock() return self._get() elif not block and get_hub().greenlet is getcurrent(): # special case to make get_nowait() runnable in the mainloop greenlet # there are no items in the queue; try to fix the situation by unlocking putters while self.putters: putter = self.putters.pop() if putter: putter.switch(putter) if self.qsize(): return self._get() raise Empty elif block: waiter = Waiter() timeout = Timeout(timeout, Empty) try: self.getters.add(waiter) if self.putters: self._schedule_unlock() return waiter.wait() finally: self.getters.discard(waiter) timeout.cancel() else: raise Empty
def test_stderr_raising(self): # testing that really egregious errors in the error handling code # (that prints tracebacks to stderr) don't cause the pool to lose # any members import sys pool = GreenPool(1) def crash(*args, **kw): raise RuntimeError("Whoa") class FakeFile(object): write = crash # we're going to do this by causing the traceback.print_exc in # safe_apply to raise an exception and thus exit _main_loop normal_err = sys.stderr try: sys.stderr = FakeFile() waiter = pool.spawn(crash) self.assertRaises(RuntimeError, waiter.wait) # the pool should have something free at this point since the # waiter returned # GreenPool change: if an exception is raised during execution of a link, # the rest of the links are scheduled to be executed on the next hub iteration # this introduces a delay in updating pool.sem which makes pool.free() report 0 # therefore, sleep: sleep(0) self.assertEqual(pool.free(), 1) # shouldn't block when trying to get t = Timeout(0.1) try: pool.spawn(sleep, 1) finally: t.cancel() finally: sys.stderr = normal_err
def test_timeout_and_final_write(self): """ This test verifies that a write on a socket that we've stopped listening for doesn't result in an incorrect switch """ server = socket.socket(socket.AF_INET, socket.SOCK_STREAM) server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) server.bind(('127.0.0.1', 0)) server.listen(50) _, bound_port = server.getsockname() self.assertNotEqual(bound_port, 0) def sender(evt): s2, addr = server.accept() wrap_wfile = s2.makefile('w') sleep(0.02) wrap_wfile.write('hi') s2.close() evt.send('sent via event') from evy import event evt = event.Event() spawn(sender, evt) sleep(0) # lets the socket enter accept mode, which # is necessary for connect to succeed on windows try: # try and get some data off of this pipe # but bail before any is sent Timeout(0.01) client = socket.socket(socket.AF_INET, socket.SOCK_STREAM) client.connect(('127.0.0.1', bound_port)) wrap_rfile = client.makefile() _c = wrap_rfile.read(1) self.fail() except socket.error, e: self.fail('could not connect to port %d: %s' % (bound_port, str(e)))
def wait(self, timeout=None, exception=None): """ Wait until another coroutine calls :meth:`send`. Returns the value the other coroutine passed to :meth:`send`. >>> from evy import event >>> import evy >>> evt = event.Event() >>> def wait_on(): ... retval = evt.wait() ... print "waited for", retval >>> _ = evy.spawn(wait_on) >>> evt.send('result') >>> sleep(0) waited for result Returns immediately if the event has already occured. >>> evt.wait() 'result' """ current = greenlet.getcurrent() if self._result is NOT_USED: with Timeout(timeout, exception): self._waiters.add(current) try: return hubs.get_hub().switch() finally: self._waiters.discard(current) if self._exc is not None: current.throw(*self._exc) return self._result
def reset_timeout (self, new_timeout): """ Changes the timeout duration; only has effect during one test case """ self.timer.cancel() self.timer = Timeout(new_timeout, TestIsTakingTooLong(new_timeout))
def test_timeout(self): import time Timeout(0.1, TimeoutError()) self.assertRaises(TimeoutError, tpool.execute, time.sleep, 0.3)
def raise_timeout(): raise Timeout()
def setUp(self): self.timer = Timeout(self.TEST_TIMEOUT, TestIsTakingTooLong(self.TEST_TIMEOUT))
def reset_timeout(self, new_timeout): """ Changes the timeout duration; only has effect during one test case """ self.timer.cancel() self.timer = Timeout(new_timeout, TestIsTakingTooLong(new_timeout))
def waiter(q): timer = Timeout(0.2) self.assertEquals(q.join(), 'hi2') timer.cancel()
def waiter (q): timer = Timeout(0.2) self.assertEquals(q.join(), 'hi2') timer.cancel()
def setUp (self): self.timer = Timeout(self.TEST_TIMEOUT, TestIsTakingTooLong(self.TEST_TIMEOUT))