test_liveserverthread.py 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. from django.db import DEFAULT_DB_ALIAS, connections
  2. from django.test import LiveServerTestCase, TestCase
  3. from django.test.testcases import LiveServerThread
  4. class LiveServerThreadTest(TestCase):
  5. def run_live_server_thread(self, connections_override=None):
  6. thread = LiveServerTestCase._create_server_thread(connections_override)
  7. thread.daemon = True
  8. thread.start()
  9. thread.is_ready.wait()
  10. thread.terminate()
  11. def test_closes_connections(self):
  12. conn = connections[DEFAULT_DB_ALIAS]
  13. # Pass a connection to the thread to check they are being closed.
  14. connections_override = {DEFAULT_DB_ALIAS: conn}
  15. conn.inc_thread_sharing()
  16. try:
  17. self.assertTrue(conn.is_usable())
  18. self.run_live_server_thread(connections_override)
  19. self.assertFalse(conn.is_usable())
  20. finally:
  21. conn.dec_thread_sharing()
  22. def test_server_class(self):
  23. class FakeServer:
  24. def __init__(*args, **kwargs):
  25. pass
  26. class MyServerThread(LiveServerThread):
  27. server_class = FakeServer
  28. class MyServerTestCase(LiveServerTestCase):
  29. server_thread_class = MyServerThread
  30. thread = MyServerTestCase._create_server_thread(None)
  31. server = thread._create_server()
  32. self.assertIs(type(server), FakeServer)