Deflake solution ordering with distinct offer times
Browse files at this commitd23fde67f92a8d90434d57a7bcb18b7f337bd57aParent: 870de5b- test
- deflake solution ordering with distinct offer times
1 changed file +15 -12
- src/domain/solution.rs +15 -12
src/domain/solution.rsmodified+15 -12
| @@ -143,8 +143,9 @@ impl Solution { | ||
| 143 | 143 | } |
| 144 | 144 | |
| 145 | 145 | /// The question's solutions, oldest first — a discussion reads downward. |
| 146 | - /// The id tiebreak keeps same-millisecond offers in a stable order (ULID | |
| 147 | - /// keys sort chronologically). | |
| 146 | + /// Ordered by `offered_at`; the `id` tiebreak only makes same-millisecond | |
| 147 | + /// offers *stable* across re-queries, not insertion-ordered (ULID low bits | |
| 148 | + /// are random within a millisecond, so same-ms order is arbitrary). | |
| 148 | 149 | pub async fn list_for( |
| 149 | 150 | question: &PoolQuestionId, |
| 150 | 151 | db: &Database, |
| @@ -252,22 +253,24 @@ mod tests { | ||
| 252 | 253 | let question_b = PoolQuestionId::generate(); |
| 253 | 254 | let author = UserId::from_key(&Ulid::new().to_string()); |
| 254 | 255 | |
| 255 | - let first = Solution::new( | |
| 256 | + // Distinct offer times so the assertion pins the real contract — | |
| 257 | + // older `offered_at` sorts first — not the same-millisecond `id` | |
| 258 | + // tiebreak, whose random ULID low bits would make the order a coin | |
| 259 | + // flip (two `now()` reads can land in one millisecond). | |
| 260 | + let mut first = Solution::new( | |
| 256 | 261 | &question_a, |
| 257 | 262 | &author, |
| 258 | 263 | SolutionBody::try_new("Önce türevi al.").unwrap(), |
| 259 | - ) | |
| 260 | - .insert(&db) | |
| 261 | - .await | |
| 262 | - .unwrap(); | |
| 263 | - let second = Solution::new( | |
| 264 | + ); | |
| 265 | + first.offered_at = Timestamp::from_millis(1); | |
| 266 | + let first = first.insert(&db).await.unwrap(); | |
| 267 | + let mut second = Solution::new( | |
| 264 | 268 | &question_a, |
| 265 | 269 | &author, |
| 266 | 270 | SolutionBody::try_new("Ya da tablo yöntemi.").unwrap(), |
| 267 | - ) | |
| 268 | - .insert(&db) | |
| 269 | - .await | |
| 270 | - .unwrap(); | |
| 271 | + ); | |
| 272 | + second.offered_at = Timestamp::from_millis(2); | |
| 273 | + let second = second.insert(&db).await.unwrap(); | |
| 271 | 274 | |
| 272 | 275 | let listed = Solution::list_for(&question_a, &db).await.unwrap(); |
| 273 | 276 | assert_eq!(listed.len(), 2); |