Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions news/+harness-freeze-heap.performance.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
`AppHarness` freezes the objects the started app built, so a full garbage collection during an integration test no longer walks the whole compiled app and stalls the backend for seconds.
7 changes: 7 additions & 0 deletions reflex/testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import contextvars
import dataclasses
import functools
import gc
import inspect
import logging
import os
Expand Down Expand Up @@ -463,6 +464,10 @@ def start(self) -> Self:
self._start_backend()
self._start_frontend()
self._wait_frontend()
# The app lives until stop(): full collections skip it instead of walking
# it, which stalls the backend's event loop in a large app.
gc.collect()
gc.freeze()
return self

@staticmethod
Expand Down Expand Up @@ -492,6 +497,8 @@ def __enter__(self) -> Self:

def stop(self) -> None:
"""Stop the frontend and backend servers."""
# First, so the collector can free this app even if a step below raises.
gc.unfreeze()
try:
import psutil
except ImportError as exc:
Expand Down
69 changes: 69 additions & 0 deletions tests/units/test_testing.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""Unit tests for the included testing tools."""

import gc
import sys
from types import ModuleType, SimpleNamespace
from unittest import mock
Expand Down Expand Up @@ -58,6 +59,74 @@ class State(rx.State):
assert harness.frontend_process.poll() is not None


def _stub_start_steps(monkeypatch, harness: AppHarness, step) -> None:
"""Replace the steps of `AppHarness.start()` with one stand-in.

Args:
monkeypatch: pytest monkeypatch fixture
harness: The harness to stub.
step: The stand-in each step calls.
"""
for name in (
"_initialize_app",
"_start_backend",
"_start_frontend",
"_wait_frontend",
):
monkeypatch.setattr(harness, name, step)


def test_app_harness_freezes_the_started_app_until_stopped(tmp_path, monkeypatch):
"""The heap is frozen once the app has started, and unfrozen when it stops.

Args:
tmp_path: pytest tmp_path fixture
monkeypatch: pytest monkeypatch fixture
"""
harness = AppHarness.create(root=tmp_path)
frozen_while_starting = []
_stub_start_steps(
monkeypatch,
harness,
lambda: frozen_while_starting.append(gc.get_freeze_count()),
)
monkeypatch.setattr(harness, "_reload_state_module", lambda: None)

try:
harness.start()
frozen_while_running = gc.get_freeze_count()
finally:
harness.stop()

assert frozen_while_starting == [0, 0, 0, 0]
assert frozen_while_running > 0
assert gc.get_freeze_count() == 0


def test_app_harness_unfreezes_when_a_stop_step_raises(tmp_path, monkeypatch):
"""A failing cleanup step does not leave the heap frozen.

Args:
tmp_path: pytest tmp_path fixture
monkeypatch: pytest monkeypatch fixture
"""
harness = AppHarness.create(root=tmp_path)
_stub_start_steps(monkeypatch, harness, lambda: None)

def failing_cleanup() -> None:
msg = "cleanup failed"
raise RuntimeError(msg)

monkeypatch.setattr(harness, "_reload_state_module", failing_cleanup)
harness.start()

with pytest.raises(RuntimeError) as exc_info:
harness.stop()

assert exc_info.value.args == ("cleanup failed",)
assert gc.get_freeze_count() == 0


@pytest.fixture
def harness_mocks(monkeypatch):
"""Common mocks for AppHarness initialization tests.
Expand Down
Loading