]> Sergey Matveev's repositories - public-inbox.git/blob - t/v2writable.t
v2writable: support "barrier" operation to avoid reforking
[public-inbox.git] / t / v2writable.t
1 # Copyright (C) 2018 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::MIME;
7 use PublicInbox::ContentId qw(content_digest);
8 use File::Temp qw/tempdir/;
9 foreach my $mod (qw(DBD::SQLite Search::Xapian)) {
10         eval "require $mod";
11         plan skip_all => "$mod missing for nntpd.t" if $@;
12 }
13 use_ok 'PublicInbox::V2Writable';
14 my $mainrepo = tempdir('pi-v2writable-XXXXXX', TMPDIR => 1, CLEANUP => 1);
15 my $ibx = {
16         mainrepo => $mainrepo,
17         name => 'test-v2writable',
18         version => 2,
19         -primary_address => 'test@example.com',
20 };
21 $ibx = PublicInbox::Inbox->new($ibx);
22 my $mime = PublicInbox::MIME->create(
23         header => [
24                 From => 'a@example.com',
25                 To => 'test@example.com',
26                 Subject => 'this is a subject',
27                 'Message-ID' => '<a-mid@b>',
28                 Date => 'Fri, 02 Oct 1993 00:00:00 +0000',
29         ],
30         body => "hello world\n",
31 );
32
33 my $im = eval {
34         local $ENV{NPROC} = '1';
35         PublicInbox::V2Writable->new($ibx, 1);
36 };
37 is($im->{partitions}, 1, 'one partition when forced');
38 ok($im->add($mime), 'ordinary message added');
39
40 if ('ensure git configs are correct') {
41         my @cmd = (qw(git config), "--file=$mainrepo/all.git/config",
42                 qw(core.sharedRepository 0644));
43         is(system(@cmd), 0, "set sharedRepository in all.git");
44         my $git0 = PublicInbox::Git->new("$mainrepo/git/0.git");
45         my $fh = $git0->popen(qw(config core.sharedRepository));
46         my $v = eval { local $/; <$fh> };
47         chomp $v;
48         is($v, '0644', 'child repo inherited core.sharedRepository');
49         $fh = $git0->popen(qw(config --bool repack.writeBitmaps));
50         $v = eval { local $/; <$fh> };
51         chomp $v;
52         is($v, 'true', 'child repo inherited repack.writeBitmaps');
53 }
54
55 {
56         my @warn;
57         local $SIG{__WARN__} = sub { push @warn, @_ };
58         is($im->add($mime), undef, 'obvious duplicate rejected');
59         like(join(' ', @warn), qr/resent/, 'warned about resent message');
60
61         @warn = ();
62         $mime->header_set('Message-Id', '<a-mid@b>', '<c@d>');
63         ok($im->add($mime), 'secondary MID used');
64         like(join(' ', @warn), qr/mismatched/, 'warned about mismatch');
65         like(join(' ', @warn), qr/alternative/, 'warned about alternative');
66         is_deeply([ '<a-mid@b>', '<c@d>' ],
67                 [ $mime->header_obj->header_raw('Message-Id') ],
68                 'no new Message-Id added');
69
70         @warn = ();
71         $mime->header_set('Message-Id', '<a-mid@b>');
72         $mime->body_set('different');
73         ok($im->add($mime), 'reused mid ok');
74         like(join(' ', @warn), qr/reused/, 'warned about reused MID');
75         my @mids = $mime->header_obj->header_raw('Message-Id');
76         is($mids[1], '<a-mid@b>', 'original mid not changed');
77         like($mids[0], qr/\A<\w+\@localhost>\z/, 'new MID added');
78         is(scalar(@mids), 2, 'only one new MID added');
79
80         @warn = ();
81         $mime->header_set('Message-Id', '<a-mid@b>');
82         $mime->body_set('this one needs a random mid');
83         my $gen = content_digest($mime)->hexdigest . '@localhost';
84         my $fake = PublicInbox::MIME->new($mime->as_string);
85         $fake->header_set('Message-Id', $gen);
86         ok($im->add($fake), 'fake added easily');
87         is_deeply(\@warn, [], 'no warnings from a faker');
88         ok($im->add($mime), 'random MID made');
89         like(join(' ', @warn), qr/using random/, 'warned about using random');
90         @mids = $mime->header_obj->header_raw('Message-Id');
91         is($mids[1], '<a-mid@b>', 'original mid not changed');
92         like($mids[0], qr/\A<\w+\@localhost>\z/, 'new MID added');
93         is(scalar(@mids), 2, 'only one new MID added');
94
95         @warn = ();
96         $mime->header_set('Message-Id');
97         ok($im->add($mime), 'random MID made for MID free message');
98         @mids = $mime->header_obj->header_raw('Message-Id');
99         like($mids[0], qr/\A<\w+\@localhost>\z/, 'mid was generated');
100         is(scalar(@mids), 1, 'new generated');
101 }
102
103 {
104         $mime->header_set('Message-Id', '<abcde@1>', '<abcde@2>');
105         ok($im->add($mime), 'message with multiple Message-ID');
106         $im->done;
107         my @found;
108         $ibx->search->reopen;
109         $ibx->search->each_smsg_by_mid('abcde@1', sub { push @found, @_; 1 });
110         is(scalar(@found), 1, 'message found by first MID');
111         $ibx->search->each_smsg_by_mid('abcde@2', sub { push @found, @_; 1 });
112         is(scalar(@found), 2, 'message found by second MID');
113         is($found[0]->{doc_id}, $found[1]->{doc_id}, 'same document');
114         ok($found[1]->{doc_id} > 0, 'doc_id is positive');
115 }
116
117 SKIP: {
118         use Fcntl qw(FD_CLOEXEC F_SETFD F_GETFD);
119         use Net::NNTP;
120         use IO::Socket;
121         use Socket qw(SO_KEEPALIVE IPPROTO_TCP TCP_NODELAY);
122         eval { require Danga::Socket };
123         skip "Danga::Socket missing $@", 2 if $@;
124         my $err = "$mainrepo/stderr.log";
125         my $out = "$mainrepo/stdout.log";
126         my %opts = (
127                 LocalAddr => '127.0.0.1',
128                 ReuseAddr => 1,
129                 Proto => 'tcp',
130                 Type => SOCK_STREAM,
131                 Listen => 1024,
132         );
133         my $group = 'inbox.comp.test.v2writable';
134         my $pi_config = "$mainrepo/pi_config";
135         open my $fh, '>', $pi_config or die "open: $!\n";
136         print $fh <<EOF
137 [publicinbox "test-v2writable"]
138         mainrepo = $mainrepo
139         version = 2
140         address = test\@example.com
141         newsgroup = $group
142 EOF
143         ;
144         close $fh or die "close: $!\n";
145         my $sock = IO::Socket::INET->new(%opts);
146         ok($sock, 'sock created');
147         my $pid;
148         my $len;
149         END { kill 'TERM', $pid if defined $pid };
150         $! = 0;
151         my $fl = fcntl($sock, F_GETFD, 0);
152         ok(! $!, 'no error from fcntl(F_GETFD)');
153         is($fl, FD_CLOEXEC, 'cloexec set by default (Perl behavior)');
154         $pid = fork;
155         if ($pid == 0) {
156                 use POSIX qw(dup2);
157                 $ENV{PI_CONFIG} = $pi_config;
158                 # pretend to be systemd
159                 fcntl($sock, F_SETFD, $fl &= ~FD_CLOEXEC);
160                 dup2(fileno($sock), 3) or die "dup2 failed: $!\n";
161                 $ENV{LISTEN_PID} = $$;
162                 $ENV{LISTEN_FDS} = 1;
163                 my $nntpd = 'blib/script/public-inbox-nntpd';
164                 exec $nntpd, "--stdout=$out", "--stderr=$err";
165                 die "FAIL: $!\n";
166         }
167         ok(defined $pid, 'forked nntpd process successfully');
168         $! = 0;
169         fcntl($sock, F_SETFD, $fl |= FD_CLOEXEC);
170         ok(! $!, 'no error from fcntl(F_SETFD)');
171         my $host_port = $sock->sockhost . ':' . $sock->sockport;
172         my $n = Net::NNTP->new($host_port);
173         $n->group($group);
174         my $x = $n->xover('1-');
175         my %uniq;
176         foreach my $num (sort { $a <=> $b } keys %$x) {
177                 my $mid = $x->{$num}->[3];
178                 is($uniq{$mid}++, 0, "MID for $num is unique in XOVER");
179                 is_deeply($n->xhdr('Message-ID', $num),
180                          { $num => $mid }, "XHDR lookup OK on num $num");
181                 is_deeply($n->xhdr('Message-ID', $mid),
182                          { $mid => $mid }, "XHDR lookup OK on MID $num");
183         }
184         my %nn;
185         foreach my $mid (@{$n->newnews(0, $group)}) {
186                 is($nn{$mid}++, 0, "MID is unique in NEWNEWS");
187         }
188         is_deeply([sort keys %nn], [sort keys %uniq]);
189 };
190 {
191         local $ENV{NPROC} = 2;
192         $im = PublicInbox::V2Writable->new($ibx, 1);
193         is($im->{partitions}, 1, 'detected single partition from previous');
194 }
195
196 done_testing();