tinystore 0.4.0

A small storage runtime for applications on SQLite: SQL, key-value state, durable jobs, files, search, logs and metrics in one directory
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
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
use std::panic::{AssertUnwindSafe, catch_unwind};
use std::sync::mpsc;
use std::sync::{Arc, Barrier, Mutex};
use std::thread;
use std::time::{Duration, Instant};

use super::*;
use crate::sqlite::group::COMMIT;
use crate::sqlite::{Config, Durability, GroupLimits, Migration};

/// A call a test makes on the file from a thread of its own.
type Call<T> = Box<dyn FnOnce(&File) -> Result<T> + Send>;

fn open(config: Config) -> (tempfile::TempDir, Arc<File>) {
    let dir = tempfile::tempdir().unwrap();
    let file = File::open(dir.path().join("test.db"), config).unwrap();
    file.transaction(|tx| {
        tx.execute_batch("create table t (id integer primary key, v text not null unique)")
            .map_err(|error| sql_error("create t", error))
    })
    .unwrap();
    (dir, Arc::new(file))
}

fn insert(file: &File, value: &str) -> Result<i64> {
    let value = value.to_owned();
    file.write(value.len(), move |tx| {
        tx.prepare_cached("insert into t (v) values (?1)")
            .and_then(|mut insert| insert.execute([&value]))
            .map_err(|error| sql_error("insert", error))?;
        Ok(tx.last_insert_rowid())
    })
}

fn count(file: &File) -> i64 {
    file.read(|connection| {
        connection.query_row("select count(*) from t", [], |row| row.get(0)).map_err(|error| sql_error("count", error))
    })
    .unwrap()
}

fn wait_until(condition: impl Fn() -> bool) {
    let deadline = Instant::now() + Duration::from_secs(10);
    while !condition() {
        assert!(Instant::now() < deadline, "the condition never held");
        thread::sleep(Duration::from_millis(1));
    }
}

/// Queues `writes` behind a held writer, so that all of them meet in the
/// group, then lets the writer go and returns each one's answer.
fn queue_behind_the_writer<T: Send + 'static>(file: &Arc<File>, writes: Vec<Call<T>>) -> Vec<Result<T>> {
    let held = lock(&file.writer);
    let count = writes.len();
    let threads: Vec<_> = writes
        .into_iter()
        .map(|write| {
            let file = Arc::clone(file);
            thread::spawn(move || write(&file))
        })
        .collect();
    wait_until(|| file.writes_waiting() == count);
    drop(held);
    threads.into_iter().map(|thread| thread.join().unwrap()).collect()
}

/// Queues `writes` behind a held writer in their order, each one's thread
/// started once the one before it waits, then lets the writer go and
/// returns each one's answer.
fn queue_in_order_behind_the_writer<T: Send + 'static>(file: &Arc<File>, writes: Vec<Call<T>>) -> Vec<Result<T>> {
    let held = lock(&file.writer);
    let mut threads = Vec::new();
    for write in writes {
        let writing = Arc::clone(file);
        threads.push(thread::spawn(move || write(&writing)));
        wait_until(|| file.writes_waiting() == threads.len());
    }
    drop(held);
    threads.into_iter().map(|thread| thread.join().unwrap()).collect()
}

/// A write that SQLite answers by rolling back the whole transaction it runs
/// in when `value` is taken, as `insert or rollback` does.
fn insert_or_roll_back(file: &File, value: &str) -> Result<()> {
    let value = value.to_owned();
    file.write(value.len(), move |tx| {
        let insert = "insert or rollback into t (v) values (?1)";
        tx.execute(insert, [&value]).map(drop).map_err(|error| sql_error("insert", error))
    })
}

fn values(file: &File) -> Vec<String> {
    let read = |connection: &rusqlite::Connection| -> rusqlite::Result<Vec<String>> {
        connection.prepare("select v from t order by id")?.query_map([], |row| row.get(0))?.collect()
    };
    file.read(|connection| read(connection).map_err(|error| sql_error("values", error))).unwrap()
}

#[test]
fn writes_from_many_threads_share_their_commits() {
    let (_dir, file) = open(Config::default());
    let start = Arc::new(Barrier::new(32));
    let threads: Vec<_> = (0..32)
        .map(|thread| {
            let file = Arc::clone(&file);
            let start = Arc::clone(&start);
            thread::spawn(move || {
                start.wait();
                for write in 0..20 {
                    insert(&file, &format!("{thread}-{write}")).unwrap();
                }
            })
        })
        .collect();
    for thread in threads {
        thread.join().unwrap();
    }
    assert_eq!(count(&file), 640);
    assert!(file.commits() < 640, "{} commits for 640 writes", file.commits());
}

#[test]
fn a_commit_gathers_the_write_that_waited_and_the_one_its_last_answered() {
    // a quarter of the first commit's 400 ms, so that a loaded machine has
    // the time to wake the answered caller
    let slow = GroupLimits { gather: Duration::from_secs(1), ..GroupLimits::default() };
    let (_dir, file) = open(Config { group: slow, ..Config::default() });
    let before = file.commits();
    let (entered, enter) = mpsc::channel();
    let (release, released) = mpsc::channel::<()>();
    let first = {
        let file = Arc::clone(&file);
        thread::spawn(move || {
            file.write(1, move |tx| {
                entered.send(()).unwrap();
                released.recv().unwrap();
                thread::sleep(Duration::from_millis(400));
                tx.execute("insert into t (v) values ('first')", [])
                    .map(drop)
                    .map_err(|error| sql_error("insert", error))
            })
            .unwrap();
            // answered, it writes again at once, as a caller in a loop does
            insert(&file, "first again").unwrap();
        })
    };
    enter.recv().unwrap();
    let second = {
        let file = Arc::clone(&file);
        thread::spawn(move || insert(&file, "second").unwrap())
    };
    wait_until(|| file.group.waiting() == 1);
    release.send(()).unwrap();
    first.join().unwrap();
    second.join().unwrap();
    assert_eq!(count(&file), 3);
    assert_eq!(file.commits() - before, 2, "the second commit waited for the first one's caller");
}

#[test]
fn every_caller_of_a_commit_gets_its_own_answer_though_callers_wake_one_another() {
    let (_dir, file) = open(Config::default());
    let before = file.commits();
    // The caller that leads, and 25 its commit answers: it wakes two, and
    // each caller the firsts of the halves it was given. Two write the same value.
    let value = |n: usize| if n == 20 { "v7".to_owned() } else { format!("v{n}") };
    let writes: Vec<Call<i64>> =
        (0..26).map(|n| Box::new(move |file: &File| insert(file, &value(n))) as Call<i64>).collect();
    let answers = queue_behind_the_writer(&file, writes);
    assert_eq!(file.commits() - before, 1, "one commit carried every write");
    let refused: Vec<usize> = (0..26).filter(|n| answers[*n].is_err()).collect();
    assert!(refused == [7] || refused == [20], "the second of the two alone is refused: {refused:?}");
    assert_eq!(count(&file), 25);
}

#[test]
fn a_failing_write_rolls_back_alone() {
    let (_dir, file) = open(Config::default());
    insert(&file, "taken").unwrap();
    let before = file.commits();
    let answers = queue_behind_the_writer(
        &file,
        vec![
            Box::new(|file| insert(file, "a")),
            Box::new(|file| insert(file, "taken")),
            Box::new(|file| insert(file, "b")),
        ],
    );
    assert_eq!(file.commits() - before, 1, "the three writes share one commit");
    assert!(answers[0].is_ok() && answers[2].is_ok());
    assert_eq!(answers[1].as_ref().unwrap_err().kind(), ErrorKind::Conflict);
    assert_eq!(count(&file), 3);
}

// contract: DUR-03, DUR-05
#[test]
fn a_write_that_rolls_back_the_commit_it_shares_fails_and_the_writes_after_it_commit_in_the_next() {
    let (_dir, file) = open(Config::default());
    insert(&file, "taken").unwrap();
    let answers = queue_in_order_behind_the_writer(
        &file,
        vec![
            Box::new(|file| insert(file, "before").map(drop)),
            Box::new(|file| insert_or_roll_back(file, "taken")),
            Box::new(|file| insert(file, "after").map(drop)),
        ],
    );
    let before = answers[0].as_ref().unwrap_err();
    assert_eq!(before.kind(), ErrorKind::Unavailable, "rolled back beside it, the same write may succeed: {before}");
    let ending = answers[1].as_ref().unwrap_err();
    assert_eq!(ending.kind(), ErrorKind::Conflict, "the write keeps its own answer: {ending}");
    assert!(answers[2].is_ok(), "the write after it commits in the next transaction: {:?}", answers[2]);
    assert_eq!(values(&file), ["taken", "after"]);
    insert(&file, "before").unwrap();
}

// contract: DUR-02
#[test]
fn a_transaction_a_statement_rolled_back_commits_nothing_of_what_runs_after() {
    let (_dir, file) = open(Config::default());
    insert(&file, "taken").unwrap();
    let ended = file.transaction(|tx| -> Result<()> {
        tx.execute("insert into t (v) values ('inside')", []).map_err(|error| sql_error("insert", error))?;
        assert!(tx.execute("insert or rollback into t (v) values ('taken')", []).is_err());
        // a function that catches the failure and goes on
        let stray = tx.execute("insert into t (v) values ('stray')", []);
        assert!(stray.is_err(), "a statement after the rollback does not commit by itself: {stray:?}");
        Ok(())
    });
    let error = ended.unwrap_err();
    assert_eq!(error.kind(), ErrorKind::Invalid, "{error}");
    assert_eq!(values(&file), ["taken"], "nothing of the transaction is in the file");
    insert(&file, "next").unwrap();
}

/// Runs `scenario` on a thread of its own, and fails the test when it has
/// not ended within a minute, as two writers waiting for each other never do.
fn within_a_minute<T: Send + 'static>(scenario: impl FnOnce() -> T + Send + 'static) -> T {
    let (done, ended) = mpsc::channel();
    thread::spawn(move || {
        let _ = done.send(scenario());
    });
    ended.recv_timeout(Duration::from_secs(60)).expect("it waited for good")
}

/// Two transactions, each of a file of its own, that call `across` on the
/// other's file once both hold their writer: what each answered.
fn each_across(across: fn(&File) -> Result<()>) -> [Result<()>; 2] {
    let (_one_dir, one) = open(Config::default());
    let (_two_dir, two) = open(Config::default());
    within_a_minute(move || {
        let both_hold = Arc::new(Barrier::new(2));
        let transactions = [(Arc::clone(&one), Arc::clone(&two)), (two, one)].map(|(own, other)| {
            let both_hold = Arc::clone(&both_hold);
            thread::spawn(move || {
                own.transaction(|tx| -> Result<()> {
                    tx.execute("insert into t (v) values ('own')", []).map_err(|error| sql_error("insert", error))?;
                    both_hold.wait();
                    across(&other)
                })
            })
        });
        transactions.map(|transaction| transaction.join().unwrap())
    })
}

/// That one of two transactions gave way, `unavailable`, and the other went on.
fn one_gave_way(answers: &[Result<()>; 2]) {
    let failed: Vec<&Error> = answers.iter().filter_map(|answer| answer.as_ref().err()).collect();
    assert_eq!(failed.len(), 1, "one gives way, and the other goes on: {answers:?}");
    assert_eq!(failed[0].kind(), ErrorKind::Unavailable, "{}", failed[0]);
}

#[test]
fn two_transactions_each_writing_the_others_file_do_not_wait_for_each_other_for_good() {
    one_gave_way(&each_across(|other| insert(other, "across").map(drop)));
}

#[test]
fn two_transactions_each_beginning_one_of_the_others_file_do_not_wait_for_each_other_for_good() {
    one_gave_way(&each_across(|other| {
        other.transaction(|tx| {
            tx.execute("insert into t (v) values ('across')", []).map(drop).map_err(|error| sql_error("insert", error))
        })
    }));
}

#[test]
fn a_panicking_write_fails_alone_and_the_writer_goes_on() {
    let (_dir, file) = open(Config::default());
    let answers = queue_behind_the_writer(
        &file,
        vec![
            Box::new(|file| insert(file, "a")),
            Box::new(|file| file.write(0, |_| -> Result<i64> { panic!("a bug in an engine") })),
        ],
    );
    assert!(answers[0].is_ok());
    assert_eq!(answers[1].as_ref().unwrap_err().kind(), ErrorKind::Internal);
    insert(&file, "b").unwrap();
    assert_eq!(count(&file), 2);
}

#[test]
fn a_write_heavier_than_the_group_commits_alone() {
    let config = Config { group: GroupLimits { bytes: 100, ..GroupLimits::default() }, ..Config::default() };
    let (_dir, file) = open(config);
    let before = file.commits();
    let heavy = |value: &'static str| -> Call<i64> {
        Box::new(move |file| {
            let value = value.to_owned();
            file.write(60, move |tx| {
                tx.execute("insert into t (v) values (?1)", [&value]).map_err(|error| sql_error("insert", error))?;
                Ok(tx.last_insert_rowid())
            })
        })
    };
    let answers = queue_behind_the_writer(&file, vec![heavy("a"), heavy("b"), heavy("c")]);
    assert!(answers.iter().all(Result::is_ok));
    assert_eq!(file.commits() - before, 3);
}

#[test]
fn closing_answers_the_queued_writes_and_refuses_new_ones() {
    let (_dir, file) = open(Config::default());
    let held = lock(&file.writer);
    let writers: Vec<_> = ["a", "b", "c"]
        .into_iter()
        .map(|value| {
            let file = Arc::clone(&file);
            thread::spawn(move || insert(&file, value))
        })
        .collect();
    wait_until(|| file.group.waiting() == 3);
    let closer = {
        let file = Arc::clone(&file);
        thread::spawn(move || file.close())
    };
    wait_until(|| file.group.waiting() == 0);
    drop(held);
    for writer in writers {
        assert_eq!(writer.join().unwrap().unwrap_err().kind(), ErrorKind::Closed);
    }
    closer.join().unwrap().unwrap();
    assert_eq!(insert(&file, "d").unwrap_err().kind(), ErrorKind::Closed);
}

#[test]
fn a_transaction_keeps_its_writes_only_when_it_succeeds() {
    let (_dir, file) = open(Config::default());
    let failed = file.transaction(|tx| {
        tx.execute("insert into t (v) values ('a')", []).map_err(|error| sql_error("insert", error))?;
        Err::<(), _>(Error::invalid("changed its mind"))
    });
    assert_eq!(failed.unwrap_err().kind(), ErrorKind::Invalid);

    let panicked = catch_unwind(AssertUnwindSafe(|| {
        file.transaction(|tx| -> Result<()> {
            tx.execute("insert into t (v) values ('b')", []).unwrap();
            panic!("a bug in an engine")
        })
    }));
    assert!(panicked.is_err());

    file.transaction(|tx| tx.execute("insert into t (v) values ('c')", []).map_err(|error| sql_error("insert", error)))
        .unwrap();
    assert_eq!(count(&file), 1);
}

#[test]
fn a_read_sees_one_snapshot_while_a_write_commits_beside_it() {
    let (_dir, file) = open(Config::default());
    insert(&file, "a").unwrap();
    let writer = Arc::clone(&file);
    let seen = file
        .read(move |connection| {
            let count = |connection: &rusqlite::Connection| -> Result<i64> {
                connection
                    .query_row("select count(*) from t", [], |row| row.get(0))
                    .map_err(|error| sql_error("count", error))
            };
            let before = count(connection)?;
            thread::spawn(move || insert(&writer, "b")).join().unwrap()?;
            Ok((before, count(connection)?))
        })
        .unwrap();
    assert_eq!(seen, (1, 1));
    assert_eq!(count(&file), 2);
}

#[test]
fn a_reader_cannot_write() {
    let (_dir, file) = open(Config::default());
    let error = file
        .read(|connection| {
            connection.execute("insert into t (v) values ('a')", []).map_err(|error| sql_error("insert", error))
        })
        .unwrap_err();
    assert_eq!(error.kind(), ErrorKind::Invalid, "{error}");
}

#[test]
fn idle_readers_close_but_one() {
    let config = Config { reader_idle: Duration::ZERO, ..Config::default() };
    let (_dir, file) = open(config);
    let together = Arc::new(Barrier::new(3));
    let readers: Vec<_> = (0..3)
        .map(|_| {
            let file = Arc::clone(&file);
            let together = Arc::clone(&together);
            thread::spawn(move || {
                file.read(move |_| {
                    together.wait();
                    Ok(())
                })
            })
        })
        .collect();
    for reader in readers {
        reader.join().unwrap().unwrap();
    }
    assert_eq!(file.readers_open(), 3);
    assert_eq!(file.sweep(), 2);
    assert_eq!(file.readers_open(), 1);
    assert_eq!(count(&file), 0);
}

#[test]
fn a_read_waits_for_a_busy_reader_and_then_gives_up() {
    let config = Config { readers: 1, reader_patience: Duration::from_millis(50), ..Config::default() };
    let (_dir, file) = open(config);
    let (reading, release) = (mpsc::channel(), mpsc::channel::<()>());
    let holder = {
        let file = Arc::clone(&file);
        let (started, held) = (reading.0, release.1);
        thread::spawn(move || {
            file.read(move |_| {
                started.send(()).unwrap();
                held.recv().unwrap();
                Ok(())
            })
        })
    };
    reading.1.recv().unwrap();
    let error = file.read(|_| Ok(())).unwrap_err();
    assert_eq!(error.kind(), ErrorKind::Unavailable, "{error}");
    release.0.send(()).unwrap();
    holder.join().unwrap().unwrap();
    file.read(|_| Ok(())).unwrap();
}

#[test]
fn a_read_that_waited_takes_the_next_reader_before_one_that_came_after_it() {
    let (_dir, file) = open(Config { readers: 1, ..Config::default() });
    let order = Arc::new(Mutex::new(Vec::new()));
    let (reading, release) = (mpsc::channel(), mpsc::channel::<()>());
    let holder = {
        let (file, order) = (Arc::clone(&file), Arc::clone(&order));
        let (started, held) = (reading.0, release.1);
        thread::spawn(move || {
            file.read(move |_| {
                started.send(()).unwrap();
                held.recv().unwrap();
                Ok(())
            })
            .unwrap();
            // straight back for another, while the read that waited sleeps
            file.read(|_| {
                order.lock().unwrap().push("came after");
                Ok(())
            })
            .unwrap();
        })
    };
    reading.1.recv().unwrap();
    let waited = {
        let (file, order) = (Arc::clone(&file), Arc::clone(&order));
        thread::spawn(move || {
            file.read(|_| {
                order.lock().unwrap().push("waited");
                Ok(())
            })
            .unwrap();
        })
    };
    wait_until(|| file.readers_waiting() == 1);
    release.0.send(()).unwrap();
    holder.join().unwrap();
    waited.join().unwrap();
    assert_eq!(*order.lock().unwrap(), ["waited", "came after"]);
}

const STEP_ONE: Migration = Migration::of(1, "0001_a.sql", "create table a (x integer)");
const STEP_TWO: Migration = Migration::of(2, "0002_b.sql", "create table b (y integer)");

#[test]
fn migrations_apply_once_in_order() {
    let (_dir, file) = open(Config::default());
    file.migrate("test", &[STEP_ONE]).unwrap();
    file.migrate("test", &[STEP_ONE]).unwrap();
    file.migrate("test", &[STEP_ONE, STEP_TWO]).unwrap();
    let tables: i64 = file
        .read(|connection| {
            connection
                .query_row("select count(*) from sqlite_schema where name in ('a', 'b')", [], |row| row.get(0))
                .map_err(|error| sql_error("tables", error))
        })
        .unwrap();
    assert_eq!(tables, 2);
}

#[test]
fn a_migration_the_program_does_not_have_or_has_edited_is_refused() {
    let (_dir, file) = open(Config::default());
    file.migrate("test", &[STEP_ONE, STEP_TWO]).unwrap();

    let older = file.migrate("test", &[STEP_ONE]).unwrap_err();
    assert!(older.to_string().contains("a newer program"), "{older}");

    let edited = Migration::of(1, "0001_a.sql", "create table a (x text)");
    let error = file.migrate("test", &[edited, STEP_TWO]).unwrap_err();
    assert!(error.to_string().contains("0001_a.sql changed after it was applied"), "{error}");

    let renamed = Migration::of(1, "0001_letters.sql", "create table a (x integer)");
    let error = file.migrate("test", &[renamed, STEP_TWO]).unwrap_err();
    assert!(error.to_string().contains("was renamed"), "{error}");

    let gap = file.migrate("other", &[STEP_TWO]).unwrap_err();
    assert_eq!(gap.kind(), ErrorKind::Invalid);
}

// contract: SQL-03
#[test]
fn a_migration_whose_lines_end_otherwise_on_another_checkout_is_the_step_it_applied() {
    let (_dir, file) = open(Config::default());
    let lf = "create table users (id integer primary key);\ncreate index users_by_id on users (id);\n";
    file.migrate("test", &[Migration::of(1, "0001_users.sql", lf)]).unwrap();

    let crlf = Migration { sql: lf.replace('\n', "\r\n").into(), ..Migration::of(1, "0001_users.sql", lf) };
    file.migrate("test", &[crlf]).unwrap();

    let edited = Migration::of(1, "0001_users.sql", "create table users (id integer primary key, email text);\r\n");
    let error = file.migrate("test", &[edited]).unwrap_err();
    assert!(error.to_string().contains("0001_users.sql changed after it was applied"), "{error}");
}

#[test]
fn durability_sets_how_far_a_commit_goes() {
    for (durability, synchronous) in [(Durability::Full, 2), (Durability::Os, 1)] {
        let config = Config { durability, ..Config::default() };
        let (_dir, file) = open(config);
        let (mode, level) = file
            .transaction(|tx| -> Result<(String, i64)> {
                let mode: String = tx
                    .pragma_query_value(None, "journal_mode", |row| row.get(0))
                    .map_err(|error| sql_error("journal_mode", error))?;
                let level: i64 = tx
                    .pragma_query_value(None, "synchronous", |row| row.get(0))
                    .map_err(|error| sql_error("synchronous", error))?;
                Ok((mode, level))
            })
            .unwrap();
        assert_eq!(mode, "wal");
        assert_eq!(level, synchronous, "{durability:?}");
    }
}

#[test]
fn the_build_gives_each_connection_a_page_cache_of_its_own() {
    // With SQLITE_ENABLE_MEMORY_MANAGEMENT, which libsqlite3-sys defines and
    // .cargo/config.toml undefines, every page a reader fetches or lets go
    // takes one mutex of the whole process, and readers stop one another.
    let (_dir, file) = open(Config::default());
    let shared: bool = file
        .read(|connection| {
            connection
                .query_row("select sqlite_compileoption_used('ENABLE_MEMORY_MANAGEMENT')", [], |row| row.get(0))
                .map_err(|error| sql_error("compile options", error))
        })
        .unwrap();
    assert!(!shared, "build with LIBSQLITE3_FLAGS as .cargo/config.toml has them");
}

#[test]
fn sqlite_takes_the_cores_mutexes_before_its_first_connection() {
    let (_dir, file) = open(Config::default());
    assert_eq!(count(&file), 0);
    assert!(crate::sqlite::mutexes_given(), "SQLite had started before the adapter opened a connection");
}

#[test]
fn sqlite_counts_the_memory_it_holds() {
    let (_dir, file) = open(Config::default());
    insert(&file, "a").unwrap();
    assert!(crate::sqlite::memory_used() > 0);
}

#[test]
fn writes_submitted_while_a_commit_runs_return_at_once_and_share_the_next() {
    let (_dir, file) = open(Config::default());
    let before = file.commits();
    let (answers, answered) = mpsc::channel();
    let submit = |file: &File, n: usize, answers: mpsc::Sender<Result<()>>| {
        let value = format!("v{n}");
        let write = move |tx: &Tx<'_>| {
            tx.execute("insert into t (v) values (?1)", [&value]).map_err(|error| sql_error("insert", error))
        };
        file.submit(4, write, move |answer| answers.send(answer.map(|_| ())).unwrap());
    };
    let held = lock(&file.writer);
    let leader = {
        let (file, answers) = (Arc::clone(&file), answers.clone());
        // with no commit running, the first submission leads, and waits for the writer
        thread::spawn(move || submit(&file, 0, answers))
    };
    wait_until(|| file.group.waiting() == 1);
    for n in 1..500 {
        submit(&file, n, answers.clone());
    }
    assert_eq!(file.group.waiting(), 500, "a submission returns at once while a leader runs");
    drop(held);
    leader.join().unwrap();
    for _ in 0..500 {
        answered.recv_timeout(Duration::from_secs(10)).unwrap().unwrap();
    }
    assert_eq!(count(&file), 500);
    assert!(file.commits() - before <= 2, "{} commits for 500 writes", file.commits() - before);
}

#[test]
fn no_connection_attaches_a_file_whoever_wrote_the_statement() {
    let (dir, file) = open(Config::default());
    let other = File::open(dir.path().join("other.db"), Config::default()).unwrap();
    other.close().unwrap();
    let attach = format!("attach database '{}' as other", dir.path().join("other.db").display());
    // not a statement the store was given: the limit refuses it, not the guard of those
    let refused = file.read(|connection| connection.execute_batch(&attach).map_err(|error| sql_error("attach", error)));
    assert!(format!("{:?}", refused.unwrap_err()).contains("too many attached databases"));
}

#[test]
fn a_given_statement_that_starts_as_the_adapters_own_is_refused_unrun() {
    let (_dir, file) = open(Config::default());
    insert(&file, "kept").unwrap();
    let ran = file.write(0, |tx| {
        crate::sqlite::given(COMMIT, || execute(tx, COMMIT).map_err(|error| sql_error("commit", error)))
    });
    assert_eq!(ran.unwrap_err().kind(), ErrorKind::Invalid);
    assert_eq!(count(&file), 1);
}

/// A write that asks for `name` to be said once it commits, and fails after
/// asking when `fails`.
#[cfg(feature = "channels")]
fn saying(said: &Arc<Mutex<Vec<String>>>, name: &str, fails: bool) -> Call<()> {
    let (said, name) = (Arc::clone(said), name.to_owned());
    Box::new(move |file: &File| {
        file.write(1, move |tx| {
            let inside = !tx.is_autocommit();
            let said = Arc::clone(&said);
            tx.after_commit(Box::new(move || {
                said.lock().unwrap().push(format!("{name}, asked inside the transaction: {inside}"))
            }));
            if fails { Err(Error::invalid("a write that fails after asking")) } else { Ok(()) }
        })
    })
}

// contract: CHAN-03
#[cfg(feature = "channels")]
#[test]
fn what_a_write_asks_to_run_after_its_commit_runs_in_commit_order_and_never_after_a_rollback() {
    let (_dir, file) = open(Config::default());
    let said = Arc::new(Mutex::new(Vec::new()));
    let calls = vec![saying(&said, "a", false), saying(&said, "b", true), saying(&said, "c", false)];
    let answers = queue_behind_the_writer(&file, calls);
    assert!(answers[0].is_ok() && answers[1].is_err() && answers[2].is_ok());
    let mut grouped = said.lock().unwrap().clone();
    grouped.sort();
    let asked = ["a, asked inside the transaction: true", "c, asked inside the transaction: true"];
    assert_eq!(grouped, asked, "b's savepoint took its back");

    said.lock().unwrap().clear();
    let seen = Arc::clone(&said);
    let writer = Arc::clone(&file);
    let rolled_back = file.transaction(|tx| -> Result<()> {
        let seen = Arc::clone(&seen);
        tx.after_commit(Box::new(move || seen.lock().unwrap().push("rolled back".to_owned())));
        Err(Error::invalid("the transaction fails"))
    });
    assert!(rolled_back.is_err());
    let held = Arc::clone(&said);
    file.transaction(|tx| -> Result<()> {
        let held = Arc::clone(&held);
        let writer = Arc::clone(&writer);
        tx.after_commit(Box::new(move || {
            let locked = writer.writer.try_lock().is_err();
            held.lock().unwrap().push(format!("committed, the writer held: {locked}"));
        }));
        Ok(())
    })
    .unwrap();
    assert_eq!(*said.lock().unwrap(), ["committed, the writer held: true"]);

    said.lock().unwrap().clear();
    let mark = file.mark().unwrap();
    {
        let begun = file.begin(&mark).unwrap();
        let dropped = Arc::clone(&said);
        begun.tx().after_commit(Box::new(move || dropped.lock().unwrap().push("dropped".to_owned())));
    }
    let begun = file.begin(&mark).unwrap();
    let kept = Arc::clone(&said);
    begun.tx().after_commit(Box::new(move || kept.lock().unwrap().push("begun".to_owned())));
    begun.commit().unwrap();
    assert_eq!(*said.lock().unwrap(), ["begun"], "a transaction dropped before its commit runs nothing");
}

#[cfg(feature = "replication")]
#[test]
fn a_copy_s_readers_paused_hold_its_file_open_nowhere_and_their_reads_wait_for_what_replaces_it() {
    let dir = tempfile::tempdir().unwrap();
    let filled = |name: &str, rows: &[&str]| {
        let file = File::open(dir.path().join(name), Config::default()).unwrap();
        file.transaction(|tx| {
            tx.execute_batch("create table t (id integer primary key, v text not null unique)")
                .map_err(|error| sql_error("create t", error))
        })
        .unwrap();
        for row in rows {
            insert(&file, row).unwrap();
        }
        file.close().unwrap();
    };
    filled("copy.db", &["old"]);
    filled("snapshot.db", &["new", "newer"]);
    let copy = Arc::new(File::open_copy(dir.path().join("copy.db"), Config::default()).unwrap());
    assert_eq!(count(&copy), 1);

    let readers = copy.switch().readers().unwrap();
    readers.pause(Duration::from_secs(1)).unwrap();
    assert_eq!(copy.readers_open(), 0);
    let reading = {
        let copy = Arc::clone(&copy);
        thread::spawn(move || count(&copy))
    };
    wait_until(|| copy.readers_waiting() == 1);
    for left in ["copy.db-wal", "copy.db-shm"] {
        let _ = std::fs::remove_file(dir.path().join(left));
    }
    // fails on Windows while any connection holds the copy open
    std::fs::rename(dir.path().join("snapshot.db"), dir.path().join("copy.db")).unwrap();
    readers.resume();
    assert_eq!(reading.join().unwrap(), 2, "the read that waited reads the file that took the copy's place");
}

/// A settler that panics once, as an engine kept in the file might.
struct PanicsOnce(std::sync::atomic::AtomicBool);

impl Settle for PanicsOnce {
    fn settle(&self, _tx: &Tx<'_>, left: Left) -> Result<()> {
        if matches!(left, Left::Before) && !self.0.swap(true, std::sync::atomic::Ordering::SeqCst) {
            panic!("a bug in a settler");
        }
        Ok(())
    }
}

#[test]
fn a_commit_that_panics_outside_its_writes_leaves_a_leader_for_the_next() {
    let (_dir, file) = open(Config::default());
    let settler: Arc<dyn Settle> = Arc::new(PanicsOnce(Default::default()));
    file.settle_with(Arc::downgrade(&settler));

    assert_eq!(insert(&file, "a").unwrap_err().kind(), ErrorKind::OutcomeUnknown);
    insert(&file, "b").unwrap();
    assert_eq!(count(&file), 1);
}

/// A settler that counts the transactions it settled whole, the first of
/// them failing, as kv's revision would on a file that no longer writes.
#[derive(Default)]
struct SettlesWhole {
    settled: std::sync::atomic::AtomicUsize,
}

impl Settle for SettlesWhole {
    fn settle(&self, _tx: &Tx<'_>, left: Left) -> Result<()> {
        if left != Left::All {
            return Ok(());
        }
        match self.settled.fetch_add(1, std::sync::atomic::Ordering::SeqCst) {
            0 => Err(Error::internal("what the writes left together")),
            _ => Ok(()),
        }
    }
}

// contract: DUR-03
#[test]
fn what_a_commits_writes_leave_together_settles_once_and_fails_only_those_that_had_not_failed_alone() {
    let (_dir, file) = open(Config::default());
    insert(&file, "taken").unwrap();
    let settler = Arc::new(SettlesWhole::default());
    let settles: Arc<dyn Settle> = settler.clone();
    file.settle_with(Arc::downgrade(&settles));
    let settled = || settler.settled.load(std::sync::atomic::Ordering::SeqCst);
    let writes = || -> Vec<Call<i64>> {
        vec![
            Box::new(|file| insert(file, "a")),
            Box::new(|file| insert(file, "taken")),
            Box::new(|file| insert(file, "b")),
        ]
    };

    let answers = queue_behind_the_writer(&file, writes());
    assert_eq!(settled(), 1, "once for the commit of three writes");
    let kinds: Vec<ErrorKind> = answers.iter().map(|answer| answer.as_ref().unwrap_err().kind()).collect();
    assert_eq!(
        kinds,
        [ErrorKind::Internal, ErrorKind::Conflict, ErrorKind::Internal],
        "the refused write keeps its own"
    );
    assert_eq!(count(&file), 1, "nothing of the commit stays");

    let answers = queue_behind_the_writer(&file, writes());
    assert_eq!(settled(), 2);
    assert!(answers[0].is_ok() && answers[2].is_ok());
    assert_eq!(count(&file), 3);
    file.transaction(|tx| tx.execute("delete from t", []).map_err(|error| sql_error("delete", error))).unwrap();
    assert_eq!(settled(), 3, "a transaction of its own settles whole too");
}

/// Inserts `value` through `submit_on`, its commit led on `lead`, and says on
/// which thread its answer came.
#[cfg(feature = "async")]
fn submit_on(file: &File, lead: &crate::polled::Lead, value: &str) -> mpsc::Receiver<(Result<i64>, Option<String>)> {
    let (answered, answer) = mpsc::channel();
    let (value, bytes) = (value.to_owned(), value.len());
    let write = move |tx: &Tx<'_>| {
        tx.execute("insert into t (v) values (?1)", [&value]).map_err(|error| sql_error("insert", error))?;
        Ok(tx.last_insert_rowid())
    };
    let done = move |id| answered.send((id, thread::current().name().map(str::to_owned))).unwrap();
    file.submit_on(lead, bytes, write, done);
    answer
}

#[cfg(feature = "async")]
#[test]
fn a_write_submitted_on_a_lead_returns_at_once_and_commits_on_the_leads_thread() {
    let (_dir, file) = open(Config::default());
    let lead: crate::polled::Lead = Arc::new(|job| {
        thread::Builder::new().name("the-pool".to_owned()).spawn(job).unwrap();
    });
    // Held on a thread of its own, for a second at most: a caller that led the
    // commit itself would wait for it, then answer, rather than hang the test.
    let (release, held_until) = mpsc::channel::<()>();
    let (holding, held) = mpsc::channel();
    let holder = {
        let file = Arc::clone(&file);
        thread::spawn(move || {
            let _writer = lock(&file.writer);
            holding.send(()).unwrap();
            let _ = held_until.recv_timeout(Duration::from_secs(1));
        })
    };
    held.recv().unwrap();
    let answer = submit_on(&file, &lead, "a");
    assert!(answer.try_recv().is_err(), "the caller waited for nothing and led nothing");
    release.send(()).unwrap();
    holder.join().unwrap();
    let (id, thread) = answer.recv_timeout(Duration::from_secs(10)).unwrap();
    assert!(id.is_ok(), "{id:?}");
    assert_eq!(thread.as_deref(), Some("the-pool"));
    assert_eq!(count(&file), 1);
}

#[cfg(feature = "async")]
#[test]
fn a_lead_dropped_unrun_still_commits_the_writes_behind_it_where_it_is_dropped() {
    let (_dir, file) = open(Config::default());
    // The last resort of a lead no thread could be started for: the store's
    // pool runs every lead, on a thread of its own once it stopped, and only
    // a system that starts no thread drops one.
    let lead: crate::polled::Lead = Arc::new(drop);
    let answer = submit_on(&file, &lead, "a");
    let (id, thread) = answer.try_recv().expect("answered before the submit returned");
    assert!(id.is_ok(), "{id:?}");
    assert_eq!(thread, thread::current().name().map(str::to_owned), "it committed where the lead was dropped");
    assert_eq!(count(&file), 1);
}

#[cfg(feature = "async")]
#[test]
fn a_blocking_write_takes_a_lead_no_thread_of_the_pool_took_yet() {
    let (_dir, file) = open(Config::default());
    // a pool whose every thread is busy, as with sixteen writes waiting for this file's writer
    let kept: Arc<Mutex<Vec<crate::polled::Job>>> = Arc::default();
    let lead: crate::polled::Lead = {
        let kept = Arc::clone(&kept);
        Arc::new(move |job| kept.lock().unwrap().push(job))
    };
    let answer = submit_on(&file, &lead, "a");
    let (inserted, caller) = mpsc::channel();
    {
        let file = Arc::clone(&file);
        let insert = move || inserted.send(insert(&file, "b")).unwrap();
        thread::Builder::new().name("a-caller".to_owned()).spawn(insert).unwrap();
    }
    let led = caller.recv_timeout(Duration::from_secs(10)).expect("the caller waited for a lead the pool holds");
    led.unwrap();
    let (id, thread) = answer.recv_timeout(Duration::from_secs(10)).unwrap();
    assert!(id.is_ok(), "{id:?}");
    assert_eq!(thread.as_deref(), Some("a-caller"), "the caller committed the write queued before its own");

    // the pool's thread comes at last, and finds nothing to lead
    let jobs: Vec<_> = kept.lock().unwrap().drain(..).collect();
    jobs.into_iter().for_each(|job| job());
    assert_eq!(count(&file), 2);
    insert(&file, "c").unwrap();
    assert_eq!(count(&file), 3, "the group leads on as before");
}