File size: 8,140 Bytes
3c8e7d8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2627940
3c8e7d8
2627940
3c8e7d8
2627940
3c8e7d8
2627940
3c8e7d8
 
 
 
 
 
 
 
 
 
2627940
3c8e7d8
2627940
 
 
3c8e7d8
2627940
3c8e7d8
 
 
 
 
 
 
2627940
3c8e7d8
2627940
 
 
3c8e7d8
2627940
3c8e7d8
 
 
 
 
 
2627940
3c8e7d8
 
 
 
 
 
2627940
3c8e7d8
 
 
 
2627940
3c8e7d8
 
 
 
 
 
 
 
 
2627940
3c8e7d8
 
 
2627940
3c8e7d8
 
2627940
3c8e7d8
 
 
 
 
 
 
2627940
3c8e7d8
 
 
2627940
3c8e7d8
 
 
 
 
 
 
 
2627940
3c8e7d8
 
2627940
3c8e7d8
 
2627940
3c8e7d8
 
2627940
3c8e7d8
 
 
 
2627940
3c8e7d8
 
 
 
 
 
 
2627940
3c8e7d8
 
 
 
 
 
 
2627940
3c8e7d8
 
 
 
 
 
 
 
2627940
3c8e7d8
 
 
 
 
 
 
 
2627940
3c8e7d8
 
 
 
 
 
 
 
 
 
2627940
3c8e7d8
 
 
 
 
 
 
2627940
3c8e7d8
 
 
 
2627940
3c8e7d8
 
 
 
2627940
3c8e7d8
 
2627940
3c8e7d8
 
 
 
2627940
3c8e7d8
 
 
 
 
 
 
 
 
 
 
2627940
3c8e7d8
 
 
 
 
 
2627940
3c8e7d8
 
 
2627940
3c8e7d8
 
2627940
3c8e7d8
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
"""Tests for the cache.py module."""

import datetime
import time
from unittest.mock import patch

from fastmcp.utilities.cache import TimedCache


class TestTimedCache:
    """Tests for the TimedCache class."""

    def test_init(self):
        """Test that a TimedCache can be initialized with an expiration."""
        expiration = datetime.timedelta(seconds=10)
        cache = TimedCache(expiration)
        assert cache.expiration == expiration
        assert isinstance(cache.cache, dict)
        assert len(cache.cache) == 0

    def test_set(self):
        """Test that values can be set in the cache."""
        cache = TimedCache(datetime.timedelta(seconds=10))
        key, value = "test_key", "test_value"

        with patch("datetime.datetime") as mock_datetime:
            now = datetime.datetime(2023, 1, 1, tzinfo=datetime.timezone.utc)
            mock_datetime.now.return_value = now

            cache.set(key, value)

            # Check that the value is stored with the correct expiration
            assert key in cache.cache
            stored_value, expiration = cache.cache[key]
            assert stored_value == value
            assert expiration == now + datetime.timedelta(seconds=10)

    def test_get_found(self):
        """Test retrieving a value that exists and has not expired."""
        cache = TimedCache(datetime.timedelta(seconds=10))
        key, value = "test_key", "test_value"

        # Set a future expiration time
        future = datetime.datetime.now(datetime.timezone.utc) + datetime.timedelta(
            seconds=30
        )
        cache.cache[key] = (value, future)

        # The value should be returned
        assert cache.get(key) == value

    def test_get_expired(self):
        """Test retrieving a value that exists but has expired."""
        cache = TimedCache(datetime.timedelta(seconds=10))
        key, value = "test_key", "test_value"

        # Set a past expiration time
        past = datetime.datetime.now(datetime.timezone.utc) - datetime.timedelta(
            seconds=1
        )
        cache.cache[key] = (value, past)

        # Should return NOT_FOUND
        assert cache.get(key) is TimedCache.NOT_FOUND

    def test_get_not_found(self):
        """Test retrieving a value that doesn't exist in the cache."""
        cache = TimedCache(datetime.timedelta(seconds=10))

        # Key doesn't exist
        assert cache.get("nonexistent_key") is TimedCache.NOT_FOUND

    def test_clear(self):
        """Test that the cache can be cleared."""
        cache = TimedCache(datetime.timedelta(seconds=10))

        # Add some items
        cache.set("key1", "value1")
        cache.set("key2", "value2")
        assert len(cache.cache) == 2

        # Clear the cache
        cache.clear()
        assert len(cache.cache) == 0

    def test_real_expiration(self):
        """Test that values actually expire after the specified time."""
        # Use a very short expiration for the test
        cache = TimedCache(datetime.timedelta(milliseconds=50))
        key, value = "test_key", "test_value"

        cache.set(key, value)
        # Value should be available immediately
        assert cache.get(key) == value

        # Wait for expiration
        time.sleep(0.06)  # 60 milliseconds, slightly longer than expiration

        # Value should now be expired
        assert cache.get(key) is TimedCache.NOT_FOUND

    def test_overwrite_value(self):
        """Test that setting a key that already exists overwrites the old value."""
        cache = TimedCache(datetime.timedelta(seconds=10))
        key = "test_key"

        # Set initial value
        cache.set(key, "initial_value")
        assert cache.get(key) == "initial_value"

        # Overwrite with new value
        cache.set(key, "new_value")
        assert cache.get(key) == "new_value"

    def test_extends_expiration_on_overwrite(self):
        """Test that overwriting a key extends its expiration time."""
        cache = TimedCache(datetime.timedelta(seconds=10))
        key = "test_key"

        with patch("datetime.datetime") as mock_datetime:
            # Set initial value at t=0
            initial_time = datetime.datetime(2023, 1, 1, tzinfo=datetime.timezone.utc)
            mock_datetime.now.return_value = initial_time
            cache.set(key, "initial_value")

            initial_expiration = cache.cache[key][1]
            assert initial_expiration == initial_time + datetime.timedelta(seconds=10)

            # Overwrite at t=5
            later_time = initial_time + datetime.timedelta(seconds=5)
            mock_datetime.now.return_value = later_time
            cache.set(key, "new_value")

            # Expiration should be extended
            new_expiration = cache.cache[key][1]
            assert new_expiration == later_time + datetime.timedelta(seconds=10)

    def test_different_key_types(self):
        """Test that different types of keys can be used."""
        cache = TimedCache(datetime.timedelta(seconds=10))

        # Test various key types
        keys_and_values = [
            (42, "int_value"),
            (3.14, "float_value"),
            ((1, 2), "tuple_value"),
            (frozenset({1, 2, 3}), "frozenset_value"),
        ]

        for key, value in keys_and_values:
            cache.set(key, value)
            assert cache.get(key) == value

    def test_none_value(self):
        """Test that None can be stored as a value."""
        cache = TimedCache(datetime.timedelta(seconds=10))
        key = "none_key"

        cache.set(key, None)
        # The stored value is None, but get() should return None, not NOT_FOUND
        assert cache.get(key) is None

    def test_edge_case_zero_expiration(self):
        """Test with a zero expiration time."""
        cache = TimedCache(datetime.timedelta(seconds=0))
        key, value = "test_key", "test_value"

        cache.set(key, value)
        # The value might already be expired by the time we call get()
        # We can't make strong assertions here due to timing variability
        retrieved = cache.get(key)
        assert retrieved in (value, TimedCache.NOT_FOUND)

    def test_negative_expiration(self):
        """Test with a negative expiration time."""
        cache = TimedCache(datetime.timedelta(seconds=-1))
        key, value = "test_key", "test_value"

        cache.set(key, value)
        # Value should be immediately expired
        assert cache.get(key) is TimedCache.NOT_FOUND

    def test_cache_consistency(self):
        """Test cache consistency with multiple operations."""
        cache = TimedCache(datetime.timedelta(seconds=10))

        # Add multiple items
        cache.set("key1", "value1")
        cache.set("key2", "value2")
        cache.set("key3", "value3")

        # Check all items
        assert cache.get("key1") == "value1"
        assert cache.get("key2") == "value2"
        assert cache.get("key3") == "value3"

        # Overwrite one item
        cache.set("key2", "updated_value")

        # Check again
        assert cache.get("key1") == "value1"
        assert cache.get("key2") == "updated_value"
        assert cache.get("key3") == "value3"

        # Clear and verify all items are gone
        cache.clear()
        assert cache.get("key1") is TimedCache.NOT_FOUND
        assert cache.get("key2") is TimedCache.NOT_FOUND
        assert cache.get("key3") is TimedCache.NOT_FOUND

    def test_large_expiration(self):
        """Test with a very large expiration time."""
        # One year expiration
        cache = TimedCache(datetime.timedelta(days=365))
        key, value = "test_key", "test_value"

        cache.set(key, value)
        assert cache.get(key) == value

    def test_many_items(self):
        """Test cache with many items."""
        cache = TimedCache(datetime.timedelta(seconds=10))

        # Add 1000 items
        for i in range(1000):
            cache.set(f"key{i}", f"value{i}")

        # Check size
        assert len(cache.cache) == 1000

        # Check some random items
        for i in [0, 123, 456, 789, 999]:
            assert cache.get(f"key{i}") == f"value{i}"