Skip to content

Commit 4f3cc21

Browse files
authored
Server: deflake test_lock (#989)
## Motivation This test repeatedly fails, but not always. A real frustration! ## Solution Rephrasing the assertions may help. Instead of using fixed deadlines, assert based on relative durations between the first and second.
2 parents 14e63f9 + d44ddd1 commit 4f3cc21

File tree

1 file changed

+8
-6
lines changed

1 file changed

+8
-6
lines changed

server/svix-server/src/core/idempotency.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -526,10 +526,9 @@ mod tests {
526526

527527
#[tokio::test]
528528
async fn test_lock() {
529-
// The sleep interval is 200ms, so have it wait 300ms causing it to sleep twice. This
530-
// means it should take at least 400ms for the second task to respond.
531-
let (_jh, endpoint, _count) =
532-
start_service(Some(std::time::Duration::from_millis(300))).await;
529+
let sleep_duration = std::time::Duration::from_millis(300);
530+
531+
let (_jh, endpoint, _count) = start_service(Some(sleep_duration)).await;
533532
let client = Client::new();
534533

535534
// Generate a new token so that keys are unique
@@ -564,8 +563,11 @@ mod tests {
564563
let resp_2 = resp_2_jh.await.unwrap().unwrap();
565564
let resp_2_instant = std::time::Instant::now();
566565

567-
assert!(resp_1_instant - start < std::time::Duration::from_millis(350));
568-
assert!(resp_2_instant - start > std::time::Duration::from_millis(400));
566+
// resp_1 should take some variable amount of time thanks to the sleep.
567+
assert!(resp_1_instant - start >= sleep_duration);
568+
// resp_2 should take less than the sleep (300) since it's just waiting for the same
569+
// response given to the first request.
570+
assert!(resp_2_instant - resp_1_instant < sleep_duration);
569571

570572
// And the responses should be equivalent
571573
assert_eq!(resp_1.status(), resp_2.status());

0 commit comments

Comments
 (0)