|
| 1 | +from typing import Any |
| 2 | + |
| 3 | +from mineru.utils import model_utils |
| 4 | + |
| 5 | + |
| 6 | +def test_trim_process_memory_invokes_malloc_trim(monkeypatch: Any) -> None: |
| 7 | + calls = [] |
| 8 | + |
| 9 | + def fake_malloc_trim(padding: int) -> int: |
| 10 | + calls.append(padding) |
| 11 | + return 1 |
| 12 | + |
| 13 | + monkeypatch.setattr(model_utils, "_get_malloc_trim", lambda: fake_malloc_trim) |
| 14 | + |
| 15 | + model_utils.trim_process_memory() |
| 16 | + |
| 17 | + assert calls == [0] |
| 18 | + |
| 19 | + |
| 20 | +def test_trim_process_memory_is_optional(monkeypatch: Any) -> None: |
| 21 | + monkeypatch.setattr(model_utils, "_get_malloc_trim", lambda: None) |
| 22 | + |
| 23 | + model_utils.trim_process_memory() |
| 24 | + |
| 25 | + |
| 26 | +def test_clean_memory_runs_process_heap_trim(monkeypatch: Any) -> None: |
| 27 | + calls = [] |
| 28 | + monkeypatch.setattr(model_utils, "trim_process_memory", lambda: calls.append(True)) |
| 29 | + |
| 30 | + model_utils.clean_memory("cpu") |
| 31 | + |
| 32 | + assert calls == [True] |
| 33 | + |
| 34 | + |
| 35 | +def test_get_malloc_trim_configures_available_symbol(monkeypatch: Any) -> None: |
| 36 | + class FakeTrim: |
| 37 | + argtypes = None |
| 38 | + restype = None |
| 39 | + |
| 40 | + def __call__(self, padding: int) -> int: |
| 41 | + return padding |
| 42 | + |
| 43 | + fake_trim = FakeTrim() |
| 44 | + fake_libc = type("FakeLibc", (), {"malloc_trim": fake_trim})() |
| 45 | + monkeypatch.setattr(model_utils.sys, "platform", "linux") |
| 46 | + monkeypatch.setattr(model_utils.ctypes, "CDLL", lambda _: fake_libc) |
| 47 | + model_utils._get_malloc_trim.cache_clear() |
| 48 | + |
| 49 | + try: |
| 50 | + assert model_utils._get_malloc_trim() is fake_trim |
| 51 | + assert fake_trim.argtypes == [model_utils.ctypes.c_size_t] |
| 52 | + assert fake_trim.restype is model_utils.ctypes.c_int |
| 53 | + finally: |
| 54 | + model_utils._get_malloc_trim.cache_clear() |
0 commit comments