test_purge.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. # -*- coding: utf-8 -*-
  2. # Copyright 2018 New Vector Ltd
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. from synapse.rest.client.v1 import room
  16. from tests.unittest import HomeserverTestCase
  17. class PurgeTests(HomeserverTestCase):
  18. user_id = "@red:server"
  19. servlets = [room.register_servlets]
  20. def make_homeserver(self, reactor, clock):
  21. hs = self.setup_test_homeserver("server", http_client=None)
  22. return hs
  23. def prepare(self, reactor, clock, hs):
  24. self.room_id = self.helper.create_room_as(self.user_id)
  25. def test_purge(self):
  26. """
  27. Purging a room will delete everything before the topological point.
  28. """
  29. # Send four messages to the room
  30. first = self.helper.send(self.room_id, body="test1")
  31. second = self.helper.send(self.room_id, body="test2")
  32. third = self.helper.send(self.room_id, body="test3")
  33. last = self.helper.send(self.room_id, body="test4")
  34. store = self.hs.get_datastore()
  35. storage = self.hs.get_storage()
  36. # Get the topological token
  37. event = store.get_topological_token_for_event(last["event_id"])
  38. self.pump()
  39. event = self.successResultOf(event)
  40. # Purge everything before this topological token
  41. purge = storage.purge_events.purge_history(self.room_id, event, True)
  42. self.pump()
  43. self.assertEqual(self.successResultOf(purge), None)
  44. # Try and get the events
  45. get_first = store.get_event(first["event_id"])
  46. get_second = store.get_event(second["event_id"])
  47. get_third = store.get_event(third["event_id"])
  48. get_last = store.get_event(last["event_id"])
  49. self.pump()
  50. # 1-3 should fail and last will succeed, meaning that 1-3 are deleted
  51. # and last is not.
  52. self.failureResultOf(get_first)
  53. self.failureResultOf(get_second)
  54. self.failureResultOf(get_third)
  55. self.successResultOf(get_last)
  56. def test_purge_wont_delete_extrems(self):
  57. """
  58. Purging a room will delete everything before the topological point.
  59. """
  60. # Send four messages to the room
  61. first = self.helper.send(self.room_id, body="test1")
  62. second = self.helper.send(self.room_id, body="test2")
  63. third = self.helper.send(self.room_id, body="test3")
  64. last = self.helper.send(self.room_id, body="test4")
  65. storage = self.hs.get_datastore()
  66. # Set the topological token higher than it should be
  67. event = storage.get_topological_token_for_event(last["event_id"])
  68. self.pump()
  69. event = self.successResultOf(event)
  70. event = "t{}-{}".format(
  71. *list(map(lambda x: x + 1, map(int, event[1:].split("-"))))
  72. )
  73. # Purge everything before this topological token
  74. purge = storage.purge_history(self.room_id, event, True)
  75. self.pump()
  76. f = self.failureResultOf(purge)
  77. self.assertIn("greater than forward", f.value.args[0])
  78. # Try and get the events
  79. get_first = storage.get_event(first["event_id"])
  80. get_second = storage.get_event(second["event_id"])
  81. get_third = storage.get_event(third["event_id"])
  82. get_last = storage.get_event(last["event_id"])
  83. self.pump()
  84. # Nothing is deleted.
  85. self.successResultOf(get_first)
  86. self.successResultOf(get_second)
  87. self.successResultOf(get_third)
  88. self.successResultOf(get_last)