test_lock.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. # Copyright 2021 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 synapse.server import HomeServer
  15. from synapse.storage.databases.main.lock import _LOCK_TIMEOUT_MS
  16. from tests import unittest
  17. class LockTestCase(unittest.HomeserverTestCase):
  18. def prepare(self, reactor, clock, hs: HomeServer):
  19. self.store = hs.get_datastore()
  20. def test_simple_lock(self):
  21. """Test that we can take out a lock and that while we hold it nobody
  22. else can take it out.
  23. """
  24. # First to acquire this lock, so it should complete
  25. lock = self.get_success(self.store.try_acquire_lock("name", "key"))
  26. self.assertIsNotNone(lock)
  27. # Enter the context manager
  28. self.get_success(lock.__aenter__())
  29. # Attempting to acquire the lock again fails.
  30. lock2 = self.get_success(self.store.try_acquire_lock("name", "key"))
  31. self.assertIsNone(lock2)
  32. # Calling `is_still_valid` reports true.
  33. self.assertTrue(self.get_success(lock.is_still_valid()))
  34. # Drop the lock
  35. self.get_success(lock.__aexit__(None, None, None))
  36. # We can now acquire the lock again.
  37. lock3 = self.get_success(self.store.try_acquire_lock("name", "key"))
  38. self.assertIsNotNone(lock3)
  39. self.get_success(lock3.__aenter__())
  40. self.get_success(lock3.__aexit__(None, None, None))
  41. def test_maintain_lock(self):
  42. """Test that we don't time out locks while they're still active"""
  43. lock = self.get_success(self.store.try_acquire_lock("name", "key"))
  44. self.assertIsNotNone(lock)
  45. self.get_success(lock.__aenter__())
  46. # Wait for ages with the lock, we should not be able to get the lock.
  47. self.reactor.advance(5 * _LOCK_TIMEOUT_MS / 1000)
  48. lock2 = self.get_success(self.store.try_acquire_lock("name", "key"))
  49. self.assertIsNone(lock2)
  50. self.get_success(lock.__aexit__(None, None, None))
  51. def test_timeout_lock(self):
  52. """Test that we time out locks if they're not updated for ages"""
  53. lock = self.get_success(self.store.try_acquire_lock("name", "key"))
  54. self.assertIsNotNone(lock)
  55. self.get_success(lock.__aenter__())
  56. # We simulate the process getting stuck by cancelling the looping call
  57. # that keeps the lock active.
  58. lock._looping_call.stop()
  59. # Wait for the lock to timeout.
  60. self.reactor.advance(2 * _LOCK_TIMEOUT_MS / 1000)
  61. lock2 = self.get_success(self.store.try_acquire_lock("name", "key"))
  62. self.assertIsNotNone(lock2)
  63. self.assertFalse(self.get_success(lock.is_still_valid()))
  64. def test_drop(self):
  65. """Test that dropping the context manager means we stop renewing the lock"""
  66. lock = self.get_success(self.store.try_acquire_lock("name", "key"))
  67. self.assertIsNotNone(lock)
  68. del lock
  69. # Wait for the lock to timeout.
  70. self.reactor.advance(2 * _LOCK_TIMEOUT_MS / 1000)
  71. lock2 = self.get_success(self.store.try_acquire_lock("name", "key"))
  72. self.assertIsNotNone(lock2)
  73. def test_shutdown(self):
  74. """Test that shutting down Synapse releases the locks"""
  75. # Acquire two locks
  76. lock = self.get_success(self.store.try_acquire_lock("name", "key1"))
  77. self.assertIsNotNone(lock)
  78. lock2 = self.get_success(self.store.try_acquire_lock("name", "key2"))
  79. self.assertIsNotNone(lock2)
  80. # Now call the shutdown code
  81. self.get_success(self.store._on_shutdown())
  82. self.assertEqual(self.store._live_tokens, {})