Skip to content

Commit 20ab97f

Browse files
committed
Add a test which verifies lazy file reading
Create a directory of 2000 instance files, then iterate through them and verify that the number of open files never exceeds 2000 at any point during iteration.
1 parent 3d49748 commit 20ab97f

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

tests/unit/test_lazy_file_handling.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import os
2+
3+
import pytest
4+
from click.testing import CliRunner
5+
6+
from check_jsonschema.cli.main_command import build_checker
7+
from check_jsonschema.cli.main_command import main as cli_main
8+
9+
10+
@pytest.fixture
11+
def runner() -> CliRunner:
12+
return CliRunner(mix_stderr=False)
13+
14+
15+
@pytest.mark.skipif(os.name != "posix", reason="test requires use of /proc/self/")
16+
def test_open_file_usage_never_exceeds_1000(runner, monkeypatch, tmp_path):
17+
schema_path = tmp_path / "schema.json"
18+
schema_path.write_text("{}")
19+
20+
args = [
21+
"--schemafile",
22+
str(schema_path),
23+
]
24+
25+
for i in range(2000):
26+
instance_path = tmp_path / f"file{i}.json"
27+
instance_path.write_text("{}")
28+
args.append(str(instance_path))
29+
30+
checker = None
31+
32+
def fake_execute(argv):
33+
nonlocal checker
34+
checker = build_checker(argv)
35+
36+
monkeypatch.setattr("check_jsonschema.cli.main_command.execute", fake_execute)
37+
res = runner.invoke(cli_main, args)
38+
assert res.exit_code == 0, res.stderr
39+
40+
assert checker is not None
41+
assert len(os.listdir("/proc/self/fd")) < 2000
42+
for _fname, _data in checker._instance_loader.iter_files():
43+
assert len(os.listdir("/proc/self/fd")), 2000

0 commit comments

Comments
 (0)