def test_still_coroutine_function(self, fo):
        """
        It's ensured that a decorated function still passes as a coroutine
        function.  Otherwise PYTHONASYNCIODEBUG=1 breaks.

        iscoroutine[function] sadly only works with async def.
        """
        func = aio.time(fo)(coro)

        assert inspect.isgenerator(func())
        assert inspect.isgeneratorfunction(func)
Example #2
0
    def test_still_coroutine_function(self, fo):
        """
        It's ensured that a decorated function still passes as a coroutine
        function.  Otherwise PYTHONASYNCIODEBUG=1 breaks.

        iscoroutine[function] sadly only works with async def.
        """
        func = aio.time(fo)(coro)

        assert inspect.isgenerator(func())
        assert inspect.isgeneratorfunction(func)
Example #3
0
    async def test_still_coroutine_function(self, fo):
        """
        It's ensured that a decorated function still passes as a coroutine
        function.  Otherwise PYTHONASYNCIODEBUG=1 breaks.
        """
        func = aio.time(fo)(coro)
        new_coro = func()

        assert inspect.iscoroutine(new_coro)
        assert inspect.iscoroutinefunction(func)

        await new_coro
    def test_future(self, fo, patch_timer, event_loop):
        """
        time works with a asyncio.Future.
        """
        fut = asyncio.Future(loop=event_loop)
        coro = aio.time(fo, fut)

        assert [] == fo._observed

        fut.set_result(42)

        assert 42 == (yield from coro)
        assert [1] == fo._observed
Example #5
0
    def test_future(self, fo, patch_timer, event_loop):
        """
        time works with a asyncio.Future.
        """
        fut = asyncio.Future(loop=event_loop)
        coro = aio.time(fo, fut)

        assert [] == fo._observed

        fut.set_result(42)

        assert 42 == (yield from coro)
        assert [1] == fo._observed
Example #6
0
    async def test_future(self, fo, patch_timer):
        """
        time works with a asyncio.Future.
        """
        fut = asyncio.Future()
        coro = aio.time(fo, fut)

        assert [] == fo._observed

        fut.set_result(42)

        assert 42 == await coro
        assert [1] == fo._observed
    def test_future_exc(self, fo, patch_timer, event_loop):
        """
        Does not swallow exceptions.
        """
        fut = asyncio.Future(loop=event_loop)
        coro = aio.time(fo, fut)
        v = ValueError("foo")

        assert [] == fo._observed

        fut.set_exception(v)

        with pytest.raises(ValueError) as e:
            yield from coro

        assert [1] == fo._observed
        assert v is e.value
Example #8
0
    def test_future_exc(self, fo, patch_timer, event_loop):
        """
        Does not swallow exceptions.
        """
        fut = asyncio.Future(loop=event_loop)
        coro = aio.time(fo, fut)
        v = ValueError("foo")

        assert [] == fo._observed

        fut.set_exception(v)

        with pytest.raises(ValueError) as e:
            yield from coro

        assert [1] == fo._observed
        assert v is e.value
Example #9
0
    async def run(self):
        async for req in self.requests:
            req = JSONRPC20Request.from_data(req)
            if req._id != None:
                assert req._id not in self.ids, "Replayed id: %s" % req._id
                self.ids.add(req._id)
            if req.method not in self.methods:
                logging.error("Unknown method: %s" % req.method)
                await write_error(self.transport, req._id,
                                    JSONRPCMethodNotFound())
                return
            f = self.methods[req.method]
            try:
                if isinstance(req.params, list):
                    t = asyncio.ensure_future(f(*req.params,
                                                __context=self.context))
                else: # It's a dict
                    req.params['__context'] = self.context
                    t = asyncio.ensure_future(f(**req.params))

                asyncio.ensure_future(self._response(req._id,
                                                     time(REQ_TIME, future=t)))
                self.tasks[req._id] = t
                #def clean_task(f):
                    #del self.tasks[req._id]
                #t.add_done_callback(clean_task)
                for cb in self.callbacks:
                    t.add_done_callback(cb)
            except Exception as e:
                if self.raven_client is None:
                    raven_id = None
                else:
                    raven_id = self.raven_client.captureException()
                await write_error(self.transport, req._id, JSONRPCServerError(
                    message=str(e),
                    data=dict(raven_id=raven_id)))
                return