test_purge.py 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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", federation_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. token = self.get_success(
  40. store.get_topological_token_for_event(last["event_id"])
  41. )
  42. token_str = self.get_success(token.to_string(self.hs.get_datastore()))
  43. # Purge everything before this topological token
  44. self.get_success(
  45. storage.purge_events.purge_history(self.room_id, token_str, True)
  46. )
  47. # 1-3 should fail and last will succeed, meaning that 1-3 are deleted
  48. # and last is not.
  49. self.get_failure(store.get_event(first["event_id"]), NotFoundError)
  50. self.get_failure(store.get_event(second["event_id"]), NotFoundError)
  51. self.get_failure(store.get_event(third["event_id"]), NotFoundError)
  52. self.get_success(store.get_event(last["event_id"]))
  53. def test_purge_wont_delete_extrems(self):
  54. """
  55. Purging a room will delete everything before the topological point.
  56. """
  57. # Send four messages to the room
  58. first = self.helper.send(self.room_id, body="test1")
  59. second = self.helper.send(self.room_id, body="test2")
  60. third = self.helper.send(self.room_id, body="test3")
  61. last = self.helper.send(self.room_id, body="test4")
  62. storage = self.hs.get_datastore()
  63. # Set the topological token higher than it should be
  64. token = self.get_success(
  65. storage.get_topological_token_for_event(last["event_id"])
  66. )
  67. event = "t{}-{}".format(token.topological + 1, token.stream + 1)
  68. # Purge everything before this topological token
  69. purge = defer.ensureDeferred(storage.purge_history(self.room_id, event, True))
  70. self.pump()
  71. f = self.failureResultOf(purge)
  72. self.assertIn("greater than forward", f.value.args[0])
  73. # Try and get the events
  74. self.get_success(storage.get_event(first["event_id"]))
  75. self.get_success(storage.get_event(second["event_id"]))
  76. self.get_success(storage.get_event(third["event_id"]))
  77. self.get_success(storage.get_event(last["event_id"]))