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 | /// The question's solutions, oldest first — a discussion reads downward. | 145 | /// The question's solutions, oldest first — a discussion reads downward. |
| 146 | - /// The id tiebreak keeps same-millisecond offers in a stable order (ULID | 146 | + /// Ordered by `offered_at`; the `id` tiebreak only makes same-millisecond |
| 147 | - /// keys sort chronologically). | 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 | pub async fn list_for( | 149 | pub async fn list_for( |
| 149 | question: &PoolQuestionId, | 150 | question: &PoolQuestionId, |
| 150 | db: &Database, | 151 | db: &Database, |
| @@ -252,22 +253,24 @@ mod tests { | |||
| 252 | let question_b = PoolQuestionId::generate(); | 253 | let question_b = PoolQuestionId::generate(); |
| 253 | let author = UserId::from_key(&Ulid::new().to_string()); | 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 | &question_a, | 261 | &question_a, |
| 257 | &author, | 262 | &author, |
| 258 | SolutionBody::try_new("Önce türevi al.").unwrap(), | 263 | SolutionBody::try_new("Önce türevi al.").unwrap(), |
| 259 | - ) | 264 | + ); |
| 260 | - .insert(&db) | 265 | + first.offered_at = Timestamp::from_millis(1); |
| 261 | - .await | 266 | + let first = first.insert(&db).await.unwrap(); |
| 262 | - .unwrap(); | 267 | + let mut second = Solution::new( |
| 263 | - let second = Solution::new( | ||
| 264 | &question_a, | 268 | &question_a, |
| 265 | &author, | 269 | &author, |
| 266 | SolutionBody::try_new("Ya da tablo yöntemi.").unwrap(), | 270 | SolutionBody::try_new("Ya da tablo yöntemi.").unwrap(), |
| 267 | - ) | 271 | + ); |
| 268 | - .insert(&db) | 272 | + second.offered_at = Timestamp::from_millis(2); |
| 269 | - .await | 273 | + let second = second.insert(&db).await.unwrap(); |
| 270 | - .unwrap(); | ||
| 271 | 274 | ||
| 272 | let listed = Solution::list_for(&question_a, &db).await.unwrap(); | 275 | let listed = Solution::list_for(&question_a, &db).await.unwrap(); |
| 273 | assert_eq!(listed.len(), 2); | 276 | assert_eq!(listed.len(), 2); |