1
0

__init__.py 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. # -*- coding: utf-8 -*-
  2. # Copyright 2019 The Matrix.org Foundation C.I.C.
  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. import sys
  16. from twisted.internet import epollreactor
  17. from twisted.internet.main import installReactor
  18. from synapse.config.homeserver import HomeServerConfig
  19. from synapse.util import Clock
  20. from tests.utils import default_config, setup_test_homeserver
  21. async def make_homeserver(reactor, config=None):
  22. """
  23. Make a Homeserver suitable for running benchmarks against.
  24. Args:
  25. reactor: A Twisted reactor to run under.
  26. config: A HomeServerConfig to use, or None.
  27. """
  28. cleanup_tasks = []
  29. clock = Clock(reactor)
  30. if not config:
  31. config = default_config("test")
  32. config_obj = HomeServerConfig()
  33. config_obj.parse_config_dict(config, "", "")
  34. hs = await setup_test_homeserver(
  35. cleanup_tasks.append, config=config_obj, reactor=reactor, clock=clock
  36. )
  37. stor = hs.get_datastore()
  38. # Run the database background updates.
  39. if hasattr(stor.db.updates, "do_next_background_update"):
  40. while not await stor.db.updates.has_completed_background_updates():
  41. await stor.db.updates.do_next_background_update(1)
  42. def cleanup():
  43. for i in cleanup_tasks:
  44. i()
  45. return hs, clock.sleep, cleanup
  46. def make_reactor():
  47. """
  48. Instantiate and install a Twisted reactor suitable for testing (i.e. not the
  49. default global one).
  50. """
  51. reactor = epollreactor.EPollReactor()
  52. if "twisted.internet.reactor" in sys.modules:
  53. del sys.modules["twisted.internet.reactor"]
  54. installReactor(reactor)
  55. return reactor