|
1 | 1 | mod common; |
2 | 2 |
|
3 | 3 | use common::*; |
4 | | -use std::time::{Duration, Instant}; |
5 | | - |
6 | | -#[tokio::test] |
7 | | -async fn test_cache_hit_behavior() { |
8 | | - let secrets = TestSecrets::setup().await; |
9 | | - let secret_name = secrets.secret_name(SecretType::Basic); |
10 | | - |
11 | | - let agent = AgentProcess::start().await; |
12 | | - |
13 | | - // First request - should fetch from AWS and cache |
14 | | - let query = AgentQueryBuilder::default() |
15 | | - .secret_id(&secret_name) |
16 | | - .build() |
17 | | - .unwrap(); |
18 | | - |
19 | | - let start_time = Instant::now(); |
20 | | - let response1 = agent.make_request(&query).await; |
21 | | - let first_request_duration = start_time.elapsed(); |
22 | | - |
23 | | - let json1: serde_json::Value = serde_json::from_str(&response1).unwrap(); |
24 | | - assert_eq!(json1["Name"], secret_name); |
25 | | - assert!(json1["SecretString"].as_str().unwrap().contains("testuser")); |
26 | | - |
27 | | - // Second request - should be served from cache (much faster) |
28 | | - let start_time = Instant::now(); |
29 | | - let response2 = agent.make_request(&query).await; |
30 | | - let second_request_duration = start_time.elapsed(); |
31 | | - |
32 | | - let json2: serde_json::Value = serde_json::from_str(&response2).unwrap(); |
33 | | - assert_eq!(json2["Name"], secret_name); |
34 | | - assert!(json2["SecretString"].as_str().unwrap().contains("testuser")); |
35 | | - |
36 | | - // Verify responses are identical (from cache) |
37 | | - assert_eq!(json1["VersionId"], json2["VersionId"]); |
38 | | - assert_eq!(json1["SecretString"], json2["SecretString"]); |
39 | | - |
40 | | - // Cache hit should be significantly faster than initial AWS call |
41 | | - // Allow some tolerance for timing variations |
42 | | - assert!( |
43 | | - second_request_duration < first_request_duration / 2, |
44 | | - "Cache hit should be faster. First: {:?}, Second: {:?}", |
45 | | - first_request_duration, |
46 | | - second_request_duration |
47 | | - ); |
48 | | -} |
49 | | - |
50 | | -#[tokio::test] |
51 | | -async fn test_refresh_now_bypasses_cache() { |
52 | | - let secrets = TestSecrets::setup().await; |
53 | | - let secret_name = secrets.secret_name(SecretType::Basic); |
54 | | - |
55 | | - let agent = AgentProcess::start().await; |
56 | | - |
57 | | - // First request - populate cache |
58 | | - let query = AgentQueryBuilder::default() |
59 | | - .secret_id(&secret_name) |
60 | | - .build() |
61 | | - .unwrap(); |
62 | | - let response1 = agent.make_request(&query).await; |
63 | | - let _json1: serde_json::Value = serde_json::from_str(&response1).unwrap(); |
64 | | - |
65 | | - // Second request with refreshNow=true - should bypass cache |
66 | | - let refresh_query = AgentQueryBuilder::default() |
67 | | - .secret_id(&secret_name) |
68 | | - .refresh_now(true) |
69 | | - .build() |
70 | | - .unwrap(); |
71 | | - |
72 | | - let start_time = Instant::now(); |
73 | | - let response2 = agent.make_request(&refresh_query).await; |
74 | | - let refresh_duration = start_time.elapsed(); |
75 | | - |
76 | | - let json2: serde_json::Value = serde_json::from_str(&response2).unwrap(); |
77 | | - |
78 | | - // Verify we got a valid response |
79 | | - assert_eq!(json2["Name"], secret_name); |
80 | | - assert!(json2["SecretString"].as_str().unwrap().contains("testuser")); |
81 | | - |
82 | | - // refreshNow should take longer than a cache hit (it goes to AWS) |
83 | | - // This is a network call, so should be measurably slower than cache |
84 | | - assert!( |
85 | | - refresh_duration > Duration::from_millis(10), |
86 | | - "refreshNow should make AWS call, duration: {:?}", |
87 | | - refresh_duration |
88 | | - ); |
89 | | - |
90 | | - // Third request without refreshNow - should use updated cache |
91 | | - let response3 = agent.make_request(&query).await; |
92 | | - let json3: serde_json::Value = serde_json::from_str(&response3).unwrap(); |
93 | | - |
94 | | - assert_eq!(json3["Name"], secret_name); |
95 | | - assert!(json3["SecretString"].as_str().unwrap().contains("testuser")); |
96 | | -} |
| 4 | +use std::time::Duration; |
97 | 5 |
|
98 | 6 | #[tokio::test] |
99 | 7 | async fn test_cache_after_secret_update() { |
@@ -175,91 +83,22 @@ async fn test_real_ttl_expiration_timing() { |
175 | 83 | .unwrap(); |
176 | 84 |
|
177 | 85 | // First request - populate cache |
178 | | - let start_time = Instant::now(); |
179 | 86 | let response1 = agent.make_request(&query).await; |
180 | | - let first_duration = start_time.elapsed(); |
181 | 87 | let json1: serde_json::Value = serde_json::from_str(&response1).unwrap(); |
182 | 88 | assert!(json1["SecretString"].as_str().unwrap().contains("testuser")); |
183 | 89 |
|
184 | | - // Second request immediately - should hit cache (fast) |
185 | | - let start_time = Instant::now(); |
| 90 | + // Second request immediately - should hit cache |
186 | 91 | let response2 = agent.make_request(&query).await; |
187 | | - let cache_hit_duration = start_time.elapsed(); |
188 | 92 | let json2: serde_json::Value = serde_json::from_str(&response2).unwrap(); |
189 | | - |
190 | | - // Verify cache hit is faster |
191 | | - assert!(cache_hit_duration < first_duration / 2); |
192 | 93 | assert_eq!(json1["VersionId"], json2["VersionId"]); |
193 | 94 |
|
194 | 95 | // Wait for TTL to expire (3 seconds + buffer) |
195 | 96 | tokio::time::sleep(Duration::from_secs(4)).await; |
196 | 97 |
|
197 | | - // Third request after TTL expiry - should fetch from AWS again (slower) |
198 | | - let start_time = Instant::now(); |
| 98 | + // Third request after TTL expiry - should fetch from AWS again |
199 | 99 | let response3 = agent.make_request(&query).await; |
200 | | - let post_ttl_duration = start_time.elapsed(); |
201 | 100 | let json3: serde_json::Value = serde_json::from_str(&response3).unwrap(); |
202 | 101 |
|
203 | | - // Post-TTL request should be slower than cache hit (goes to AWS) |
204 | | - assert!( |
205 | | - post_ttl_duration > cache_hit_duration * 2, |
206 | | - "Post-TTL request should be slower. Cache hit: {:?}, Post-TTL: {:?}", |
207 | | - cache_hit_duration, |
208 | | - post_ttl_duration |
209 | | - ); |
210 | | - |
211 | | - // Should still get valid response |
| 102 | + // Should still get valid response after TTL expiry |
212 | 103 | assert!(json3["SecretString"].as_str().unwrap().contains("testuser")); |
213 | 104 | } |
214 | | - |
215 | | -#[tokio::test] |
216 | | -async fn test_ttl_zero_disables_caching() { |
217 | | - let secrets = TestSecrets::setup().await; |
218 | | - let secret_name = secrets.secret_name(SecretType::Basic); |
219 | | - |
220 | | - // Start agent with TTL=0 to disable caching |
221 | | - let agent = AgentProcess::start_with_config(2775, 0).await; |
222 | | - |
223 | | - let query = AgentQueryBuilder::default() |
224 | | - .secret_id(&secret_name) |
225 | | - .build() |
226 | | - .unwrap(); |
227 | | - |
228 | | - // Make multiple requests and verify they all take significant time (go to AWS) |
229 | | - let mut durations = Vec::new(); |
230 | | - |
231 | | - for i in 0..4 { |
232 | | - let start_time = Instant::now(); |
233 | | - let response = agent.make_request(&query).await; |
234 | | - let duration = start_time.elapsed(); |
235 | | - durations.push(duration); |
236 | | - |
237 | | - let json: serde_json::Value = serde_json::from_str(&response).unwrap(); |
238 | | - assert!(json["SecretString"].as_str().unwrap().contains("testuser")); |
239 | | - |
240 | | - // Each request should take significant time (network call to AWS) |
241 | | - assert!( |
242 | | - duration > Duration::from_millis(20), |
243 | | - "Request {} should go to AWS with TTL=0, duration: {:?}", |
244 | | - i + 1, |
245 | | - duration |
246 | | - ); |
247 | | - |
248 | | - // Small delay between requests to avoid overwhelming AWS |
249 | | - if i < 3 { |
250 | | - tokio::time::sleep(Duration::from_millis(100)).await; |
251 | | - } |
252 | | - } |
253 | | - |
254 | | - // Verify no request was significantly faster (indicating cache hit) |
255 | | - let min_duration = durations.iter().min().unwrap(); |
256 | | - |
257 | | - // All requests should be in reasonable range (no cache speedup) |
258 | | - // Allow for network variance but ensure no sub-15ms cache hits |
259 | | - assert!( |
260 | | - min_duration > &Duration::from_millis(15), |
261 | | - "Minimum duration too fast, suggests caching: {:?}. All durations: {:?}", |
262 | | - min_duration, |
263 | | - durations |
264 | | - ); |
265 | | -} |
0 commit comments