Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 0fc86c9

Browse files
committed
improve poll_query_until() API
1 parent 3006ecf commit 0fc86c9

File tree

1 file changed

+32
-14
lines changed

1 file changed

+32
-14
lines changed

testgres/testgres.py

Lines changed: 32 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -827,36 +827,54 @@ def poll_query_until(self,
827827
query,
828828
username=None,
829829
max_attempts=60,
830-
sleep_time=1):
830+
sleep_time=1,
831+
expected=True,
832+
raise_programming_error=True,
833+
raise_internal_error=True):
831834
"""
832-
Run a query once a second until it returs True.
835+
Run a query once a second until it returs 'expected'.
833836
834837
Args:
835838
dbname: database name to connect to (str).
836839
query: query to be executed (str).
837840
username: database user name (str).
838841
max_attempts: how many times should we try?
839842
sleep_time: how long should we sleep after a failure?
843+
expected: what should be returned to break the cycle?
844+
raise_programming_error: mute ProgrammingError?
845+
raise_internal_error: mute InternalError?
840846
"""
841847

842848
attemps = 0
843849
while attemps < max_attempts:
844-
res = self.execute(dbname=dbname,
845-
query=query,
846-
username=username,
847-
commit=True)
850+
try:
851+
res = self.execute(dbname=dbname,
852+
query=query,
853+
username=username,
854+
commit=True)
855+
856+
if expected is None and res is None:
857+
return # done
858+
859+
if res is None:
860+
raise QueryException('Query returned None')
861+
862+
if len(res) == 0:
863+
raise QueryException('Query returned 0 rows')
848864

849-
if res is None:
850-
raise QueryException('Query returned None')
865+
if len(res[0]) == 0:
866+
raise QueryException('Query returned 0 columns')
851867

852-
if len(res) == 0:
853-
raise QueryException('Query returned 0 rows')
868+
if res[0][0]:
869+
return # done
854870

855-
if len(res[0]) == 0:
856-
raise QueryException('Query returned 0 columns')
871+
except pglib.ProgrammingError as e:
872+
if raise_programming_error:
873+
raise e
857874

858-
if res[0][0]:
859-
return # done
875+
except pglib.InternalError as e:
876+
if raise_internal_error:
877+
raise e
860878

861879
time.sleep(sleep_time)
862880
attemps += 1

0 commit comments

Comments
 (0)