Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b1cba73

Browse files
committed
Some minor refactoring of logical replication
1 parent f8b95c6 commit b1cba73

File tree

3 files changed

+18
-31
lines changed

3 files changed

+18
-31
lines changed

testgres/node.py

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -847,8 +847,7 @@ def poll_query_until(self,
847847
expected=True,
848848
commit=True,
849849
raise_programming_error=True,
850-
raise_internal_error=True,
851-
zero_rows_is_ok=False):
850+
raise_internal_error=True):
852851
"""
853852
Run a query once per second until it returns 'expected'.
854853
Query should return a single value (1 row, 1 column).
@@ -884,18 +883,14 @@ def poll_query_until(self,
884883
if res is None:
885884
raise QueryException('Query returned None', query)
886885

887-
if len(res) == 0:
888-
if zero_rows_is_ok:
889-
time.sleep(sleep_time)
890-
attempts += 1
891-
continue
892-
else:
893-
raise QueryException('Query returned 0 rows', query)
894-
895-
if len(res[0]) == 0:
896-
raise QueryException('Query returned 0 columns', query)
897-
898-
if res[0][0] == expected:
886+
# result set is not empty
887+
if len(res):
888+
if len(res[0]) == 0:
889+
raise QueryException('Query returned 0 columns', query)
890+
if res[0][0] == expected:
891+
return # done
892+
# empty result set is considered as None
893+
elif expected is None:
899894
return # done
900895

901896
except ProgrammingError as e:

testgres/pubsub.py

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -138,24 +138,17 @@ def catchup(self, username=None):
138138
Args:
139139
username: remote node's user name
140140
"""
141-
if pg_version_ge('10'):
142-
query = (
143-
"select pg_current_wal_lsn() - replay_lsn = 0 "
144-
"from pg_stat_replication where application_name = '{}'"
145-
).format(self.name)
146-
else:
147-
query = (
148-
"select pg_current_xlog_location() - replay_location = 0 "
149-
"from pg_stat_replication where application_name = '{}'"
150-
).format(self.name)
141+
query = (
142+
"select pg_current_wal_lsn() - replay_lsn = 0 "
143+
"from pg_stat_replication where application_name = '{}'"
144+
).format(self.name)
151145

152146
try:
153147
# wait until this LSN reaches subscriber
154148
self.pub.node.poll_query_until(
155149
query=query,
156150
dbname=self.pub.dbname,
157151
username=username or self.pub.username,
158-
max_attempts=60,
159-
zero_rows_is_ok=True) # statistics may have not updated yet
152+
max_attempts=60)
160153
except Exception as e:
161154
raise_from(CatchUpException("Failed to catch up", query), e)

tests/test_simple.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -495,11 +495,6 @@ def test_poll_query_until(self):
495495

496496
self.assertTrue(end_time - start_time >= 5)
497497

498-
# check 0 rows
499-
with self.assertRaises(QueryException):
500-
node.poll_query_until(
501-
query='select * from pg_class where true = false')
502-
503498
# check 0 columns
504499
with self.assertRaises(QueryException):
505500
node.poll_query_until(query='select from pg_class limit 1')
@@ -512,6 +507,10 @@ def test_poll_query_until(self):
512507
node.poll_query_until(
513508
query='create table def()', expected=None) # returns nothing
514509

510+
# check 0 rows equivalent to expected=None
511+
node.poll_query_until(
512+
query='select * from pg_class where true = false', expected=None)
513+
515514
# check arbitrary expected value, fail
516515
with self.assertRaises(TimeoutException):
517516
node.poll_query_until(

0 commit comments

Comments
 (0)