Skip to content

Commit

Permalink
Allow to start LiveServerThread separately from init
Browse files Browse the repository at this point in the history
  • Loading branch information
samamorgan authored and bluetech committed Oct 25, 2023
1 parent bf9c965 commit 73b2ea5
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions pytest_django/live_server_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class LiveServer:
The ``live_server`` fixture handles creation and stopping.
"""

def __init__(self, addr: str) -> None:
def __init__(self, addr: str, *, start: bool = True) -> None:
from django.db import connections
from django.test.testcases import LiveServerThread
from django.test.utils import modify_settings
Expand All @@ -20,8 +20,6 @@ def __init__(self, addr: str) -> None:
# If using in-memory sqlite databases, pass the connections to
# the server thread.
if conn.vendor == "sqlite" and conn.is_in_memory_db():
# Explicitly enable thread-shareability for this connection.
conn.inc_thread_sharing()
connections_override[conn.alias] = conn

liveserver_kwargs["connections_override"] = connections_override
Expand Down Expand Up @@ -51,6 +49,16 @@ def __init__(self, addr: str) -> None:
# `_live_server_helper`.

self.thread.daemon = True

if start:
self.start()

def start(self) -> None:
"""Start the server"""
for conn in self.thread.connections_override.values():
# Explicitly enable thread-shareability for this connection.
conn.inc_thread_sharing()

self.thread.start()
self.thread.is_ready.wait()

Expand Down

0 comments on commit 73b2ea5

Please sign in to comment.