1
0

test_background_update.py 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. from mock import Mock
  2. from twisted.internet import defer
  3. from tests import unittest
  4. from tests.utils import setup_test_homeserver
  5. class BackgroundUpdateTestCase(unittest.TestCase):
  6. @defer.inlineCallbacks
  7. def setUp(self):
  8. hs = yield setup_test_homeserver(
  9. self.addCleanup
  10. )
  11. self.store = hs.get_datastore()
  12. self.clock = hs.get_clock()
  13. self.update_handler = Mock()
  14. yield self.store.register_background_update_handler(
  15. "test_update", self.update_handler
  16. )
  17. # run the real background updates, to get them out the way
  18. # (perhaps we should run them as part of the test HS setup, since we
  19. # run all of the other schema setup stuff there?)
  20. while True:
  21. res = yield self.store.do_next_background_update(1000)
  22. if res is None:
  23. break
  24. @defer.inlineCallbacks
  25. def test_do_background_update(self):
  26. desired_count = 1000
  27. duration_ms = 42
  28. # first step: make a bit of progress
  29. @defer.inlineCallbacks
  30. def update(progress, count):
  31. self.clock.advance_time_msec(count * duration_ms)
  32. progress = {"my_key": progress["my_key"] + 1}
  33. yield self.store.runInteraction(
  34. "update_progress",
  35. self.store._background_update_progress_txn,
  36. "test_update",
  37. progress,
  38. )
  39. defer.returnValue(count)
  40. self.update_handler.side_effect = update
  41. yield self.store.start_background_update("test_update", {"my_key": 1})
  42. self.update_handler.reset_mock()
  43. result = yield self.store.do_next_background_update(duration_ms * desired_count)
  44. self.assertIsNotNone(result)
  45. self.update_handler.assert_called_once_with(
  46. {"my_key": 1}, self.store.DEFAULT_BACKGROUND_BATCH_SIZE
  47. )
  48. # second step: complete the update
  49. @defer.inlineCallbacks
  50. def update(progress, count):
  51. yield self.store._end_background_update("test_update")
  52. defer.returnValue(count)
  53. self.update_handler.side_effect = update
  54. self.update_handler.reset_mock()
  55. result = yield self.store.do_next_background_update(duration_ms * desired_count)
  56. self.assertIsNotNone(result)
  57. self.update_handler.assert_called_once_with({"my_key": 2}, desired_count)
  58. # third step: we don't expect to be called any more
  59. self.update_handler.reset_mock()
  60. result = yield self.store.do_next_background_update(duration_ms * desired_count)
  61. self.assertIsNone(result)
  62. self.assertFalse(self.update_handler.called)