| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | mod common; |
| 6 | |
| 7 | use axum::http::StatusCode; |
| 8 | use common::*; |
| 9 | use serde_json::json; |
| 10 | |
| 11 | #[tokio::test] |
| 12 | async fn concurrent_partial_patches_keep_both_fields() { |
| 13 | let (app, db) = app_and_db().await; |
| 14 | let manager = login_as(&app, &db, "term_race_manager", "manager").await; |
| 15 | |
| 16 | for round in 0..20 { |
| 17 | let created = send( |
| 18 | &app, |
| 19 | "POST", |
| 20 | "/terms", |
| 21 | Some(&manager), |
| 22 | Some(json!({ |
| 23 | "name": "before", |
| 24 | "starts_at": 1_780_000_000_000_i64, |
| 25 | "ends_at": 1_790_000_000_000_i64, |
| 26 | })), |
| 27 | ) |
| 28 | .await; |
| 29 | assert_eq!(created.status, StatusCode::CREATED, "round {round} create"); |
| 30 | let id = id_of(&created.body); |
| 31 | let uri = format!("/terms/{id}"); |
| 32 | |
| 33 | let name_patch = json!({ "name": "after" }); |
| 34 | let ends_patch = json!({ "ends_at": 1_795_000_000_000_i64 }); |
| 35 | let (a, b) = tokio::join!( |
| 36 | send(&app, "PATCH", &uri, Some(&manager), Some(name_patch)), |
| 37 | send(&app, "PATCH", &uri, Some(&manager), Some(ends_patch)), |
| 38 | ); |
| 39 | assert_eq!(a.status, StatusCode::OK, "round {round} name patch"); |
| 40 | assert_eq!(b.status, StatusCode::OK, "round {round} ends_at patch"); |
| 41 | |
| 42 | let after = send(&app, "GET", &uri, Some(&manager), None).await.body; |
| 43 | assert_eq!(after["name"], "after", "round {round}: name reverted"); |
| 44 | assert_eq!( |
| 45 | after["ends_at"], 1_795_000_000_000_i64, |
| 46 | "round {round}: ends_at reverted" |
| 47 | ); |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | |
| 52 | |
| 53 | |
| 54 | #[tokio::test] |
| 55 | async fn concurrent_range_patches_never_invert_the_term() { |
| 56 | let (app, db) = app_and_db().await; |
| 57 | let manager = login_as(&app, &db, "term_range_manager", "manager").await; |
| 58 | |
| 59 | for round in 0..20 { |
| 60 | let created = send( |
| 61 | &app, |
| 62 | "POST", |
| 63 | "/terms", |
| 64 | Some(&manager), |
| 65 | Some(json!({ |
| 66 | "name": "range", |
| 67 | "starts_at": 1_000_000_000_000_i64, |
| 68 | "ends_at": 2_000_000_000_000_i64, |
| 69 | })), |
| 70 | ) |
| 71 | .await; |
| 72 | assert_eq!(created.status, StatusCode::CREATED, "round {round} create"); |
| 73 | let uri = format!("/terms/{}", id_of(&created.body)); |
| 74 | |
| 75 | |
| 76 | |
| 77 | let (a, b) = tokio::join!( |
| 78 | send( |
| 79 | &app, |
| 80 | "PATCH", |
| 81 | &uri, |
| 82 | Some(&manager), |
| 83 | Some(json!({ "starts_at": 1_900_000_000_000_i64 })), |
| 84 | ), |
| 85 | send( |
| 86 | &app, |
| 87 | "PATCH", |
| 88 | &uri, |
| 89 | Some(&manager), |
| 90 | Some(json!({ "ends_at": 1_100_000_000_000_i64 })), |
| 91 | ), |
| 92 | ); |
| 93 | assert!( |
| 94 | a.status == StatusCode::OK || a.status == StatusCode::BAD_REQUEST, |
| 95 | "round {round} starts_at patch: {}", |
| 96 | a.status |
| 97 | ); |
| 98 | assert!( |
| 99 | b.status == StatusCode::OK || b.status == StatusCode::BAD_REQUEST, |
| 100 | "round {round} ends_at patch: {}", |
| 101 | b.status |
| 102 | ); |
| 103 | |
| 104 | let after = send(&app, "GET", &uri, Some(&manager), None).await.body; |
| 105 | let starts = after["starts_at"].as_i64().expect("starts_at"); |
| 106 | let ends = after["ends_at"].as_i64().expect("ends_at"); |
| 107 | assert!( |
| 108 | starts <= ends, |
| 109 | "round {round}: stored range inverted — starts_at {starts} > ends_at {ends}" |
| 110 | ); |
| 111 | } |
| 112 | } |