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

Commit

Permalink
Add regression test
Browse files Browse the repository at this point in the history
  • Loading branch information
erikjohnston committed Sep 23, 2020
1 parent f5a91ab commit c08456d
Showing 1 changed file with 44 additions and 0 deletions.
44 changes: 44 additions & 0 deletions tests/storage/test_id_generators.py
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,50 @@ async def _get_next_async():
# `persisted_upto_position` in this case, then it will be correct in the
# other cases that are tested above (since they'll hit the same code).

def test_restart_during_out_of_order_persistence(self):
"""Test that restarting a process while another process is writing out
of order updates are handled correctly.
"""

# Prefill table with 7 rows written by 'master'
self._insert_rows("master", 7)

id_gen = self._create_id_generator()

self.assertEqual(id_gen.get_positions(), {"master": 7})
self.assertEqual(id_gen.get_current_token_for_writer("master"), 7)

# Persist two rows at once
ctx1 = self.get_success(id_gen.get_next())
ctx2 = self.get_success(id_gen.get_next())

s1 = self.get_success(ctx1.__aenter__())
s2 = self.get_success(ctx2.__aenter__())

self.assertEqual(s1, 8)
self.assertEqual(s2, 9)

self.assertEqual(id_gen.get_positions(), {"master": 7})
self.assertEqual(id_gen.get_current_token_for_writer("master"), 7)

# We finish persisting the second row before restart
self.get_success(ctx2.__aexit__(None, None, None))

# We simulate a restart of another worker by just creating a new ID gen.
id_gen_worker = self._create_id_generator("worker")

# Restarted worker should not see the second persisted row
self.assertEqual(id_gen_worker.get_positions(), {"master": 7})
self.assertEqual(id_gen_worker.get_current_token_for_writer("master"), 7)

# Now if we persist the first row then both instances should jump ahead
# correctly.
self.get_success(ctx1.__aexit__(None, None, None))

self.assertEqual(id_gen.get_positions(), {"master": 9})
id_gen_worker.advance("master", 9)
self.assertEqual(id_gen_worker.get_positions(), {"master": 9})


class BackwardsMultiWriterIdGeneratorTestCase(HomeserverTestCase):
"""Tests MultiWriterIdGenerator that produce *negative* stream IDs.
Expand Down

0 comments on commit c08456d

Please sign in to comment.