Skip to content
Merged
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
95 changes: 37 additions & 58 deletions tests/utils/test_manifest.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
# specific language governing permissions and limitations
# under the License.
# pylint: disable=redefined-outer-name,arguments-renamed,fixme
import importlib
from dataclasses import replace
from pathlib import Path
from tempfile import TemporaryDirectory
Expand Down Expand Up @@ -1560,53 +1559,45 @@ def test_clear_manifest_cache() -> None:
def test_manifest_cache_can_be_disabled_with_size_zero(monkeypatch: pytest.MonkeyPatch) -> None:
"""Test that manifest-cache-size=0 disables caching."""
monkeypatch.setenv("PYICEBERG_MANIFEST_CACHE_SIZE", "0")
importlib.reload(manifest_module)
monkeypatch.setattr(manifest_module, "_manifest_cache", manifest_module._ManifestCache())

try:
assert manifest_module._manifest_cache.maxsize == 0
assert len(manifest_module._manifest_cache) == 0
assert manifest_module._manifest_cache.maxsize == 0
assert len(manifest_module._manifest_cache) == 0

io = PyArrowFileIO()
io = PyArrowFileIO()

with TemporaryDirectory() as tmp_dir:
list_path = _create_test_manifest_list(manifest_module, io, tmp_dir, name="disabled", snapshot_id=1)
with TemporaryDirectory() as tmp_dir:
list_path = _create_test_manifest_list(manifest_module, io, tmp_dir, name="disabled", snapshot_id=1)

manifests_first_call = manifest_module._manifests(io, list_path)
manifests_second_call = manifest_module._manifests(io, list_path)
manifests_first_call = manifest_module._manifests(io, list_path)
manifests_second_call = manifest_module._manifests(io, list_path)

assert len(manifest_module._manifest_cache) == 0
assert manifests_first_call[0] is not manifests_second_call[0]
finally:
monkeypatch.delenv("PYICEBERG_MANIFEST_CACHE_SIZE", raising=False)
importlib.reload(manifest_module)
assert len(manifest_module._manifest_cache) == 0
assert manifests_first_call[0] is not manifests_second_call[0]


def test_manifest_cache_respects_positive_env_size(monkeypatch: pytest.MonkeyPatch) -> None:
"""Test that a positive manifest-cache-size enables a bounded cache."""
monkeypatch.setenv("PYICEBERG_MANIFEST_CACHE_SIZE", "1")
importlib.reload(manifest_module)
monkeypatch.setattr(manifest_module, "_manifest_cache", manifest_module._ManifestCache())

try:
assert manifest_module._manifest_cache.maxsize == 1
assert manifest_module._manifest_cache.maxsize == 1

io = PyArrowFileIO()
io = PyArrowFileIO()

with TemporaryDirectory() as tmp_dir:
first_list_path = _create_test_manifest_list(manifest_module, io, tmp_dir, name="first", snapshot_id=1)
second_list_path = _create_test_manifest_list(manifest_module, io, tmp_dir, name="second", snapshot_id=2)
with TemporaryDirectory() as tmp_dir:
first_list_path = _create_test_manifest_list(manifest_module, io, tmp_dir, name="first", snapshot_id=1)
second_list_path = _create_test_manifest_list(manifest_module, io, tmp_dir, name="second", snapshot_id=2)

manifests_first_call = manifest_module._manifests(io, first_list_path)
manifests_second_call = manifest_module._manifests(io, first_list_path)
manifests_first_call = manifest_module._manifests(io, first_list_path)
manifests_second_call = manifest_module._manifests(io, first_list_path)

assert manifests_first_call[0] is manifests_second_call[0]
assert len(manifest_module._manifest_cache) == 1
assert manifests_first_call[0] is manifests_second_call[0]
assert len(manifest_module._manifest_cache) == 1

manifest_module._manifests(io, second_list_path)
manifest_module._manifests(io, second_list_path)

assert len(manifest_module._manifest_cache) == 1
finally:
monkeypatch.delenv("PYICEBERG_MANIFEST_CACHE_SIZE", raising=False)
importlib.reload(manifest_module)
assert len(manifest_module._manifest_cache) == 1


def test_manifest_cache_reads_size_from_configuration_file(monkeypatch: pytest.MonkeyPatch, tmp_path: Path) -> None:
Expand All @@ -1617,47 +1608,35 @@ def test_manifest_cache_reads_size_from_configuration_file(monkeypatch: pytest.M

monkeypatch.delenv("PYICEBERG_MANIFEST_CACHE_SIZE", raising=False)
monkeypatch.setenv("PYICEBERG_HOME", str(config_dir))
importlib.reload(manifest_module)
monkeypatch.setattr(manifest_module, "_manifest_cache", manifest_module._ManifestCache())

try:
assert manifest_module._manifest_cache.maxsize == 2
assert manifest_module._manifest_cache.maxsize == 2

io = PyArrowFileIO()
io = PyArrowFileIO()

with TemporaryDirectory() as tmp_dir:
first_list_path = _create_test_manifest_list(manifest_module, io, tmp_dir, name="first", snapshot_id=1)
second_list_path = _create_test_manifest_list(manifest_module, io, tmp_dir, name="second", snapshot_id=2)
third_list_path = _create_test_manifest_list(manifest_module, io, tmp_dir, name="third", snapshot_id=3)
with TemporaryDirectory() as tmp_dir:
first_list_path = _create_test_manifest_list(manifest_module, io, tmp_dir, name="first", snapshot_id=1)
second_list_path = _create_test_manifest_list(manifest_module, io, tmp_dir, name="second", snapshot_id=2)
third_list_path = _create_test_manifest_list(manifest_module, io, tmp_dir, name="third", snapshot_id=3)

manifest_module._manifests(io, first_list_path)
manifest_module._manifests(io, second_list_path)
manifest_module._manifests(io, third_list_path)
manifest_module._manifests(io, first_list_path)
manifest_module._manifests(io, second_list_path)
manifest_module._manifests(io, third_list_path)

assert len(manifest_module._manifest_cache) == 2
finally:
monkeypatch.delenv("PYICEBERG_HOME", raising=False)
importlib.reload(manifest_module)
assert len(manifest_module._manifest_cache) == 2


def test_invalid_manifest_cache_size_raises_value_error(monkeypatch: pytest.MonkeyPatch) -> None:
"""Test that invalid manifest-cache-size values raise a helpful error."""
monkeypatch.setenv("PYICEBERG_MANIFEST_CACHE_SIZE", "not-an-int")

try:
with pytest.raises(ValueError, match="manifest-cache-size should be an integer or left unset"):
importlib.reload(manifest_module)
finally:
monkeypatch.delenv("PYICEBERG_MANIFEST_CACHE_SIZE", raising=False)
importlib.reload(manifest_module)
with pytest.raises(ValueError, match="manifest-cache-size should be an integer or left unset"):
manifest_module._ManifestCache()


def test_negative_manifest_cache_size_raises_value_error(monkeypatch: pytest.MonkeyPatch) -> None:
"""Test that negative manifest-cache-size values raise a helpful error."""
monkeypatch.setenv("PYICEBERG_MANIFEST_CACHE_SIZE", "-1")

try:
with pytest.raises(ValueError, match="manifest-cache-size should be a non-negative integer or left unset"):
importlib.reload(manifest_module)
finally:
monkeypatch.delenv("PYICEBERG_MANIFEST_CACHE_SIZE", raising=False)
importlib.reload(manifest_module)
with pytest.raises(ValueError, match="manifest-cache-size should be a non-negative integer or left unset"):
manifest_module._ManifestCache()