Skip to content

Commit 259c27c

Browse files
committed
improve poll_query_until()
1 parent 9feb091 commit 259c27c

File tree

2 files changed

+50
-4
lines changed

2 files changed

+50
-4
lines changed

‎testgres/testgres.py

+22-3
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,10 @@ class QueryException(Exception):
9393
pass
9494

9595

96+
class TimeoutException(Exception):
97+
pass
98+
99+
96100
class StartNodeException(Exception):
97101
pass
98102

@@ -115,8 +119,8 @@ def __init__(self, node_name, fd):
115119

116120
threading.Thread.__init__(self)
117121

118-
self.node_name = node_name
119122
self.fd = fd
123+
self.node_name = node_name
120124
self.stop_event = threading.Event()
121125
self.logger = logging.getLogger(node_name)
122126
self.logger.setLevel(logging.INFO)
@@ -366,13 +370,19 @@ def __bool__(self):
366370

367371

368372
class PostgresNode(object):
369-
def __init__(self, name, port=None, base_dir=None, use_logging=False):
373+
def __init__(self,
374+
name,
375+
port=None,
376+
base_dir=None,
377+
use_logging=False,
378+
master=None):
370379
global bound_ports
371380

372381
# check that port is not used
373382
if port in bound_ports:
374383
raise InitNodeException('port {} is already in use'.format(port))
375384

385+
self.master = master
376386
self.name = name
377387
self.host = '127.0.0.1'
378388
self.port = port or reserve_port()
@@ -809,13 +819,22 @@ def poll_query_until(self, dbname, query, username=None, max_attempts=60, sleep_
809819
username=username,
810820
commit=True)
811821

822+
if res is None:
823+
raise QueryException('Query returned None')
824+
825+
if len(res) == 0:
826+
raise QueryException('Query returned 0 rows')
827+
828+
if len(res[0]) == 0:
829+
raise QueryException('Query returned 0 columns')
830+
812831
if res[0][0]:
813832
return # done
814833

815834
time.sleep(sleep_time)
816835
attemps += 1
817836

818-
raise QueryException('Query timeout')
837+
raise TimeoutException('Query timeout')
819838

820839
def execute(self, dbname, query, username=None, commit=False):
821840
"""

‎tests/test_simple.py

+28-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,10 @@
99

1010
from distutils.version import LooseVersion
1111

12-
from testgres import InitNodeException, StartNodeException, ExecUtilException, BackupException
12+
from testgres import InitNodeException, \
13+
StartNodeException, ExecUtilException, \
14+
BackupException, QueryException
15+
1316
from testgres import get_new_node, get_pg_config
1417
from testgres import bound_ports
1518
from testgres import NodeStatus
@@ -283,6 +286,30 @@ def test_poll_query_until(self):
283286

284287
self.assertTrue(end_time - start_time >= 5)
285288

289+
# check 0 rows
290+
got_exception = False
291+
try:
292+
node.poll_query_until('postgres', 'select * from pg_class where true = false')
293+
except QueryException as e:
294+
got_exception = True
295+
self.assertTrue(got_exception)
296+
297+
# check 0 columns
298+
got_exception = False
299+
try:
300+
node.poll_query_until('postgres', 'select from pg_class limit 1')
301+
except QueryException as e:
302+
got_exception = True
303+
self.assertTrue(got_exception)
304+
305+
# check None
306+
got_exception = False
307+
try:
308+
node.poll_query_until('postgres', 'create table abc (val int)')
309+
except QueryException as e:
310+
got_exception = True
311+
self.assertTrue(got_exception)
312+
286313
def test_logging(self):
287314
regex = re.compile('.+?LOG:.*')
288315
logfile = tempfile.NamedTemporaryFile('w', delete=True)

0 commit comments

Comments
 (0)