예제 #1
0
def test_data_stream_exc(loop):
    fut = helpers.create_future(loop)

    def gen():
        yield b'binary data'
        yield from fut

    req = ClientRequest(
        'POST', URL('http://python.org/'), data=gen(), loop=loop)
    assert req.chunked
    assert inspect.isgenerator(req.body)
    assert req.headers['TRANSFER-ENCODING'] == 'chunked'

    @asyncio.coroutine
    def exc():
        yield from asyncio.sleep(0.01, loop=loop)
        fut.set_exception(ValueError)

    helpers.ensure_future(exc(), loop=loop)

    protocol = mock.Mock()
    resp = req.send(mock.Mock(), protocol)
    connection = mock.Mock()
    resp._connection = connection
    yield from req._writer
    assert connection.close.called
    assert protocol.set_exception.called
    yield from req.close()
예제 #2
0
def test_data_stream_continue(loop, transport):
    def gen():
        yield b'binary data'
        return b' result'

    req = ClientRequest('POST',
                        URL('http://python.org/'),
                        data=gen(),
                        expect100=True,
                        loop=loop)
    assert req.chunked
    assert inspect.isgenerator(req.body)

    def coro():
        yield from asyncio.sleep(0.0001, loop=loop)
        req._continue.set_result(1)

    helpers.ensure_future(coro(), loop=loop)

    transport, buf = transport
    resp = req.send(transport)
    yield from req._writer
    assert buf.split(b'\r\n\r\n', 1)[1] == \
        b'b\r\nbinary data\r\n7\r\n result\r\n0\r\n\r\n'
    yield from req.close()
    resp.close()
예제 #3
0
def test_data_stream_exc_chain(loop):
    fut = helpers.create_future(loop)

    def gen():
        yield from fut

    req = ClientRequest('POST', URL('http://python.org/'),
                        data=gen(), loop=loop)

    inner_exc = ValueError()

    @asyncio.coroutine
    def exc():
        yield from asyncio.sleep(0.01, loop=loop)
        fut.set_exception(inner_exc)

    helpers.ensure_future(exc(), loop=loop)

    protocol = mock.Mock()
    resp = req.send(mock.Mock(), protocol)
    connection = mock.Mock()
    resp._connection = connection
    yield from req._writer
    assert connection.close.called
    assert protocol.set_exception.called
    outer_exc = protocol.set_exception.call_args[0][0]
    assert isinstance(outer_exc, aiohttp.ClientRequestError)
    assert inner_exc is outer_exc.__context__
    assert inner_exc is outer_exc.__cause__
    yield from req.close()
예제 #4
0
def test_data_stream_exc_chain(loop, conn):
    fut = helpers.create_future(loop)

    @aiohttp.streamer
    def gen(writer):
        yield from fut

    req = ClientRequest('POST', URL('http://python.org/'),
                        data=gen(), loop=loop)

    inner_exc = ValueError()

    @asyncio.coroutine
    def exc():
        yield from asyncio.sleep(0.01, loop=loop)
        fut.set_exception(inner_exc)

    helpers.ensure_future(exc(), loop=loop)

    req.send(conn)
    yield from req._writer
    # assert connection.close.called
    assert conn.protocol.set_exception.called
    outer_exc = conn.protocol.set_exception.call_args[0][0]
    assert isinstance(outer_exc, ValueError)
    assert inner_exc is outer_exc
    assert inner_exc is outer_exc
    yield from req.close()
예제 #5
0
def test_data_stream_continue(loop):
    def gen():
        yield b'binary data'
        return b' result'

    req = ClientRequest(
        'POST', URL('http://python.org/'), data=gen(),
        expect100=True, loop=loop)
    assert req.chunked
    assert inspect.isgenerator(req.body)

    def coro():
        yield from asyncio.sleep(0.0001, loop=loop)
        req._continue.set_result(1)

    helpers.ensure_future(coro(), loop=loop)

    transport = mock.Mock()
    resp = req.send(transport, mock.Mock())
    yield from req._writer
    assert transport.write.mock_calls[-2:] == [
        mock.call(b'12\r\nbinary data result\r\n'),
        mock.call(b'0\r\n\r\n')]
    yield from req.close()
    resp.close()
예제 #6
0
def test_data_stream_exc(loop, conn):
    fut = helpers.create_future(loop)

    @aiohttp.streamer
    def gen(writer):
        writer.write(b'binary data')
        yield from fut

    req = ClientRequest(
        'POST', URL('http://python.org/'), data=gen(), loop=loop)
    assert req.chunked
    assert req.headers['TRANSFER-ENCODING'] == 'chunked'

    @asyncio.coroutine
    def exc():
        yield from asyncio.sleep(0.01, loop=loop)
        fut.set_exception(ValueError)

    helpers.ensure_future(exc(), loop=loop)

    req.send(conn)
    yield from req._writer
    # assert conn.close.called
    assert conn.protocol.set_exception.called
    yield from req.close()
예제 #7
0
def function1224(arg1835, function2369):
    var362 = helpers.create_future(arg1835)

    @aiohttp.streamer
    def function200(arg854):
        arg854.function1694(b'binary data')
        yield from fut

    var2568 = ClientRequest('POST',
                            URL('http://python.org/'),
                            data=function200(),
                            loop=arg1835)
    assert var2568.chunked
    assert (var2568.headers['TRANSFER-ENCODING'] == 'chunked')

    @asyncio.coroutine
    def function33():
        yield from asyncio.sleep(0.01, loop=arg1835)
        var362.set_exception(ValueError)

    helpers.ensure_future(function33(), loop=arg1835)
    var2568.send(function2369)
    yield from var2568._writer
    assert function2369.protocol.set_exception.called
    yield from var2568.close()
예제 #8
0
def test_data_stream_exc(loop):
    fut = helpers.create_future(loop)

    def gen():
        yield b'binary data'
        yield from fut

    req = ClientRequest('POST',
                        URL('http://python.org/'),
                        data=gen(),
                        loop=loop)
    assert req.chunked
    assert inspect.isgenerator(req.body)
    assert req.headers['TRANSFER-ENCODING'] == 'chunked'

    @asyncio.coroutine
    def exc():
        yield from asyncio.sleep(0.01, loop=loop)
        fut.set_exception(ValueError)

    helpers.ensure_future(exc(), loop=loop)

    protocol = mock.Mock()
    resp = req.send(mock.Mock(), protocol)
    connection = mock.Mock()
    resp._connection = connection
    yield from req._writer
    assert connection.close.called
    assert protocol.set_exception.called
    yield from req.close()
예제 #9
0
def test_data_stream_exc_chain(loop):
    fut = helpers.create_future(loop)

    def gen():
        yield from fut

    req = ClientRequest('POST',
                        URL('http://python.org/'),
                        data=gen(),
                        loop=loop)

    inner_exc = ValueError()

    @asyncio.coroutine
    def exc():
        yield from asyncio.sleep(0.01, loop=loop)
        fut.set_exception(inner_exc)

    helpers.ensure_future(exc(), loop=loop)

    protocol = mock.Mock()
    resp = req.send(mock.Mock(), protocol)
    connection = mock.Mock()
    resp._connection = connection
    yield from req._writer
    assert connection.close.called
    assert protocol.set_exception.called
    outer_exc = protocol.set_exception.call_args[0][0]
    assert isinstance(outer_exc, aiohttp.ClientRequestError)
    assert inner_exc is outer_exc.__context__
    assert inner_exc is outer_exc.__cause__
    yield from req.close()
예제 #10
0
def function1233(arg14, function1845, function2369):
    @aiohttp.streamer
    def function200(arg365):
        arg365.function1694(b'binary data')
        arg365.function1694(b' result')
        yield from arg365.function2644()

    var372 = ClientRequest('POST',
                           URL('http://python.org/'),
                           data=function200(),
                           expect100=True,
                           loop=arg14)
    assert var372.chunked

    def function587():
        yield from asyncio.sleep(0.0001, loop=arg14)
        var372._continue.set_result(1)

    helpers.ensure_future(function587(), loop=arg14)
    var1945 = var372.send(function2369)
    yield from var372._writer
    assert (function1845.split(
        b'\r\n\r\n', 1)[1] == b'b\r\nbinary data\r\n7\r\n result\r\n0\r\n\r\n')
    yield from var372.close()
    var1945.close()
예제 #11
0
def function1824(arg134, function2369):
    var514 = helpers.create_future(arg134)

    @aiohttp.streamer
    def function200(arg106):
        yield from fut

    var1844 = ClientRequest('POST',
                            URL('http://python.org/'),
                            data=function200(),
                            loop=arg134)
    var3351 = ValueError()

    @asyncio.coroutine
    def function33():
        yield from asyncio.sleep(0.01, loop=arg134)
        var514.set_exception(var3351)

    helpers.ensure_future(function33(), loop=arg134)
    var1844.send(function2369)
    yield from var1844._writer
    assert function2369.protocol.set_exception.called
    var2612 = function2369.protocol.set_exception.call_args[0][0]
    assert isinstance(var2612, ValueError)
    assert (var3351 is var2612)
    assert (var3351 is var2612)
    yield from var1844.close()
예제 #12
0
def test_data_stream_continue(loop, buf, conn):
    @aiohttp.streamer
    def gen(writer):
        writer.write(b'binary data')
        writer.write(b' result')
        yield from writer.write_eof()

    req = ClientRequest('POST',
                        URL('http://python.org/'),
                        data=gen(),
                        expect100=True,
                        loop=loop)
    assert req.chunked

    def coro():
        yield from asyncio.sleep(0.0001, loop=loop)
        req._continue.set_result(1)

    helpers.ensure_future(coro(), loop=loop)

    resp = req.send(conn)
    yield from req._writer
    assert buf.split(b'\r\n\r\n', 1)[1] == \
        b'b\r\nbinary data\r\n7\r\n result\r\n0\r\n\r\n'
    yield from req.close()
    resp.close()
예제 #13
0
def test_data_stream_continue(loop):
    def gen():
        yield b'binary data'
        return b' result'

    req = ClientRequest('POST',
                        URL('http://python.org/'),
                        data=gen(),
                        expect100=True,
                        loop=loop)
    assert req.chunked
    assert inspect.isgenerator(req.body)

    def coro():
        yield from asyncio.sleep(0.0001, loop=loop)
        req._continue.set_result(1)

    helpers.ensure_future(coro(), loop=loop)

    transport = mock.Mock()
    resp = req.send(transport, mock.Mock())
    yield from req._writer
    assert transport.write.mock_calls[-2:] == [
        mock.call(b'12\r\nbinary data result\r\n'),
        mock.call(b'0\r\n\r\n')
    ]
    yield from req.close()
    resp.close()
예제 #14
0
    def data_received(self, data):
        if self._closing:
            return

        while self._messages:
            if self._waiters:
                waiter = self._waiters.popleft()
                message = self._messages.popleft()
                waiter.set_result(message)
            else:
                break

        # parse http messages
        if self._payload_parser is None and not self._upgrade:
            try:
                messages, upgraded, tail = self._request_parser.feed_data(data)
            except errors.HttpProcessingError as exc:
                # something happened during parsing
                self._closing = True
                self._request_handlers.append(
                    ensure_future(self.handle_error(400, None, None, exc,
                                                    exc.headers, exc.message),
                                  loop=self._loop))
                return
            except Exception as exc:
                self._closing = True
                self._request_handlers.append(
                    ensure_future(self.handle_error(500, None, None, exc),
                                  loop=self._loop))
                return

            for (msg, payload) in messages:
                self._request_count += 1
                self._reading_request = True

                if self._waiters:
                    waiter = self._waiters.popleft()
                    waiter.set_result((msg, payload))
                elif self._max_concurrent_handlers:
                    self._max_concurrent_handlers -= 1
                    handler = ensure_future(self.start(msg, payload),
                                            loop=self._loop)
                    self._request_handlers.append(handler)
                else:
                    self._messages.append((msg, payload))

            self._upgraded = upgraded
            if upgraded:
                self._message_tail = tail

        # no parser, just store
        elif self._payload_parser is None and self._upgrade and data:
            self._message_tail += data

        # feed payload
        elif data:
            eof, tail = self._payload_parser.feed_data(data)
            if eof:
                self._closing = True
예제 #15
0
def test_tcp_connector_dns_throttle_requests(loop, dns_response):
    with mock.patch('aiohttp.connector.DefaultResolver') as m_resolver:
        conn = aiohttp.TCPConnector(loop=loop,
                                    use_dns_cache=True,
                                    ttl_dns_cache=10)
        m_resolver().resolve.return_value = dns_response()
        helpers.ensure_future(conn._resolve_host('localhost', 8080), loop=loop)
        helpers.ensure_future(conn._resolve_host('localhost', 8080), loop=loop)
        yield from asyncio.sleep(0, loop=loop)
        m_resolver().resolve.assert_called_once_with('localhost',
                                                     8080,
                                                     family=0)
예제 #16
0
async def post_message(request):
    if request.content_type != 'application/json':
        return web.HTTPBadRequest()
    data = await request.json()
    try:
        message = data['message']
    except KeyError:
        return web.HTTPBadRequest()
    # cancel the display clock
    request.app['clock'].cancel()
    logger.debug(f'Message received from {request.user}: {message}')
    now = datetime.datetime.now(request.app['timezone'])
    helpers.ensure_future(request.app['epd'].display_message(message, request.user, now))
    return web.json_response({'message': message}, status=201)
예제 #17
0
def test_tcp_connector_dns_throttle_requests_exception_spread(loop):
    with mock.patch('aiohttp.connector.DefaultResolver') as m_resolver:
        conn = aiohttp.TCPConnector(loop=loop,
                                    use_dns_cache=True,
                                    ttl_dns_cache=10)
        e = Exception()
        m_resolver().resolve.side_effect = e
        r1 = helpers.ensure_future(conn._resolve_host('localhost', 8080),
                                   loop=loop)
        r2 = helpers.ensure_future(conn._resolve_host('localhost', 8080),
                                   loop=loop)
        yield from asyncio.sleep(0, loop=loop)
        assert r1.exception() == e
        assert r2.exception() == e
예제 #18
0
def test_error_on_connection_with_cancelled_waiter(loop):
    conn = aiohttp.BaseConnector(limit=1, loop=loop)

    req = mock.Mock()
    req.connection_key = 'key'
    proto = mock.Mock()
    i = 0

    fut1 = helpers.create_future(loop=loop)
    fut2 = helpers.create_future(loop=loop)
    exc = OSError()

    @asyncio.coroutine
    def create_connection(req):
        nonlocal i
        i += 1
        if i == 1:
            yield from fut1
            raise exc
        if i == 2:
            yield from fut2
        elif i == 3:
            return proto

    conn._create_connection = create_connection

    t1 = helpers.ensure_future(conn.connect(req), loop=loop)
    t2 = helpers.ensure_future(conn.connect(req), loop=loop)
    t3 = helpers.ensure_future(conn.connect(req), loop=loop)
    yield from asyncio.sleep(0, loop=loop)
    assert not t1.done()
    assert not t2.done()
    assert len(conn._acquired_per_host['key']) == 1

    fut1.set_result(None)
    fut2.cancel()
    with pytest.raises(OSError):
        yield from t1

    with pytest.raises(asyncio.CancelledError):
        yield from t2

    ret = yield from t3
    assert len(conn._acquired_per_host['key']) == 1

    assert ret._key == 'key'
    assert ret.protocol == proto
    assert proto in conn._acquired
예제 #19
0
    def connection_made(self, transport):
        super().connection_made(transport)

        self._request_handler = ensure_future(self.start(), loop=self._loop)

        if self._tcp_keepalive:
            tcp_keepalive(self, transport)
예제 #20
0
def test_connect_with_no_limit_and_limit_per_host(loop, key, transport):
    proto = unittest.mock.Mock()
    proto.is_connected.return_value = True

    req = ClientRequest('GET',
                        URL('http://localhost1:80'),
                        loop=loop,
                        response_class=unittest.mock.Mock())

    conn = aiohttp.BaseConnector(loop=loop, limit=0, limit_per_host=1)
    conn._conns[key] = [(transport, proto, loop.time())]
    conn._create_connection = unittest.mock.Mock()
    conn._create_connection.return_value = helpers.create_future(loop)
    conn._create_connection.return_value.set_result((transport, proto))

    acquired = False
    connection1 = yield from conn.connect(req)

    @asyncio.coroutine
    def f():
        nonlocal acquired
        connection2 = yield from conn.connect(req)
        acquired = True
        connection2.release()

    task = helpers.ensure_future(f(), loop=loop)

    yield from asyncio.sleep(0.01, loop=loop)
    assert not acquired
    connection1.release()
    yield from asyncio.sleep(0, loop=loop)
    assert acquired
    yield from task
    conn.close()
예제 #21
0
def test_connect_with_no_limits(loop, key):
    proto = mock.Mock()
    proto.is_connected.return_value = True

    req = ClientRequest('GET', URL('http://localhost1:80'), loop=loop)

    conn = aiohttp.BaseConnector(loop=loop, limit=0, limit_per_host=0)
    conn._conns[key] = [(proto, loop.time())]
    conn._create_connection = mock.Mock()
    conn._create_connection.return_value = helpers.create_future(loop)
    conn._create_connection.return_value.set_result(proto)

    acquired = False
    connection1 = yield from conn.connect(req)

    @asyncio.coroutine
    def f():
        nonlocal acquired
        connection2 = yield from conn.connect(req)
        acquired = True
        assert 1 == len(conn._acquired)
        assert 1 == len(conn._acquired_per_host[key])
        connection2.release()

    task = helpers.ensure_future(f(), loop=loop)

    yield from asyncio.sleep(0.01, loop=loop)
    assert acquired
    connection1.release()
    yield from task
    conn.close()
예제 #22
0
def test_connect_with_no_limit_and_limit_per_host(loop, key, transport):
    proto = unittest.mock.Mock()
    proto.is_connected.return_value = True

    req = ClientRequest('GET', URL('http://localhost1:80'),
                        loop=loop,
                        response_class=unittest.mock.Mock())

    conn = aiohttp.BaseConnector(loop=loop, limit=0, limit_per_host=1)
    conn._conns[key] = [(transport, proto, loop.time())]
    conn._create_connection = unittest.mock.Mock()
    conn._create_connection.return_value = helpers.create_future(loop)
    conn._create_connection.return_value.set_result((transport, proto))

    acquired = False
    connection1 = yield from conn.connect(req)

    @asyncio.coroutine
    def f():
        nonlocal acquired
        connection2 = yield from conn.connect(req)
        acquired = True
        connection2.release()

    task = helpers.ensure_future(f(), loop=loop)

    yield from asyncio.sleep(0.01, loop=loop)
    assert not acquired
    connection1.release()
    yield from asyncio.sleep(0, loop=loop)
    assert acquired
    yield from task
    conn.close()
def function2540(arg1260, function1139):
    var3583 = mock.Mock()
    var3583.is_connected.return_value = True
    var3002 = ClientRequest('GET', URL('http://localhost1:80'), loop=arg1260)
    var742 = aiohttp.BaseConnector(loop=arg1260, limit=0, limit_per_host=0)
    var742._conns[function1139] = [(var3583, arg1260.time())]
    var742._create_connection = mock.Mock()
    var742._create_connection.return_value = helpers.create_future(arg1260)
    var742._create_connection.return_value.set_result(var3583)
    var3355 = False
    var3007 = yield from var742.connect(var3002)

    @asyncio.coroutine
    def function301():
        nonlocal acquired
        var2225 = yield from var742.connect(var3002)
        var3355 = True
        assert (1 == len(var742._acquired))
        assert (1 == len(var742._acquired_per_host[function1139]))
        var2225.release()

    var2804 = helpers.ensure_future(function301(), loop=arg1260)
    yield from asyncio.sleep(0.01, loop=arg1260)
    assert acquired
    var3007.release()
    yield from task
    var742.close()
def function712(arg516, function1139):
    var4045 = mock.Mock()
    var4045.is_connected.return_value = True
    var384 = ClientRequest('GET', URL('http://localhost1:80'), loop=arg516)
    var3117 = aiohttp.BaseConnector(loop=arg516, limit=0, limit_per_host=1)
    var3117._conns[function1139] = [(var4045, arg516.time())]
    var3117._create_connection = mock.Mock()
    var3117._create_connection.return_value = helpers.create_future(arg516)
    var3117._create_connection.return_value.set_result(var4045)
    var4490 = False
    var712 = yield from var3117.connect(var384)

    @asyncio.coroutine
    def function301():
        nonlocal acquired
        var993 = yield from var3117.connect(var384)
        var4490 = True
        var993.release()

    var4482 = helpers.ensure_future(function301(), loop=arg516)
    yield from asyncio.sleep(0.01, loop=arg516)
    assert (not var4490)
    var712.release()
    yield from asyncio.sleep(0, loop=arg516)
    assert acquired
    yield from task
    var3117.close()
def function2543(arg440, function1139):
    var4520 = mock.Mock()
    var4520.is_connected.return_value = True
    var1008 = ClientRequest('GET', URL('http://localhost1:80'), loop=arg440)
    var4698 = aiohttp.BaseConnector(loop=arg440, limit=1000, limit_per_host=1)
    var4698._conns[function1139] = [(var4520, arg440.time())]
    var4698._create_connection = mock.Mock()
    var4698._create_connection.return_value = helpers.create_future(arg440)
    var4698._create_connection.return_value.set_result(var4520)
    var3298 = False
    var4590 = yield from var4698.connect(var1008)

    @asyncio.coroutine
    def function301():
        nonlocal acquired
        var916 = yield from var4698.connect(var1008)
        var3298 = True
        assert (1 == len(var4698._acquired))
        assert (1 == len(var4698._acquired_per_host[function1139]))
        var916.release()

    var3195 = helpers.ensure_future(function301(), loop=arg440)
    yield from asyncio.sleep(0.01, loop=arg440)
    assert (not var3298)
    var4590.release()
    yield from asyncio.sleep(0, loop=arg440)
    assert acquired
    yield from task
    var4698.close()
예제 #26
0
def test_connect_with_no_limits(loop, key):
    proto = mock.Mock()
    proto.is_connected.return_value = True

    req = ClientRequest('GET', URL('http://localhost1:80'), loop=loop)

    conn = aiohttp.BaseConnector(loop=loop, limit=0, limit_per_host=0)
    conn._conns[key] = [(proto, loop.time())]
    conn._create_connection = mock.Mock()
    conn._create_connection.return_value = helpers.create_future(loop)
    conn._create_connection.return_value.set_result(proto)

    acquired = False
    connection1 = yield from conn.connect(req)

    @asyncio.coroutine
    def f():
        nonlocal acquired
        connection2 = yield from conn.connect(req)
        acquired = True
        assert 1 == len(conn._acquired)
        assert 1 == len(conn._acquired_per_host[key])
        connection2.release()

    task = helpers.ensure_future(f(), loop=loop)

    yield from asyncio.sleep(0.01, loop=loop)
    assert acquired
    connection1.release()
    yield from task
    conn.close()
예제 #27
0
def test_tcp_connector_dns_throttle_requests(loop, dns_response):
    with mock.patch('aiohttp.connector.DefaultResolver') as m_resolver:
        conn = aiohttp.TCPConnector(
            loop=loop,
            use_dns_cache=True,
            ttl_dns_cache=10
        )
        m_resolver().resolve.return_value = dns_response()
        helpers.ensure_future(conn._resolve_host('localhost', 8080), loop=loop)
        helpers.ensure_future(conn._resolve_host('localhost', 8080), loop=loop)
        yield from asyncio.sleep(0, loop=loop)
        m_resolver().resolve.assert_called_once_with(
            'localhost',
            8080,
            family=0
        )
예제 #28
0
    def connection_made(self, transport):
        super().connection_made(transport)

        self._request_handler = ensure_future(self.start(), loop=self._loop)

        if self._tcp_keepalive:
            tcp_keepalive(self, transport)
예제 #29
0
def function808(arg1474, function1845, function2369):
    var2354 = ClientRequest('POST',
                            URL('http://python.org/'),
                            data=b'data',
                            expect100=True,
                            loop=arg1474)

    def function587():
        yield from asyncio.sleep(0.0001, loop=arg1474)
        var2354._continue.set_result(1)

    helpers.ensure_future(function587(), loop=arg1474)
    var3880 = var2354.send(function2369)
    yield from var2354._writer
    assert (function1845.split(b'\r\n\r\n', 1)[1] == b'data')
    yield from var2354.close()
    var3880.close()
예제 #30
0
def test_data_continue(loop):
    req = ClientRequest("POST", URL("http://python.org/"), data=b"data", expect100=True, loop=loop)

    def coro():
        yield from asyncio.sleep(0.0001, loop=loop)
        req._continue.set_result(1)

    helpers.ensure_future(coro(), loop=loop)

    transport = mock.Mock()
    resp = req.send(transport, mock.Mock())
    assert 1 == len(transport.write.mock_calls)

    yield from req._writer
    assert transport.write.mock_calls[-1] == mock.call(b"data")
    yield from req.close()
    resp.close()
예제 #31
0
def test_tcp_connector_dns_throttle_requests_cancelled_when_close(
        loop, dns_response):

    with mock.patch('aiohttp.connector.DefaultResolver') as m_resolver:
        conn = aiohttp.TCPConnector(loop=loop,
                                    use_dns_cache=True,
                                    ttl_dns_cache=10)
        m_resolver().resolve.return_value = dns_response()
        helpers.ensure_future(conn._resolve_host('localhost', 8080), loop=loop)
        f = helpers.ensure_future(conn._resolve_host('localhost', 8080),
                                  loop=loop)

        yield from asyncio.sleep(0, loop=loop)
        conn.close()

        with pytest.raises(asyncio.futures.CancelledError):
            yield from f
예제 #32
0
def test_data_continue(loop, buf, conn):
    req = ClientRequest(
        'POST', URL('http://python.org/'), data=b'data',
        expect100=True, loop=loop)

    def coro():
        yield from asyncio.sleep(0.0001, loop=loop)
        req._continue.set_result(1)

    helpers.ensure_future(coro(), loop=loop)

    resp = req.send(conn)

    yield from req._writer
    assert buf.split(b'\r\n\r\n', 1)[1] == b'data'
    yield from req.close()
    resp.close()
예제 #33
0
파일: worker.py 프로젝트: argaen/aiohttp
    def run(self):
        self._runner = ensure_future(self._run(), loop=self.loop)

        try:
            self.loop.run_until_complete(self._runner)
        finally:
            self.loop.close()

        sys.exit(self.exit_code)
예제 #34
0
    def test_cancel_waiters(self, loop):
        ev = EventResultOrError(loop=loop)

        @asyncio.coroutine
        def c():
            yield from ev.wait()

        t1 = helpers.ensure_future(c(), loop=loop)
        t2 = helpers.ensure_future(c(), loop=loop)
        yield from asyncio.sleep(0, loop=loop)
        ev.cancel()
        ev.set()

        with pytest.raises(asyncio.futures.CancelledError):
            yield from t1

        with pytest.raises(asyncio.futures.CancelledError):
            yield from t2
예제 #35
0
    def run(self):
        self._runner = ensure_future(self._run(), loop=self.loop)

        try:
            self.loop.run_until_complete(self._runner)
        finally:
            self.loop.close()

        sys.exit(self.exit_code)
예제 #36
0
    def test_cancel_waiters(self, loop):
        ev = EventResultOrError(loop=loop)

        @asyncio.coroutine
        def c():
            yield from ev.wait()

        t1 = helpers.ensure_future(c(), loop=loop)
        t2 = helpers.ensure_future(c(), loop=loop)
        yield from asyncio.sleep(0, loop=loop)
        ev.cancel()
        ev.set()

        with pytest.raises(asyncio.futures.CancelledError):
            yield from t1

        with pytest.raises(asyncio.futures.CancelledError):
            yield from t2
예제 #37
0
파일: worker.py 프로젝트: sk2/aiohttp
    def handle_quit(self, sig, frame):
        self.alive = False

        # worker_int callback
        self.cfg.worker_int(self)

        # init closing process
        self._closing = ensure_future(self.close(), loop=self.loop)

        # close loop
        self.loop.call_later(0.1, self._notify_waiter_done)
예제 #38
0
    def test_feed_data_cancels_timeout(self):
        stream = self._make_one(timeout=1)
        task = helpers.ensure_future(stream.readany(), loop=self.loop)
        self.loop.run_until_complete(asyncio.sleep(0, loop=self.loop))

        self.assertIsNotNone(stream._canceller)
        canceller = stream._canceller = mock.Mock()
        stream.feed_data(b'data')
        self.assertIsNone(stream._canceller)
        canceller.cancel.assert_called_with()
        self.assertEqual(b'data', self.loop.run_until_complete(task))
예제 #39
0
파일: worker.py 프로젝트: sk2/aiohttp
    def run(self):
        if hasattr(self.wsgi, 'startup'):
            self.loop.run_until_complete(self.wsgi.startup())
        self._runner = ensure_future(self._run(), loop=self.loop)

        try:
            self.loop.run_until_complete(self._runner)
        finally:
            self.loop.close()

        sys.exit(self.exit_code)
예제 #40
0
파일: worker.py 프로젝트: cr0hn/aiohttp
    def run(self):
        if hasattr(self.wsgi, 'startup'):
            self.loop.run_until_complete(self.wsgi.startup())
        self._runner = ensure_future(self._run(), loop=self.loop)

        try:
            self.loop.run_until_complete(self._runner)
        finally:
            self.loop.close()

        sys.exit(self.exit_code)
예제 #41
0
def test_tcp_connector_dns_throttle_requests_exception_spread(loop):
    with mock.patch('aiohttp.connector.DefaultResolver') as m_resolver:
        conn = aiohttp.TCPConnector(
            loop=loop,
            use_dns_cache=True,
            ttl_dns_cache=10
        )
        e = Exception()
        m_resolver().resolve.side_effect = e
        r1 = helpers.ensure_future(
            conn._resolve_host('localhost', 8080),
            loop=loop
        )
        r2 = helpers.ensure_future(
            conn._resolve_host('localhost', 8080),
            loop=loop
        )
        yield from asyncio.sleep(0, loop=loop)
        assert r1.exception() == e
        assert r2.exception() == e
예제 #42
0
파일: worker.py 프로젝트: cr0hn/aiohttp
    def handle_quit(self, sig, frame):
        self.alive = False

        # worker_int callback
        self.cfg.worker_int(self)

        # init closing process
        self._closing = ensure_future(self.close(), loop=self.loop)

        # close loop
        self.loop.call_later(0.1, self._notify_waiter_done)
예제 #43
0
    def test_feed_data_cancels_timeout(self):
        stream = self._make_one(timeout=1)
        task = helpers.ensure_future(stream.readany(), loop=self.loop)
        self.loop.run_until_complete(asyncio.sleep(0, loop=self.loop))

        self.assertIsNotNone(stream._canceller)
        canceller = stream._canceller = mock.Mock()
        stream.feed_data(b'data')
        self.assertIsNone(stream._canceller)
        canceller.cancel.assert_called_with()
        self.assertEqual(b'data', self.loop.run_until_complete(task))
예제 #44
0
    def test_set_exception_cancels_timeout(self):
        stream = self._make_one(timeout=1)
        task = helpers.ensure_future(stream.readany(), loop=self.loop)
        self.loop.run_until_complete(asyncio.sleep(0, loop=self.loop))

        self.assertIsNotNone(stream._canceller)
        canceller = stream._canceller = mock.Mock()
        stream.set_exception(ValueError())
        self.assertIsNone(stream._canceller)
        canceller.cancel.assert_called_with()
        self.assertRaises(ValueError, self.loop.run_until_complete, task)
예제 #45
0
    def test_wait_cancels_timeout(self):
        # Read bytes.
        stream = self._make_one(timeout=1)
        task = helpers.ensure_future(stream._wait('test'), loop=self.loop)
        self.loop.run_until_complete(asyncio.sleep(0, loop=self.loop))

        self.assertIsNotNone(stream._canceller)
        canceller = stream._canceller = mock.Mock()
        stream._waiter.set_result(None)
        self.loop.run_until_complete(task)
        self.assertIsNone(stream._canceller)
        canceller.cancel.assert_called_with()
예제 #46
0
def test_tcp_connector_dns_throttle_requests_cancelled_when_close(
        loop,
        dns_response):

    with mock.patch('aiohttp.connector.DefaultResolver') as m_resolver:
        conn = aiohttp.TCPConnector(
            loop=loop,
            use_dns_cache=True,
            ttl_dns_cache=10
        )
        m_resolver().resolve.return_value = dns_response()
        helpers.ensure_future(
            conn._resolve_host('localhost', 8080), loop=loop)
        f = helpers.ensure_future(
            conn._resolve_host('localhost', 8080), loop=loop)

        yield from asyncio.sleep(0, loop=loop)
        conn.close()

        with pytest.raises(asyncio.futures.CancelledError):
            yield from f
예제 #47
0
    def test_set_exception_cancels_timeout(self):
        stream = self._make_one(timeout=1)
        task = helpers.ensure_future(stream.readany(), loop=self.loop)
        self.loop.run_until_complete(asyncio.sleep(0, loop=self.loop))

        self.assertIsNotNone(stream._canceller)
        canceller = stream._canceller = mock.Mock()
        stream.set_exception(ValueError())
        self.assertIsNone(stream._canceller)
        canceller.cancel.assert_called_with()
        self.assertRaises(
            ValueError, self.loop.run_until_complete, task)
예제 #48
0
    def test_set(self, loop):
        ev = EventResultOrError(loop=loop)

        @asyncio.coroutine
        def c():
            yield from ev.wait()
            return 1

        t = helpers.ensure_future(c(), loop=loop)
        yield from asyncio.sleep(0, loop=loop)
        ev.set()
        assert (yield from t) == 1
예제 #49
0
파일: server.py 프로젝트: graingert/aiohttp
    def connection_made(self, transport):
        super().connection_made(transport)

        self._request_handler = ensure_future(self.start(), loop=self._loop)

        # start slow request timer
        if self._timeout:
            self._timeout_handle = self._loop.call_later(
                self._timeout, self.cancel_slow_request)

        if self._keep_alive_on:
            tcp_keepalive(self, transport)
예제 #50
0
    def connection_made(self, transport):
        super().connection_made(transport)

        self._request_handler = ensure_future(self.start(), loop=self._loop)

        # start slow request timer
        if self._slow_request_timeout:
            now = self._loop.time()
            self._slow_request_timeout_handle = self._loop.call_at(
                ceil(now+self._slow_request_timeout), self.cancel_slow_request)

        if self._tcp_keepalive:
            tcp_keepalive(self, transport)
예제 #51
0
 def f(start=True):
     nonlocal num_requests
     if num_requests == max_requests:
         return
     num_requests += 1
     if not start:
         connection = yield from conn.connect(req)
         yield from asyncio.sleep(0, loop=loop)
         connection.release()
     tasks = [
         helpers.ensure_future(f(start=False), loop=loop)
         for i in range(start_requests)
     ]
     yield from asyncio.wait(tasks, loop=loop)
예제 #52
0
def test_cancel_outer_coro(loop):
    fut = asyncio.Future(loop=loop)

    @asyncio.coroutine
    def outer():
        fut.set_result(None)
        yield from asyncio.sleep(1, loop=loop)

    task = ensure_future(outer(), loop=loop)
    yield from fut
    task.cancel()
    with pytest.raises(asyncio.CancelledError):
        yield from task
    assert task.cancelled()
    assert task.done()
예제 #53
0
def test_data_stream_continue(loop, buf, conn):
    @aiohttp.streamer
    def gen(writer):
        writer.write(b'binary data')
        writer.write(b' result')
        yield from writer.write_eof()

    req = ClientRequest(
        'POST', URL('http://python.org/'), data=gen(),
        expect100=True, loop=loop)
    assert req.chunked

    def coro():
        yield from asyncio.sleep(0.0001, loop=loop)
        req._continue.set_result(1)

    helpers.ensure_future(coro(), loop=loop)

    resp = req.send(conn)
    yield from req._writer
    assert buf.split(b'\r\n\r\n', 1)[1] == \
        b'b\r\nbinary data\r\n7\r\n result\r\n0\r\n\r\n'
    yield from req.close()
    resp.close()
예제 #54
0
def test_data_stream_continue(loop, transport):
    def gen():
        yield b'binary data'
        return b' result'

    req = ClientRequest(
        'POST', URL('http://python.org/'), data=gen(),
        expect100=True, loop=loop)
    assert req.chunked
    assert inspect.isgenerator(req.body)

    def coro():
        yield from asyncio.sleep(0.0001, loop=loop)
        req._continue.set_result(1)

    helpers.ensure_future(coro(), loop=loop)

    transport, buf = transport
    resp = req.send(transport)
    yield from req._writer
    assert buf.split(b'\r\n\r\n', 1)[1] == \
        b'b\r\nbinary data\r\n7\r\n result\r\n0\r\n\r\n'
    yield from req.close()
    resp.close()
예제 #55
0
    def test_set_exception(self, loop):
        ev = EventResultOrError(loop=loop)

        @asyncio.coroutine
        def c():
            try:
                yield from ev.wait()
            except Exception as e:
                return e
            return 1

        t = helpers.ensure_future(c(), loop=loop)
        yield from asyncio.sleep(0, loop=loop)
        e = Exception()
        ev.set(exc=e)
        assert (yield from t) == e
예제 #56
0
def test_outer_coro_is_not_cancelled(loop):

    has_timeout = False

    @asyncio.coroutine
    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

    task = ensure_future(outer(), loop=loop)
    yield from task
    assert has_timeout
    assert not task.cancelled()
    assert task.done()
예제 #57
0
def test_connect_with_limit(loop):

    tr, proto = unittest.mock.Mock(), unittest.mock.Mock()
    proto.is_connected.return_value = True

    req = ClientRequest('GET', URL('http://host:80'),
                        loop=loop,
                        response_class=unittest.mock.Mock())

    conn = aiohttp.BaseConnector(loop=loop, limit=1)
    key = ('host', 80, False)
    conn._conns[key] = [(tr, proto, loop.time())]
    conn._create_connection = unittest.mock.Mock()
    conn._create_connection.return_value = helpers.create_future(loop)
    conn._create_connection.return_value.set_result((tr, proto))

    connection1 = yield from conn.connect(req)
    assert connection1._transport == tr

    assert 1 == len(conn._acquired[key])

    acquired = False

    @asyncio.coroutine
    def f():
        nonlocal acquired
        connection2 = yield from conn.connect(req)
        acquired = True
        assert 1 == len(conn._acquired[key])
        connection2.release()

    task = helpers.ensure_future(f(), loop=loop)

    yield from asyncio.sleep(0.01, loop=loop)
    assert not acquired
    connection1.release()
    yield from asyncio.sleep(0, loop=loop)
    assert acquired
    yield from task
    conn.close()