|
| 1 | +"""Tests for recursion depth protection in hash function.""" |
| 2 | + |
| 3 | +from datetime import timedelta |
| 4 | + |
| 5 | +import pytest |
| 6 | + |
| 7 | +from cachier import cachier |
| 8 | + |
| 9 | + |
| 10 | +@pytest.mark.parametrize( |
| 11 | + "backend", |
| 12 | + [ |
| 13 | + pytest.param("memory", marks=pytest.mark.memory), |
| 14 | + pytest.param("pickle", marks=pytest.mark.pickle), |
| 15 | + ], |
| 16 | +) |
| 17 | +def test_moderately_nested_structures_work(backend, tmp_path): |
| 18 | + """Verify that moderately nested structures (< 100 levels) work fine.""" |
| 19 | + call_count = 0 |
| 20 | + |
| 21 | + decorator_kwargs = {"backend": backend, "stale_after": timedelta(seconds=120)} |
| 22 | + if backend == "pickle": |
| 23 | + decorator_kwargs["cache_dir"] = tmp_path |
| 24 | + |
| 25 | + @cachier(**decorator_kwargs) |
| 26 | + def process_nested(data): |
| 27 | + nonlocal call_count |
| 28 | + call_count += 1 |
| 29 | + return "processed" |
| 30 | + |
| 31 | + # Create a nested structure with 50 levels (well below the 100 limit) |
| 32 | + nested_list = [] |
| 33 | + current = nested_list |
| 34 | + for _ in range(50): |
| 35 | + inner = [] |
| 36 | + current.append(inner) |
| 37 | + current = inner |
| 38 | + current.append("leaf") |
| 39 | + |
| 40 | + # Should work without issues |
| 41 | + result1 = process_nested(nested_list) |
| 42 | + assert result1 == "processed" |
| 43 | + assert call_count == 1 |
| 44 | + |
| 45 | + # Second call should hit cache |
| 46 | + result2 = process_nested(nested_list) |
| 47 | + assert result2 == "processed" |
| 48 | + assert call_count == 1 |
| 49 | + |
| 50 | + process_nested.clear_cache() |
| 51 | + |
| 52 | + |
| 53 | +@pytest.mark.parametrize( |
| 54 | + "backend", |
| 55 | + [ |
| 56 | + pytest.param("memory", marks=pytest.mark.memory), |
| 57 | + pytest.param("pickle", marks=pytest.mark.pickle), |
| 58 | + ], |
| 59 | +) |
| 60 | +def test_deeply_nested_structures_raise_error(backend, tmp_path): |
| 61 | + """Verify that deeply nested structures (> 100 levels) raise RecursionError.""" |
| 62 | + decorator_kwargs = {"backend": backend, "stale_after": timedelta(seconds=120)} |
| 63 | + if backend == "pickle": |
| 64 | + decorator_kwargs["cache_dir"] = tmp_path |
| 65 | + |
| 66 | + @cachier(**decorator_kwargs) |
| 67 | + def process_nested(data): |
| 68 | + return "processed" |
| 69 | + |
| 70 | + # Create a nested structure with 150 levels (exceeds the 100 limit) |
| 71 | + nested_list = [] |
| 72 | + current = nested_list |
| 73 | + for _ in range(150): |
| 74 | + inner = [] |
| 75 | + current.append(inner) |
| 76 | + current = inner |
| 77 | + current.append("leaf") |
| 78 | + |
| 79 | + # Should raise RecursionError with a clear message |
| 80 | + with pytest.raises( |
| 81 | + RecursionError, |
| 82 | + match=r"Maximum recursion depth \(100\) exceeded while hashing nested", |
| 83 | + ): |
| 84 | + process_nested(nested_list) |
| 85 | + |
| 86 | + |
| 87 | +@pytest.mark.parametrize( |
| 88 | + "backend", |
| 89 | + [ |
| 90 | + pytest.param("memory", marks=pytest.mark.memory), |
| 91 | + pytest.param("pickle", marks=pytest.mark.pickle), |
| 92 | + ], |
| 93 | +) |
| 94 | +def test_nested_dicts_respect_depth_limit(backend, tmp_path): |
| 95 | + """Verify that nested dictionaries also respect the depth limit.""" |
| 96 | + decorator_kwargs = {"backend": backend, "stale_after": timedelta(seconds=120)} |
| 97 | + if backend == "pickle": |
| 98 | + decorator_kwargs["cache_dir"] = tmp_path |
| 99 | + |
| 100 | + @cachier(**decorator_kwargs) |
| 101 | + def process_dict(data): |
| 102 | + return "processed" |
| 103 | + |
| 104 | + # Create nested dictionaries beyond the limit |
| 105 | + nested_dict = {} |
| 106 | + current = nested_dict |
| 107 | + for i in range(150): |
| 108 | + current[f"level_{i}"] = {} |
| 109 | + current = current[f"level_{i}"] |
| 110 | + current["leaf"] = "value" |
| 111 | + |
| 112 | + # Should raise RecursionError |
| 113 | + with pytest.raises( |
| 114 | + RecursionError, |
| 115 | + match=r"Maximum recursion depth \(100\) exceeded while hashing nested", |
| 116 | + ): |
| 117 | + process_dict(nested_dict) |
| 118 | + |
| 119 | + |
| 120 | +@pytest.mark.parametrize( |
| 121 | + "backend", |
| 122 | + [ |
| 123 | + pytest.param("memory", marks=pytest.mark.memory), |
| 124 | + pytest.param("pickle", marks=pytest.mark.pickle), |
| 125 | + ], |
| 126 | +) |
| 127 | +def test_nested_tuples_respect_depth_limit(backend, tmp_path): |
| 128 | + """Verify that nested tuples also respect the depth limit.""" |
| 129 | + decorator_kwargs = {"backend": backend, "stale_after": timedelta(seconds=120)} |
| 130 | + if backend == "pickle": |
| 131 | + decorator_kwargs["cache_dir"] = tmp_path |
| 132 | + |
| 133 | + @cachier(**decorator_kwargs) |
| 134 | + def process_tuple(data): |
| 135 | + return "processed" |
| 136 | + |
| 137 | + # Create nested tuples beyond the limit |
| 138 | + nested_tuple = ("leaf",) |
| 139 | + for _ in range(150): |
| 140 | + nested_tuple = (nested_tuple,) |
| 141 | + |
| 142 | + # Should raise RecursionError |
| 143 | + with pytest.raises( |
| 144 | + RecursionError, |
| 145 | + match=r"Maximum recursion depth \(100\) exceeded while hashing nested", |
| 146 | + ): |
| 147 | + process_tuple(nested_tuple) |
0 commit comments