Skip to content

add 'suppress' to PostgresNode.poll_query_until() to mute exceptions #51

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 11, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion testgres/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@

from .connection import \
NodeConnection, \
DatabaseError, \
InternalError, \
ProgrammingError
ProgrammingError, \
OperationalError

from .exceptions import *
from .enums import *
Expand Down
4 changes: 3 additions & 1 deletion testgres/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@

from .exceptions import QueryException

# export these exceptions
# export some exceptions
DatabaseError = pglib.DatabaseError
InternalError = pglib.InternalError
ProgrammingError = pglib.ProgrammingError
OperationalError = pglib.OperationalError


class NodeConnection(object):
Expand Down
42 changes: 17 additions & 25 deletions testgres/node.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,7 @@

from .config import testgres_config

from .connection import \
NodeConnection, \
InternalError, \
ProgrammingError
from .connection import NodeConnection

from .consts import \
DATA_DIR, \
Expand Down Expand Up @@ -531,10 +528,10 @@ def append_conf(self, line='', filename=PG_CONF_FILE, **kwargs):
This instance of :class:`.PostgresNode`.

Examples:
append_conf(fsync=False)
append_conf('log_connections = yes')
append_conf(random_page_cost=1.5, fsync=True, ...)
append_conf('postgresql.conf', 'synchronous_commit = off')
>>> append_conf(fsync=False)
>>> append_conf('log_connections = yes')
>>> append_conf(random_page_cost=1.5, fsync=True, ...)
>>> append_conf('postgresql.conf', 'synchronous_commit = off')
"""

lines = [line]
Expand Down Expand Up @@ -970,8 +967,7 @@ def poll_query_until(self,
sleep_time=1,
expected=True,
commit=True,
raise_programming_error=True,
raise_internal_error=True):
suppress=None):
"""
Run a query once per second until it returns 'expected'.
Query should return a single value (1 row, 1 column).
Expand All @@ -984,13 +980,13 @@ def poll_query_until(self,
sleep_time: how much should we sleep after a failure?
expected: what should be returned to break the cycle?
commit: should (possible) changes be committed?
raise_programming_error: enable ProgrammingError?
raise_internal_error: enable InternalError?
suppress: a collection of exceptions to be suppressed.

Examples:
poll_query_until('select true')
poll_query_until('postgres', "select now() > '01.01.2018'")
poll_query_until('select false', expected=True, max_attempts=4)
>>> poll_query_until('select true')
>>> poll_query_until('postgres', "select now() > '01.01.2018'")
>>> poll_query_until('select false', expected=True, max_attempts=4)
>>> poll_query_until('select 1', suppress={testgres.OperationalError})
"""

# sanity checks
Expand Down Expand Up @@ -1022,13 +1018,8 @@ def poll_query_until(self,
elif expected is None:
return # done

except ProgrammingError as e:
if raise_programming_error:
raise e

except InternalError as e:
if raise_internal_error:
raise e
except tuple(suppress or []):
pass # we're suppressing them

time.sleep(sleep_time)
attempts += 1
Expand Down Expand Up @@ -1219,13 +1210,14 @@ def pgbench_run(self, dbname=None, username=None, options=[], **kwargs):
options: additional options for pgbench (list).

**kwargs: named options for pgbench.
Examples:
pgbench_run(initialize=True, scale=2)
pgbench_run(time=10)
Run pgbench --help to learn more.

Returns:
Stdout produced by pgbench.

Examples:
>>> pgbench_run(initialize=True, scale=2)
>>> pgbench_run(time=10)
"""

# Set default arguments
Expand Down
2 changes: 1 addition & 1 deletion tests/test_simple.py
Original file line number Diff line number Diff line change
Expand Up @@ -625,7 +625,7 @@ def test_poll_query_until(self):
query='dummy2',
max_attempts=3,
sleep_time=0.01,
raise_programming_error=False)
suppress={testgres.ProgrammingError})

# check 1 arg, ok
node.poll_query_until('select true')
Expand Down