]> Sergey Matveev's repositories - public-inbox.git/blob - t/xcpdb-reshard.t
testcommon: add require_mods method and use it
[public-inbox.git] / t / xcpdb-reshard.t
1 # Copyright (C) 2019 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
3 use strict;
4 use warnings;
5 use Test::More;
6 use PublicInbox::TestCommon;
7 require_mods(qw(DBD::SQLite Search::Xapian));
8 require_git('2.6');
9 use PublicInbox::MIME;
10 use PublicInbox::InboxWritable;
11
12 my $mime = PublicInbox::MIME->create(
13         header => [
14                 From => 'a@example.com',
15                 To => 'test@example.com',
16                 Subject => 'this is a subject',
17                 Date => 'Fri, 02 Oct 1993 00:00:00 +0000',
18         ],
19         body => '',
20 );
21
22 my ($this) = (split('/', $0))[-1];
23 my ($tmpdir, $for_destroy) = tmpdir();
24 local $ENV{PI_CONFIG} = "$tmpdir/config";
25 my $ibx = PublicInbox::Inbox->new({
26         inboxdir => "$tmpdir/testbox",
27         name => $this,
28         version => 2,
29         -primary_address => 'test@example.com',
30         indexlevel => 'medium',
31 });
32 my @xcpdb = qw(-xcpdb -q);
33 my $nproc = 8;
34 my $ndoc = 13;
35 my $im = PublicInbox::InboxWritable->new($ibx, {nproc => $nproc})->importer(1);
36 for my $i (1..$ndoc) {
37         $mime->header_set('Message-ID', "<m$i\@example.com>");
38         ok($im->add($mime), "message $i added");
39 }
40 $im->done;
41 my @shards = grep(m!/\d+\z!, glob("$ibx->{inboxdir}/xap*/*"));
42 is(scalar(@shards), $nproc, 'got expected shards');
43 my $orig = $ibx->over->query_xover(1, $ndoc);
44 my %nums = map {; "$_->{num}" => 1 } @$orig;
45
46 # ensure we can go up or down in shards, or stay the same:
47 for my $R (qw(2 4 1 3 3)) {
48         delete $ibx->{search}; # release old handles
49         my $cmd = [@xcpdb, "-R$R", $ibx->{inboxdir}];
50         push @$cmd, '--compact' if $R == 1;
51         ok(run_script($cmd), "xcpdb -R$R");
52         my @new_shards = grep(m!/\d+\z!, glob("$ibx->{inboxdir}/xap*/*"));
53         is(scalar(@new_shards), $R, 'resharded to two shards');
54         my $msgs = $ibx->search->query('s:this');
55         is(scalar(@$msgs), $ndoc, 'got expected docs after resharding');
56         my %by_mid = map {; "$_->{mid}" => $_ } @$msgs;
57         ok($by_mid{"m$_\@example.com"}, "$_ exists") for (1..$ndoc);
58
59         delete $ibx->{search}; # release old handles
60
61         # ensure docids in Xapian match NNTP article numbers
62         my $tot = 0;
63         my %tmp = %nums;
64         foreach my $d (@new_shards) {
65                 my $xdb = Search::Xapian::Database->new($d);
66                 $tot += $xdb->get_doccount;
67                 my $it = $xdb->postlist_begin('');
68                 my $end = $xdb->postlist_end('');
69                 for (; $it != $end; $it++) {
70                         my $docid = $it->get_docid;
71                         if ($xdb->get_document($docid)) {
72                                 ok(delete($tmp{$docid}), "saw #$docid");
73                         }
74                 }
75         }
76         is(scalar keys %tmp, 0, 'all docids seen');
77 }
78
79 done_testing();
80 1;