test_background_update.py 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. from 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 = (
  7. self.hs.get_datastore().db_pool.updates
  8. ) # type: BackgroundUpdater
  9. # the base test class should have run the real bg updates for us
  10. self.assertTrue(
  11. self.get_success(self.updates.has_completed_background_updates())
  12. )
  13. self.update_handler = Mock()
  14. self.updates.register_background_update_handler(
  15. "test_update", self.update_handler
  16. )
  17. def test_do_background_update(self):
  18. # the time we claim each update takes
  19. duration_ms = 42
  20. # the target runtime for each bg update
  21. target_background_update_duration_ms = 50000
  22. store = self.hs.get_datastore()
  23. self.get_success(
  24. store.db_pool.simple_insert(
  25. "background_updates",
  26. values={"update_name": "test_update", "progress_json": '{"my_key": 1}'},
  27. )
  28. )
  29. # first step: make a bit of progress
  30. async def update(progress, count):
  31. await self.clock.sleep((count * duration_ms) / 1000)
  32. progress = {"my_key": progress["my_key"] + 1}
  33. await store.db_pool.runInteraction(
  34. "update_progress",
  35. self.updates._background_update_progress_txn,
  36. "test_update",
  37. progress,
  38. )
  39. return count
  40. self.update_handler.side_effect = update
  41. self.update_handler.reset_mock()
  42. res = self.get_success(
  43. self.updates.do_next_background_update(
  44. target_background_update_duration_ms
  45. ),
  46. by=0.1,
  47. )
  48. self.assertFalse(res)
  49. # on the first call, we should get run with the default background update size
  50. self.update_handler.assert_called_once_with(
  51. {"my_key": 1}, self.updates.DEFAULT_BACKGROUND_BATCH_SIZE
  52. )
  53. # second step: complete the update
  54. # we should now get run with a much bigger number of items to update
  55. async def update(progress, count):
  56. self.assertEqual(progress, {"my_key": 2})
  57. self.assertAlmostEqual(
  58. count, target_background_update_duration_ms / duration_ms, 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)