|
| 1 | +import pytest |
| 2 | + |
| 3 | +from databricks.sql.backend import reyden_warehouse_cache |
| 4 | +from databricks.sql.backend.reyden_warehouse_cache import ( |
| 5 | + _ReydenWarehouseCache, |
| 6 | + extract_warehouse_id, |
| 7 | +) |
| 8 | + |
| 9 | + |
| 10 | +class TestExtractWarehouseId: |
| 11 | + @pytest.mark.parametrize( |
| 12 | + "path, expected", |
| 13 | + [ |
| 14 | + ("/sql/1.0/warehouses/abc123", "abc123"), |
| 15 | + ("/sql/1.0/endpoints/def456", "def456"), |
| 16 | + ("/sql/1.0/warehouses/abc123?o=42", "abc123"), |
| 17 | + ("sql/1.0/warehouses/wh?param=1&o=2", "wh"), |
| 18 | + # All-purpose-compute cluster path — no warehouse id. |
| 19 | + ("/sql/protocolv1/o/1234567890/0101-cluster", None), |
| 20 | + ("", None), |
| 21 | + (None, None), |
| 22 | + ], |
| 23 | + ) |
| 24 | + def test_extract(self, path, expected): |
| 25 | + assert extract_warehouse_id(path) == expected |
| 26 | + |
| 27 | + |
| 28 | +class TestReydenWarehouseCacheClass: |
| 29 | + def test_mark_then_known(self): |
| 30 | + cache = _ReydenWarehouseCache() |
| 31 | + assert cache.is_known_reyden("host", "wh") is False |
| 32 | + cache.mark_reyden("host", "wh") |
| 33 | + assert cache.is_known_reyden("host", "wh") is True |
| 34 | + |
| 35 | + def test_host_case_insensitive(self): |
| 36 | + cache = _ReydenWarehouseCache() |
| 37 | + cache.mark_reyden("Host.Example.COM", "wh") |
| 38 | + assert cache.is_known_reyden("host.example.com", "wh") is True |
| 39 | + |
| 40 | + def test_distinct_hosts_do_not_collide(self): |
| 41 | + cache = _ReydenWarehouseCache() |
| 42 | + cache.mark_reyden("host-a", "wh") |
| 43 | + # Same warehouse id on a different host must not be treated as Reyden. |
| 44 | + assert cache.is_known_reyden("host-b", "wh") is False |
| 45 | + |
| 46 | + def test_distinct_warehouses_do_not_collide(self): |
| 47 | + cache = _ReydenWarehouseCache() |
| 48 | + cache.mark_reyden("host", "wh-a") |
| 49 | + assert cache.is_known_reyden("host", "wh-b") is False |
| 50 | + |
| 51 | + def test_entry_expires_and_is_evicted(self): |
| 52 | + cache = _ReydenWarehouseCache(ttl_seconds=0) |
| 53 | + cache.mark_reyden("host", "wh") |
| 54 | + # A zero TTL means the deadline is already in the past on read. |
| 55 | + assert cache.is_known_reyden("host", "wh") is False |
| 56 | + # Expired entry is evicted, not just reported absent. |
| 57 | + assert cache._expiry == {} |
| 58 | + |
| 59 | + def test_mark_sweeps_expired_entries(self): |
| 60 | + # A zero TTL makes the first entry expired by the time the second mark |
| 61 | + # runs, so the opportunistic sweep must purge it even though it was |
| 62 | + # never read back. |
| 63 | + cache = _ReydenWarehouseCache(ttl_seconds=0) |
| 64 | + cache.mark_reyden("host", "old") |
| 65 | + assert ("host", "old") in cache._expiry |
| 66 | + cache.mark_reyden("host", "new") |
| 67 | + assert ("host", "old") not in cache._expiry |
| 68 | + assert ("host", "new") in cache._expiry |
| 69 | + |
| 70 | + def test_mark_keeps_live_entries(self): |
| 71 | + # Live (unexpired) entries survive the sweep on a subsequent mark. |
| 72 | + cache = _ReydenWarehouseCache(ttl_seconds=3600) |
| 73 | + cache.mark_reyden("host", "a") |
| 74 | + cache.mark_reyden("host", "b") |
| 75 | + assert ("host", "a") in cache._expiry |
| 76 | + assert ("host", "b") in cache._expiry |
| 77 | + |
| 78 | + |
| 79 | +class TestModuleSingleton: |
| 80 | + def test_mark_and_clear(self): |
| 81 | + reyden_warehouse_cache.clear_cache() |
| 82 | + reyden_warehouse_cache.mark_reyden("host", "wh") |
| 83 | + assert reyden_warehouse_cache.is_known_reyden("host", "wh") is True |
| 84 | + reyden_warehouse_cache.clear_cache() |
| 85 | + assert reyden_warehouse_cache.is_known_reyden("host", "wh") is False |
0 commit comments