test_account_data.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. # -*- coding: utf-8 -*-
  2. # Copyright 2020 The Matrix.org Foundation C.I.C.
  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.replication.tcp.streams._base import (
  16. _STREAM_UPDATE_TARGET_ROW_COUNT,
  17. AccountDataStream,
  18. )
  19. from tests.replication._base import BaseStreamTestCase
  20. class AccountDataStreamTestCase(BaseStreamTestCase):
  21. def test_update_function_room_account_data_limit(self):
  22. """Test replication with many room account data updates"""
  23. store = self.hs.get_datastore()
  24. # generate lots of account data updates
  25. updates = []
  26. for i in range(_STREAM_UPDATE_TARGET_ROW_COUNT + 5):
  27. update = "m.test_type.%i" % (i,)
  28. self.get_success(
  29. store.add_account_data_to_room("test_user", "test_room", update, {})
  30. )
  31. updates.append(update)
  32. # also one global update
  33. self.get_success(store.add_account_data_for_user("test_user", "m.global", {}))
  34. # tell the notifier to catch up to avoid duplicate rows.
  35. # workaround for https://github.com/matrix-org/synapse/issues/7360
  36. # FIXME remove this when the above is fixed
  37. self.replicate()
  38. # check we're testing what we think we are: no rows should yet have been
  39. # received
  40. self.assertEqual([], self.test_handler.received_rdata_rows)
  41. # now reconnect to pull the updates
  42. self.reconnect()
  43. self.replicate()
  44. # we should have received all the expected rows in the right order
  45. received_rows = self.test_handler.received_rdata_rows
  46. for t in updates:
  47. (stream_name, token, row) = received_rows.pop(0)
  48. self.assertEqual(stream_name, AccountDataStream.NAME)
  49. self.assertIsInstance(row, AccountDataStream.AccountDataStreamRow)
  50. self.assertEqual(row.data_type, t)
  51. self.assertEqual(row.room_id, "test_room")
  52. (stream_name, token, row) = received_rows.pop(0)
  53. self.assertIsInstance(row, AccountDataStream.AccountDataStreamRow)
  54. self.assertEqual(row.data_type, "m.global")
  55. self.assertIsNone(row.room_id)
  56. self.assertEqual([], received_rows)
  57. def test_update_function_global_account_data_limit(self):
  58. """Test replication with many global account data updates"""
  59. store = self.hs.get_datastore()
  60. # generate lots of account data updates
  61. updates = []
  62. for i in range(_STREAM_UPDATE_TARGET_ROW_COUNT + 5):
  63. update = "m.test_type.%i" % (i,)
  64. self.get_success(store.add_account_data_for_user("test_user", update, {}))
  65. updates.append(update)
  66. # also one per-room update
  67. self.get_success(
  68. store.add_account_data_to_room("test_user", "test_room", "m.per_room", {})
  69. )
  70. # tell the notifier to catch up to avoid duplicate rows.
  71. # workaround for https://github.com/matrix-org/synapse/issues/7360
  72. # FIXME remove this when the above is fixed
  73. self.replicate()
  74. # check we're testing what we think we are: no rows should yet have been
  75. # received
  76. self.assertEqual([], self.test_handler.received_rdata_rows)
  77. # now reconnect to pull the updates
  78. self.reconnect()
  79. self.replicate()
  80. # we should have received all the expected rows in the right order
  81. received_rows = self.test_handler.received_rdata_rows
  82. for t in updates:
  83. (stream_name, token, row) = received_rows.pop(0)
  84. self.assertEqual(stream_name, AccountDataStream.NAME)
  85. self.assertIsInstance(row, AccountDataStream.AccountDataStreamRow)
  86. self.assertEqual(row.data_type, t)
  87. self.assertIsNone(row.room_id)
  88. (stream_name, token, row) = received_rows.pop(0)
  89. self.assertIsInstance(row, AccountDataStream.AccountDataStreamRow)
  90. self.assertEqual(row.data_type, "m.per_room")
  91. self.assertEqual(row.room_id, "test_room")
  92. self.assertEqual([], received_rows)