1
0

test_background_update.py 3.2 KB

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