Skip to content

Commit 4d3a673

Browse files
committed
test: add unit tests for nested_zip_loader
1 parent 652d078 commit 4d3a673

2 files changed

Lines changed: 88 additions & 181 deletions

File tree

tests/test_nested_zip_loader.py

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
import importlib
2+
import multiprocessing
3+
import shutil
4+
import sys
5+
import tempfile
6+
import zipfile
7+
from pathlib import Path
8+
9+
import pytest
10+
11+
# We have to use importlib here because nested_zip_loader calls load_nested_zip
12+
# at IMPORT TIME, which causes us a world of hurt in these tests if we try to
13+
# import it "normally" here.
14+
LOADER_PATH = Path(__file__).parent.parent / "package_python_function" / "nested_zip_loader.py"
15+
PKG_NAME = "_test_nested_zip"
16+
17+
def _make_deps_zip(path: Path) -> None:
18+
with zipfile.ZipFile(path, "w") as zf:
19+
zf.writestr(f"{PKG_NAME}/__init__.py", "LOADED = True\n")
20+
21+
@pytest.fixture()
22+
def lambda_env(tmp_path, monkeypatch):
23+
"""Simulate a Lambda-like layout: a task dir with a package whose __init__.py
24+
is the nested_zip_loader code, and a .dependencies.zip with the 'real' code."""
25+
task_dir = tmp_path / "task"
26+
pkg_dir = task_dir / PKG_NAME
27+
pkg_dir.mkdir(parents=True)
28+
shutil.copy(LOADER_PATH, pkg_dir / "__init__.py")
29+
_make_deps_zip(pkg_dir / ".dependencies.zip")
30+
31+
tmp_dir = tmp_path / "tmp"
32+
tmp_dir.mkdir()
33+
monkeypatch.setenv("TMPDIR", str(tmp_dir))
34+
tempfile.tempdir = None
35+
36+
monkeypatch.syspath_prepend(str(task_dir))
37+
38+
yield tmp_path
39+
40+
sys.modules.pop(PKG_NAME, None)
41+
tempfile.tempdir = None
42+
43+
def test_cold_start_extracts(lambda_env):
44+
mod = importlib.import_module(PKG_NAME)
45+
assert mod.LOADED is True
46+
assert (lambda_env / "tmp" / "package-python-function").exists()
47+
48+
def test_warm_start_skips_extraction(lambda_env):
49+
target_pkg = lambda_env / "tmp" / "package-python-function" / PKG_NAME
50+
target_pkg.mkdir(parents=True)
51+
(target_pkg / "__init__.py").write_text("LOADED = 'warm'\n")
52+
53+
mod = importlib.import_module(PKG_NAME)
54+
assert mod.LOADED == "warm"
55+
56+
def test_stale_staging_cleaned(lambda_env):
57+
staging = lambda_env / "tmp" / ".stage.package-python-function"
58+
staging.mkdir(parents=True)
59+
(staging / "stale.txt").write_text("leftover")
60+
61+
importlib.import_module(PKG_NAME)
62+
assert not staging.exists()
63+
64+
def _fork_worker(results, index):
65+
import importlib
66+
import sys
67+
68+
sys.modules.pop(PKG_NAME, None)
69+
try:
70+
mod = importlib.import_module(PKG_NAME)
71+
results[index] = mod.LOADED is True
72+
except Exception as e:
73+
print(f"Worker {index} failed: {e}")
74+
results[index] = False
75+
76+
def test_concurrent_no_race(lambda_env):
77+
"""Two forked processes race to extract; both should succeed."""
78+
ctx = multiprocessing.get_context("fork")
79+
results = ctx.Array("b", [0, 0])
80+
81+
procs = [ctx.Process(target=_fork_worker, args=(results, i)) for i in range(2)]
82+
for p in procs:
83+
p.start()
84+
for p in procs:
85+
p.join(timeout=10)
86+
87+
assert all(results), f"Workers returned: {list(results)}"
88+
assert (lambda_env / "tmp" / "package-python-function").exists()

tests/test_package_python_function.py

Lines changed: 0 additions & 181 deletions
This file was deleted.

0 commit comments

Comments
 (0)