test_linearizer.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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 six.moves import range
  17. from twisted.internet import defer, reactor
  18. from twisted.internet.defer import CancelledError
  19. from synapse.util import Clock, logcontext
  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. def test_lots_of_queued_things(self):
  36. # we have one slow thing, and lots of fast things queued up behind it.
  37. # it should *not* explode the stack.
  38. linearizer = Linearizer()
  39. @defer.inlineCallbacks
  40. def func(i, sleep=False):
  41. with logcontext.LoggingContext("func(%s)" % i) as lc:
  42. with (yield linearizer.queue("")):
  43. self.assertEqual(logcontext.LoggingContext.current_context(), lc)
  44. if sleep:
  45. yield Clock(reactor).sleep(0)
  46. self.assertEqual(logcontext.LoggingContext.current_context(), lc)
  47. func(0, sleep=True)
  48. for i in range(1, 100):
  49. func(i)
  50. return func(1000)
  51. @defer.inlineCallbacks
  52. def test_multiple_entries(self):
  53. limiter = Linearizer(max_count=3)
  54. key = object()
  55. d1 = limiter.queue(key)
  56. cm1 = yield d1
  57. d2 = limiter.queue(key)
  58. cm2 = yield d2
  59. d3 = limiter.queue(key)
  60. cm3 = yield d3
  61. d4 = limiter.queue(key)
  62. self.assertFalse(d4.called)
  63. d5 = limiter.queue(key)
  64. self.assertFalse(d5.called)
  65. with cm1:
  66. self.assertFalse(d4.called)
  67. self.assertFalse(d5.called)
  68. cm4 = yield d4
  69. self.assertFalse(d5.called)
  70. with cm3:
  71. self.assertFalse(d5.called)
  72. cm5 = yield d5
  73. with cm2:
  74. pass
  75. with cm4:
  76. pass
  77. with cm5:
  78. pass
  79. d6 = limiter.queue(key)
  80. with (yield d6):
  81. pass
  82. @defer.inlineCallbacks
  83. def test_cancellation(self):
  84. linearizer = Linearizer()
  85. key = object()
  86. d1 = linearizer.queue(key)
  87. cm1 = yield d1
  88. d2 = linearizer.queue(key)
  89. self.assertFalse(d2.called)
  90. d3 = linearizer.queue(key)
  91. self.assertFalse(d3.called)
  92. d2.cancel()
  93. with cm1:
  94. pass
  95. self.assertTrue(d2.called)
  96. try:
  97. yield d2
  98. self.fail("Expected d2 to raise CancelledError")
  99. except CancelledError:
  100. pass
  101. with (yield d3):
  102. pass