test_linearizer.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. # -*- coding: utf-8 -*-
  2. # Copyright 2016 OpenMarket Ltd
  3. # Copyright 2018 New Vector Ltd
  4. #
  5. # Licensed under the Apache License, Version 2.0 (the "License");
  6. # you may not use this file except in compliance with the License.
  7. # You may obtain a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing, software
  12. # distributed under the License is distributed on an "AS IS" BASIS,
  13. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. # See the License for the specific language governing permissions and
  15. # limitations under the License.
  16. from twisted.internet import defer, reactor
  17. from twisted.internet.defer import CancelledError
  18. from synapse.logging.context import LoggingContext, current_context
  19. from synapse.util import Clock
  20. from synapse.util.async_helpers import Linearizer
  21. from tests import unittest
  22. class LinearizerTestCase(unittest.TestCase):
  23. @defer.inlineCallbacks
  24. def test_linearizer(self):
  25. linearizer = Linearizer()
  26. key = object()
  27. d1 = linearizer.queue(key)
  28. cm1 = yield d1
  29. d2 = linearizer.queue(key)
  30. self.assertFalse(d2.called)
  31. with cm1:
  32. self.assertFalse(d2.called)
  33. with (yield d2):
  34. pass
  35. @defer.inlineCallbacks
  36. def test_linearizer_is_queued(self):
  37. linearizer = Linearizer()
  38. key = object()
  39. d1 = linearizer.queue(key)
  40. cm1 = yield d1
  41. # Since d1 gets called immediately, "is_queued" should return false.
  42. self.assertFalse(linearizer.is_queued(key))
  43. d2 = linearizer.queue(key)
  44. self.assertFalse(d2.called)
  45. # Now d2 is queued up behind successful completion of cm1
  46. self.assertTrue(linearizer.is_queued(key))
  47. with cm1:
  48. self.assertFalse(d2.called)
  49. # cm1 still not done, so d2 still queued.
  50. self.assertTrue(linearizer.is_queued(key))
  51. # And now d2 is called and nothing is in the queue again
  52. self.assertFalse(linearizer.is_queued(key))
  53. with (yield d2):
  54. self.assertFalse(linearizer.is_queued(key))
  55. self.assertFalse(linearizer.is_queued(key))
  56. def test_lots_of_queued_things(self):
  57. # we have one slow thing, and lots of fast things queued up behind it.
  58. # it should *not* explode the stack.
  59. linearizer = Linearizer()
  60. @defer.inlineCallbacks
  61. def func(i, sleep=False):
  62. with LoggingContext("func(%s)" % i) as lc:
  63. with (yield linearizer.queue("")):
  64. self.assertEqual(current_context(), lc)
  65. if sleep:
  66. yield Clock(reactor).sleep(0)
  67. self.assertEqual(current_context(), lc)
  68. func(0, sleep=True)
  69. for i in range(1, 100):
  70. func(i)
  71. return func(1000)
  72. @defer.inlineCallbacks
  73. def test_multiple_entries(self):
  74. limiter = Linearizer(max_count=3)
  75. key = object()
  76. d1 = limiter.queue(key)
  77. cm1 = yield d1
  78. d2 = limiter.queue(key)
  79. cm2 = yield d2
  80. d3 = limiter.queue(key)
  81. cm3 = yield d3
  82. d4 = limiter.queue(key)
  83. self.assertFalse(d4.called)
  84. d5 = limiter.queue(key)
  85. self.assertFalse(d5.called)
  86. with cm1:
  87. self.assertFalse(d4.called)
  88. self.assertFalse(d5.called)
  89. cm4 = yield d4
  90. self.assertFalse(d5.called)
  91. with cm3:
  92. self.assertFalse(d5.called)
  93. cm5 = yield d5
  94. with cm2:
  95. pass
  96. with cm4:
  97. pass
  98. with cm5:
  99. pass
  100. d6 = limiter.queue(key)
  101. with (yield d6):
  102. pass
  103. @defer.inlineCallbacks
  104. def test_cancellation(self):
  105. linearizer = Linearizer()
  106. key = object()
  107. d1 = linearizer.queue(key)
  108. cm1 = yield d1
  109. d2 = linearizer.queue(key)
  110. self.assertFalse(d2.called)
  111. d3 = linearizer.queue(key)
  112. self.assertFalse(d3.called)
  113. d2.cancel()
  114. with cm1:
  115. pass
  116. self.assertTrue(d2.called)
  117. try:
  118. yield d2
  119. self.fail("Expected d2 to raise CancelledError")
  120. except CancelledError:
  121. pass
  122. with (yield d3):
  123. pass