test_background_update.py 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. from unittest.mock import Mock
  2. from synapse.storage.background_updates import BackgroundUpdater
  3. from tests import unittest
  4. class BackgroundUpdateTestCase(unittest.HomeserverTestCase):
  5. def prepare(self, reactor, clock, homeserver):
  6. self.updates: BackgroundUpdater = self.hs.get_datastore().db_pool.updates
  7. # the base test class should have run the real bg updates for us
  8. self.assertTrue(
  9. self.get_success(self.updates.has_completed_background_updates())
  10. )
  11. self.update_handler = Mock()
  12. self.updates.register_background_update_handler(
  13. "test_update", self.update_handler
  14. )
  15. def test_do_background_update(self):
  16. # the time we claim each update takes
  17. duration_ms = 42
  18. # the target runtime for each bg update
  19. target_background_update_duration_ms = 50000
  20. store = self.hs.get_datastore()
  21. self.get_success(
  22. store.db_pool.simple_insert(
  23. "background_updates",
  24. values={"update_name": "test_update", "progress_json": '{"my_key": 1}'},
  25. )
  26. )
  27. # first step: make a bit of progress
  28. async def update(progress, count):
  29. await self.clock.sleep((count * duration_ms) / 1000)
  30. progress = {"my_key": progress["my_key"] + 1}
  31. await store.db_pool.runInteraction(
  32. "update_progress",
  33. self.updates._background_update_progress_txn,
  34. "test_update",
  35. progress,
  36. )
  37. return count
  38. self.update_handler.side_effect = update
  39. self.update_handler.reset_mock()
  40. res = self.get_success(
  41. self.updates.do_next_background_update(
  42. target_background_update_duration_ms
  43. ),
  44. by=0.1,
  45. )
  46. self.assertFalse(res)
  47. # on the first call, we should get run with the default background update size
  48. self.update_handler.assert_called_once_with(
  49. {"my_key": 1}, self.updates.DEFAULT_BACKGROUND_BATCH_SIZE
  50. )
  51. # second step: complete the update
  52. # we should now get run with a much bigger number of items to update
  53. async def update(progress, count):
  54. self.assertEqual(progress, {"my_key": 2})
  55. self.assertAlmostEqual(
  56. count,
  57. target_background_update_duration_ms / duration_ms,
  58. places=0,
  59. )
  60. await self.updates._end_background_update("test_update")
  61. return count
  62. self.update_handler.side_effect = update
  63. self.update_handler.reset_mock()
  64. result = self.get_success(
  65. self.updates.do_next_background_update(target_background_update_duration_ms)
  66. )
  67. self.assertFalse(result)
  68. self.update_handler.assert_called_once()
  69. # third step: we don't expect to be called any more
  70. self.update_handler.reset_mock()
  71. result = self.get_success(
  72. self.updates.do_next_background_update(target_background_update_duration_ms)
  73. )
  74. self.assertTrue(result)
  75. self.assertFalse(self.update_handler.called)