2 # Copyright (C) all contributors <meta@public-inbox.org>
3 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
5 use PublicInbox::TestCommon;
8 use PublicInbox::Spawn qw(popen_rd);
9 require_mods('DBD::SQLite');
10 use_ok 'PublicInbox::Msgmap';
11 my ($tmpdir, $for_destroy) = tmpdir();
12 my $f = "$tmpdir/msgmap.sqlite3";
13 my $d = PublicInbox::Msgmap->new_file($f, 1);
17 my @mids = qw(a@b c@d e@f g@h aa@bb aa@cc);
18 is_deeply([$d->minmax], [0,0], "zero min max on new DB");
20 foreach my $mid (@mids) {
21 my $n = $d->mid_insert($mid);
22 ok($n, "mid $mid inserted");
28 my $ret = $d->mid_insert('a@b');
29 is($ret, undef, 'duplicate mid_insert in undef result');
30 is($d->num_for('a@b'), $mid2num{'a@b'}, 'existing number not clobbered');
31 my $next = (sort(keys %num2mid))[-1];
32 is($d->mid_insert('ok@unique'), $next + 1,
33 'got expected num after failing mid_insert');
35 foreach my $n (keys %num2mid) {
36 is($d->mid_for($n), $num2mid{$n}, "num:$n maps correctly");
38 foreach my $mid (@mids) {
39 is($d->num_for($mid), $mid2num{$mid}, "mid:$mid maps correctly");
42 is(undef, $d->last_commit, "last commit not set");
43 my $lc = 'deadbeef' x 5;
44 is(undef, $d->last_commit($lc), 'previous last commit (undef) returned');
45 is($lc, $d->last_commit, 'last commit was set correctly');
47 my $nc = 'deaddead' x 5;
48 is($lc, $d->last_commit($nc), 'returned previously set commit');
49 is($nc, $d->last_commit, 'new commit was set correctly');
51 is($d->mid_delete('a@b'), 1, 'deleted a@b');
52 is($d->mid_delete('a@b') + 0, 0, 'delete again returns zero');
53 is(undef, $d->num_for('a@b'), 'num_for fails on deleted msg');
56 ok($d = PublicInbox::Msgmap->new_file($f, 1), 'idempotent DB creation');
57 my ($min, $max) = $d->minmax;
58 ok($min > 0, "article min OK");
59 ok($max > 0 && $max < 10, "article max OK");
60 ok($min < $max, "article counts OK");
62 my $orig = $d->mid_insert('spam@1');
63 $d->mid_delete('spam@1');
64 is($d->mid_insert('spam@2'), 1 + $orig, "last number not recycled");
66 my $tmp = $d->tmp_clone($tmpdir);
67 is_deeply([$d->minmax], [$tmp->minmax], 'Cloned temporary DB matches');
68 ok($tmp->mid_delete('spam@2'), 'temporary DB is writable');
74 }, 'ok', 'atfork_* work on tmp_clone');
77 my $strace = strace_inject(1);
78 open my $fh, '>', my $trace = "$tmpdir/trace.out";
79 my $rd = popen_rd([ $strace, '-p', $$, '-o', $trace,
80 '-e', 'inject=pwrite64:error=ENOSPC'], undef, { 2 => 1 });
81 $rd->poll_in(10_000) or die 'strace not ready';
82 is eval { $d->mid_insert('this-better-trigger-ENOSPC@error') },
83 undef, 'insert fails w/ ENOSPC';
84 like $@, qr/ disk is full/, '$@ reports ENOSPC';
85 kill 'TERM', $rd->attached_pid;