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