コード例 #1
0
 def outer():
     nonlocal has_timeout
     try:
         with Timeout(0.001, loop=loop):
             yield from asyncio.sleep(1, loop=loop)
     except asyncio.TimeoutError:
         has_timeout = True
コード例 #2
0
ファイル: test_timeout.py プロジェクト: greghaynes/aiohttp
def test_timeout_suppress_exception_chain(loop):

    with pytest.raises(asyncio.TimeoutError) as ctx:
        with Timeout(0.01, loop=loop) as t:
            yield from asyncio.sleep(10, loop=loop)
            assert t._loop is loop
    assert ctx.value.__suppress_context__
コード例 #3
0
def test_timeout_blocking_loop(loop):
    @asyncio.coroutine
    def long_running_task():
        time.sleep(0.1)
        return 'done'

    with Timeout(0.01, loop=loop):
        result = yield from long_running_task()
    assert result == 'done'
コード例 #4
0
def test_timeout_finish_in_time(loop):
    @asyncio.coroutine
    def long_running_task():
        yield from asyncio.sleep(0.01, loop=loop)
        return 'done'

    with Timeout(0.1, loop=loop):
        resp = yield from long_running_task()
    assert resp == 'done'
コード例 #5
0
def test_timeout_disable(loop):
    @asyncio.coroutine
    def long_running_task():
        yield from asyncio.sleep(0.1, loop=loop)
        return 'done'

    t0 = loop.time()
    with Timeout(None, loop=loop):
        resp = yield from long_running_task()
    assert resp == 'done'
    dt = loop.time() - t0
    assert 0.09 < dt < 0.11, dt
コード例 #6
0
def test_timeout_time(loop):
    foo_running = None

    start = loop.time()
    with pytest.raises(asyncio.TimeoutError):
        with Timeout(0.1, loop=loop):
            foo_running = True
            try:
                yield from asyncio.sleep(0.2, loop=loop)
            finally:
                foo_running = False

    dt = loop.time() - start
    assert 0.09 < dt < 0.11
    assert not foo_running
コード例 #7
0
def test_timeout(loop):
    canceled_raised = False

    @asyncio.coroutine
    def long_running_task():
        try:
            yield from asyncio.sleep(10, loop=loop)
        except asyncio.CancelledError:
            nonlocal canceled_raised
            canceled_raised = True
            raise

    with pytest.raises(asyncio.TimeoutError):
        with Timeout(0.01, loop=loop) as t:
            yield from long_running_task()
            assert t._loop is loop
    assert canceled_raised, 'CancelledError was not raised'
コード例 #8
0
def test_timeout_time(loop):
    foo_running = None

    start = loop.time()
    with pytest.raises(asyncio.TimeoutError):
        with Timeout(0.1, loop=loop):
            foo_running = True
            try:
                yield from asyncio.sleep(0.2, loop=loop)
            finally:
                foo_running = False

    dt = loop.time() - start
    if not (0.09 < dt < 0.11) and os.environ.get('APPVEYOR'):
        pytest.xfail('appveyor sometimes is toooo sloooow')
    assert 0.09 < dt < 0.11
    assert not foo_running
コード例 #9
0
def test_for_race_conditions(loop):
    fut = asyncio.Future(loop=loop)
    loop.call_later(0.1, fut.set_result('done'))
    with Timeout(0.2, loop=loop):
        resp = yield from fut
    assert resp == 'done'
コード例 #10
0
def test_timeout_canceled_error_is_converted_to_timeout(loop):
    yield from asyncio.sleep(0, loop=loop)
    with pytest.raises(asyncio.CancelledError):
        with Timeout(0.001, loop=loop):
            raise asyncio.CancelledError
コード例 #11
0
def test_timeout_not_relevant_exception(loop):
    yield from asyncio.sleep(0, loop=loop)
    with pytest.raises(KeyError):
        with Timeout(0.1, loop=loop):
            raise KeyError
コード例 #12
0
 def run():
     with Timeout(0.1) as t:
         yield from asyncio.sleep(0.01)
         assert t._loop is loop
コード例 #13
0
def test_raise_runtimeerror_if_no_task(loop):
    with pytest.raises(RuntimeError):
        with Timeout(0.1, loop=loop):
            pass
コード例 #14
0
ファイル: server.py プロジェクト: marciopocebon/aiohttp
    def start(self):
        """Start processing of incoming requests.

        It reads request line, request headers and request payload, then
        calls handle_request() method. Subclass has to override
        handle_request(). start() handles various exceptions in request
        or response handling. Connection is being closed always unless
        keep_alive(True) specified.
        """
        reader = self.reader
        self.writer.set_tcp_nodelay(True)

        try:
            while not self._closing:
                message = None
                self._keepalive = False
                self._request_count += 1
                self._reading_request = False

                payload = None

                # slow request timer
                with Timeout(max(self._slow_request_timeout,
                                 self._keepalive_timeout),
                             loop=self._loop):
                    # read request headers
                    httpstream = reader.set_parser(self._request_parser)
                    message = yield from httpstream.read()

                # request may not have payload
                try:
                    content_length = int(
                        message.headers.get(hdrs.CONTENT_LENGTH, 0))
                except ValueError:
                    raise errors.InvalidHeader(hdrs.CONTENT_LENGTH) from None

                if (content_length > 0 or message.method == hdrs.METH_CONNECT
                        or hdrs.SEC_WEBSOCKET_KEY1 in message.headers
                        or 'chunked' in message.headers.get(
                            hdrs.TRANSFER_ENCODING, '')):
                    payload = streams.FlowControlStreamReader(reader,
                                                              loop=self._loop)
                    reader.set_parser(aiohttp.HttpPayloadParser(message),
                                      payload)
                else:
                    payload = EMPTY_PAYLOAD

                yield from self.handle_request(message, payload)

                if payload and not payload.is_eof():
                    self.log_debug('Uncompleted request.')
                    self._closing = True
                else:
                    reader.unset_parser()
                    if not self._keepalive or not self._keepalive_timeout:
                        self._closing = True

        except asyncio.CancelledError:
            self.log_debug('Request handler cancelled.')
            return
        except asyncio.TimeoutError:
            self.log_debug('Request handler timed out.')
            return
        except errors.ClientDisconnectedError:
            self.log_debug('Ignored premature client disconnection #1.')
            return
        except errors.HttpProcessingError as exc:
            yield from self.handle_error(exc.code, message, None, exc,
                                         exc.headers, exc.message)
        except Exception as exc:
            yield from self.handle_error(500, message, None, exc)
        finally:
            self._request_handler = None
            if self.transport is None:
                self.log_debug('Ignored premature client disconnection #2.')
            else:
                self.transport.close()