reord 0.2.2

Run your tests multi-threaded, but in a reproducible way
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
use futures::future::join_all;
use rand::{rngs::StdRng, Rng, SeedableRng};
use tokio::sync::Mutex;

use crate as reord;
use std::{
    sync::{
        atomic::{AtomicUsize, Ordering},
        Arc,
    },
    time::Duration,
};

#[tokio::test]
async fn basic_working_test() {
    println!("before initializing test");
    reord::init_test(reord::Config::with_random_seed()).await;
    println!("initialized test");

    let a = tokio::task::spawn(reord::new_task(async move {
        println!("started task 1");
        reord::point().await;
        println!("finished task 1");
    }));

    let b = tokio::task::spawn(reord::new_task(async move {
        println!("started task 2");
        reord::point().await;
        println!("finished task 2");
    }));

    println!("before running tests");
    let h = reord::start(2).await;
    println!("finished tests");

    tokio::try_join!(a, b, h).unwrap();
}

#[tokio::test]
async fn basic_failing_test() {
    reord::init_test(reord::Config::from_seed(Default::default())).await;

    let data = Arc::new(AtomicUsize::new(0));
    let data2 = data.clone();

    let a = tokio::task::spawn(reord::new_task(async move {
        data.fetch_add(1, Ordering::Relaxed);
        reord::point().await;
        assert!(data.load(Ordering::Relaxed) < 2);
    }));

    let b = tokio::task::spawn(reord::new_task(async move {
        data2.fetch_add(1, Ordering::Relaxed);
        reord::point().await;
        assert!(data2.load(Ordering::Relaxed) < 2);
    }));

    let h = reord::start(2).await;

    tokio::try_join!(a, b, h).unwrap_err();
}

#[tokio::test]
async fn check_failing_locks() {
    tracing_subscriber::FmtSubscriber::builder()
        .with_max_level(tracing::Level::TRACE)
        .init();

    reord::init_test(reord::Config {
        check_named_locks_work_for: Some(Duration::from_secs(1)),
        ..reord::Config::from_seed(0)
    })
    .await;

    let a = tokio::task::spawn(reord::new_task(async move {
        println!("before lock 1");
        {
            let _l = reord::Lock::take_named(String::from("foo")).await;
            println!("in lock 1");
            reord::point().await;
            reord::point().await;
            reord::point().await;
            reord::point().await;
            reord::point().await;
            reord::point().await;
        }
        println!("after lock 1");
        reord::point().await;
    }));

    let b = tokio::task::spawn(reord::new_task(async move {
        println!("before lock 2");
        {
            let _l = reord::Lock::take_named(String::from("foo")).await;
            println!("in lock 2");
            reord::point().await;
            reord::point().await;
            reord::point().await;
            reord::point().await;
            reord::point().await;
            reord::point().await;
            reord::point().await;
        }
        println!("after lock 2");
        reord::point().await;
    }));

    let h = reord::start(2).await;

    let (_, _, msg) = tokio::join!(a, b, h);
    assert!(msg.unwrap_err().is_panic());
}

#[tokio::test]
async fn check_passing_locks() {
    tracing_subscriber::FmtSubscriber::builder()
        .with_max_level(tracing::Level::TRACE)
        .init();

    reord::init_test(reord::Config {
        check_named_locks_work_for: Some(Duration::from_secs(1)),
        ..reord::Config::from_seed(Default::default())
    })
    .await;

    let lock = std::sync::Arc::new(tokio::sync::Mutex::new(()));
    let lock2 = lock.clone();
    let a = tokio::task::spawn(reord::new_task(async move {
        println!("before lock 1");
        {
            let _l = reord::Lock::take_named(String::from("foo")).await;
            println!("taking lock 1");
            let _l = lock.lock().await;
            println!("in lock 1");
            reord::point().await;
        }
        reord::point().await;
        println!("after lock 1");
    }));

    let b = tokio::task::spawn(reord::new_task(async move {
        println!("before lock 2");
        {
            let _l = reord::Lock::take_named(String::from("foo")).await;
            println!("taking lock 2");
            let _l = lock2.lock().await;
            println!("in lock 2");
            reord::point().await;
        }
        reord::point().await;
        println!("after lock 2");
    }));

    let h = reord::start(2).await;

    tokio::try_join!(a, b, h).unwrap();
}

#[tokio::test]
async fn detect_deadlock() {
    tracing_subscriber::FmtSubscriber::builder()
        .with_max_level(tracing::Level::TRACE)
        .init();

    reord::init_test(reord::Config {
        check_addressed_locks_work_for: Some(Duration::from_secs(1)),
        ..reord::Config::from_seed(0)
    })
    .await;

    let lock1 = std::sync::Arc::new(tokio::sync::Mutex::new(()));
    let lock1_clone = lock1.clone();
    let lock2 = std::sync::Arc::new(tokio::sync::Mutex::new(()));
    let lock2_clone = lock2.clone();
    let a = tokio::task::spawn(reord::new_task(async move {
        {
            println!("A taking lock 1");
            let _l = reord::Lock::take_addressed(1).await;
            let _l = lock1.lock().await;
            reord::point().await;
            reord::point().await;
            reord::point().await;
            reord::point().await;
            eprintln!("A taking lock 2");
            let _l = reord::Lock::take_addressed(2).await;
            let _l = lock2.lock().await;
            println!("A successfully taken both locks");
            reord::point().await;
        }
        println!("A after unlock");
    }));

    let b = tokio::task::spawn(reord::new_task(async move {
        {
            println!("B taking lock 2");
            let _l = reord::Lock::take_addressed(2).await;
            let _l = lock2_clone.lock().await;
            reord::point().await;
            reord::point().await;
            reord::point().await;
            reord::point().await;
            eprintln!("B taking lock 1");
            let _l = reord::Lock::take_addressed(1).await;
            let _l = lock1_clone.lock().await;
            println!("B successfully taken both locks");
            reord::point().await;
        }
        println!("B after unlock");
    }));

    let h = reord::start(2).await;

    tokio::try_join!(a, b, h).unwrap_err();
}

#[tokio::test]
#[allow(non_snake_case)]
async fn lock_LUL_vs_L_deadlocked() {
    // This bug was with the following interleaving:
    // - A takes lock 1
    // - B takes lock 1. Reord lets B go for it in order to check that B doesn't progress. It works fine, reord continues.
    // - A releases lock 1. This should give the lock to B, but here reord was then counting the lock as being entirely free.
    // - A takes lock 1. This should be prevented by reord, but reord mistakenly thought that lock 1 was free. Ergo, deadlock.
    tracing_subscriber::FmtSubscriber::builder()
        .with_max_level(tracing::Level::TRACE)
        .init();

    reord::init_test(reord::Config {
        check_addressed_locks_work_for: Some(Duration::from_secs(1)),
        ..reord::Config::from_seed(Default::default())
    })
    .await;

    let lock = std::sync::Arc::new(tokio::sync::Mutex::new(()));
    let lock_clone = lock.clone();
    let a = tokio::task::spawn(reord::new_task(async move {
        {
            {
                println!("A taking lock 1");
                let _l = reord::Lock::take_addressed(1).await;
                let _l = lock.lock().await;
                reord::point().await;
                eprintln!("A releasing lock 1");
            }
            reord::point().await;
            eprintln!("A taking lock 1");
            let _l = reord::Lock::take_addressed(1).await;
            let _l = lock.lock().await;
        }
        println!("A after unlock");
    }));

    let b = tokio::task::spawn(reord::new_task(async move {
        {
            eprintln!("B taking lock 1");
            let _l = reord::Lock::take_addressed(1).await;
            let _l = lock_clone.lock().await;
            // A few awaits to make sure the RNG makes B keep the lock for long enough to get back to A
            reord::point().await;
            reord::point().await;
            reord::point().await;
            reord::point().await;
            reord::point().await;
        }
        println!("B after unlock");
    }));

    let h = reord::start(2).await;

    tokio::try_join!(a, b, h).unwrap();
}

#[tokio::test]
async fn check_passing_two_locks() {
    reord::init_test(reord::Config {
        check_named_locks_work_for: Some(Duration::from_secs(1)),
        ..reord::Config::from_seed(Default::default())
    })
    .await;

    let lock1 = std::sync::Arc::new(tokio::sync::Mutex::new(()));
    let lock1_clone = lock1.clone();
    let lock2 = std::sync::Arc::new(tokio::sync::Mutex::new(()));
    let lock2_clone = lock2.clone();
    let a = tokio::task::spawn(reord::new_task(async move {
        println!("before lock 1");
        {
            let _l = reord::Lock::take_atomic(vec![
                reord::LockInfo::Named(String::from("lock1")),
                reord::LockInfo::Named(String::from("lock2")),
            ])
            .await;
            println!("taking lock 1");
            let _l = lock1.lock().await;
            let _l = lock2.lock().await;
            println!("in lock 1");
            reord::point().await;
        }
        reord::point().await;
        println!("after lock 1");
    }));

    let b = tokio::task::spawn(reord::new_task(async move {
        println!("before lock 2");
        {
            let _l = reord::Lock::take_atomic(vec![
                reord::LockInfo::Named(String::from("lock1")),
                reord::LockInfo::Named(String::from("lock2")),
            ])
            .await;
            println!("taking lock 2");
            let _l = lock1_clone.lock().await;
            let _l = lock2_clone.lock().await;
            println!("in lock 2");
            reord::point().await;
        }
        reord::point().await;
        println!("after lock 2");
    }));

    let h = reord::start(2).await;

    tokio::try_join!(a, b, h).unwrap();
}

#[tokio::test]
async fn waiting_on_two_locks_vs_one() {
    reord::init_test(reord::Config {
        check_addressed_locks_work_for: Some(std::time::Duration::from_millis(100)),
        ..reord::Config::from_seed(Default::default())
    })
    .await;

    let lock = std::sync::Arc::new(tokio::sync::Mutex::new(()));
    let lock_clone = lock.clone();

    let a = tokio::task::spawn(reord::new_task(async move {
        let _l = reord::Lock::take_addressed(0).await;
        let _l = lock.lock().await;
        reord::point().await;
    }));

    let b = tokio::task::spawn(reord::new_task(async move {
        let _l = reord::Lock::take_atomic(vec![
            reord::LockInfo::Addressed(0),
            reord::LockInfo::Addressed(1),
        ])
        .await;
        let _l = lock_clone.lock().await;
        reord::point().await;
    }));

    let h = reord::start(2).await;

    tokio::try_join!(a, b, h).unwrap();
}

#[tokio::test]
async fn functions_without_init_dont_break() {
    let lock = std::sync::Arc::new(tokio::sync::Mutex::new(()));
    let lock2 = lock.clone();
    let a = tokio::task::spawn(reord::new_task(async move {
        {
            let _l = reord::Lock::take_named(String::from("foo")).await;
            let _l = lock.lock().await;
            reord::point().await;
        }
        reord::point().await;
    }));

    let b = tokio::task::spawn(reord::new_task(async move {
        {
            let _l = reord::Lock::take_named(String::from("foo")).await;
            let _l = lock2.lock().await;
            reord::point().await;
        }
        reord::point().await;
    }));

    tokio::try_join!(a, b).unwrap();
}

#[tokio::test]
async fn two_tests_same_thread() {
    // First test
    reord::init_test(reord::Config::with_random_seed()).await;

    let a = tokio::task::spawn(reord::new_task(async move {
        reord::point().await;
    }));

    let b = tokio::task::spawn(reord::new_task(async move {
        reord::point().await;
    }));

    let h = reord::start(2).await;

    tokio::try_join!(a, b, h).unwrap();

    // Second test
    reord::init_test(reord::Config::with_random_seed()).await;

    let a = tokio::task::spawn(reord::new_task(async move {
        reord::point().await;
    }));

    let b = tokio::task::spawn(reord::new_task(async move {
        reord::point().await;
    }));

    let h = reord::start(2).await;

    tokio::try_join!(a, b, h).unwrap();
}

#[tokio::test]
#[should_panic]
async fn join_does_not_deadlock() {
    reord::init_test(reord::Config::with_random_seed()).await;

    let a = tokio::task::spawn(reord::new_task(async move {
        reord::point().await;
    }));

    let b = tokio::task::spawn(reord::new_task(async move {
        panic!("willing fail");
    }));

    let h = reord::start(2).await;

    let (a, b, h) = tokio::join!(a, b, h);
    a.unwrap();
    b.unwrap();
    h.unwrap();
}

#[tokio::test]
async fn maybe_lock_smoke_test() {
    tracing_subscriber::FmtSubscriber::builder()
        .with_max_level(tracing::Level::TRACE)
        .init();

    let cfg = reord::Config::from_seed(0);
    let mut rng = StdRng::seed_from_u64(cfg.seed);
    reord::init_test(cfg).await;

    let the_lock = Arc::new(Mutex::new(()));

    const NUM_TASKS: usize = 32;
    let do_lock_it = (0..NUM_TASKS).map(|_| rng.gen::<bool>());
    let tasks = do_lock_it
        .map(|do_lock_it| {
            tokio::task::spawn(reord::new_task({
                let the_lock = the_lock.clone();
                async move {
                    reord::maybe_lock().await;
                    if do_lock_it {
                        tracing::info!("taking the lock");
                        let _lock = the_lock.lock().await;
                        tracing::info!("taken the lock");
                        reord::point().await;
                    } else {
                        tracing::info!("skipping the lock");
                        reord::point().await;
                    }
                }
            }))
        })
        .collect::<Vec<_>>();

    let h = reord::start(NUM_TASKS).await;
    let (results, h) = tokio::join!(join_all(tasks), h);
    for r in results {
        r.unwrap();
    }
    h.unwrap();
}