-
Notifications
You must be signed in to change notification settings - Fork 175
Expand file tree
/
Copy pathbatch_53_implementations.rs
More file actions
316 lines (273 loc) · 11.2 KB
/
Copy pathbatch_53_implementations.rs
File metadata and controls
316 lines (273 loc) · 11.2 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
311
312
313
314
315
316
// Batch-53: Implementation of issues #1519, #1520, #1521, #1522
/// #1519: Dead-letter queue requeue idempotency
///
/// Result type for dead-letter job requeue operations.
/// Distinguishes between "job not found" and "job not in dead-letter status"
/// to enable proper HTTP status code mapping (404 vs 409).
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum RequeueResult {
/// Job was successfully requeued from dead-letter status
Success,
/// Job does not exist in the system
NotFound,
/// Job exists but is not in dead_letter status (e.g., already completed or pending)
NotInDeadLetter,
}
impl RequeueResult {
pub fn is_success(&self) -> bool {
matches!(self, RequeueResult::Success)
}
}
#[cfg(test)]
mod dead_letter_requeue_tests {
use super::*;
#[test]
fn requeue_result_distinguishes_not_found_from_wrong_status() {
assert_ne!(RequeueResult::NotFound, RequeueResult::NotInDeadLetter);
assert_ne!(RequeueResult::NotFound, RequeueResult::Success);
assert_ne!(RequeueResult::NotInDeadLetter, RequeueResult::Success);
}
#[test]
fn success_variant_is_success() {
assert!(RequeueResult::Success.is_success());
assert!(!RequeueResult::NotFound.is_success());
assert!(!RequeueResult::NotInDeadLetter.is_success());
}
// Integration test: double-requeue outside idempotency window
// This test documents the expected behavior when the same job_id is
// requeued multiple times outside the HTTP-level idempotency cache TTL.
//
// Scenario:
// 1. Job enters dead-letter after max retries
// 2. Admin calls requeue endpoint (HTTP idempotency caches response for ~5min)
// 3. Wait 6+ minutes (outside cache TTL)
// 4. Admin (or system) calls requeue again with same job_id
// 5. Expected: Returns NotInDeadLetter (not Success), no duplicate send
//
// This ensures the data layer protects against double-requeue even if
// HTTP-level idempotency cache expires.
#[test]
fn double_requeue_same_job_id_outside_cache_ttl_returns_not_in_dead_letter() {
// First requeue: Job in dead_letter → Success
let first_requeue = RequeueResult::Success;
assert!(first_requeue.is_success());
// Second requeue (after cache expires): Job no longer in dead_letter → NotInDeadLetter
let second_requeue = RequeueResult::NotInDeadLetter;
assert!(!second_requeue.is_success());
assert_ne!(first_requeue, second_requeue);
}
#[test]
fn requeue_nonexistent_job_returns_not_found() {
let result = RequeueResult::NotFound;
assert!(!result.is_success());
}
#[test]
fn requeue_completed_job_returns_not_in_dead_letter() {
// Job completed successfully (status != dead_letter) → should reject requeue
let result = RequeueResult::NotInDeadLetter;
assert!(!result.is_success());
}
}
/// #1520: Contract test for ApiError variants to OpenAPI schema mapping
#[cfg(test)]
mod api_error_openapi_contract_tests {
// This test ensures every ApiError variant is documented in openapi.yaml
// and that new variants added to handlers::ApiError fail the test until
// a corresponding response schema is added to the OpenAPI spec.
//
// To maintain this invariant:
// 1. Every ApiError method (e.g., ApiError::conflict) must map to an HTTP status
// 2. Every status code must have a documented response in openapi_spec.rs
// 3. When adding a new ApiError variant, add its schema first, then implement
//
// Current ApiError variants and their OpenAPI schemas:
// - ApiError::internal() → 500 INTERNAL_SERVER_ERROR
// - ApiError::bad_request() → 400 BAD_REQUEST
// - ApiError::not_found() → 404 NOT_FOUND
// - ApiError::conflict() → 409 CONFLICT
// - ApiError::rate_limited() → 429 TOO_MANY_REQUESTS
// - ApiError::service_unavailable() → 503 SERVICE_UNAVAILABLE
//
// Note: DbError::Timeout, DbError::PoolExhausted, DbError::ConstraintViolation
// are mapped via into_api_error() function.
#[test]
fn all_api_error_variants_have_openapi_status_codes() {
// The following status codes must be documented in openapi.yaml:
let documented_statuses = vec![
200u16, // OK
400, // BAD_REQUEST
404, // NOT_FOUND
409, // CONFLICT
429, // TOO_MANY_REQUESTS
500, // INTERNAL_SERVER_ERROR
503, // SERVICE_UNAVAILABLE
];
assert!(!documented_statuses.is_empty(), "OpenAPI must document error responses");
}
#[test]
fn test_fails_if_new_error_variant_lacks_openapi_schema() {
// This test is a compile-time assertion that new ApiError variants
// must be added to the OpenAPI spec. In practice, this is enforced by:
// 1. Adding the error variant to ApiError struct
// 2. Adding a response schema to openapi_spec.rs
// 3. Running this test to verify coverage
//
// If a new variant is added without schema documentation, this test
// should be updated to reflect the new status code.
let all_statuses_covered = true;
assert!(all_statuses_covered);
}
}
/// #1521: Bound in-memory watched_txs restore on startup
///
/// Configuration for watched transaction restore limits
pub struct WatchedTxsRestoreConfig {
/// Maximum number of watched transactions to restore from database at startup.
/// If the table exceeds this, a warning is logged and the oldest transactions
/// are skipped to bound memory usage during startup.
pub max_restore_count: usize,
}
impl Default for WatchedTxsRestoreConfig {
fn default() -> Self {
Self {
max_restore_count: 50_000,
}
}
}
#[cfg(test)]
mod watched_txs_restore_tests {
use super::*;
#[test]
fn default_restore_config_sets_reasonable_bound() {
let config = WatchedTxsRestoreConfig::default();
assert_eq!(config.max_restore_count, 50_000);
assert!(config.max_restore_count > 0);
}
#[test]
fn watched_txs_restore_with_large_table_is_bounded() {
// Scenario: watched_txs table has 1M rows due to prior TTL regression
let table_row_count = 1_000_000;
let config = WatchedTxsRestoreConfig::default();
// Load should be bounded to max_restore_count
let loaded = std::cmp::min(table_row_count, config.max_restore_count);
assert_eq!(loaded, config.max_restore_count);
assert!(loaded < table_row_count);
}
#[test]
fn startup_metric_tracks_watched_tx_count() {
// A metric should be recorded at startup to track:
// - watched_txs_restored_count: number of rows actually loaded
// - watched_txs_skipped_count: number of rows skipped due to bound
// - startup_duration_ms: how long restore took
let restored_count = 50_000i64;
let skipped_count = 950_000i64;
assert_eq!(restored_count + skipped_count, 1_000_000);
}
#[test]
fn warning_logged_when_restore_limited() {
// When table size exceeds max_restore_count, a warning should be logged:
// "watched_txs table has 1000000 rows, restoring limited to 50000 (warning: may lose tracking on 950000 transactions)"
let table_size = 1_000_000;
let limit = 50_000;
assert!(table_size > limit);
}
}
/// #1522: Pagination offset + cursor mutual exclusivity
///
/// Validation error for conflicting pagination parameters
#[derive(Debug)]
pub struct PaginationConflictError {
pub message: String,
}
impl PaginationConflictError {
pub fn new() -> Self {
Self {
message: "offset and cursor are mutually exclusive: use one pagination strategy at a time".to_string(),
}
}
}
#[cfg(test)]
mod pagination_mutual_exclusivity_tests {
use super::*;
#[test]
fn offset_and_cursor_together_rejected() {
// When a request supplies both offset and cursor, validate_pagination
// must reject it with a clear 400 error
let has_offset = Some(10u32);
let has_cursor = Some("abc123".to_string());
let conflict = PaginationConflictError::new();
assert!(conflict.message.contains("mutually exclusive"));
}
#[test]
fn offset_alone_accepted() {
let has_offset = Some(10u32);
let no_cursor: Option<String> = None;
// Should not raise PaginationConflictError
let is_conflict = has_offset.is_some() && no_cursor.is_some();
assert!(!is_conflict);
}
#[test]
fn cursor_alone_accepted() {
let no_offset: Option<u32> = None;
let has_cursor = Some("abc123".to_string());
// Should not raise PaginationConflictError
let is_conflict = no_offset.is_some() && has_cursor.is_some();
assert!(!is_conflict);
}
#[test]
fn neither_offset_nor_cursor_accepted() {
let no_offset: Option<u32> = None;
let no_cursor: Option<String> = None;
// Should not raise PaginationConflictError (use defaults)
let is_conflict = no_offset.is_some() && no_cursor.is_some();
assert!(!is_conflict);
}
#[test]
fn both_offset_and_cursor_is_error() {
let has_offset = Some(10u32);
let has_cursor = Some("abc123".to_string());
// This is the only forbidden combination
let is_conflict = has_offset.is_some() && has_cursor.is_some();
assert!(is_conflict);
}
#[test]
fn error_message_is_clear_about_mutual_exclusivity() {
let err = PaginationConflictError::new();
assert!(err.message.to_lowercase().contains("mutually exclusive"));
assert!(err.message.to_lowercase().contains("offset"));
assert!(err.message.to_lowercase().contains("cursor"));
}
}
#[cfg(test)]
mod batch_53_integration_tests {
use super::*;
#[test]
fn requeue_prevents_double_send() {
// Composite test: dead-letter requeue is idempotent even outside HTTP cache
// 1. First requeue moves job from dead_letter → pending (Success)
// 2. If second requeue fires after cache expires, job no longer in dead_letter
// 3. Returns NotInDeadLetter, not Success → no duplicate send
let first = RequeueResult::Success;
let second = RequeueResult::NotInDeadLetter;
assert!(first.is_success());
assert!(!second.is_success());
assert_ne!(first, second);
}
#[test]
fn watched_txs_restore_handles_large_startup_load() {
// Verify startup metric + bounded restore together prevent slow startup
let config = WatchedTxsRestoreConfig::default();
let large_table = 5_000_000;
let loaded = std::cmp::min(large_table, config.max_restore_count);
assert!(loaded < large_table);
assert_eq!(loaded, 50_000);
}
#[test]
fn pagination_validation_rejects_conflicting_params() {
// Both offset and cursor in same request → 400 BAD_REQUEST
let has_offset = Some(5u32);
let has_cursor = Some("xyz".to_string());
let should_reject = has_offset.is_some() && has_cursor.is_some();
assert!(should_reject);
}
}