test_partial_state_events_tracker.py 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. # Copyright 2022 The Matrix.org Foundation C.I.C.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. from typing import Dict
  15. from unittest import mock
  16. from twisted.internet.defer import CancelledError, ensureDeferred
  17. from synapse.storage.util.partial_state_events_tracker import (
  18. PartialCurrentStateTracker,
  19. PartialStateEventsTracker,
  20. )
  21. from tests.test_utils import make_awaitable
  22. from tests.unittest import TestCase
  23. class PartialStateEventsTrackerTestCase(TestCase):
  24. def setUp(self) -> None:
  25. # the results to be returned by the mocked get_partial_state_events
  26. self._events_dict: Dict[str, bool] = {}
  27. async def get_partial_state_events(events):
  28. return {e: self._events_dict[e] for e in events}
  29. self.mock_store = mock.Mock(spec_set=["get_partial_state_events"])
  30. self.mock_store.get_partial_state_events.side_effect = get_partial_state_events
  31. self.tracker = PartialStateEventsTracker(self.mock_store)
  32. def test_does_not_block_for_full_state_events(self):
  33. self._events_dict = {"event1": False, "event2": False}
  34. self.successResultOf(
  35. ensureDeferred(self.tracker.await_full_state(["event1", "event2"]))
  36. )
  37. self.mock_store.get_partial_state_events.assert_called_once_with(
  38. ["event1", "event2"]
  39. )
  40. def test_blocks_for_partial_state_events(self):
  41. self._events_dict = {"event1": True, "event2": False}
  42. d = ensureDeferred(self.tracker.await_full_state(["event1", "event2"]))
  43. # there should be no result yet
  44. self.assertNoResult(d)
  45. # notifying that the event has been de-partial-stated should unblock
  46. self.tracker.notify_un_partial_stated("event1")
  47. self.successResultOf(d)
  48. def test_un_partial_state_race(self):
  49. # if the event is un-partial-stated between the initial check and the
  50. # registration of the listener, it should not block.
  51. self._events_dict = {"event1": True, "event2": False}
  52. async def get_partial_state_events(events):
  53. res = {e: self._events_dict[e] for e in events}
  54. # change the result for next time
  55. self._events_dict = {"event1": False, "event2": False}
  56. return res
  57. self.mock_store.get_partial_state_events.side_effect = get_partial_state_events
  58. self.successResultOf(
  59. ensureDeferred(self.tracker.await_full_state(["event1", "event2"]))
  60. )
  61. def test_un_partial_state_during_get_partial_state_events(self):
  62. # we should correctly handle a call to notify_un_partial_stated during the
  63. # second call to get_partial_state_events.
  64. self._events_dict = {"event1": True, "event2": False}
  65. async def get_partial_state_events1(events):
  66. self.mock_store.get_partial_state_events.side_effect = (
  67. get_partial_state_events2
  68. )
  69. return {e: self._events_dict[e] for e in events}
  70. async def get_partial_state_events2(events):
  71. self.tracker.notify_un_partial_stated("event1")
  72. self._events_dict["event1"] = False
  73. return {e: self._events_dict[e] for e in events}
  74. self.mock_store.get_partial_state_events.side_effect = get_partial_state_events1
  75. self.successResultOf(
  76. ensureDeferred(self.tracker.await_full_state(["event1", "event2"]))
  77. )
  78. def test_cancellation(self):
  79. self._events_dict = {"event1": True, "event2": False}
  80. d1 = ensureDeferred(self.tracker.await_full_state(["event1", "event2"]))
  81. self.assertNoResult(d1)
  82. d2 = ensureDeferred(self.tracker.await_full_state(["event1"]))
  83. self.assertNoResult(d2)
  84. d1.cancel()
  85. self.assertFailure(d1, CancelledError)
  86. # d2 should still be waiting!
  87. self.assertNoResult(d2)
  88. self.tracker.notify_un_partial_stated("event1")
  89. self.successResultOf(d2)
  90. class PartialCurrentStateTrackerTestCase(TestCase):
  91. def setUp(self) -> None:
  92. self.mock_store = mock.Mock(spec_set=["is_partial_state_room"])
  93. self.tracker = PartialCurrentStateTracker(self.mock_store)
  94. def test_does_not_block_for_full_state_rooms(self):
  95. self.mock_store.is_partial_state_room.return_value = make_awaitable(False)
  96. self.successResultOf(ensureDeferred(self.tracker.await_full_state("room_id")))
  97. def test_blocks_for_partial_room_state(self):
  98. self.mock_store.is_partial_state_room.return_value = make_awaitable(True)
  99. d = ensureDeferred(self.tracker.await_full_state("room_id"))
  100. # there should be no result yet
  101. self.assertNoResult(d)
  102. # notifying that the room has been de-partial-stated should unblock
  103. self.tracker.notify_un_partial_stated("room_id")
  104. self.successResultOf(d)
  105. def test_un_partial_state_race(self):
  106. # We should correctly handle race between awaiting the state and us
  107. # un-partialling the state
  108. async def is_partial_state_room(events):
  109. self.tracker.notify_un_partial_stated("room_id")
  110. return True
  111. self.mock_store.is_partial_state_room.side_effect = is_partial_state_room
  112. self.successResultOf(ensureDeferred(self.tracker.await_full_state("room_id")))
  113. def test_cancellation(self):
  114. self.mock_store.is_partial_state_room.return_value = make_awaitable(True)
  115. d1 = ensureDeferred(self.tracker.await_full_state("room_id"))
  116. self.assertNoResult(d1)
  117. d2 = ensureDeferred(self.tracker.await_full_state("room_id"))
  118. self.assertNoResult(d2)
  119. d1.cancel()
  120. self.assertFailure(d1, CancelledError)
  121. # d2 should still be waiting!
  122. self.assertNoResult(d2)
  123. self.tracker.notify_un_partial_stated("room_id")
  124. self.successResultOf(d2)