-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun_tests.py
More file actions
310 lines (235 loc) · 8.12 KB
/
run_tests.py
File metadata and controls
310 lines (235 loc) · 8.12 KB
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
"""
模块验证脚本
直接测试模块功能,不依赖pytest
"""
import sys
import os
import time
import threading
sys.path.insert(0, os.path.join(os.path.dirname(__file__), "src"))
from multilevel_cache.config import (
CacheConfig,
MemoryCacheConfig,
TTLConfig,
CacheKeyBuilder,
EvictionPolicy,
DegradationConfig,
)
from multilevel_cache.core import (
MemoryCache,
MultiLevelCache,
CacheManager,
)
from multilevel_cache.protection import (
CacheProtection,
LocalLock,
CircuitBreaker,
)
from multilevel_cache.monitoring import (
CacheMetrics,
CacheMonitor,
)
def test_memory_cache():
"""测试内存缓存"""
print("\n=== 测试内存缓存 ===")
config = MemoryCacheConfig(max_size=100, default_ttl=60)
cache = MemoryCache(config)
cache.set('key1', 'value1')
assert cache.get('key1') == 'value1', 'set/get测试失败'
print('[OK] set/get: 通过')
assert cache.delete('key1') == True, 'delete测试失败'
assert cache.get('key1') is None, 'delete后get测试失败'
print('[OK] delete: 通过')
stats_cache = MemoryCache(MemoryCacheConfig(max_size=100, default_ttl=60))
stats_cache.set('key_stats', 'value_stats')
stats_cache.get('key_stats')
stats_cache.get('nonexistent')
stats = stats_cache.get_stats()
assert stats['hits'] == 1, f'hits统计错误: hits={stats["hits"]}, expected 1'
assert stats['misses'] == 1, f'misses统计错误: misses={stats["misses"]}, expected 1'
print('[OK] stats: 通过')
cache.set('key3', 'value3')
cache.clear()
assert cache.get('key3') is None
print('[OK] clear: 通过')
ttl_cache = MemoryCache(MemoryCacheConfig(max_size=100, default_ttl=60))
ttl_cache.set('expire_key', 'value', ttl=1)
assert ttl_cache.get('expire_key') == 'value'
time.sleep(1.5)
assert ttl_cache.get('expire_key') is None
print('[OK] TTL expiration: 通过')
def test_cache_key_builder():
"""测试缓存键构建器"""
print("\n=== 测试缓存键构建器 ===")
key = CacheKeyBuilder.build('user', 123)
assert key == 'v1:user:123', f'键构建错误: {key}'
print('[OK] build: 通过')
key_with_extra = CacheKeyBuilder.build('user', 123, 'profile', 'avatar')
assert key_with_extra == 'v1:user:123:profile:avatar'
print('[OK] build with extra: 通过')
pattern = CacheKeyBuilder.build_pattern('user')
assert pattern == 'v1:user:*', f'模式构建错误: {pattern}'
print('[OK] build_pattern: 通过')
parts = CacheKeyBuilder.parse('v1:user:123:profile')
assert parts['version'] == 'v1'
assert parts['prefix'] == 'user'
assert parts['identifier'] == '123'
assert parts['extra'] == ['profile']
print('[OK] parse: 通过')
def test_cache_protection():
"""测试缓存防护"""
print("\n=== 测试缓存防护 ===")
protection = CacheProtection()
assert protection.is_null_value('__NULL__') == True
assert protection.is_null_value('value') == False
print('[OK] null_value检测: 通过')
for _ in range(10):
ttl = protection.add_jitter(100)
assert 90 <= ttl <= 110, f'TTL抖动范围错误: {ttl}'
print('[OK] TTL抖动: 通过')
def test_local_lock():
"""测试本地锁"""
print("\n=== 测试本地锁 ===")
lock = LocalLock(timeout=5)
assert lock.acquire() == True
assert lock._acquired == True
print('[OK] acquire: 通过')
assert lock.release() == True
assert lock._acquired == False
print('[OK] release: 通过')
with LocalLock() as l:
assert l._acquired == True
print('[OK] context manager: 通过')
def test_circuit_breaker():
"""测试熔断器"""
print("\n=== 测试熔断器 ===")
cb = CircuitBreaker(failure_threshold=3)
assert cb.state == "closed"
assert cb.is_available() == True
print('[OK] initial state: 通过')
cb.record_failure()
assert cb.state == "closed"
print('[OK] first failure: 通过')
cb.record_failure()
cb.record_failure()
assert cb.state == "open"
assert cb.is_available() == False
print('[OK] open after failures: 通过')
cb.reset()
assert cb.state == "closed"
print('[OK] reset: 通过')
def test_cache_metrics():
"""测试缓存指标"""
print("\n=== 测试缓存指标 ===")
metrics = CacheMetrics()
metrics.l1_hits = 80
metrics.l1_misses = 20
assert metrics.l1_hit_rate == 80.0, f'命中率计算错误: {metrics.l1_hit_rate}'
print('[OK] hit_rate: 通过')
result = metrics.to_dict()
assert 'l1_hits' in result
assert 'l1_hit_rate' in result
print('[OK] to_dict: 通过')
def test_cache_monitor():
"""测试缓存监控"""
print("\n=== 测试缓存监控 ===")
monitor = CacheMonitor()
monitor.record_hit('l1')
monitor.record_miss('l1')
assert monitor.metrics.l1_hits == 1
assert monitor.metrics.l1_misses == 1
print('[OK] record operations: 通过')
stats = monitor.get_stats()
assert 'l1_hits' in stats
assert 'l1_misses' in stats
print('[OK] get_stats: 通过')
def test_multi_level_cache():
"""测试多级缓存"""
print("\n=== 测试多级缓存 ===")
config = CacheConfig()
config.degradation_config.enable_l2_fallback = False
cache = MultiLevelCache(config)
cache.set('test_key', 'test_value')
assert cache.get('test_key') == 'test_value'
print('[OK] set/get: 通过')
cache.delete('test_key')
assert cache.get('test_key') is None
print('[OK] delete: 通过')
stats = cache.get_stats()
assert 'enabled' in stats
assert 'l1_stats' in stats
print('[OK] get_stats: 通过')
def test_cache_manager():
"""测试缓存管理器"""
print("\n=== 测试缓存管理器 ===")
manager1 = CacheManager()
manager2 = CacheManager()
assert manager1 is manager2
print('[OK] singleton: 通过')
manager1.set('mgr_key', 'mgr_value')
assert manager1.get('mgr_key') == 'mgr_value'
print('[OK] set/get: 通过')
manager1.delete('mgr_key')
assert manager1.get('mgr_key') is None
print('[OK] delete: 通过')
manager1.disable()
stats = manager1.get_stats()
assert stats['enabled'] == False
manager1.enable()
stats = manager1.get_stats()
assert stats['enabled'] == True
print('[OK] enable/disable: 通过')
def test_thread_safety():
"""测试线程安全"""
print("\n=== 测试线程安全 ===")
cache = MemoryCache(MemoryCacheConfig(thread_safe=True, max_size=1000))
errors = []
def writer(start, count):
try:
for i in range(count):
cache.set(f'key_{start}_{i}', f'value_{start}_{i}')
except Exception as e:
errors.append(str(e))
threads = [
threading.Thread(target=writer, args=(i, 100))
for i in range(10)
]
for t in threads:
t.start()
for t in threads:
t.join()
assert len(errors) == 0, f'线程安全错误: {errors}'
stats = cache.get_stats()
assert stats['size'] > 0
print('[OK] thread safety: 通过')
def run_all_tests():
"""运行所有测试"""
print("=" * 50)
print("开始运行多级缓存模块测试")
print("=" * 50)
try:
test_memory_cache()
test_cache_key_builder()
test_cache_protection()
test_local_lock()
test_circuit_breaker()
test_cache_metrics()
test_cache_monitor()
test_multi_level_cache()
test_cache_manager()
test_thread_safety()
print("\n" + "=" * 50)
print("[SUCCESS] 所有测试通过!")
print("=" * 50)
return True
except AssertionError as e:
print(f"\n[FAILED] 测试失败: {str(e)}")
return False
except Exception as e:
print(f"\n[ERROR] 测试异常: {str(e)}")
import traceback
traceback.print_exc()
return False
if __name__ == '__main__':
success = run_all_tests()
sys.exit(0 if success else 1)