Unverified Commit 296b7b1d authored by Nicola Soranzo's avatar Nicola Soranzo
Browse files

Fix ``test_application_stack_post_fork`` unit test

Make the late postfork thread a class attribute, and explicitly wait
for it in the test. Fix test failure where the postfork thread was
executed after the test finished.
parent 95afc66c
Loading
Loading
Loading
Loading
+3 −2
Original line number Diff line number Diff line
@@ -199,6 +199,7 @@ class GunicornApplicationStack(ApplicationStack):
    postfork_functions: List[Callable] = []
    # Will be set to True by external hook
    late_postfork_event = threading.Event()
    late_postfork_thread: threading.Thread

    @classmethod
    def register_postfork_function(cls, f, *args, **kwargs):
@@ -222,8 +223,8 @@ class GunicornApplicationStack(ApplicationStack):
    def late_postfork(cls):
        # We can't run postfork functions immediately, because this is before the gunicorn `post_fork` hook runs,
        # and we depend on the `post_fork` hook to set a worker id.
        t = threading.Thread(target=cls.run_postfork)
        t.start()
        cls.late_postfork_thread = threading.Thread(target=cls.run_postfork)
        cls.late_postfork_thread.start()

    def log_startup(self):
        msg = [f"Galaxy server instance '{self.config.server_name}' is running"]
+5 −5
Original line number Diff line number Diff line
from gunicorn import SERVER_SOFTWARE

from galaxy.model.unittest_utils import GalaxyDataTestApp
from galaxy.tool_util.verify.wait import wait_on
from galaxy.web_stack import (
    application_stack_class,
    application_stack_instance,
@@ -31,8 +30,9 @@ def test_application_stack_post_fork(monkeypatch):
    monkeypatch.setattr(GunicornApplicationStack, "do_post_fork", True)
    stack_instance = application_stack_instance(app=app, config=app.config)
    assert isinstance(stack_instance, GunicornApplicationStack)
    stack_instance.register_postfork_function(lambda: stack_instance.set_postfork_server_name(app))
    GunicornApplicationStack.register_postfork_function(lambda: stack_instance.set_postfork_server_name(app))
    assert not app.config.server_name.endswith(".100")
    stack_instance.late_postfork()
    stack_instance.late_postfork_event.set()
    wait_on(lambda: app.config.server_name.endswith(".100") or None, desc="server name to change", timeout=1, delta=0.1)
    GunicornApplicationStack.late_postfork()
    GunicornApplicationStack.late_postfork_event.set()
    GunicornApplicationStack.late_postfork_thread.join()
    assert app.config.server_name.endswith(".100")