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)
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()
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
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)
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()
def test_capacity_property_default(loop): conn = aiohttp.BaseConnector(loop=loop) assert conn.capacity == 20 conn.close()
def test_default_force_close(loop): connector = aiohttp.BaseConnector(loop=loop) assert not connector.force_close
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()
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
def test_create_conn(loop): conn = aiohttp.BaseConnector(loop=loop) with pytest.raises(NotImplementedError): yield from conn._create_connection(object())
def test_limit_property(self): conn = aiohttp.BaseConnector(loop=self.loop, limit=15) self.assertEqual(15, conn.limit) conn.close()
def test_limit_per_host_property(loop): conn = aiohttp.BaseConnector(loop=loop, limit_per_host=15) assert 15 == conn.limit_per_host conn.close()
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()
def test_ctor_loop(self, asyncio): session = aiohttp.BaseConnector() self.assertIs(session._loop, asyncio.get_event_loop.return_value)
def test_default_force_close(self): connector = aiohttp.BaseConnector(loop=self.loop) self.assertFalse(connector.force_close)
def test_limit_per_host_property_default(loop): conn = aiohttp.BaseConnector(loop=loop) assert conn.limit_per_host == 0 conn.close()
def test_limit_property_default(self): conn = aiohttp.BaseConnector(loop=self.loop) self.assertIsNone(conn.limit) conn.close()
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
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
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
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
def test_del(self): conn = aiohttp.BaseConnector(loop=self.loop) close = conn.close = unittest.mock.Mock() del conn self.assertTrue(close.called)
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
def test_create_conn(self): conn = aiohttp.BaseConnector(loop=self.loop) self.assertRaises(NotImplementedError, conn._create_connection, object())
def test_limit_property(loop): with pytest.warns(DeprecationWarning): conn = aiohttp.BaseConnector(loop=loop, limit=15) assert 15 == conn.capacity conn.close()
async def test_connect_with_connector(monkeypatch): connector = aiohttp.BaseConnector() docker = Docker(connector=connector) assert docker.connector == connector await docker.close()
def test_limitless(loop): with aiohttp.BaseConnector(loop=loop, capacity=None) as conn: assert conn.capacity == 0
def go(): conn = aiohttp.BaseConnector(loop=self.loop) with self.assertRaises(NotImplementedError): yield from conn._create_connection(object())