Skip to content
Merged
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
4 changes: 4 additions & 0 deletions src/sqlbuild/cli/entry/constants.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
"""Stable constants for the CLI process entrypoint."""

NATIVE_ALLOCATOR_PURGE_DELAY_VARIABLE: str = "MIMALLOC_PURGE_DELAY"
NATIVE_ALLOCATOR_PURGE_DELAY_MILLISECONDS: str = "10"
13 changes: 11 additions & 2 deletions src/sqlbuild/cli/entry/main/entry.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,20 @@
"""Public SQLBuild CLI process entrypoint."""

import os
from collections.abc import Sequence

from sqlbuild.cli.commands.main.entrypoint.entry import main as _main
from sqlbuild.cli.entry.constants import (
NATIVE_ALLOCATOR_PURGE_DELAY_MILLISECONDS,
NATIVE_ALLOCATOR_PURGE_DELAY_VARIABLE,
)


def main(argv: Sequence[str] | None = None) -> int:
"""Run the SQLBuild CLI with optional explicit arguments."""

return _main(argv)
os.environ.setdefault(
NATIVE_ALLOCATOR_PURGE_DELAY_VARIABLE, NATIVE_ALLOCATOR_PURGE_DELAY_MILLISECONDS
)
from sqlbuild.cli.commands.main.entrypoint.entry import main as run_cli

return run_cli(argv)
Original file line number Diff line number Diff line change
Expand Up @@ -809,3 +809,10 @@ class DiscoveryWorkTestCase:
command: tuple[str, ...]
expected_exit_code: int
expected_eager_output_column_scans: int


@dataclass(frozen=True)
class NativeAllocatorEntryTestCase:
description: str
inherited_environment: tuple[tuple[str, str], ...]
expected_purge_delay: str
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
"""Fresh-process integration coverage for the native allocator entrypoint default."""

from __future__ import annotations

import json
import os
import subprocess
import sys

import pytest

from tests.integration.src.sqlbuild.cli.commands.main._test_types import (
NativeAllocatorEntryTestCase,
)

_PURGE_DELAY_VARIABLE: str = "MIMALLOC_PURGE_DELAY"
_SCRIPT: str = """
import json
import os
import sys
from sqlbuild.cli.entry.main.entry import main

loaded_at_import = "sqlbuild._native" in sys.modules
exit_code = main(["--version"])
print(json.dumps({
"loaded_at_import": loaded_at_import,
"loaded_after_main": "sqlbuild._native" in sys.modules,
"purge_delay": os.environ.get("MIMALLOC_PURGE_DELAY"),
"exit_code": exit_code,
}))
"""


@pytest.mark.parametrize(
"test_case",
[
NativeAllocatorEntryTestCase(
description="unset purge delay defaults to ten milliseconds",
inherited_environment=(),
expected_purge_delay="10",
),
NativeAllocatorEntryTestCase(
description="user purge delay is preserved",
inherited_environment=((_PURGE_DELAY_VARIABLE, "250"),),
expected_purge_delay="250",
),
],
ids=lambda case: case.description,
)
def test_given_fresh_process_when_running_cli_entry_then_purge_delay_is_set_before_native_load(
test_case: NativeAllocatorEntryTestCase,
) -> None:
environment: dict[str, str] = os.environ.copy()
environment.pop(_PURGE_DELAY_VARIABLE, None)
environment.update(test_case.inherited_environment)

result: subprocess.CompletedProcess[str] = subprocess.run(
[sys.executable, "-c", _SCRIPT],
check=True,
capture_output=True,
env=environment,
text=True,
)
payload: dict[str, object] = json.loads(result.stdout.strip().splitlines()[-1])

assert payload == {
"loaded_at_import": False,
"loaded_after_main": True,
"purge_delay": test_case.expected_purge_delay,
"exit_code": 0,
}


if __name__ == "__main__":
pytest.main([__file__, "-vv"])
Loading