| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | use crate::constant::CAS_UPDATE_RETRIES; |
| 7 | use crate::database::{Database, tx_with_retry}; |
| 8 | use crate::domain::settings::{ExamKindDef, GradeBand, MealSlotDef, Settings}; |
| 9 | use crate::error::AppError; |
| 10 | |
| 11 | |
| 12 | |
| 13 | |
| 14 | |
| 15 | |
| 16 | |
| 17 | |
| 18 | |
| 19 | const STALE_SAVE: &str = "settings_save_lost_cas"; |
| 20 | |
| 21 | |
| 22 | pub async fn load(db: &Database) -> Result<Settings, AppError> { |
| 23 | crate::db::settings::load(db).await |
| 24 | } |
| 25 | |
| 26 | |
| 27 | |
| 28 | |
| 29 | pub async fn save_if_unchanged( |
| 30 | db: &Database, |
| 31 | settings: Settings, |
| 32 | expected: &Settings, |
| 33 | ) -> Result<Option<Settings>, AppError> { |
| 34 | crate::db::settings::save_if_unchanged(db, settings, expected).await |
| 35 | } |
| 36 | |
| 37 | |
| 38 | |
| 39 | |
| 40 | |
| 41 | |
| 42 | pub struct SettingsPatch { |
| 43 | pub exam_kinds: Option<Vec<ExamKindInput>>, |
| 44 | pub attendance_statuses: Option<Vec<String>>, |
| 45 | pub grade_bands: Option<Vec<GradeBandInput>>, |
| 46 | pub max_file_bytes: Option<i64>, |
| 47 | pub chatbot_history_turns: Option<i64>, |
| 48 | pub max_chatbot_threads: Option<i64>, |
| 49 | pub max_chatbot_message_len: Option<i64>, |
| 50 | pub meal_slots: Option<Vec<MealSlotInput>>, |
| 51 | pub dietary_tags: Option<Vec<String>>, |
| 52 | |
| 53 | pub meal_cancel_cutoff_minutes: Option<Option<i64>>, |
| 54 | |
| 55 | |
| 56 | |
| 57 | pub branches: Option<Vec<String>>, |
| 58 | |
| 59 | pub excuse_kinds: Option<Vec<String>>, |
| 60 | |
| 61 | pub max_excused_absent_days: Option<Option<i64>>, |
| 62 | pub max_unexcused_absent_days: Option<Option<i64>>, |
| 63 | |
| 64 | pub timezone: Option<Option<String>>, |
| 65 | } |
| 66 | |
| 67 | |
| 68 | pub struct ExamKindInput { |
| 69 | pub name: String, |
| 70 | pub weight: i64, |
| 71 | } |
| 72 | |
| 73 | |
| 74 | pub struct GradeBandInput { |
| 75 | pub min: i64, |
| 76 | pub label: String, |
| 77 | } |
| 78 | |
| 79 | |
| 80 | pub struct MealSlotInput { |
| 81 | pub name: String, |
| 82 | pub serving_minute: Option<i64>, |
| 83 | } |
| 84 | |
| 85 | |
| 86 | |
| 87 | |
| 88 | |
| 89 | |
| 90 | |
| 91 | |
| 92 | |
| 93 | |
| 94 | |
| 95 | |
| 96 | |
| 97 | |
| 98 | |
| 99 | |
| 100 | |
| 101 | |
| 102 | |
| 103 | |
| 104 | |
| 105 | pub async fn apply(db: &Database, patch: &SettingsPatch) -> Result<Settings, AppError> { |
| 106 | |
| 107 | |
| 108 | |
| 109 | |
| 110 | for _ in 0..CAS_UPDATE_RETRIES { |
| 111 | let current = load(db).await?; |
| 112 | |
| 113 | let exam_kinds = match &patch.exam_kinds { |
| 114 | Some(kinds) => kinds |
| 115 | .iter() |
| 116 | .map(|kind| ExamKindDef::try_new(&kind.name, kind.weight)) |
| 117 | .collect::<Result<Vec<_>, _>>()?, |
| 118 | None => current.get_exam_kinds().to_vec(), |
| 119 | }; |
| 120 | let was_slots = slot_names(¤t.get_meal_slots()); |
| 121 | let meal_slots = match &patch.meal_slots { |
| 122 | Some(slots) => slots |
| 123 | .iter() |
| 124 | .map(|slot| { |
| 125 | |
| 126 | |
| 127 | |
| 128 | |
| 129 | if was_slots.iter().any(|kept| kept == slot.name.trim()) { |
| 130 | MealSlotDef::try_kept(&slot.name, slot.serving_minute) |
| 131 | } else { |
| 132 | MealSlotDef::try_new(&slot.name, slot.serving_minute) |
| 133 | } |
| 134 | }) |
| 135 | .collect::<Result<Vec<_>, _>>()?, |
| 136 | None => current.get_meal_slots(), |
| 137 | }; |
| 138 | let attendance_statuses = patch |
| 139 | .attendance_statuses |
| 140 | .clone() |
| 141 | .unwrap_or_else(|| current.get_attendance_statuses().to_vec()); |
| 142 | let grade_bands = match &patch.grade_bands { |
| 143 | Some(bands) => bands |
| 144 | .iter() |
| 145 | .map(|band| GradeBand::try_new(band.min, &band.label)) |
| 146 | .collect::<Result<Vec<_>, _>>()?, |
| 147 | None => current.get_grade_bands().to_vec(), |
| 148 | }; |
| 149 | |
| 150 | |
| 151 | let was_kinds = kind_names(current.get_exam_kinds()); |
| 152 | let now_kinds = kind_names(&exam_kinds); |
| 153 | let now_slots = slot_names(&meal_slots); |
| 154 | |
| 155 | |
| 156 | |
| 157 | let mut params = current.params(); |
| 158 | params.exam_kinds = exam_kinds; |
| 159 | params.attendance_statuses = attendance_statuses; |
| 160 | params.grade_bands = grade_bands; |
| 161 | params.max_file_bytes = patch.max_file_bytes.unwrap_or(params.max_file_bytes); |
| 162 | params.chatbot_history_turns = patch |
| 163 | .chatbot_history_turns |
| 164 | .unwrap_or(params.chatbot_history_turns); |
| 165 | params.max_chatbot_threads = patch |
| 166 | .max_chatbot_threads |
| 167 | .unwrap_or(params.max_chatbot_threads); |
| 168 | params.max_chatbot_message_len = patch |
| 169 | .max_chatbot_message_len |
| 170 | .unwrap_or(params.max_chatbot_message_len); |
| 171 | params.meal_slots = meal_slots; |
| 172 | params.dietary_tags = patch |
| 173 | .dietary_tags |
| 174 | .clone() |
| 175 | .unwrap_or_else(|| current.get_dietary_tags()); |
| 176 | |
| 177 | params.meal_cancel_cutoff_minutes = patch |
| 178 | .meal_cancel_cutoff_minutes |
| 179 | .unwrap_or(params.meal_cancel_cutoff_minutes); |
| 180 | |
| 181 | |
| 182 | |
| 183 | |
| 184 | |
| 185 | |
| 186 | |
| 187 | params.branches = patch |
| 188 | .branches |
| 189 | .clone() |
| 190 | .unwrap_or_else(|| current.get_branches()); |
| 191 | params.excuse_kinds = patch |
| 192 | .excuse_kinds |
| 193 | .clone() |
| 194 | .unwrap_or_else(|| current.get_excuse_kinds()); |
| 195 | |
| 196 | |
| 197 | params.max_excused_absent_days = patch |
| 198 | .max_excused_absent_days |
| 199 | .unwrap_or(params.max_excused_absent_days); |
| 200 | params.max_unexcused_absent_days = patch |
| 201 | .max_unexcused_absent_days |
| 202 | .unwrap_or(params.max_unexcused_absent_days); |
| 203 | |
| 204 | |
| 205 | params.timezone = patch.timezone.clone().unwrap_or(params.timezone.clone()); |
| 206 | |
| 207 | let settings = Settings::try_new(params)?; |
| 208 | |
| 209 | |
| 210 | |
| 211 | |
| 212 | let attempted = tx_with_retry(db, false, async move |tx| { |
| 213 | |
| 214 | |
| 215 | |
| 216 | |
| 217 | |
| 218 | for gone in missing(&was_kinds, &now_kinds) { |
| 219 | retire_kind(&mut *tx, &gone).await?; |
| 220 | } |
| 221 | |
| 222 | |
| 223 | |
| 224 | |
| 225 | for gone in missing(&was_slots, &now_slots) { |
| 226 | retire_slot(&mut *tx, &gone).await?; |
| 227 | } |
| 228 | |
| 229 | |
| 230 | |
| 231 | |
| 232 | |
| 233 | for back in missing(&now_kinds, &was_kinds) { |
| 234 | unretire_kind(&mut *tx, &back).await?; |
| 235 | } |
| 236 | for back in missing(&now_slots, &was_slots) { |
| 237 | unretire_slot(&mut *tx, &back).await?; |
| 238 | } |
| 239 | |
| 240 | match crate::db::settings::save_if_unchanged(&mut *tx, settings.clone(), ¤t) |
| 241 | .await? |
| 242 | { |
| 243 | Some(saved) => Ok(saved), |
| 244 | |
| 245 | |
| 246 | |
| 247 | |
| 248 | None => Err(AppError::Internal(STALE_SAVE.to_string())), |
| 249 | } |
| 250 | }) |
| 251 | .await; |
| 252 | |
| 253 | match attempted { |
| 254 | Ok(saved) => return Ok(saved), |
| 255 | Err(AppError::Internal(marker)) if marker == STALE_SAVE => continue, |
| 256 | Err(err) => return Err(err), |
| 257 | } |
| 258 | } |
| 259 | Err(AppError::Conflict( |
| 260 | "the settings kept changing underneath this update — try again", |
| 261 | )) |
| 262 | } |
| 263 | |
| 264 | |
| 265 | |
| 266 | |
| 267 | |
| 268 | |
| 269 | |
| 270 | |
| 271 | |
| 272 | |
| 273 | |
| 274 | async fn retire_kind(tx: &mut sqlx::PgConnection, name: &str) -> Result<(), AppError> { |
| 275 | let landed = sqlx::query!( |
| 276 | r#"INSERT INTO kind_ref (name, count, retired) VALUES ($1, 0, TRUE) |
| 277 | ON CONFLICT (name) DO UPDATE SET retired = TRUE |
| 278 | WHERE kind_ref.count = 0 AND kind_ref.retired IS DISTINCT FROM TRUE |
| 279 | RETURNING 1 AS "landed!: i64""#, |
| 280 | name |
| 281 | ) |
| 282 | .fetch_optional(&mut *tx) |
| 283 | .await? |
| 284 | .is_some(); |
| 285 | if landed { |
| 286 | |
| 287 | |
| 288 | return Ok(()); |
| 289 | } |
| 290 | |
| 291 | |
| 292 | |
| 293 | |
| 294 | |
| 295 | |
| 296 | let row = sqlx::query!( |
| 297 | r#"SELECT count AS "count!: i64", retired AS "retired!: bool" |
| 298 | FROM kind_ref WHERE name = $1 FOR UPDATE"#, |
| 299 | name |
| 300 | ) |
| 301 | .fetch_one(&mut *tx) |
| 302 | .await?; |
| 303 | if row.count > 0 { |
| 304 | return Err(AppError::ConflictOwned(format!( |
| 305 | "exams of kind '{name}' are already graded — the kind cannot be removed" |
| 306 | ))); |
| 307 | } |
| 308 | let _ = row.retired; |
| 309 | Ok(()) |
| 310 | } |
| 311 | |
| 312 | |
| 313 | async fn retire_slot(tx: &mut sqlx::PgConnection, name: &str) -> Result<(), AppError> { |
| 314 | |
| 315 | |
| 316 | |
| 317 | |
| 318 | let landed = sqlx::query!( |
| 319 | r#"INSERT INTO slot_ref (name, count, retired) VALUES ($1, 0, TRUE) |
| 320 | ON CONFLICT (name) DO UPDATE SET retired = TRUE |
| 321 | WHERE slot_ref.count = 0 AND slot_ref.retired IS DISTINCT FROM TRUE |
| 322 | RETURNING 1 AS "landed!: i64""#, |
| 323 | name |
| 324 | ) |
| 325 | .fetch_optional(&mut *tx) |
| 326 | .await? |
| 327 | .is_some(); |
| 328 | if landed { |
| 329 | return Ok(()); |
| 330 | } |
| 331 | let row = sqlx::query!( |
| 332 | r#"SELECT count AS "count!: i64" FROM slot_ref WHERE name = $1 FOR UPDATE"#, |
| 333 | name |
| 334 | ) |
| 335 | .fetch_one(&mut *tx) |
| 336 | .await?; |
| 337 | if row.count > 0 { |
| 338 | return Err(AppError::ConflictOwned(format!( |
| 339 | "menus are already published for the '{name}' slot — it cannot be removed" |
| 340 | ))); |
| 341 | } |
| 342 | Ok(()) |
| 343 | } |
| 344 | |
| 345 | |
| 346 | |
| 347 | |
| 348 | |
| 349 | |
| 350 | async fn unretire_kind(tx: &mut sqlx::PgConnection, name: &str) -> Result<(), AppError> { |
| 351 | sqlx::query!( |
| 352 | r#"INSERT INTO kind_ref (name, count, retired) VALUES ($1, 0, FALSE) |
| 353 | ON CONFLICT (name) DO UPDATE SET retired = FALSE |
| 354 | WHERE kind_ref.retired IS DISTINCT FROM FALSE |
| 355 | RETURNING 1 AS "landed!: i64""#, |
| 356 | name |
| 357 | ) |
| 358 | .fetch_optional(&mut *tx) |
| 359 | .await?; |
| 360 | Ok(()) |
| 361 | } |
| 362 | |
| 363 | |
| 364 | async fn unretire_slot(tx: &mut sqlx::PgConnection, name: &str) -> Result<(), AppError> { |
| 365 | sqlx::query!( |
| 366 | r#"INSERT INTO slot_ref (name, count, retired) VALUES ($1, 0, FALSE) |
| 367 | ON CONFLICT (name) DO UPDATE SET retired = FALSE |
| 368 | WHERE slot_ref.retired IS DISTINCT FROM FALSE |
| 369 | RETURNING 1 AS "landed!: i64""#, |
| 370 | name |
| 371 | ) |
| 372 | .fetch_optional(&mut *tx) |
| 373 | .await?; |
| 374 | Ok(()) |
| 375 | } |
| 376 | |
| 377 | fn kind_names(kinds: &[ExamKindDef]) -> Vec<String> { |
| 378 | kinds |
| 379 | .iter() |
| 380 | .map(|kind| kind.get_name().to_string()) |
| 381 | .collect() |
| 382 | } |
| 383 | |
| 384 | fn slot_names(slots: &[MealSlotDef]) -> Vec<String> { |
| 385 | slots |
| 386 | .iter() |
| 387 | .map(|slot| slot.get_name().to_string()) |
| 388 | .collect() |
| 389 | } |
| 390 | |
| 391 | |
| 392 | |
| 393 | fn missing(before: &[String], after: &[String]) -> Vec<String> { |
| 394 | before |
| 395 | .iter() |
| 396 | .filter(|name| !after.contains(name)) |
| 397 | .cloned() |
| 398 | .collect() |
| 399 | } |
| 400 | |
| 401 | #[cfg(test)] |
| 402 | mod tests { |
| 403 | use super::*; |
| 404 | use crate::database::init_test_db; |
| 405 | use crate::db::settings::save; |
| 406 | |
| 407 | |
| 408 | |
| 409 | async fn ref_bit(db: &Database, table: &str, name: &str) -> Option<bool> { |
| 410 | use sqlx::Row as _; |
| 411 | |
| 412 | let row = sqlx::query(sqlx::AssertSqlSafe(format!( |
| 413 | "SELECT retired FROM {table} WHERE name = $1" |
| 414 | ))) |
| 415 | .bind(name) |
| 416 | .fetch_optional(db) |
| 417 | .await |
| 418 | .unwrap(); |
| 419 | row.map(|row| row.try_get::<bool, _>(0).unwrap()) |
| 420 | } |
| 421 | |
| 422 | |
| 423 | async fn kind_bit(db: &Database, name: &str) -> Option<bool> { |
| 424 | ref_bit(db, "kind_ref", name).await |
| 425 | } |
| 426 | |
| 427 | |
| 428 | async fn slot_bit(db: &Database, name: &str) -> Option<bool> { |
| 429 | ref_bit(db, "slot_ref", name).await |
| 430 | } |
| 431 | |
| 432 | |
| 433 | |
| 434 | |
| 435 | |
| 436 | |
| 437 | |
| 438 | |
| 439 | |
| 440 | #[tokio::test] |
| 441 | async fn the_loser_of_a_settings_race_cannot_un_retire_the_winner_s_kind() { |
| 442 | let (db, _leases) = init_test_db().await; |
| 443 | |
| 444 | |
| 445 | |
| 446 | |
| 447 | |
| 448 | |
| 449 | |
| 450 | let without_sozlu = SettingsPatch { |
| 451 | exam_kinds: Some(kinds(&["yazili", "uygulama"])), |
| 452 | ..a_patch() |
| 453 | }; |
| 454 | apply(&db, &without_sozlu).await.unwrap(); |
| 455 | apply(&db, &without_sozlu).await.unwrap(); |
| 456 | |
| 457 | assert_eq!( |
| 458 | kind_bit(&db, "sozlu").await, |
| 459 | Some(true), |
| 460 | "the winner's retirement must outlive the loser's rollback" |
| 461 | ); |
| 462 | assert!(!stored(&db).await.0.contains(&"sozlu".to_string())); |
| 463 | } |
| 464 | |
| 465 | |
| 466 | |
| 467 | |
| 468 | #[tokio::test] |
| 469 | async fn a_rollback_re_retires_a_name_this_attempt_put_back_in_service() { |
| 470 | let (db, _leases) = init_test_db().await; |
| 471 | |
| 472 | let without = SettingsPatch { |
| 473 | meal_slots: Some(slots(&["breakfast", "snack"])), |
| 474 | ..a_patch() |
| 475 | }; |
| 476 | apply(&db, &without).await.unwrap(); |
| 477 | assert_eq!(slot_bit(&db, "lunch").await, Some(true)); |
| 478 | |
| 479 | |
| 480 | |
| 481 | |
| 482 | |
| 483 | |
| 484 | let stale_snapshot = load(&db).await.unwrap(); |
| 485 | let elsewhere = SettingsPatch { |
| 486 | max_file_bytes: Some(2048), |
| 487 | ..a_patch() |
| 488 | }; |
| 489 | apply(&db, &elsewhere).await.unwrap(); |
| 490 | |
| 491 | let mut params = Settings::defaults().params(); |
| 492 | params.meal_slots = vec![MealSlotDef::try_new("lunch", None).unwrap()]; |
| 493 | let re_added = Settings::try_new(params).unwrap(); |
| 494 | let landed = crate::db::settings::save_if_unchanged(&db, re_added, &stale_snapshot) |
| 495 | .await |
| 496 | .unwrap(); |
| 497 | assert!(landed.is_none(), "the stale save must be refused"); |
| 498 | |
| 499 | assert_eq!( |
| 500 | slot_bit(&db, "lunch").await, |
| 501 | Some(true), |
| 502 | "a re-add that never landed must leave the slot retired" |
| 503 | ); |
| 504 | } |
| 505 | |
| 506 | |
| 507 | |
| 508 | |
| 509 | |
| 510 | #[tokio::test] |
| 511 | async fn a_rollback_leaves_a_name_that_gained_a_reference_in_service() { |
| 512 | let (db, _leases) = init_test_db().await; |
| 513 | |
| 514 | |
| 515 | let without = SettingsPatch { |
| 516 | meal_slots: Some(slots(&["breakfast", "snack"])), |
| 517 | ..a_patch() |
| 518 | }; |
| 519 | apply(&db, &without).await.unwrap(); |
| 520 | let with = SettingsPatch { |
| 521 | meal_slots: Some(slots(&["breakfast", "lunch", "snack"])), |
| 522 | ..a_patch() |
| 523 | }; |
| 524 | apply(&db, &with).await.unwrap(); |
| 525 | sqlx::query("UPDATE slot_ref SET count = 1 WHERE name = 'lunch'") |
| 526 | .execute(&db) |
| 527 | .await |
| 528 | .unwrap(); |
| 529 | |
| 530 | |
| 531 | |
| 532 | |
| 533 | let refused = apply(&db, &without).await; |
| 534 | assert!( |
| 535 | matches!(refused, Err(AppError::ConflictOwned(ref msg)) if msg.contains("'lunch'")), |
| 536 | "a referenced slot's removal must be refused: {refused:?}" |
| 537 | ); |
| 538 | assert_eq!(slot_bit(&db, "lunch").await, Some(false)); |
| 539 | } |
| 540 | |
| 541 | |
| 542 | |
| 543 | |
| 544 | fn a_patch() -> SettingsPatch { |
| 545 | SettingsPatch { |
| 546 | exam_kinds: None, |
| 547 | attendance_statuses: None, |
| 548 | grade_bands: None, |
| 549 | max_file_bytes: None, |
| 550 | chatbot_history_turns: None, |
| 551 | max_chatbot_threads: None, |
| 552 | max_chatbot_message_len: None, |
| 553 | meal_slots: None, |
| 554 | dietary_tags: None, |
| 555 | meal_cancel_cutoff_minutes: None, |
| 556 | branches: None, |
| 557 | excuse_kinds: None, |
| 558 | max_excused_absent_days: None, |
| 559 | max_unexcused_absent_days: None, |
| 560 | timezone: None, |
| 561 | } |
| 562 | } |
| 563 | |
| 564 | |
| 565 | |
| 566 | fn kinds(names: &[&str]) -> Vec<ExamKindInput> { |
| 567 | names |
| 568 | .iter() |
| 569 | .map(|name| ExamKindInput { |
| 570 | name: (*name).to_string(), |
| 571 | weight: 1, |
| 572 | }) |
| 573 | .collect() |
| 574 | } |
| 575 | |
| 576 | |
| 577 | fn slots(names: &[&str]) -> Vec<MealSlotInput> { |
| 578 | names |
| 579 | .iter() |
| 580 | .map(|name| MealSlotInput { |
| 581 | name: (*name).to_string(), |
| 582 | serving_minute: None, |
| 583 | }) |
| 584 | .collect() |
| 585 | } |
| 586 | |
| 587 | |
| 588 | async fn stored(db: &Database) -> (Vec<String>, Vec<String>) { |
| 589 | let settings = load(db).await.unwrap(); |
| 590 | ( |
| 591 | kind_names(settings.get_exam_kinds()), |
| 592 | slot_names(&settings.get_meal_slots()), |
| 593 | ) |
| 594 | } |
| 595 | |
| 596 | |
| 597 | |
| 598 | |
| 599 | |
| 600 | |
| 601 | |
| 602 | |
| 603 | |
| 604 | |
| 605 | |
| 606 | |
| 607 | #[tokio::test] |
| 608 | async fn two_edits_dropping_the_same_kind_leave_it_retired() { |
| 609 | let (db, _leases) = init_test_db().await; |
| 610 | let without = SettingsPatch { |
| 611 | exam_kinds: Some(kinds(&["yazili", "uygulama"])), |
| 612 | ..a_patch() |
| 613 | }; |
| 614 | |
| 615 | let (a, b) = tokio::join!(apply(&db, &without), apply(&db, &without)); |
| 616 | a.unwrap(); |
| 617 | b.unwrap(); |
| 618 | |
| 619 | let (kinds, _) = stored(&db).await; |
| 620 | assert!( |
| 621 | !kinds.contains(&"sozlu".to_string()), |
| 622 | "dropped from the list" |
| 623 | ); |
| 624 | assert_eq!( |
| 625 | kind_bit(&db, "sozlu").await, |
| 626 | Some(true), |
| 627 | "a kind the stored list no longer offers must not grade" |
| 628 | ); |
| 629 | } |
| 630 | |
| 631 | |
| 632 | |
| 633 | |
| 634 | |
| 635 | |
| 636 | |
| 637 | |
| 638 | |
| 639 | |
| 640 | |
| 641 | |
| 642 | #[tokio::test] |
| 643 | async fn a_rival_edit_cannot_run_between_a_retirement_and_its_save() { |
| 644 | let (db, _leases) = init_test_db().await; |
| 645 | let without = SettingsPatch { |
| 646 | exam_kinds: Some(kinds(&["yazili", "uygulama"])), |
| 647 | ..a_patch() |
| 648 | }; |
| 649 | |
| 650 | |
| 651 | |
| 652 | |
| 653 | |
| 654 | |
| 655 | let rival = tokio::spawn({ |
| 656 | let db = db.clone(); |
| 657 | async move { apply(&db, &without).await.map(|_| ()) } |
| 658 | }); |
| 659 | while !rival.is_finished() { |
| 660 | |
| 661 | |
| 662 | |
| 663 | |
| 664 | let retired = kind_bit(&db, "sozlu").await; |
| 665 | let (kinds, _) = stored(&db).await; |
| 666 | if retired == Some(true) { |
| 667 | assert!( |
| 668 | !kinds.contains(&"sozlu".to_string()), |
| 669 | "a retirement became visible before its list commit" |
| 670 | ); |
| 671 | } |
| 672 | if retired == Some(false) { |
| 673 | assert!( |
| 674 | kinds.contains(&"sozlu".to_string()), |
| 675 | "an un-retirement became visible before its list commit" |
| 676 | ); |
| 677 | } |
| 678 | tokio::task::yield_now().await; |
| 679 | } |
| 680 | |
| 681 | rival.await.unwrap().unwrap(); |
| 682 | assert_eq!(kind_bit(&db, "sozlu").await, Some(true)); |
| 683 | assert!(!stored(&db).await.0.contains(&"sozlu".to_string())); |
| 684 | } |
| 685 | |
| 686 | |
| 687 | |
| 688 | |
| 689 | #[tokio::test] |
| 690 | async fn two_edits_re_adding_the_same_kind_leave_it_in_service() { |
| 691 | let (db, _leases) = init_test_db().await; |
| 692 | let without = SettingsPatch { |
| 693 | exam_kinds: Some(kinds(&["yazili", "uygulama"])), |
| 694 | ..a_patch() |
| 695 | }; |
| 696 | apply(&db, &without).await.unwrap(); |
| 697 | let with = SettingsPatch { |
| 698 | exam_kinds: Some(kinds(&["yazili", "sozlu", "uygulama"])), |
| 699 | ..a_patch() |
| 700 | }; |
| 701 | |
| 702 | let (a, b) = tokio::join!(apply(&db, &with), apply(&db, &with)); |
| 703 | a.unwrap(); |
| 704 | b.unwrap(); |
| 705 | |
| 706 | let (kinds, _) = stored(&db).await; |
| 707 | assert!(kinds.contains(&"sozlu".to_string()), "back on the list"); |
| 708 | assert_eq!( |
| 709 | kind_bit(&db, "sozlu").await, |
| 710 | Some(false), |
| 711 | "a kind the stored list offers must grade" |
| 712 | ); |
| 713 | } |
| 714 | |
| 715 | |
| 716 | #[tokio::test] |
| 717 | async fn two_edits_dropping_the_same_slot_leave_it_retired() { |
| 718 | let (db, _leases) = init_test_db().await; |
| 719 | let without = SettingsPatch { |
| 720 | meal_slots: Some(slots(&["breakfast", "snack"])), |
| 721 | ..a_patch() |
| 722 | }; |
| 723 | |
| 724 | let (a, b) = tokio::join!(apply(&db, &without), apply(&db, &without)); |
| 725 | a.unwrap(); |
| 726 | b.unwrap(); |
| 727 | |
| 728 | let (_, stored_slots) = stored(&db).await; |
| 729 | assert!(!stored_slots.contains(&"lunch".to_string())); |
| 730 | assert_eq!(slot_bit(&db, "lunch").await, Some(true)); |
| 731 | } |
| 732 | |
| 733 | |
| 734 | |
| 735 | |
| 736 | async fn a_school_with_a_stale_slot(db: &Database) { |
| 737 | let mut params = Settings::defaults().params(); |
| 738 | params.meal_slots = vec![ |
| 739 | MealSlotDef::try_kept("a/b", None).unwrap(), |
| 740 | MealSlotDef::try_new("lunch", None).unwrap(), |
| 741 | ]; |
| 742 | save(db, Settings::try_new(params).unwrap()).await.unwrap(); |
| 743 | } |
| 744 | |
| 745 | |
| 746 | |
| 747 | |
| 748 | |
| 749 | #[tokio::test] |
| 750 | async fn a_stored_slot_name_stays_submittable_but_a_new_one_does_not() { |
| 751 | let (db, _leases) = init_test_db().await; |
| 752 | a_school_with_a_stale_slot(&db).await; |
| 753 | |
| 754 | let edit = SettingsPatch { |
| 755 | meal_slots: Some(slots(&["a/b", "lunch", "dinner"])), |
| 756 | ..a_patch() |
| 757 | }; |
| 758 | apply(&db, &edit).await.unwrap(); |
| 759 | let (_, stored_slots) = stored(&db).await; |
| 760 | assert_eq!(stored_slots, ["a/b", "lunch", "dinner"]); |
| 761 | |
| 762 | let fresh = SettingsPatch { |
| 763 | meal_slots: Some(slots(&["a/b", "lunch", "c/d"])), |
| 764 | ..a_patch() |
| 765 | }; |
| 766 | assert!( |
| 767 | apply(&db, &fresh).await.is_err(), |
| 768 | "a name no school ever stored is still refused" |
| 769 | ); |
| 770 | } |
| 771 | |
| 772 | |
| 773 | |
| 774 | #[tokio::test] |
| 775 | async fn an_unrelated_edit_carries_a_stale_slot_name_over() { |
| 776 | let (db, _leases) = init_test_db().await; |
| 777 | a_school_with_a_stale_slot(&db).await; |
| 778 | |
| 779 | let elsewhere = SettingsPatch { |
| 780 | max_file_bytes: Some(2048), |
| 781 | ..a_patch() |
| 782 | }; |
| 783 | let saved = apply(&db, &elsewhere).await.unwrap(); |
| 784 | |
| 785 | assert_eq!(saved.get_max_file_bytes(), 2048); |
| 786 | assert_eq!(slot_names(&saved.get_meal_slots()), ["a/b", "lunch"]); |
| 787 | } |
| 788 | } |