test_purge.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. storage = self.hs.get_datastore()
  35. # Get the topological token
  36. event = storage.get_topological_token_for_event(last["event_id"])
  37. self.pump()
  38. event = self.successResultOf(event)
  39. # Purge everything before this topological token
  40. purge = storage.purge_history(self.room_id, event, True)
  41. self.pump()
  42. self.assertEqual(self.successResultOf(purge), None)
  43. # Try and get the events
  44. get_first = storage.get_event(first["event_id"])
  45. get_second = storage.get_event(second["event_id"])
  46. get_third = storage.get_event(third["event_id"])
  47. get_last = storage.get_event(last["event_id"])
  48. self.pump()
  49. # 1-3 should fail and last will succeed, meaning that 1-3 are deleted
  50. # and last is not.
  51. self.failureResultOf(get_first)
  52. self.failureResultOf(get_second)
  53. self.failureResultOf(get_third)
  54. self.successResultOf(get_last)
  55. def test_purge_wont_delete_extrems(self):
  56. """
  57. Purging a room will delete everything before the topological point.
  58. """
  59. # Send four messages to the room
  60. first = self.helper.send(self.room_id, body="test1")
  61. second = self.helper.send(self.room_id, body="test2")
  62. third = self.helper.send(self.room_id, body="test3")
  63. last = self.helper.send(self.room_id, body="test4")
  64. storage = self.hs.get_datastore()
  65. # Set the topological token higher than it should be
  66. event = storage.get_topological_token_for_event(last["event_id"])
  67. self.pump()
  68. event = self.successResultOf(event)
  69. event = "t{}-{}".format(
  70. *list(map(lambda x: x + 1, map(int, event[1:].split("-"))))
  71. )
  72. # Purge everything before this topological token
  73. purge = storage.purge_history(self.room_id, event, True)
  74. self.pump()
  75. f = self.failureResultOf(purge)
  76. self.assertIn("greater than forward", f.value.args[0])
  77. # Try and get the events
  78. get_first = storage.get_event(first["event_id"])
  79. get_second = storage.get_event(second["event_id"])
  80. get_third = storage.get_event(third["event_id"])
  81. get_last = storage.get_event(last["event_id"])
  82. self.pump()
  83. # Nothing is deleted.
  84. self.successResultOf(get_first)
  85. self.successResultOf(get_second)
  86. self.successResultOf(get_third)
  87. self.successResultOf(get_last)