test_background_update.py 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. from tests import unittest
  2. from twisted.internet import defer
  3. from tests.utils import setup_test_homeserver
  4. from mock import Mock
  5. class BackgroundUpdateTestCase(unittest.TestCase):
  6. @defer.inlineCallbacks
  7. def setUp(self):
  8. hs = yield setup_test_homeserver()
  9. self.store = hs.get_datastore()
  10. self.clock = hs.get_clock()
  11. self.update_handler = Mock()
  12. yield self.store.register_background_update_handler(
  13. "test_update", self.update_handler
  14. )
  15. @defer.inlineCallbacks
  16. def test_do_background_update(self):
  17. desired_count = 1000
  18. duration_ms = 42
  19. @defer.inlineCallbacks
  20. def update(progress, count):
  21. self.clock.advance_time_msec(count * duration_ms)
  22. progress = {"my_key": progress["my_key"] + 1}
  23. yield self.store.runInteraction(
  24. "update_progress",
  25. self.store._background_update_progress_txn,
  26. "test_update",
  27. progress,
  28. )
  29. defer.returnValue(count)
  30. self.update_handler.side_effect = update
  31. yield self.store.start_background_update("test_update", {"my_key": 1})
  32. self.update_handler.reset_mock()
  33. result = yield self.store.do_background_update(
  34. duration_ms * desired_count
  35. )
  36. self.assertIsNotNone(result)
  37. self.update_handler.assert_called_once_with(
  38. {"my_key": 1}, self.store.DEFAULT_BACKGROUND_BATCH_SIZE
  39. )
  40. @defer.inlineCallbacks
  41. def update(progress, count):
  42. yield self.store._end_background_update("test_update")
  43. defer.returnValue(count)
  44. self.update_handler.side_effect = update
  45. self.update_handler.reset_mock()
  46. result = yield self.store.do_background_update(
  47. duration_ms * desired_count
  48. )
  49. self.assertIsNotNone(result)
  50. self.update_handler.assert_called_once_with(
  51. {"my_key": 2}, desired_count
  52. )
  53. self.update_handler.reset_mock()
  54. result = yield self.store.do_background_update(
  55. duration_ms * desired_count
  56. )
  57. self.assertIsNone(result)
  58. self.assertFalse(self.update_handler.called)