]> Sergey Matveev's repositories - public-inbox.git/blob - t/psgi_v2.t
search: reopen DB if each_smsg_by_mid fails
[public-inbox.git] / t / psgi_v2.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 File::Temp qw/tempdir/;
7 use PublicInbox::MIME;
8 use PublicInbox::Config;
9 use PublicInbox::WWW;
10 my @mods = qw(DBD::SQLite Search::Xapian HTTP::Request::Common Plack::Test
11                 URI::Escape Plack::Builder);
12 foreach my $mod (@mods) {
13         eval "require $mod";
14         plan skip_all => "$mod missing for psgi_v2_dupes.t" if $@;
15 }
16 use_ok($_) for @mods;
17 use_ok 'PublicInbox::V2Writable';
18 my $mainrepo = tempdir('pi-v2_dupes-XXXXXX', TMPDIR => 1, CLEANUP => 1);
19 my $ibx = {
20         mainrepo => $mainrepo,
21         name => 'test-v2writable',
22         version => 2,
23         -primary_address => 'test@example.com',
24 };
25 $ibx = PublicInbox::Inbox->new($ibx);
26 my $new_mid;
27
28 my $im = PublicInbox::V2Writable->new($ibx, 1);
29 $im->{parallel} = 0;
30
31 my $mime = PublicInbox::MIME->create(
32         header => [
33                 From => 'a@example.com',
34                 To => 'test@example.com',
35                 Subject => 'this is a subject',
36                 'Message-ID' => '<a-mid@b>',
37                 Date => 'Fri, 02 Oct 1993 00:00:00 +0000',
38         ],
39         body => "hello world\n",
40 );
41 ok($im->add($mime), 'added one message');
42 $mime->body_set("hello world!\n");
43
44 my @warn;
45 local $SIG{__WARN__} = sub { push @warn, @_ };
46 ok($im->add($mime), 'added duplicate-but-different message');
47 is(scalar(@warn), 1, 'got one warning');
48 my @mids = $mime->header_obj->header_raw('Message-Id');
49 $new_mid = PublicInbox::MID::mid_clean($mids[0]);
50 $im->done;
51
52 my $cfgpfx = "publicinbox.v2test";
53 my $cfg = {
54         "$cfgpfx.address" => $ibx->{-primary_address},
55         "$cfgpfx.mainrepo" => $mainrepo,
56 };
57 my $config = PublicInbox::Config->new($cfg);
58 my $www = PublicInbox::WWW->new($config);
59 my ($res, $raw, @from_);
60 test_psgi(sub { $www->call(@_) }, sub {
61         my ($cb) = @_;
62         $res = $cb->(GET('/v2test/a-mid@b/raw'));
63         $raw = $res->content;
64         like($raw, qr/^hello world$/m, 'got first message');
65         like($raw, qr/^hello world!$/m, 'got second message');
66         @from_ = ($raw =~ m/^From /mg);
67         is(scalar(@from_), 2, 'two From_ lines');
68
69         $res = $cb->(GET("/v2test/$new_mid/raw"));
70         $raw = $res->content;
71         like($raw, qr/^hello world!$/m, 'second message with new Message-Id');
72         @from_ = ($raw =~ m/^From /mg);
73         is(scalar(@from_), 1, 'only one From_ line');
74 });
75
76 $mime->header_set('Message-Id', 'a-mid@b');
77 $mime->body_set("hello ghosts\n");
78 ok($im->add($mime), 'added 3rd duplicate-but-different message');
79 is(scalar(@warn), 2, 'got another warning');
80 like($warn[0], qr/mismatched/, 'warned about mismatched messages');
81 is($warn[0], $warn[1], 'both warnings are the same');
82
83 @mids = $mime->header_obj->header_raw('Message-Id');
84 my $third = PublicInbox::MID::mid_clean($mids[0]);
85 $im->done;
86
87 test_psgi(sub { $www->call(@_) }, sub {
88         my ($cb) = @_;
89         $res = $cb->(GET("/v2test/$third/raw"));
90         $raw = $res->content;
91         like($raw, qr/^hello ghosts$/m, 'got third message');
92         @from_ = ($raw =~ m/^From /mg);
93         is(scalar(@from_), 1, 'one From_ line');
94
95         $res = $cb->(GET('/v2test/a-mid@b/raw'));
96         $raw = $res->content;
97         like($raw, qr/^hello world$/m, 'got first message');
98         like($raw, qr/^hello world!$/m, 'got second message');
99         like($raw, qr/^hello ghosts$/m, 'got third message');
100         @from_ = ($raw =~ m/^From /mg);
101         is(scalar(@from_), 3, 'three From_ lines');
102 });
103
104 done_testing();
105
106 1;