lei/store: stop shard workers + cat-file on idle
[public-inbox.git] / t / inbox_idle.t
blob0ccffab73a60de44fec190b486e65b849229c637
1 #!perl -w
2 # Copyright (C) all contributors <meta@public-inbox.org>
3 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
4 use v5.12;
5 use PublicInbox::TestCommon;
6 require_git 2.6;
7 require_mods(qw(DBD::SQLite));
8 require PublicInbox::SearchIdx;
9 use_ok 'PublicInbox::InboxIdle';
10 my ($tmpdir, $for_destroy) = tmpdir();
12 for my $V (1, 2) {
13         my $inboxdir = "$tmpdir/$V";
14         my $ibx = create_inbox "idle$V", tmpdir => $inboxdir, version => $V,
15                                 indexlevel => 'basic', -no_gc => 1, sub {
16                 my ($im, $ibx) = @_; # capture
17                 $im->done;
18                 $ibx->init_inbox(0);
19                 $_[0] = undef;
20                 return if $V != 1;
21                 my $sidx = PublicInbox::SearchIdx->new($ibx, 1);
22                 $sidx->idx_acquire;
23                 $sidx->set_metadata_once;
24                 $sidx->idx_release; # allow watching on lockfile
25         };
26         my $obj = InboxIdleTestObj->new;
27         my $pi_cfg = cfg_new $tmpdir, <<EOF;
28 [publicinbox "inbox-idle"]
29         inboxdir = $inboxdir
30         indexlevel = basic
31         address = $ibx->{-primary_address}
32 EOF
33         my $ident = 'whatever';
34         $pi_cfg->each_inbox(sub { shift->subscribe_unlock($ident, $obj) });
35         my $ii = PublicInbox::InboxIdle->new($pi_cfg);
36         ok($ii, 'InboxIdle created');
37         SKIP: {
38                 skip('inotify or kqueue missing', 1) unless $ii->{sock};
39                 ok(fileno($ii->{sock}) >= 0, 'fileno() gave valid FD');
40         }
41         my $im = $ibx->importer(0);
42         ok($im->add(eml_load('t/utf8.eml')), "$V added");
43         $im->done;
44         $ii->event_step;
45         is(scalar @{$obj->{called}}, 1, 'called on unlock');
46         $pi_cfg->each_inbox(sub { shift->unsubscribe_unlock($ident) });
47         ok($im->add(eml_load('t/data/0001.patch')), "$V added #2");
48         $im->done;
49         $ii->event_step;
50         is(scalar @{$obj->{called}}, 1, 'not called when unsubbed');
51         $ii->close;
54 done_testing;
56 package InboxIdleTestObj;
57 use strict;
59 sub new { bless {}, shift }
61 sub on_inbox_unlock {
62         my ($self, $ibx) = @_;
63         push @{$self->{called}}, $ibx;