Esempio n. 1
0
def test_del_with_scheduled_cleanup(loop):
    loop.set_debug(True)
    conn = aiohttp.BaseConnector(loop=loop, keepalive_timeout=0.01)
    transp = mock.Mock()
    conn._conns['a'] = [(transp, 'proto', 123)]

    conns_impl = conn._conns
    exc_handler = mock.Mock()
    loop.set_exception_handler(exc_handler)

    with pytest.warns(ResourceWarning):
        # obviously doesn't deletion because loop has a strong
        # reference to connector's instance method, isn't it?
        del conn
        yield from asyncio.sleep(0.01, loop=loop)
        gc.collect()

    assert not conns_impl
    transp.close.assert_called_with()
    msg = {'connector': mock.ANY,  # conn was deleted
           'message': 'Unclosed connector'}
    if loop.get_debug():
        msg['source_traceback'] = mock.ANY
    exc_handler.assert_called_with(loop, msg)
Esempio n. 2
0
def test_connect_with_limit_cancelled(loop):

    proto = mock.Mock()
    proto.is_connected.return_value = True

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

    conn = aiohttp.BaseConnector(loop=loop, limit=1)
    key = ('host', 80, False)
    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)

    connection = yield from conn.connect(req)
    assert connection._protocol == proto
    assert connection.transport == proto.transport

    assert 1 == len(conn._acquired)

    with pytest.raises(asyncio.TimeoutError):
        # limit exhausted
        yield from asyncio.wait_for(conn.connect(req), 0.01, loop=loop)
    connection.close()
Esempio n. 3
0
async def test_close_with_acquired_connection(loop):
    proto = mock.Mock()
    proto.is_connected.return_value = True

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

    conn = aiohttp.BaseConnector(loop=loop, limit=1)
    key = ('host', 80, False)
    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)

    connection = await conn.connect(req)

    assert 1 == len(conn._acquired)
    conn.close()
    assert 0 == len(conn._acquired)
    assert conn.closed
    proto.close.assert_called_with()

    assert not connection.closed
    connection.close()
    assert connection.closed
Esempio n. 4
0
    def test_del_with_scheduled_cleanup(self):
        conn = aiohttp.BaseConnector(loop=self.loop, keepalive_timeout=0.01)
        transp = unittest.mock.Mock()
        conn._conns['a'] = [(transp, 'proto', 123)]

        conns_impl = conn._conns
        conn._start_cleanup_task()
        exc_handler = unittest.mock.Mock()
        self.loop.set_exception_handler(exc_handler)

        with self.assertWarns(ResourceWarning):
            del conn
            yield from asyncio.sleep(0.01)
            gc.collect()

        self.assertFalse(conns_impl)
        transp.close.assert_called_with()
        msg = {
            'connector': unittest.mock.ANY,  # conn was deleted
            'message': 'Unclosed connector'
        }
        if self.loop.get_debug():
            msg['source_traceback'] = unittest.mock.ANY
        exc_handler.assert_called_with(self.loop, msg)
Esempio n. 5
0
def test_connect_with_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=1000, 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
        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 not acquired
    connection1.release()
    yield from asyncio.sleep(0, loop=loop)
    assert acquired
    yield from task
    conn.close()
Esempio n. 6
0
def test_capacity_property_default(loop):
    conn = aiohttp.BaseConnector(loop=loop)
    assert conn.capacity == 20
    conn.close()
Esempio n. 7
0
def test_default_force_close(loop):
    connector = aiohttp.BaseConnector(loop=loop)
    assert not connector.force_close
Esempio n. 8
0
def test_ctor_with_default_loop():
    loop = asyncio.new_event_loop()
    asyncio.set_event_loop(loop)
    conn = aiohttp.BaseConnector()
    assert loop is conn._loop
    loop.close()
Esempio n. 9
0
def test_ctor_cleanup():
    loop = unittest.mock.Mock()
    loop.time.return_value = 1.5
    conn = aiohttp.BaseConnector(loop=loop, keepalive_timeout=10)
    assert conn._cleanup_handle is not None
Esempio n. 10
0
def test_create_conn(loop):
    conn = aiohttp.BaseConnector(loop=loop)
    with pytest.raises(NotImplementedError):
        yield from conn._create_connection(object())
Esempio n. 11
0
 def test_limit_property(self):
     conn = aiohttp.BaseConnector(loop=self.loop, limit=15)
     self.assertEqual(15, conn.limit)
     conn.close()
Esempio n. 12
0
def test_limit_per_host_property(loop):
    conn = aiohttp.BaseConnector(loop=loop, limit_per_host=15)
    assert 15 == conn.limit_per_host

    conn.close()
Esempio n. 13
0
def test_limitless(loop):
    with aiohttp.BaseConnector(loop=loop, limit=None) as conn:
        assert conn.limit is None
def function1550(arg646):
    var2102 = aiohttp.BaseConnector(loop=arg646)
    assert (var2102.limit_per_host == 0)
    var2102.close()
def function566(arg1098):
    var2418 = aiohttp.BaseConnector(loop=arg1098)
    assert (var2418.limit == 100)
    var2418.close()
Esempio n. 16
0
 def test_ctor_loop(self, asyncio):
     session = aiohttp.BaseConnector()
     self.assertIs(session._loop, asyncio.get_event_loop.return_value)
Esempio n. 17
0
 def test_default_force_close(self):
     connector = aiohttp.BaseConnector(loop=self.loop)
     self.assertFalse(connector.force_close)
Esempio n. 18
0
def test_limit_per_host_property_default(loop):
    conn = aiohttp.BaseConnector(loop=loop)
    assert conn.limit_per_host == 0
    conn.close()
Esempio n. 19
0
 def test_limit_property_default(self):
     conn = aiohttp.BaseConnector(loop=self.loop)
     self.assertIsNone(conn.limit)
     conn.close()
Esempio n. 20
0
def test_ctor_loop():
    with mock.patch('aiohttp.connector.asyncio') as m_asyncio:
        session = aiohttp.BaseConnector()

    assert session._loop is m_asyncio.get_event_loop.return_value
Esempio n. 21
0
def test_ctor_loop():
    with unittest.mock.patch('aiohttp.connector.asyncio') as m_asyncio:
        session = aiohttp.BaseConnector(time_service=unittest.mock.Mock())

    assert session._loop is m_asyncio.get_event_loop.return_value
Esempio n. 22
0
def test_close_cancels_cleanup_handle(loop):
    conn = aiohttp.BaseConnector(loop=loop)
    conn._release(1, mock.Mock(should_close=False))
    assert conn._cleanup_handle is not None
    conn.close()
    assert conn._cleanup_handle is None
Esempio n. 23
0
def test_close_cancels_cleanup_closed_handle(loop):
    conn = aiohttp.BaseConnector(loop=loop)
    assert conn._cleanup_closed_handle is not None
    conn.close()
    assert conn._cleanup_closed_handle is None
Esempio n. 24
0
    def test_del(self):
        conn = aiohttp.BaseConnector(loop=self.loop)
        close = conn.close = unittest.mock.Mock()

        del conn
        self.assertTrue(close.called)
Esempio n. 25
0
def test_connect_with_capacity_concurrent(loop):
    proto = unittest.mock.Mock()
    proto.is_connected.return_value = True

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

    max_connections = 2
    num_connections = 0

    conn = aiohttp.BaseConnector(capacity=max_connections, loop=loop)

    # Use a real coroutine for _create_connection; a mock would mask
    # problems that only happen when the method yields.

    @asyncio.coroutine
    def create_connection(req):
        nonlocal num_connections
        num_connections += 1
        yield from asyncio.sleep(0, loop=loop)

        # Make a new transport mock each time because acquired
        # transports are stored in a set. Reusing the same object
        # messes with the count.
        tr = unittest.mock.Mock()

        return tr, proto

    conn._create_connection = create_connection

    # Simulate something like a crawler. It opens a connection, does
    # something with it, closes it, then creates tasks that make more
    # connections and waits for them to finish. The crawler is started
    # with multiple concurrent requests and stops when it hits a
    # predefined maximum number of requests.

    max_requests = 10
    num_requests = 0
    start_requests = max_connections + 1

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

    yield from f()
    conn.close()

    assert max_connections == num_connections
Esempio n. 26
0
 def test_create_conn(self):
     conn = aiohttp.BaseConnector(loop=self.loop)
     self.assertRaises(NotImplementedError, conn._create_connection,
                       object())
Esempio n. 27
0
def test_limit_property(loop):
    with pytest.warns(DeprecationWarning):
        conn = aiohttp.BaseConnector(loop=loop, limit=15)
        assert 15 == conn.capacity
        conn.close()
Esempio n. 28
0
async def test_connect_with_connector(monkeypatch):
    connector = aiohttp.BaseConnector()
    docker = Docker(connector=connector)
    assert docker.connector == connector
    await docker.close()
Esempio n. 29
0
def test_limitless(loop):
    with aiohttp.BaseConnector(loop=loop, capacity=None) as conn:
        assert conn.capacity == 0
Esempio n. 30
0
 def go():
     conn = aiohttp.BaseConnector(loop=self.loop)
     with self.assertRaises(NotImplementedError):
         yield from conn._create_connection(object())