From d23fde67f92a8d90434d57a7bcb18b7f337bd57a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Wed, 22 Jul 2026 21:08:38 +0300 Subject: [PATCH] =?UTF-8?q?=E2=9C=85=20test(questions):=20deflake=20soluti?= =?UTF-8?q?on=20ordering=20with=20distinct=20offer=20times?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Two solutions built back-to-back can land in one millisecond, tying offered_at; the id tiebreak then rides on random ULID low bits, so the oldest-first assertion was a coin flip (~1-2/60 runs failed). Stamp distinct offered_at values so the test pins the real contract instead. Also correct the list_for comment that over-claimed same-ms ULID order. --- src/domain/solution.rs | 27 +++++++++++++++------------ 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/src/domain/solution.rs b/src/domain/solution.rs index 3d3d319..2616399 100644 --- a/src/domain/solution.rs +++ b/src/domain/solution.rs @@ -143,8 +143,9 @@ impl Solution { } /// The question's solutions, oldest first — a discussion reads downward. - /// The id tiebreak keeps same-millisecond offers in a stable order (ULID - /// keys sort chronologically). + /// Ordered by `offered_at`; the `id` tiebreak only makes same-millisecond + /// offers *stable* across re-queries, not insertion-ordered (ULID low bits + /// are random within a millisecond, so same-ms order is arbitrary). pub async fn list_for( question: &PoolQuestionId, db: &Database, @@ -252,22 +253,24 @@ mod tests { let question_b = PoolQuestionId::generate(); let author = UserId::from_key(&Ulid::new().to_string()); - let first = Solution::new( + // Distinct offer times so the assertion pins the real contract — + // older `offered_at` sorts first — not the same-millisecond `id` + // tiebreak, whose random ULID low bits would make the order a coin + // flip (two `now()` reads can land in one millisecond). + let mut first = Solution::new( &question_a, &author, SolutionBody::try_new("Önce türevi al.").unwrap(), - ) - .insert(&db) - .await - .unwrap(); - let second = Solution::new( + ); + first.offered_at = Timestamp::from_millis(1); + let first = first.insert(&db).await.unwrap(); + let mut second = Solution::new( &question_a, &author, SolutionBody::try_new("Ya da tablo yöntemi.").unwrap(), - ) - .insert(&db) - .await - .unwrap(); + ); + second.offered_at = Timestamp::from_millis(2); + let second = second.insert(&db).await.unwrap(); let listed = Solution::list_for(&question_a, &db).await.unwrap(); assert_eq!(listed.len(), 2);