test_background_update.py 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. from mock import Mock
  2. from twisted.internet import defer
  3. from synapse.storage.background_updates import BackgroundUpdater
  4. from tests import unittest
  5. class BackgroundUpdateTestCase(unittest.HomeserverTestCase):
  6. def prepare(self, reactor, clock, homeserver):
  7. self.updates = self.hs.get_datastore().db.updates # type: BackgroundUpdater
  8. # the base test class should have run the real bg updates for us
  9. self.assertTrue(
  10. self.get_success(self.updates.has_completed_background_updates())
  11. )
  12. self.update_handler = Mock()
  13. self.updates.register_background_update_handler(
  14. "test_update", self.update_handler
  15. )
  16. def test_do_background_update(self):
  17. # the time we claim each update takes
  18. duration_ms = 42
  19. # the target runtime for each bg update
  20. target_background_update_duration_ms = 50000
  21. store = self.hs.get_datastore()
  22. self.get_success(
  23. store.db.simple_insert(
  24. "background_updates",
  25. values={"update_name": "test_update", "progress_json": '{"my_key": 1}'},
  26. )
  27. )
  28. # first step: make a bit of progress
  29. @defer.inlineCallbacks
  30. def update(progress, count):
  31. yield self.clock.sleep((count * duration_ms) / 1000)
  32. progress = {"my_key": progress["my_key"] + 1}
  33. yield store.db.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. @defer.inlineCallbacks
  56. def update(progress, count):
  57. self.assertEqual(progress, {"my_key": 2})
  58. self.assertAlmostEqual(
  59. count, target_background_update_duration_ms / duration_ms, places=0,
  60. )
  61. yield self.updates._end_background_update("test_update")
  62. return count
  63. self.update_handler.side_effect = update
  64. self.update_handler.reset_mock()
  65. result = self.get_success(
  66. self.updates.do_next_background_update(target_background_update_duration_ms)
  67. )
  68. self.assertFalse(result)
  69. self.update_handler.assert_called_once()
  70. # third step: we don't expect to be called any more
  71. self.update_handler.reset_mock()
  72. result = self.get_success(
  73. self.updates.do_next_background_update(target_background_update_duration_ms)
  74. )
  75. self.assertTrue(result)
  76. self.assertFalse(self.update_handler.called)