Repository navigation
Expand file tree
/
Copy pathtest-idempotency.sh
More file actions
executable file
·273 lines (225 loc) · 9.49 KB
/
Copy pathtest-idempotency.sh
File metadata and controls
executable file
·273 lines (225 loc) · 9.49 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
#!/bin/bash
# Idempotency Testing Script for Event-Driven Microservices System
# This script demonstrates and verifies idempotency at multiple levels
set -e
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# Base URL for the system
BASE_URL="http://localhost"
echo -e "${BLUE}🔄 Event-Driven System Idempotency Test Suite${NC}"
echo "=============================================="
# Check if system is running
check_system() {
echo -e "${YELLOW}🔍 Checking system availability...${NC}"
if ! curl -s "$BASE_URL/api/write" >/dev/null 2>&1; then
echo -e "${RED}❌ System not available at $BASE_URL${NC}"
echo "Please ensure the system is running with: docker compose up -d"
exit 1
fi
echo -e "${GREEN}✅ System is available${NC}"
echo ""
}
# Test HTTP API level idempotency
test_http_idempotency() {
local event_type=$1
local test_id="http-idem-test-$(date +%s)"
local idem_key="test-key-$(date +%s)-$(openssl rand -hex 4)"
echo -e "${YELLOW}🧪 Testing HTTP API idempotency for $event_type events...${NC}"
# First request
echo " 📤 Sending first request..."
response1=$(curl -s -w "\n%{http_code}" -X POST "$BASE_URL/api/write/$event_type" \
-H "Content-Type: application/json" \
-H "Idempotency-Key: $idem_key" \
-d "{\"id\":\"$test_id\",\"data\":\"Test Data for $event_type\"}")
# Extract HTTP status and response body
http_code1=$(echo "$response1" | tail -n1)
response_body1=$(echo "$response1" | head -n -1)
# Wait a moment
sleep 1
# Second request (duplicate)
echo " 📤 Sending duplicate request with same idempotency key..."
response2=$(curl -s -w "\n%{http_code}" -X POST "$BASE_URL/api/write/$event_type" \
-H "Content-Type: application/json" \
-H "Idempotency-Key: $idem_key" \
-d "{\"id\":\"$test_id\",\"data\":\"Test Data for $event_type\"}")
# Extract HTTP status and response body
http_code2=$(echo "$response2" | tail -n1)
response_body2=$(echo "$response2" | head -n -1)
# Compare responses
if [ "$response_body1" = "$response_body2" ] && [ "$http_code1" = "$http_code2" ]; then
echo -e " ${GREEN}✅ HTTP Idempotency test PASSED for $event_type${NC}"
echo " Both requests returned identical responses"
echo " HTTP Status: $http_code1"
else
echo -e " ${RED}❌ HTTP Idempotency test FAILED for $event_type${NC}"
echo " Response 1 (HTTP $http_code1): $response_body1"
echo " Response 2 (HTTP $http_code2): $response_body2"
return 1
fi
echo ""
}
# Test event processing idempotency
test_event_idempotency() {
local event_type=$1
local test_id="event-idem-test-$(date +%s)"
echo -e "${YELLOW}🎯 Testing Event Processing idempotency for $event_type...${NC}"
# Send multiple events with same ID (different idempotency keys to bypass HTTP cache)
echo " 📤 Sending first event..."
idem_key1="event1-$(date +%s)-$(openssl rand -hex 4)"
curl -s -X POST "$BASE_URL/api/write/$event_type" \
-H "Content-Type: application/json" \
-H "Idempotency-Key: $idem_key1" \
-d "{\"id\":\"$test_id\",\"data\":\"Event Data\"}" >/dev/null
echo " 📤 Sending second event with same ID..."
idem_key2="event2-$(date +%s)-$(openssl rand -hex 4)"
curl -s -X POST "$BASE_URL/api/write/$event_type" \
-H "Content-Type: application/json" \
-H "Idempotency-Key: $idem_key2" \
-d "{\"id\":\"$test_id\",\"data\":\"Event Data\"}" >/dev/null
# Wait for event processing
echo " ⏳ Waiting for event processing..."
sleep 3
# Check service logs for duplicate processing detection
if [ "$event_type" = "user" ]; then
service_name="user_service"
else
service_name="worker"
fi
container_name=$(docker ps --format "table {{.Names}}" | grep "$service_name" | head -1)
if [ -n "$container_name" ]; then
duplicate_logs=$(docker logs "$container_name" --tail 20 2>/dev/null | grep -i "already processed\|skipping" || true)
if [ -n "$duplicate_logs" ]; then
echo -e " ${GREEN}✅ Event Processing idempotency PASSED for $event_type${NC}"
echo " Duplicate event processing was detected and prevented"
else
echo -e " ${YELLOW}⚠️ Event Processing idempotency unclear for $event_type${NC}"
echo " Could not find duplicate processing logs (may still be working correctly)"
fi
else
echo -e " ${YELLOW}⚠️ Could not find $service_name container for log verification${NC}"
fi
echo ""
}
# Test performance difference
test_performance_difference() {
local event_type="user"
local test_id="perf-test-$(date +%s)"
local idem_key="perf-key-$(date +%s)-$(openssl rand -hex 4)"
echo -e "${YELLOW}⚡ Testing Performance Difference...${NC}"
# Measure first request (processing)
echo " 📊 Measuring first request time..."
start_time=$(date +%s%N)
curl -s -X POST "$BASE_URL/api/write/$event_type" \
-H "Content-Type: application/json" \
-H "Idempotency-Key: $idem_key" \
-d "{\"id\":\"$test_id\",\"data\":\"Performance Test\"}" >/dev/null
end_time=$(date +%s%N)
first_request_time=$((($end_time - $start_time) / 1000000)) # Convert to milliseconds
# Wait a moment
sleep 1
# Measure second request (cached)
echo " 📊 Measuring duplicate request time..."
start_time=$(date +%s%N)
curl -s -X POST "$BASE_URL/api/write/$event_type" \
-H "Content-Type: application/json" \
-H "Idempotency-Key: $idem_key" \
-d "{\"id\":\"$test_id\",\"data\":\"Performance Test\"}" >/dev/null
end_time=$(date +%s%N)
second_request_time=$((($end_time - $start_time) / 1000000)) # Convert to milliseconds
# Calculate improvement
if [ $first_request_time -gt 0 ]; then
improvement=$(echo "scale=1; (1 - $second_request_time / $first_request_time) * 100" | bc -l 2>/dev/null || echo "N/A")
echo -e " ${GREEN}✅ Performance Test Results:${NC}"
echo " First request (processing): ${first_request_time}ms"
echo " Second request (cached): ${second_request_time}ms"
echo " Performance improvement: ${improvement}%"
else
echo -e " ${YELLOW}⚠️ Performance measurement inconclusive${NC}"
fi
echo ""
}
# Check Redis for idempotency keys
check_redis_keys() {
echo -e "${YELLOW}🔍 Checking Redis for idempotency keys...${NC}"
redis_container=$(docker ps --format "table {{.Names}}" | grep "redis" | head -1)
if [ -n "$redis_container" ]; then
idem_keys=$(docker exec "$redis_container" redis-cli KEYS "idempotency:*" 2>/dev/null | wc -l)
processed_keys=$(docker exec "$redis_container" redis-cli KEYS "user:processed:*" 2>/dev/null | wc -l)
echo " 📊 Idempotency cache keys: $idem_keys"
echo " 📊 Processed event markers: $processed_keys"
if [ "$idem_keys" -gt 0 ] || [ "$processed_keys" -gt 0 ]; then
echo -e " ${GREEN}✅ Redis contains idempotency data${NC}"
else
echo -e " ${YELLOW}⚠️ No idempotency data found in Redis${NC}"
fi
else
echo -e " ${RED}❌ Could not find Redis container${NC}"
fi
echo ""
}
# Main test execution
main() {
echo "Starting comprehensive idempotency tests..."
echo ""
# Check system availability
check_system
# Test different event types
echo -e "${BLUE}🧪 Testing HTTP API Level Idempotency${NC}"
echo "======================================"
test_http_idempotency "user"
test_http_idempotency "order"
test_http_idempotency "product"
echo -e "${BLUE}🎯 Testing Event Processing Level Idempotency${NC}"
echo "=============================================="
test_event_idempotency "user"
echo -e "${BLUE}⚡ Performance Testing${NC}"
echo "===================="
test_performance_difference
echo -e "${BLUE}🔍 Redis Verification${NC}"
echo "==================="
check_redis_keys
echo -e "${GREEN}🎉 Idempotency Test Suite Complete!${NC}"
echo ""
echo "💡 For detailed information, see: IDEMPOTENCY_DESIGN.md"
echo "🌐 For visual testing, visit: http://localhost/ (Idempotency Tester tab)"
}
# Handle script arguments
case "${1:-}" in
"http")
check_system
test_http_idempotency "user"
;;
"event")
check_system
test_event_idempotency "user"
;;
"performance")
check_system
test_performance_difference
;;
"redis")
check_redis_keys
;;
"help"|"-h"|"--help")
echo "Usage: $0 [test_type]"
echo ""
echo "Test types:"
echo " http - Test HTTP API level idempotency only"
echo " event - Test event processing level idempotency only"
echo " performance - Test performance difference only"
echo " redis - Check Redis keys only"
echo " (no args) - Run all tests"
echo ""
echo "Examples:"
echo " $0 # Run all tests"
echo " $0 http # Test HTTP idempotency only"
echo " $0 performance # Test performance only"
;;
*)
main
;;
esac