]> Sergey Matveev's repositories - public-inbox.git/blob - t/xcpdb-reshard.t
tests: consistently check for xapian-compact
[public-inbox.git] / t / xcpdb-reshard.t
1 # Copyright (C) 2019-2020 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::Eml;
10 use PublicInbox::InboxWritable;
11 require PublicInbox::Search;
12
13 my $mime = PublicInbox::Eml->new(<<'EOF');
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 EOF
20 my ($this) = (split('/', $0))[-1];
21 my ($tmpdir, $for_destroy) = tmpdir();
22 local $ENV{PI_CONFIG} = "$tmpdir/config";
23 my $ibx = PublicInbox::Inbox->new({
24         inboxdir => "$tmpdir/testbox",
25         name => $this,
26         version => 2,
27         -primary_address => 'test@example.com',
28         indexlevel => 'medium',
29 });
30 my @xcpdb = qw(-xcpdb -q);
31 my $nproc = 8;
32 my $ndoc = 13;
33 my $im = PublicInbox::InboxWritable->new($ibx, {nproc => $nproc})->importer;
34 for my $i (1..$ndoc) {
35         $mime->header_set('Message-ID', "<m$i\@example.com>");
36         ok($im->add($mime), "message $i added");
37 }
38 $im->done;
39 my @shards = grep(m!/\d+\z!, glob("$ibx->{inboxdir}/xap*/*"));
40 is(scalar(@shards), $nproc - 1, 'got expected shards');
41 my $orig = $ibx->over->query_xover(1, $ndoc);
42 my %nums = map {; "$_->{num}" => 1 } @$orig;
43
44 # ensure we can go up or down in shards, or stay the same:
45 for my $R (qw(2 4 1 3 3)) {
46         delete $ibx->{search}; # release old handles
47         my $cmd = [@xcpdb, "-R$R", $ibx->{inboxdir}];
48         push @$cmd, '--compact' if $R == 1 && have_xapian_compact;
49         ok(run_script($cmd), "xcpdb -R$R");
50         my @new_shards = grep(m!/\d+\z!, glob("$ibx->{inboxdir}/xap*/*"));
51         is(scalar(@new_shards), $R, 'resharded to two shards');
52         my $mset = $ibx->search->mset('s:this');
53         my $msgs = $ibx->search->mset_to_smsg($ibx, $mset);
54         is(scalar(@$msgs), $ndoc, 'got expected docs after resharding');
55         my %by_mid = map {; "$_->{mid}" => $_ } @$msgs;
56         ok($by_mid{"m$_\@example.com"}, "$_ exists") for (1..$ndoc);
57
58         delete $ibx->{search}; # release old handles
59
60         # ensure docids in Xapian match NNTP article numbers
61         my $tot = 0;
62         my %tmp = %nums;
63         my $XapianDatabase = do {
64                 no warnings 'once';
65                 $PublicInbox::Search::X{Database};
66         };
67         foreach my $d (@new_shards) {
68                 my $xdb = $XapianDatabase->new($d);
69                 $tot += $xdb->get_doccount;
70                 my $it = $xdb->postlist_begin('');
71                 my $end = $xdb->postlist_end('');
72                 for (; $it != $end; $it++) {
73                         my $docid = $it->get_docid;
74                         if ($xdb->get_document($docid)) {
75                                 ok(delete($tmp{$docid}), "saw #$docid");
76                         }
77                 }
78         }
79         is(scalar keys %tmp, 0, 'all docids seen');
80 }
81
82 done_testing();
83 1;