]> Sergey Matveev's repositories - public-inbox.git/blob - xt/create-many-inboxes.t
update copyrights for 2021
[public-inbox.git] / xt / create-many-inboxes.t
1 #!perl -w
2 # Copyright (C) 2020-2021 all contributors <meta@public-inbox.org>
3 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
4 use strict;
5 use Test::More;
6 use PublicInbox::TestCommon;
7 use PublicInbox::Eml;
8 use File::Path qw(mkpath);
9 use IO::Handle (); # autoflush
10 use POSIX qw(_exit);
11 use Cwd qw(getcwd abs_path);
12 use File::Spec;
13 my $many_root = $ENV{TEST_MANY_ROOT} or
14         plan skip_all => 'TEST_MANY_ROOT not defined';
15 my $cwd = getcwd();
16 mkpath($many_root);
17 -d $many_root or BAIL_OUT "$many_root: $!";
18 $many_root = abs_path($many_root);
19 $many_root =~ m!\A\Q$cwd\E/! and BAIL_OUT "$many_root must not be in $cwd";
20 require_git 2.6;
21 require_mods(qw(DBD::SQLite Search::Xapian));
22 use_ok 'PublicInbox::V2Writable';
23 my $nr_inbox = $ENV{NR_INBOX} // 10;
24 my $nproc = $ENV{NPROC} || PublicInbox::V2Writable::detect_nproc() || 2;
25 my $indexlevel = $ENV{TEST_INDEXLEVEL} // 'basic';
26 diag "NR_INBOX=$nr_inbox NPROC=$nproc TEST_INDEXLEVEL=$indexlevel";
27 diag "TEST_MANY_ROOT=$many_root";
28 my $level_cfg = $indexlevel eq 'full' ? '' : "\tindexlevel = $indexlevel\n";
29 my $pfx = "$many_root/$nr_inbox-$indexlevel";
30 mkpath($pfx);
31 open my $cfg_fh, '>>', "$pfx/config" or BAIL_OUT $!;
32 $cfg_fh->autoflush(1);
33 my $v2_init_add = sub {
34         my ($i) = @_;
35         my $ibx = PublicInbox::Inbox->new({
36                 inboxdir => "$pfx/test-$i",
37                 name => "test-$i",
38                 newsgroup => "inbox.comp.test.foo.test-$i",
39                 address => [ "test-$i\@example.com" ],
40                 url => [ "//example.com/test-$i" ],
41                 version => 2,
42         });
43         $ibx->{indexlevel} = $indexlevel if $level_cfg ne '';
44         my $entry = <<EOF;
45 [publicinbox "$ibx->{name}"]
46         address = $ibx->{-primary_address}
47         url = $ibx->{url}->[0]
48         newsgroup = $ibx->{newsgroup}
49         inboxdir = $ibx->{inboxdir}
50 EOF
51         $entry .= $level_cfg;
52         print $cfg_fh $entry or die $!;
53         my $v2w = PublicInbox::V2Writable->new($ibx, { nproc => 0 });
54         $v2w->init_inbox(0);
55         $v2w->add(PublicInbox::Eml->new(<<EOM));
56 Date: Sat, 02 Oct 2010 00:00:00 +0000
57 From: Lorelei <l\@example.com>
58 To: test-$i\@example.com
59 Message-ID: <20101002-000000-$i\@example.com>
60 Subject: hello world $i
61
62 hi
63 EOM
64         $v2w->done;
65 };
66
67 my @children;
68 for my $i (1..$nproc) {
69         my ($r, $w);
70         pipe($r, $w) or BAIL_OUT $!;
71         my $pid = fork;
72         if ($pid == 0) {
73                 close $w;
74                 while (my $i = <$r>) {
75                         chomp $i;
76                         $v2_init_add->($i);
77                 }
78                 _exit(0);
79         }
80         defined $pid or BAIL_OUT "fork: $!";
81         close $r or BAIL_OUT $!;
82         push @children, [ $w, $pid ];
83         $w->autoflush(1);
84 }
85
86 for my $i (0..$nr_inbox) {
87         print { $children[$i % @children]->[0] } "$i\n" or BAIL_OUT $!;
88 }
89
90 for my $c (@children) {
91         close $c->[0] or BAIL_OUT "close $!";
92 }
93 my $i = 0;
94 for my $c (@children) {
95         my $pid = waitpid($c->[1], 0);
96         is($?, 0, ++$i.' exited ok');
97 }
98 ok(close($cfg_fh), 'config written');
99 done_testing;