Funktionierender Prototyp des Serious Games zur Vermittlung von Wissen zu Software-Engineering-Arbeitsmodellen.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

live.py 2.5KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. from functools import partial
  2. from daphne.testing import DaphneProcess
  3. from django.contrib.staticfiles.handlers import ASGIStaticFilesHandler
  4. from django.core.exceptions import ImproperlyConfigured
  5. from django.db import connections
  6. from django.test.testcases import TransactionTestCase
  7. from django.test.utils import modify_settings
  8. from channels.routing import get_default_application
  9. def make_application(*, static_wrapper):
  10. # Module-level function for pickle-ability
  11. application = get_default_application()
  12. if static_wrapper is not None:
  13. application = static_wrapper(application)
  14. return application
  15. class ChannelsLiveServerTestCase(TransactionTestCase):
  16. """
  17. Does basically the same as TransactionTestCase but also launches a
  18. live Daphne server in a separate process, so
  19. that the tests may use another test framework, such as Selenium,
  20. instead of the built-in dummy client.
  21. """
  22. host = "localhost"
  23. ProtocolServerProcess = DaphneProcess
  24. static_wrapper = ASGIStaticFilesHandler
  25. serve_static = True
  26. @property
  27. def live_server_url(self):
  28. return "http://%s:%s" % (self.host, self._port)
  29. @property
  30. def live_server_ws_url(self):
  31. return "ws://%s:%s" % (self.host, self._port)
  32. def _pre_setup(self):
  33. for connection in connections.all():
  34. if self._is_in_memory_db(connection):
  35. raise ImproperlyConfigured(
  36. "ChannelLiveServerTestCase can not be used with in memory databases"
  37. )
  38. super(ChannelsLiveServerTestCase, self)._pre_setup()
  39. self._live_server_modified_settings = modify_settings(
  40. ALLOWED_HOSTS={"append": self.host}
  41. )
  42. self._live_server_modified_settings.enable()
  43. get_application = partial(
  44. make_application,
  45. static_wrapper=self.static_wrapper if self.serve_static else None,
  46. )
  47. self._server_process = self.ProtocolServerProcess(self.host, get_application)
  48. self._server_process.start()
  49. self._server_process.ready.wait()
  50. self._port = self._server_process.port.value
  51. def _post_teardown(self):
  52. self._server_process.terminate()
  53. self._server_process.join()
  54. self._live_server_modified_settings.disable()
  55. super(ChannelsLiveServerTestCase, self)._post_teardown()
  56. def _is_in_memory_db(self, connection):
  57. """
  58. Check if DatabaseWrapper holds in memory database.
  59. """
  60. if connection.vendor == "sqlite":
  61. return connection.is_in_memory_db()