-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathverify_phase2.py
More file actions
59 lines (47 loc) · 1.72 KB
/
Copy pathverify_phase2.py
File metadata and controls
59 lines (47 loc) · 1.72 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
import httpx
import asyncio
import redis.asyncio as redis
import json
import sys
BASE_URL = "http://localhost:8002"
REDIS_URL = "redis://localhost:6381"
async def verify_redis_caching():
print("Verifying Phase 2: Redis Caching...")
# 1. Setup Redis Client
r = redis.from_url(REDIS_URL, decode_responses=True)
try:
await r.ping()
print("Connected to Redis.")
except Exception as e:
print(f"Failed to connect to Redis: {e}")
return False
async with httpx.AsyncClient(base_url=BASE_URL) as client:
# 2. Create Task
response = await client.post("/tasks/")
if response.status_code != 201:
print(f"Failed to create task: {response.text}")
return False
task_data = response.json()
task_id = task_data["id"]
print(f"Created Task ID: {task_id}")
# 3. Verify it's in Redis
cached_value = await r.get(f"task:{task_id}")
if not cached_value:
print("Error: Task not found in Redis after creation!")
return False
cached_json = json.loads(cached_value)
if cached_json["id"] != task_id:
print("Error: Redis data mismatch!")
return False
print("Redis Cache Hit Verified (Write-through on Create).")
response = await client.get(f"/tasks/{task_id}")
if response.status_code != 200:
print(f"Failed to get task: {response.text}")
return False
print("GET /tasks/{id} successful.")
await r.aclose()
return True
if __name__ == "__main__":
if not asyncio.run(verify_redis_caching()):
sys.exit(1)
print("Phase 2 Verification Passed!")