test_linearizer.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. # Copyright 2016 OpenMarket Ltd
  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, reactor
  16. from twisted.internet.defer import CancelledError
  17. from synapse.logging.context import LoggingContext, current_context
  18. from synapse.util import Clock
  19. from synapse.util.async_helpers import Linearizer
  20. from tests import unittest
  21. class LinearizerTestCase(unittest.TestCase):
  22. @defer.inlineCallbacks
  23. def test_linearizer(self):
  24. linearizer = Linearizer()
  25. key = object()
  26. d1 = linearizer.queue(key)
  27. cm1 = yield d1
  28. d2 = linearizer.queue(key)
  29. self.assertFalse(d2.called)
  30. with cm1:
  31. self.assertFalse(d2.called)
  32. with (yield d2):
  33. pass
  34. @defer.inlineCallbacks
  35. def test_linearizer_is_queued(self):
  36. linearizer = Linearizer()
  37. key = object()
  38. d1 = linearizer.queue(key)
  39. cm1 = yield d1
  40. # Since d1 gets called immediately, "is_queued" should return false.
  41. self.assertFalse(linearizer.is_queued(key))
  42. d2 = linearizer.queue(key)
  43. self.assertFalse(d2.called)
  44. # Now d2 is queued up behind successful completion of cm1
  45. self.assertTrue(linearizer.is_queued(key))
  46. with cm1:
  47. self.assertFalse(d2.called)
  48. # cm1 still not done, so d2 still queued.
  49. self.assertTrue(linearizer.is_queued(key))
  50. # And now d2 is called and nothing is in the queue again
  51. self.assertFalse(linearizer.is_queued(key))
  52. with (yield d2):
  53. self.assertFalse(linearizer.is_queued(key))
  54. self.assertFalse(linearizer.is_queued(key))
  55. def test_lots_of_queued_things(self):
  56. # we have one slow thing, and lots of fast things queued up behind it.
  57. # it should *not* explode the stack.
  58. linearizer = Linearizer()
  59. @defer.inlineCallbacks
  60. def func(i, sleep=False):
  61. with LoggingContext("func(%s)" % i) as lc:
  62. with (yield linearizer.queue("")):
  63. self.assertEqual(current_context(), lc)
  64. if sleep:
  65. yield Clock(reactor).sleep(0)
  66. self.assertEqual(current_context(), lc)
  67. func(0, sleep=True)
  68. for i in range(1, 100):
  69. func(i)
  70. return func(1000)
  71. @defer.inlineCallbacks
  72. def test_multiple_entries(self):
  73. limiter = Linearizer(max_count=3)
  74. key = object()
  75. d1 = limiter.queue(key)
  76. cm1 = yield d1
  77. d2 = limiter.queue(key)
  78. cm2 = yield d2
  79. d3 = limiter.queue(key)
  80. cm3 = yield d3
  81. d4 = limiter.queue(key)
  82. self.assertFalse(d4.called)
  83. d5 = limiter.queue(key)
  84. self.assertFalse(d5.called)
  85. with cm1:
  86. self.assertFalse(d4.called)
  87. self.assertFalse(d5.called)
  88. cm4 = yield d4
  89. self.assertFalse(d5.called)
  90. with cm3:
  91. self.assertFalse(d5.called)
  92. cm5 = yield d5
  93. with cm2:
  94. pass
  95. with cm4:
  96. pass
  97. with cm5:
  98. pass
  99. d6 = limiter.queue(key)
  100. with (yield d6):
  101. pass
  102. @defer.inlineCallbacks
  103. def test_cancellation(self):
  104. linearizer = Linearizer()
  105. key = object()
  106. d1 = linearizer.queue(key)
  107. cm1 = yield d1
  108. d2 = linearizer.queue(key)
  109. self.assertFalse(d2.called)
  110. d3 = linearizer.queue(key)
  111. self.assertFalse(d3.called)
  112. d2.cancel()
  113. with cm1:
  114. pass
  115. self.assertTrue(d2.called)
  116. try:
  117. yield d2
  118. self.fail("Expected d2 to raise CancelledError")
  119. except CancelledError:
  120. pass
  121. with (yield d3):
  122. pass