def go(): c1 = Connection(Endpoint('http', 'h1', 1), loop=self.loop) c2 = Connection(Endpoint('http', 'h2', 2), loop=self.loop) pool = self.make_pool(connections=[c1, c2]) yield from pool.mark_dead(c1) yield from pool.mark_dead(c2) conn = yield from pool.get_connection() self.assertEqual(1, pool._dead.qsize()) self.assertIs(c1, conn)
def test_get_connection(loop, make_pool): c1 = Connection(Endpoint('http', 'h1', 1), loop=loop) c2 = Connection(Endpoint('http', 'h2', 2), loop=loop) pool = make_pool(connections=[c1, c2]) yield from pool.mark_dead(c1) yield from pool.mark_dead(c2) conn = yield from pool.get_connection() assert 1 == pool._dead.qsize() assert c1 is conn
def go(): c1 = Connection(Endpoint('http', 'h1', 1), loop=self.loop) c2 = Connection(Endpoint('http', 'h2', 2), loop=self.loop) pool = self.make_pool(connections=[c1, c2]) yield from pool.mark_dead(c1) yield from pool.mark_dead(c2) yield from pool.resurrect() self.assertEqual(2, pool._dead.qsize()) yield from pool.resurrect(True) self.assertEqual(1, pool._dead.qsize()) self.assertEqual([c1], pool.connections)
def test_resurrect(loop, make_pool): c1 = Connection(Endpoint('http', 'h1', 1), loop=loop) c2 = Connection(Endpoint('http', 'h2', 2), loop=loop) pool = make_pool(connections=[c1, c2]) yield from pool.mark_dead(c1) yield from pool.mark_dead(c2) yield from pool.resurrect() assert 2 == pool._dead.qsize() yield from pool.resurrect(True) assert 1 == pool._dead.qsize() assert [c1] == pool.connections
def go(): pool = self.make_pool(connections=[]) conn = Connection(Endpoint('http', 'localhost', 9200), loop=self.loop) yield from pool.mark_live(conn) self.assertNotIn(conn, pool._dead_count)
def test_ctor(self): tr = self.make_transport() self.assertEqual(3, tr.max_retries) self.assertGreaterEqual(time.monotonic(), tr.last_sniff) self.assertIsNone(tr.sniffer_interval) self.assertAlmostEqual(0.1, tr.sniffer_timeout) self.assertEqual([Endpoint('http', 'localhost', 9200)], tr.endpoints) self.assertEqual(1, len(tr._pool.connections))
def test_ctor(make_transport, es_params): tr = make_transport() assert 3 == tr.max_retries assert time.monotonic() >= tr.last_sniff assert tr.sniffer_interval is None assert 0.1 == tr.sniffer_timeout assert [Endpoint('http', es_params['host'], 9200)] == tr.endpoints assert 1 == len(tr._pool.connections)
def make_pool(self, connections=default): if connections is default: connections = [ Connection(Endpoint('http', 'localhost', 9200), loop=self.loop) ] pool = ConnectionPool(connections, loop=self.loop) self.addCleanup(pool.close) return pool
def go(*, no_loop=False, **kwargs): nonlocal conn conn = Connection(Endpoint('http', es_params['host'], es_params['port']), loop=loop) conns.append(conn) return conn
def test_username_password_endpoints_with_port_https(self): tr = self.make_transport(endpoints=['https://*****:*****@localhost:9200']) self.assertEqual([Endpoint('https', 'john:doe@localhost', 9200)], tr.endpoints) self.assertEqual( ('https', 'john:doe@localhost:9200', '/', '', '', ''), tuple(urllib.parse.urlparse(tr._pool.connections[0]._base_url)) )
def go(): conn = Connection(Endpoint('http', 'host', 9999), loop=self.loop) resp = mock.Mock() resp.status = 404 r2 = asyncio.Future(loop=self.loop) r2.set_result('{"a": 1}') resp.text.return_value = r2 fut = asyncio.Future(loop=self.loop) fut.set_result(resp) conn._session.request = mock.Mock(return_value=fut) with self.assertRaises(NotFoundError) as ctx: yield from conn.perform_request('GET', '/data', None, None) self.assertEqual(404, ctx.exception.status_code) self.assertEqual('{"a": 1}', ctx.exception.error) self.assertEqual({"a": 1}, ctx.exception.info)
def test_bad_status_409(loop): conn = Connection(Endpoint('http', 'host', 9999), loop=loop) resp = mock.Mock() resp.status = 409 r2 = asyncio.Future(loop=loop) r2.set_result('{"a": 1}') resp.text.return_value = r2 fut = asyncio.Future(loop=loop) fut.set_result(resp) conn._session.request = mock.Mock(return_value=fut) with pytest.raises(ConflictError) as ctx: yield from conn.perform_request('GET', '/data', None, None) assert 409 == ctx.value.status_code assert '{"a": 1}' == ctx.value.error assert {"a": 1} == ctx.value.info
def test_username_password_endpoints_without_port(make_transport): tr = make_transport(endpoints=['john:doe@localhost']) assert [Endpoint('http', 'john:doe@localhost', 9200)] == tr.endpoints
def test_username_password_endpoints_with_port_https(make_transport): tr = make_transport(endpoints=['https://*****:*****@localhost:9200']) assert [Endpoint('https', 'john:doe@localhost', 9200)] == tr.endpoints assert ('https', 'john:doe@localhost:9200', '/', '', '', '') == \ tuple(urllib.parse.urlparse(str(tr._pool.connections[0]._base_url)))
def test_use_connector_param(loop): connector = aiohttp.TCPConnector(loop=loop) c = Connection(Endpoint('http', 'host', 9999), loop=loop, connector=connector) assert c._session.connector is connector
def test_default_port_http(make_transport): tr = make_transport(endpoints=['http://localhost']) assert [Endpoint('http', 'localhost', 9200)] == tr.endpoints
def test_default_port_https(make_transport): tr = make_transport(endpoints=['https://localhost']) assert [Endpoint('https', 'localhost', 443)] == tr.endpoints
def test_username_password_endpoints_without_port(self): tr = self.make_transport(endpoints=['john:doe@localhost']) self.assertEqual([Endpoint('http', 'john:doe@localhost', 9200)], tr.endpoints)
def test_set_endpoints_Endpoint(make_transport): tr = make_transport([]) assert [] == tr.endpoints tr.endpoints = [Endpoint('http', 'localhost', 9200)] assert [Endpoint('http', 'localhost', 9200)] == tr.endpoints assert 1 == len(tr._pool.connections)
def test_set_malformed_endpoints(self): tr = self.make_transport() with self.assertRaises(RuntimeError): tr.endpoints = [123] self.assertEqual([Endpoint('http', 'localhost', 9200)], tr.endpoints) self.assertEqual(1, len(tr._pool.connections))
def test_set_host_port_string(self): tr = self.make_transport() tr.endpoints = ['host:123'] self.assertEqual([Endpoint('http', 'host', 123)], tr.endpoints) self.assertEqual(1, len(tr._pool.connections))
def test_set_endpoints_Endpoint(self): tr = self.make_transport([]) self.assertEqual([], tr.endpoints) tr.endpoints = [Endpoint('http', 'localhost', 9200)] self.assertEqual([Endpoint('http', 'localhost', 9200)], tr.endpoints) self.assertEqual(1, len(tr._pool.connections))
def test_dont_recreate_existing_connections(self): tr = self.make_transport() connections = tr._pool.connections tr.endpoints = [{'host': 'localhost'}] self.assertEqual([Endpoint('http', 'localhost', 9200)], tr.endpoints) self.assertEqual(connections, tr._pool.connections)
def test_dont_recreate_existing_connections(make_transport): tr = make_transport() tr.endpoints = [{'host': 'localhost'}] assert [Endpoint('http', 'localhost', 9200)] == tr.endpoints
def test_set_malformed_endpoints(make_transport, es_params): tr = make_transport() with pytest.raises(RuntimeError): tr.endpoints = [123] assert [Endpoint('http', es_params['host'], 9200)] == tr.endpoints assert 1 == len(tr._pool.connections)
def test_set_host_port_string(make_transport): tr = make_transport() tr.endpoints = ['host:123'] assert [Endpoint('http', 'host', 123)] == tr.endpoints assert 1 == len(tr._pool.connections)
def test_ctor(loop): c = Connection(Endpoint('http', 'host', 9999), loop=loop) assert Endpoint('http', 'host', 9999) == c.endpoint
def test_set_host_port_string_invalid(self): tr = self.make_transport() with self.assertRaises(RuntimeError): tr.endpoints = ['host:123:abc'] self.assertEqual([Endpoint('http', 'localhost', 9200)], tr.endpoints) self.assertEqual(1, len(tr._pool.connections))
def test_default_port_http(self): tr = self.make_transport(endpoints=['http://localhost']) self.assertEqual([Endpoint('http', 'localhost', 9200)], tr.endpoints)
def test_set_host_port_string_invalid(make_transport, es_params): tr = make_transport() with pytest.raises(RuntimeError): tr.endpoints = ['host:123:abc'] assert [Endpoint('http', es_params['host'], 9200)] == tr.endpoints assert 1 == len(tr._pool.connections)