Skip to content

Commit 64bfed4

Browse files
committed
test: add unit tests for nested_zip_loader
1 parent 652d078 commit 64bfed4

4 files changed

Lines changed: 85 additions & 341 deletions

File tree

tests/conftest.py

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

tests/test_local.py

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

tests/test_nested_zip_loader.py

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

0 commit comments

Comments
 (0)