-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathintegration_dry_run.rs
More file actions
293 lines (245 loc) · 10.7 KB
/
integration_dry_run.rs
File metadata and controls
293 lines (245 loc) · 10.7 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
mod common;
use bytes::Bytes;
use common::MockS3Backend;
use common::helpers::*;
use s3_cache::S3CachingProxy;
use s3s::S3;
#[tokio::test]
async fn cache_miss_populates_with_hash() {
// Setup: MockS3Backend with test data
let backend = MockS3Backend::new();
backend
.put_object_sync("test-bucket", "key.txt", b"test content")
.await;
// Setup: Cache + Proxy in dry-run mode
let cache = create_test_cache(100, usize::MAX, 300);
let proxy = S3CachingProxy::new(backend.clone(), Some(cache.clone()), usize::MAX, true);
// First request in dry-run mode: cache miss
let req = build_get_request("test-bucket", "key.txt", None);
let resp = proxy.get_object(req).await.unwrap();
let body = extract_body(resp.output.body).await;
assert_eq!(body, Bytes::from("test content"));
assert_eq!(backend.get_request_count().await, 1);
// Verify object was cached (even in dry-run mode)
assert_cache_contains(&cache, "test-bucket", "key.txt").await;
}
#[tokio::test]
async fn always_fetches_from_upstream() {
// Setup: MockS3Backend with test data
let backend = MockS3Backend::new();
backend
.put_object_sync("test-bucket", "key.txt", b"test content")
.await;
// Setup: Cache + Proxy in dry-run mode
let cache = create_test_cache(100, usize::MAX, 300);
let proxy = S3CachingProxy::new(backend.clone(), Some(cache.clone()), usize::MAX, true);
// First request: cache miss
let req = build_get_request("test-bucket", "key.txt", None);
let resp = proxy.get_object(req).await.unwrap();
let body = extract_body(resp.output.body).await;
assert_eq!(body, Bytes::from("test content"));
assert_eq!(backend.get_request_count().await, 1);
// Second request: even though cache hit, should still fetch from upstream in dry-run mode
let req = build_get_request("test-bucket", "key.txt", None);
let resp = proxy.get_object(req).await.unwrap();
let body = extract_body(resp.output.body).await;
assert_eq!(body, Bytes::from("test content"));
// Backend should be called again (dry-run always fetches fresh)
assert_eq!(backend.get_request_count().await, 2);
}
#[tokio::test]
async fn returns_fresh_data() {
// Setup: MockS3Backend with initial data
let backend = MockS3Backend::new();
backend
.put_object_sync("test-bucket", "mutable.txt", b"version1")
.await;
// Setup: Cache + Proxy in dry-run mode
let cache = create_test_cache(100, usize::MAX, 300);
let proxy = S3CachingProxy::new(backend.clone(), Some(cache.clone()), usize::MAX, true);
// First request: populates cache with "version1"
let req = build_get_request("test-bucket", "mutable.txt", None);
let resp = proxy.get_object(req).await.unwrap();
assert_eq!(
extract_body(resp.output.body).await,
Bytes::from("version1")
);
// Update backend data directly (simulating upstream change)
backend
.put_object_sync("test-bucket", "mutable.txt", b"version2")
.await;
// Second request in dry-run mode: should return fresh "version2" from backend
// even though cache has "version1"
let req = build_get_request("test-bucket", "mutable.txt", None);
let resp = proxy.get_object(req).await.unwrap();
assert_eq!(
extract_body(resp.output.body).await,
Bytes::from("version2")
);
assert_eq!(backend.get_request_count().await, 2);
}
#[tokio::test]
async fn with_matching_cache_data() {
// Setup: MockS3Backend with test data
let backend = MockS3Backend::new();
backend
.put_object_sync("test-bucket", "stable.txt", b"stable content")
.await;
// Setup: Cache + Proxy in dry-run mode
let cache = create_test_cache(100, usize::MAX, 300);
let proxy = S3CachingProxy::new(backend.clone(), Some(cache.clone()), usize::MAX, true);
// First request: populates cache
let req = build_get_request("test-bucket", "stable.txt", None);
proxy.get_object(req).await.unwrap();
// Second request: cache hit with matching data (should not trigger mismatch)
let req = build_get_request("test-bucket", "stable.txt", None);
let resp = proxy.get_object(req).await.unwrap();
let body = extract_body(resp.output.body).await;
assert_eq!(body, Bytes::from("stable content"));
// Should not crash and should return correct data
assert_eq!(backend.get_request_count().await, 2);
}
#[tokio::test]
async fn with_mismatched_cache_data() {
// Setup: MockS3Backend with test data
let backend = MockS3Backend::new();
backend
.put_object_sync("test-bucket", "changing.txt", b"original")
.await;
// Setup: Cache + Proxy in dry-run mode
let cache = create_test_cache(100, usize::MAX, 300);
let proxy = S3CachingProxy::new(backend.clone(), Some(cache.clone()), usize::MAX, true);
// First request: populates cache with "original"
let req = build_get_request("test-bucket", "changing.txt", None);
proxy.get_object(req).await.unwrap();
// Update backend data
backend
.put_object_sync("test-bucket", "changing.txt", b"modified")
.await;
// Second request: cache hit but data differs
// Should detect mismatch, log warning, but not crash
let req = build_get_request("test-bucket", "changing.txt", None);
let resp = proxy.get_object(req).await.unwrap();
let body = extract_body(resp.output.body).await;
// Should return fresh data from backend
assert_eq!(body, Bytes::from("modified"));
assert_eq!(backend.get_request_count().await, 2);
}
#[tokio::test]
async fn multiple_objects() {
// Setup: MockS3Backend with multiple objects
let backend = MockS3Backend::new();
for i in 0..5 {
backend
.put_object_sync("test-bucket", &format!("file{}.txt", i), b"data")
.await;
}
// Setup: Cache + Proxy in dry-run mode
let cache = create_test_cache(100, usize::MAX, 300);
let proxy = S3CachingProxy::new(backend.clone(), Some(cache.clone()), usize::MAX, true);
// Fetch all objects
for i in 0..5 {
let req = build_get_request("test-bucket", &format!("file{}.txt", i), None);
let resp = proxy.get_object(req).await.unwrap();
let body = extract_body(resp.output.body).await;
assert_eq!(body, Bytes::from("data"));
}
// First pass: 5 requests
assert_eq!(backend.get_request_count().await, 5);
// Fetch all again - in dry-run mode, should hit backend again for each
for i in 0..5 {
let req = build_get_request("test-bucket", &format!("file{}.txt", i), None);
let resp = proxy.get_object(req).await.unwrap();
let body = extract_body(resp.output.body).await;
assert_eq!(body, Bytes::from("data"));
}
// Second pass: should have made 5 more requests (total 10)
assert_eq!(backend.get_request_count().await, 10);
}
#[tokio::test]
async fn with_large_objects() {
// Setup: MockS3Backend with large object
let backend = MockS3Backend::new();
let large_data = vec![b'x'; 1_000_000];
backend
.put_object_sync("test-bucket", "large.bin", &large_data)
.await;
// Setup: Cache + Proxy in dry-run mode with size limit
let cache = create_test_cache(100, 100_000, 300);
let proxy = S3CachingProxy::new(backend.clone(), Some(cache.clone()), 100_000, true);
// Request large object in dry-run mode
let req = build_get_request("test-bucket", "large.bin", None);
let resp = proxy.get_object(req).await.unwrap();
let body = extract_body(resp.output.body).await;
assert_eq!(body.len(), 1_000_000);
assert_eq!(backend.get_request_count().await, 1);
// Object too large should not be cached (same as non-dry-run mode)
assert_cache_missing(&cache, "test-bucket", "large.bin").await;
// Second request should hit backend again
let req = build_get_request("test-bucket", "large.bin", None);
proxy.get_object(req).await.unwrap();
assert_eq!(backend.get_request_count().await, 2);
}
#[tokio::test]
async fn concurrent_access() {
// Setup: MockS3Backend with test data
let backend = MockS3Backend::new();
backend
.put_object_sync("test-bucket", "concurrent.txt", b"data")
.await;
// Setup: Cache + Proxy in dry-run mode
let cache = create_test_cache(100, usize::MAX, 300);
let proxy = S3CachingProxy::new(backend.clone(), Some(cache.clone()), usize::MAX, true);
for _ in 0..10 {
let req = build_get_request("test-bucket", "concurrent.txt", None);
let resp = proxy.get_object(req).await.unwrap();
let body = extract_body(resp.output.body).await;
assert_eq!(body, Bytes::from("data"));
}
// In dry-run mode, every request hits the backend
assert_eq!(backend.get_request_count().await, 10);
// Object should be cached
assert_cache_contains(&cache, "test-bucket", "concurrent.txt").await;
}
#[tokio::test]
async fn backend_error_not_cached() {
// Setup: MockS3Backend without adding the object
let backend = MockS3Backend::new();
// Setup: Cache + Proxy in dry-run mode
let cache = create_test_cache(100, usize::MAX, 300);
let proxy = S3CachingProxy::new(backend.clone(), Some(cache.clone()), usize::MAX, true);
// Request non-existent object
let req = build_get_request("test-bucket", "missing.txt", None);
let result = proxy.get_object(req).await;
assert!(result.is_err(), "Should propagate backend error");
// Nothing should be cached
assert_cache_missing(&cache, "test-bucket", "missing.txt").await;
assert!(cache.is_empty().await);
}
#[tokio::test]
async fn preserves_metadata() {
// Setup: MockS3Backend with test data
let backend = MockS3Backend::new();
backend
.put_object_sync("test-bucket", "meta.txt", b"hello")
.await;
// Setup: Cache + Proxy in dry-run mode
let cache = create_test_cache(100, usize::MAX, 300);
let proxy = S3CachingProxy::new(backend.clone(), Some(cache.clone()), usize::MAX, true);
// First request
let req = build_get_request("test-bucket", "meta.txt", None);
let resp1 = proxy.get_object(req).await.unwrap();
let body1 = extract_body(resp1.output.body).await;
let content_length1 = resp1.output.content_length;
// Second request (cache hit in dry-run mode)
let req = build_get_request("test-bucket", "meta.txt", None);
let resp2 = proxy.get_object(req).await.unwrap();
let body2 = extract_body(resp2.output.body).await;
let content_length2 = resp2.output.content_length;
// Both responses should be identical
assert_eq!(body1, body2);
assert_eq!(body1, Bytes::from("hello"));
assert_eq!(content_length1, content_length2);
assert_eq!(content_length1, Some(5));
assert_eq!(backend.get_request_count().await, 2);
}