|
| 1 | +"""Tests for TTL removal feature (issue #66).""" |
| 2 | + |
| 3 | +import time |
| 4 | +from uuid import uuid4 |
| 5 | + |
| 6 | +import pytest |
| 7 | +from langgraph.checkpoint.base import create_checkpoint, empty_checkpoint |
| 8 | + |
| 9 | +from langgraph.checkpoint.redis import AsyncRedisSaver, RedisSaver |
| 10 | + |
| 11 | + |
| 12 | +def test_ttl_removal_with_negative_one(redis_url: str) -> None: |
| 13 | + """Test that ttl_minutes=-1 removes TTL from keys.""" |
| 14 | + saver = RedisSaver(redis_url, ttl={"default_ttl": 1}) # 1 minute default TTL |
| 15 | + saver.setup() |
| 16 | + |
| 17 | + thread_id = str(uuid4()) |
| 18 | + checkpoint = create_checkpoint( |
| 19 | + checkpoint=empty_checkpoint(), channels={"messages": ["test"]}, step=1 |
| 20 | + ) |
| 21 | + checkpoint["channel_values"]["messages"] = ["test"] |
| 22 | + |
| 23 | + config = {"configurable": {"thread_id": thread_id, "checkpoint_ns": ""}} |
| 24 | + |
| 25 | + # Save checkpoint (will have TTL) |
| 26 | + saved_config = saver.put(config, checkpoint, {"source": "test", "step": 1}, {}) |
| 27 | + |
| 28 | + checkpoint_key = f"checkpoint:{thread_id}:__empty__:{saved_config['configurable']['checkpoint_id']}" |
| 29 | + |
| 30 | + # Verify TTL is set |
| 31 | + ttl = saver._redis.ttl(checkpoint_key) |
| 32 | + assert 50 <= ttl <= 60, f"TTL should be around 60 seconds, got {ttl}" |
| 33 | + |
| 34 | + # Remove TTL using -1 |
| 35 | + saver._apply_ttl_to_keys(checkpoint_key, ttl_minutes=-1) |
| 36 | + |
| 37 | + # Verify TTL is removed |
| 38 | + ttl_after = saver._redis.ttl(checkpoint_key) |
| 39 | + assert ttl_after == -1, "Key should be persistent after setting ttl_minutes=-1" |
| 40 | + |
| 41 | + |
| 42 | +def test_ttl_removal_with_related_keys(redis_url: str) -> None: |
| 43 | + """Test that TTL removal works for main key and related keys.""" |
| 44 | + saver = RedisSaver(redis_url, ttl={"default_ttl": 1}) |
| 45 | + saver.setup() |
| 46 | + |
| 47 | + thread_id = str(uuid4()) |
| 48 | + |
| 49 | + # Create a checkpoint with writes (to have related keys) |
| 50 | + checkpoint = create_checkpoint( |
| 51 | + checkpoint=empty_checkpoint(), channels={"messages": ["test"]}, step=1 |
| 52 | + ) |
| 53 | + checkpoint["channel_values"]["messages"] = ["test"] |
| 54 | + |
| 55 | + config = { |
| 56 | + "configurable": { |
| 57 | + "thread_id": thread_id, |
| 58 | + "checkpoint_ns": "", |
| 59 | + "checkpoint_id": "test-checkpoint", |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + # Save checkpoint and writes |
| 64 | + saved_config = saver.put(config, checkpoint, {"source": "test", "step": 1}, {}) |
| 65 | + saver.put_writes( |
| 66 | + saved_config, [("channel1", "value1"), ("channel2", "value2")], "task-1" |
| 67 | + ) |
| 68 | + |
| 69 | + # Get the keys |
| 70 | + checkpoint_key = f"checkpoint:{thread_id}:__empty__:{saved_config['configurable']['checkpoint_id']}" |
| 71 | + write_key1 = f"checkpoint_write:{thread_id}:__empty__:{saved_config['configurable']['checkpoint_id']}:task-1:0" |
| 72 | + write_key2 = f"checkpoint_write:{thread_id}:__empty__:{saved_config['configurable']['checkpoint_id']}:task-1:1" |
| 73 | + |
| 74 | + # All keys should have TTL |
| 75 | + assert 50 <= saver._redis.ttl(checkpoint_key) <= 60 |
| 76 | + assert 50 <= saver._redis.ttl(write_key1) <= 60 |
| 77 | + assert 50 <= saver._redis.ttl(write_key2) <= 60 |
| 78 | + |
| 79 | + # Remove TTL from all keys |
| 80 | + saver._apply_ttl_to_keys(checkpoint_key, [write_key1, write_key2], ttl_minutes=-1) |
| 81 | + |
| 82 | + # All keys should be persistent |
| 83 | + assert saver._redis.ttl(checkpoint_key) == -1 |
| 84 | + assert saver._redis.ttl(write_key1) == -1 |
| 85 | + assert saver._redis.ttl(write_key2) == -1 |
| 86 | + |
| 87 | + |
| 88 | +def test_no_ttl_means_persistent(redis_url: str) -> None: |
| 89 | + """Test that no TTL configuration means keys are persistent.""" |
| 90 | + # Create saver with no TTL config |
| 91 | + saver = RedisSaver(redis_url) # No TTL config |
| 92 | + saver.setup() |
| 93 | + |
| 94 | + thread_id = str(uuid4()) |
| 95 | + checkpoint = create_checkpoint( |
| 96 | + checkpoint=empty_checkpoint(), channels={"messages": ["test"]}, step=1 |
| 97 | + ) |
| 98 | + checkpoint["channel_values"]["messages"] = ["test"] |
| 99 | + |
| 100 | + config = {"configurable": {"thread_id": thread_id, "checkpoint_ns": ""}} |
| 101 | + |
| 102 | + # Save checkpoint |
| 103 | + saved_config = saver.put(config, checkpoint, {"source": "test", "step": 1}, {}) |
| 104 | + |
| 105 | + # Check TTL |
| 106 | + checkpoint_key = f"checkpoint:{thread_id}:__empty__:{saved_config['configurable']['checkpoint_id']}" |
| 107 | + ttl = saver._redis.ttl(checkpoint_key) |
| 108 | + |
| 109 | + # Should be -1 (persistent) when no TTL configured |
| 110 | + assert ttl == -1, "Key should be persistent when no TTL configured" |
| 111 | + |
| 112 | + |
| 113 | +def test_ttl_removal_preserves_data(redis_url: str) -> None: |
| 114 | + """Test that removing TTL doesn't affect the data.""" |
| 115 | + saver = RedisSaver(redis_url, ttl={"default_ttl": 1}) |
| 116 | + saver.setup() |
| 117 | + |
| 118 | + thread_id = str(uuid4()) |
| 119 | + checkpoint = create_checkpoint( |
| 120 | + checkpoint=empty_checkpoint(), channels={"messages": ["original data"]}, step=1 |
| 121 | + ) |
| 122 | + checkpoint["channel_values"]["messages"] = ["original data"] |
| 123 | + |
| 124 | + config = {"configurable": {"thread_id": thread_id, "checkpoint_ns": ""}} |
| 125 | + |
| 126 | + # Save checkpoint |
| 127 | + saved_config = saver.put(config, checkpoint, {"source": "test", "step": 1}, {}) |
| 128 | + |
| 129 | + # Load data before TTL removal |
| 130 | + loaded_before = saver.get_tuple(saved_config) |
| 131 | + assert loaded_before.checkpoint["channel_values"]["messages"] == ["original data"] |
| 132 | + |
| 133 | + # Remove TTL |
| 134 | + checkpoint_key = f"checkpoint:{thread_id}:__empty__:{saved_config['configurable']['checkpoint_id']}" |
| 135 | + saver._apply_ttl_to_keys(checkpoint_key, ttl_minutes=-1) |
| 136 | + |
| 137 | + # Load data after TTL removal |
| 138 | + loaded_after = saver.get_tuple(saved_config) |
| 139 | + assert loaded_after.checkpoint["channel_values"]["messages"] == ["original data"] |
| 140 | + |
| 141 | + # Verify TTL is removed |
| 142 | + assert saver._redis.ttl(checkpoint_key) == -1 |
| 143 | + |
| 144 | + |
| 145 | +@pytest.mark.asyncio |
| 146 | +async def test_async_ttl_removal(redis_url: str) -> None: |
| 147 | + """Test TTL removal with async saver.""" |
| 148 | + async with AsyncRedisSaver.from_conn_string( |
| 149 | + redis_url, ttl={"default_ttl": 1} |
| 150 | + ) as saver: |
| 151 | + thread_id = str(uuid4()) |
| 152 | + checkpoint = create_checkpoint( |
| 153 | + checkpoint=empty_checkpoint(), channels={"messages": ["async test"]}, step=1 |
| 154 | + ) |
| 155 | + checkpoint["channel_values"]["messages"] = ["async test"] |
| 156 | + |
| 157 | + config = {"configurable": {"thread_id": thread_id, "checkpoint_ns": ""}} |
| 158 | + |
| 159 | + # Save checkpoint |
| 160 | + saved_config = await saver.aput( |
| 161 | + config, checkpoint, {"source": "test", "step": 1}, {} |
| 162 | + ) |
| 163 | + |
| 164 | + checkpoint_key = f"checkpoint:{thread_id}:__empty__:{saved_config['configurable']['checkpoint_id']}" |
| 165 | + |
| 166 | + # Verify TTL is set |
| 167 | + ttl = await saver._redis.ttl(checkpoint_key) |
| 168 | + assert 50 <= ttl <= 60, f"TTL should be around 60 seconds, got {ttl}" |
| 169 | + |
| 170 | + # Remove TTL using -1 |
| 171 | + await saver._apply_ttl_to_keys(checkpoint_key, ttl_minutes=-1) |
| 172 | + |
| 173 | + # Verify TTL is removed |
| 174 | + ttl_after = await saver._redis.ttl(checkpoint_key) |
| 175 | + assert ttl_after == -1, "Key should be persistent after setting ttl_minutes=-1" |
| 176 | + |
| 177 | + |
| 178 | +def test_pin_thread_use_case(redis_url: str) -> None: |
| 179 | + """Test the 'pin thread' use case from issue #66. |
| 180 | +
|
| 181 | + This simulates pinning a specific thread by removing its TTL, |
| 182 | + making it persistent while other threads expire. |
| 183 | + """ |
| 184 | + saver = RedisSaver( |
| 185 | + redis_url, ttl={"default_ttl": 0.1} |
| 186 | + ) # 6 seconds TTL for quick test |
| 187 | + saver.setup() |
| 188 | + |
| 189 | + # Create two threads |
| 190 | + thread_to_pin = str(uuid4()) |
| 191 | + thread_to_expire = str(uuid4()) |
| 192 | + |
| 193 | + # Store checkpoint IDs to avoid using wildcards (more efficient and precise) |
| 194 | + checkpoint_ids = {} |
| 195 | + |
| 196 | + for thread_id in [thread_to_pin, thread_to_expire]: |
| 197 | + checkpoint = create_checkpoint( |
| 198 | + checkpoint=empty_checkpoint(), |
| 199 | + channels={"messages": [f"Thread {thread_id}"]}, |
| 200 | + step=1, |
| 201 | + ) |
| 202 | + checkpoint["channel_values"]["messages"] = [f"Thread {thread_id}"] |
| 203 | + |
| 204 | + config = {"configurable": {"thread_id": thread_id, "checkpoint_ns": ""}} |
| 205 | + |
| 206 | + saved_config = saver.put(config, checkpoint, {"source": "test", "step": 1}, {}) |
| 207 | + checkpoint_ids[thread_id] = saved_config["configurable"]["checkpoint_id"] |
| 208 | + |
| 209 | + # Pin the first thread by removing its TTL using exact key |
| 210 | + pinned_checkpoint_key = ( |
| 211 | + f"checkpoint:{thread_to_pin}:__empty__:{checkpoint_ids[thread_to_pin]}" |
| 212 | + ) |
| 213 | + saver._apply_ttl_to_keys(pinned_checkpoint_key, ttl_minutes=-1) |
| 214 | + |
| 215 | + # Verify pinned thread has no TTL |
| 216 | + assert saver._redis.exists(pinned_checkpoint_key) == 1 |
| 217 | + assert saver._redis.ttl(pinned_checkpoint_key) == -1 |
| 218 | + |
| 219 | + # Verify other thread still has TTL |
| 220 | + expiring_checkpoint_key = ( |
| 221 | + f"checkpoint:{thread_to_expire}:__empty__:{checkpoint_ids[thread_to_expire]}" |
| 222 | + ) |
| 223 | + assert saver._redis.exists(expiring_checkpoint_key) == 1 |
| 224 | + ttl = saver._redis.ttl(expiring_checkpoint_key) |
| 225 | + assert 0 < ttl <= 6 |
| 226 | + |
| 227 | + # Wait for expiring thread to expire |
| 228 | + time.sleep(7) |
| 229 | + |
| 230 | + # Pinned thread should still exist |
| 231 | + assert saver._redis.exists(pinned_checkpoint_key) == 1 |
| 232 | + |
| 233 | + # Expiring thread should be gone |
| 234 | + assert saver._redis.exists(expiring_checkpoint_key) == 0 |
0 commit comments