Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

Commit

Permalink
Fixup ID gen tests
Browse files Browse the repository at this point in the history
  • Loading branch information
erikjohnston committed Oct 9, 2020
1 parent 2cd097e commit 8254986
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 9 deletions.
2 changes: 2 additions & 0 deletions synapse/storage/util/sequence.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,8 @@ def check_consistency(
if max_stream_id > last_value:
logger.warning(
"Postgres sequence %s is behind table %s: %d < %d",
self._sequence_name,
table,
last_value,
max_stream_id,
)
Expand Down
25 changes: 16 additions & 9 deletions tests/storage/test_id_generators.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,10 +199,17 @@ def test_multi_instance(self):
first_id_gen = self._create_id_generator("first", writers=["first", "second"])
second_id_gen = self._create_id_generator("second", writers=["first", "second"])

self.assertEqual(first_id_gen.get_positions(), {"first": 3, "second": 7})
self.assertEqual(first_id_gen.get_current_token_for_writer("first"), 3)
# The first ID gen will notice that it can advance its token to 7 as it
# has no in progress writes...
self.assertEqual(first_id_gen.get_positions(), {"first": 7, "second": 7})
self.assertEqual(first_id_gen.get_current_token_for_writer("first"), 7)
self.assertEqual(first_id_gen.get_current_token_for_writer("second"), 7)

# ... but the second ID gen doesn't know that.
self.assertEqual(second_id_gen.get_positions(), {"first": 3, "second": 7})
self.assertEqual(second_id_gen.get_current_token_for_writer("first"), 3)
self.assertEqual(second_id_gen.get_current_token_for_writer("second"), 7)

# Try allocating a new ID gen and check that we only see position
# advanced after we leave the context manager.

Expand All @@ -211,7 +218,7 @@ async def _get_next_async():
self.assertEqual(stream_id, 8)

self.assertEqual(
first_id_gen.get_positions(), {"first": 3, "second": 7}
first_id_gen.get_positions(), {"first": 7, "second": 7}
)

self.get_success(_get_next_async())
Expand Down Expand Up @@ -279,7 +286,7 @@ def test_get_persisted_upto_position(self):
self._insert_row_with_id("first", 3)
self._insert_row_with_id("second", 5)

id_gen = self._create_id_generator("first", writers=["first", "second"])
id_gen = self._create_id_generator("worker", writers=["first", "second"])

self.assertEqual(id_gen.get_positions(), {"first": 3, "second": 5})

Expand Down Expand Up @@ -319,14 +326,14 @@ def test_get_persisted_upto_position_get_next(self):

id_gen = self._create_id_generator("first", writers=["first", "second"])

self.assertEqual(id_gen.get_positions(), {"first": 3, "second": 5})
self.assertEqual(id_gen.get_positions(), {"first": 5, "second": 5})

self.assertEqual(id_gen.get_persisted_upto_position(), 3)
self.assertEqual(id_gen.get_persisted_upto_position(), 5)

async def _get_next_async():
async with id_gen.get_next() as stream_id:
self.assertEqual(stream_id, 6)
self.assertEqual(id_gen.get_persisted_upto_position(), 3)
self.assertEqual(id_gen.get_persisted_upto_position(), 5)

self.get_success(_get_next_async())

Expand Down Expand Up @@ -388,7 +395,7 @@ def test_writer_config_change(self):
self._insert_row_with_id("second", 5)

# Initial config has two writers
id_gen = self._create_id_generator("first", writers=["first", "second"])
id_gen = self._create_id_generator("worker", writers=["first", "second"])
self.assertEqual(id_gen.get_persisted_upto_position(), 3)
self.assertEqual(id_gen.get_current_token_for_writer("first"), 3)
self.assertEqual(id_gen.get_current_token_for_writer("second"), 5)
Expand Down Expand Up @@ -568,7 +575,7 @@ async def _get_next_async2():

self.get_success(_get_next_async2())

self.assertEqual(id_gen_1.get_positions(), {"first": -1, "second": -2})
self.assertEqual(id_gen_1.get_positions(), {"first": -2, "second": -2})
self.assertEqual(id_gen_2.get_positions(), {"first": -1, "second": -2})
self.assertEqual(id_gen_1.get_persisted_upto_position(), -2)
self.assertEqual(id_gen_2.get_persisted_upto_position(), -2)

0 comments on commit 8254986

Please sign in to comment.