lei/store: stop shard workers + cat-file on idle
[public-inbox.git] / t / watch_filter_rubylang.t
blobf72feb9f87f267dafe37766fea2228d61e126283
1 # Copyright (C) all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
3 use v5.12;
4 use PublicInbox::TestCommon;
5 use PublicInbox::Eml;
6 require_mods(qw(DBD::SQLite Xapian));
7 use_ok 'PublicInbox::Watch';
8 use_ok 'PublicInbox::Emergency';
9 my ($tmpdir, $for_destroy) = tmpdir();
10 local $ENV{PI_CONFIG} = "$tmpdir/pi_config";
12 my @v = qw(V1);
13 SKIP: {
14         if (require_git(2.6, 1)) {
15                 use_ok 'PublicInbox::V2Writable';
16                 push @v, 'V2';
17         } else {
18                 skip 'git 2.6+ needed for V2', 40;
19         }
22 for my $v (@v) {
23         my @warn;
24         local $SIG{__WARN__} = sub { push @warn, @_ };
25         my $inboxdir = "$tmpdir/$v";
26         my $maildir = "$tmpdir/md-$v";
27         my $spamdir = "$tmpdir/spam-$v";
28         my $addr = "test-$v\@example.com";
29         my @cmd = ('-init', '-Lfull', "-$v", $v, $inboxdir,
30                 "http://example.com/$v", $addr);
31         ok(run_script(\@cmd), 'public-inbox init');
32         PublicInbox::Emergency->new($spamdir);
34         for my $i (1..15) {
35                 my $msg = <<EOF;
36 From: user\@example.com
37 To: $addr
38 Subject: blah $i
39 X-Mail-Count: $i
40 Message-Id: <a.$i\@b.com>
41 Date: Sat, 05 Jan 2019 04:19:17 +0000
43 something
44 EOF
45                 PublicInbox::Emergency->new($maildir)->prepare(\$msg);
46         }
48         my $spam = <<EOF;
49 From: spammer\@example.com
50 To: $addr
51 Subject: spam
52 X-Mail-Count: 99
53 Message-Id: <a.99\@b.com>
54 Date: Sat, 05 Jan 2019 04:19:17 +0000
56 spam
57 EOF
58         PublicInbox::Emergency->new($maildir)->prepare(\"$spam");
59         my $cfg = cfg_new $tmpdir, <<EOM;
60 [publicinbox "$v"]
61         address = $addr
62         inboxdir = $inboxdir
63         watch = maildir:$maildir
64         filter = PublicInbox::Filter::RubyLang
65         altid = serial:alerts:file=msgmap.sqlite3
66 [publicinboxwatch]
67         watchspam = maildir:$spamdir
68 EOM
69         my $ibx = $cfg->lookup_name($v);
70         $ibx->{-no_fsync} = 1;
71         ok($ibx, 'found inbox by name');
73         my $w = PublicInbox::Watch->new($cfg);
74         for my $i (1..2) {
75                 $w->scan('full');
76         }
78         # make sure all serials are searchable:
79         for my $i (1..15) {
80                 my $mset = $ibx->search->mset("alerts:$i");
81                 is($mset->size, 1, "got one result for alerts:$i");
82                 my $msgs = $ibx->search->mset_to_smsg($ibx, $mset);
83                 is($msgs->[0]->{mid}, "a.$i\@b.com", "got expected MID for $i");
84         }
85         is($ibx->search->mset('b:spam')->size, 1, 'got spam message');
87         my $nr = unlink <$maildir/new/*>;
88         is(16, $nr);
89         {
90                 PublicInbox::Emergency->new($spamdir)->prepare(\$spam);
91                 my @new = glob("$spamdir/new/*");
92                 my @p = split(m!/+!, $new[0]);
93                 ok(link($new[0], "$spamdir/cur/".$p[-1].":2,S"));
94                 is(unlink($new[0]), 1);
95         }
96         $w->scan('full');
98         # ensure orderly destruction to avoid SQLite segfault:
99         PublicInbox::DS->Reset;
101         $cfg = PublicInbox::Config->new($cfg->{-f});
102         $ibx = $cfg->lookup_name($v);
103         $ibx->{-no_fsync} = 1;
104         is($ibx->search->reopen->mset('b:spam')->size, 0, 'spam removed');
106         is_deeply([], \@warn, 'no warnings');
109 done_testing();