From 0f0630d8d74dec24ab69fa5b9afac37b6bf61427 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Honza=20Kr=C3=A1l?= Date: Mon, 8 Dec 2014 14:32:52 +0100 Subject: [PATCH] Make sure we always have connections defined --- elasticsearch/connection_pool.py | 5 +++++ test_elasticsearch/test_connection_pool.py | 4 ++++ 2 files changed, 9 insertions(+) diff --git a/elasticsearch/connection_pool.py b/elasticsearch/connection_pool.py index 13d545e3..a36a820c 100644 --- a/elasticsearch/connection_pool.py +++ b/elasticsearch/connection_pool.py @@ -7,6 +7,8 @@ try: except ImportError: from queue import PriorityQueue, Empty +from .exceptions import ImproperlyConfigured + logger = logging.getLogger('elasticsearch') class ConnectionSelector(object): @@ -100,6 +102,9 @@ class ConnectionPool(object): :arg randomize_hosts: shuffle the list of connections upon arrival to avoid dog piling effect across processes """ + if not connections: + raise ImproperlyConfigured("No defined connections, you need to \ + specify at least one host.") self.connection_opts = connections self.connections = [c for (c, opts) in connections] # PriorityQueue for thread safety and ease of timeout management diff --git a/test_elasticsearch/test_connection_pool.py b/test_elasticsearch/test_connection_pool.py index e67a7d87..13b316a5 100644 --- a/test_elasticsearch/test_connection_pool.py +++ b/test_elasticsearch/test_connection_pool.py @@ -1,10 +1,14 @@ import time from elasticsearch.connection_pool import ConnectionPool, RoundRobinSelector +from elasticsearch.exceptions import ImproperlyConfigured from .test_cases import TestCase class TestConnectionPool(TestCase): + def test_raises_exception_when_no_connections_defined(self): + self.assertRaises(ImproperlyConfigured, ConnectionPool, []) + def test_default_round_robin(self): pool = ConnectionPool([(x, {}) for x in range(100)])