예제 #1
0
    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)
예제 #2
0
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()
예제 #3
0
    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)
예제 #4
0
파일: __init__.py 프로젝트: inercia/evy
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()
예제 #5
0
 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()
예제 #6
0
 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()
예제 #7
0
    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
예제 #8
0
파일: queue.py 프로젝트: inercia/evy
    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
예제 #9
0
    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()
예제 #10
0
파일: test_pools.py 프로젝트: inercia/evy
    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()
예제 #11
0
    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
예제 #12
0
파일: queue.py 프로젝트: inercia/evy
    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
예제 #13
0
    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
예제 #14
0
    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
예제 #15
0
 def waiter(q):
     timer = Timeout(0.2)
     self.assertEquals(q.join(), 'hi2')
     timer.cancel()
예제 #16
0
파일: test_queue.py 프로젝트: inercia/evy
 def waiter (q):
     timer = Timeout(0.2)
     self.assertEquals(q.join(), 'hi2')
     timer.cancel()