test_purge.py 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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 twisted.internet import defer
  16. from synapse.api.errors import NotFoundError
  17. from synapse.rest.client.v1 import room
  18. from tests.unittest import HomeserverTestCase
  19. class PurgeTests(HomeserverTestCase):
  20. user_id = "@red:server"
  21. servlets = [room.register_servlets]
  22. def make_homeserver(self, reactor, clock):
  23. hs = self.setup_test_homeserver("server", http_client=None)
  24. return hs
  25. def prepare(self, reactor, clock, hs):
  26. self.room_id = self.helper.create_room_as(self.user_id)
  27. def test_purge(self):
  28. """
  29. Purging a room will delete everything before the topological point.
  30. """
  31. # Send four messages to the room
  32. first = self.helper.send(self.room_id, body="test1")
  33. second = self.helper.send(self.room_id, body="test2")
  34. third = self.helper.send(self.room_id, body="test3")
  35. last = self.helper.send(self.room_id, body="test4")
  36. store = self.hs.get_datastore()
  37. storage = self.hs.get_storage()
  38. # Get the topological token
  39. event = self.get_success(
  40. store.get_topological_token_for_event(last["event_id"])
  41. )
  42. # Purge everything before this topological token
  43. self.get_success(storage.purge_events.purge_history(self.room_id, event, True))
  44. # 1-3 should fail and last will succeed, meaning that 1-3 are deleted
  45. # and last is not.
  46. self.get_failure(store.get_event(first["event_id"]), NotFoundError)
  47. self.get_failure(store.get_event(second["event_id"]), NotFoundError)
  48. self.get_failure(store.get_event(third["event_id"]), NotFoundError)
  49. self.get_success(store.get_event(last["event_id"]))
  50. def test_purge_wont_delete_extrems(self):
  51. """
  52. Purging a room will delete everything before the topological point.
  53. """
  54. # Send four messages to the room
  55. first = self.helper.send(self.room_id, body="test1")
  56. second = self.helper.send(self.room_id, body="test2")
  57. third = self.helper.send(self.room_id, body="test3")
  58. last = self.helper.send(self.room_id, body="test4")
  59. storage = self.hs.get_datastore()
  60. # Set the topological token higher than it should be
  61. event = self.get_success(
  62. storage.get_topological_token_for_event(last["event_id"])
  63. )
  64. event = "t{}-{}".format(
  65. *list(map(lambda x: x + 1, map(int, event[1:].split("-"))))
  66. )
  67. # Purge everything before this topological token
  68. purge = defer.ensureDeferred(storage.purge_history(self.room_id, event, True))
  69. self.pump()
  70. f = self.failureResultOf(purge)
  71. self.assertIn("greater than forward", f.value.args[0])
  72. # Try and get the events
  73. self.get_success(storage.get_event(first["event_id"]))
  74. self.get_success(storage.get_event(second["event_id"]))
  75. self.get_success(storage.get_event(third["event_id"]))
  76. self.get_success(storage.get_event(last["event_id"]))