]> Sergey Matveev's repositories - public-inbox.git/blobdiff - t/nntpd.t
watch: add NNTP support
[public-inbox.git] / t / nntpd.t
index b24720eb9dd0c323158995b0a4c3532e1829ba6d..d72d6a1ce7ee629482e61633a5a61705ada6cb6e 100644 (file)
--- a/t/nntpd.t
+++ b/t/nntpd.t
@@ -12,6 +12,8 @@ use IO::Socket;
 use Socket qw(IPPROTO_TCP TCP_NODELAY);
 use Net::NNTP;
 use Sys::Hostname;
+use POSIX qw(_exit);
+use Digest::SHA;
 
 # FIXME: make easier to test both versions
 my $version = $ENV{PI_TEST_VERSION} || 1;
@@ -109,11 +111,6 @@ EOF
                $n->can('starttls') or
                        skip('Net::NNTP too old to support STARTTLS', 2);
                require_mods('IO::Socket::SSL', 2);
-               eval {
-                       IO::Socket::SSL->VERSION(2.007);
-               } or skip(<<EOF, 2);
-IO::Socket::SSL <2.007 not supported by Net::NNTP
-EOF
                ok(!$n->starttls, 'STARTTLS fails when unconfigured');
                is($n->code, 580, 'got 580 code on server w/o TLS');
        };
@@ -287,21 +284,37 @@ Date: Fri, 02 Oct 1993 00:00:00 +0000
        # pipelined requests:
        {
                my $nreq = 90;
+               my $nart = 2;
                syswrite($s, "GROUP $group\r\n");
                my $res = <$s>;
                my $rdr = fork;
                if ($rdr == 0) {
-                       use POSIX qw(_exit);
                        for (1..$nreq) {
                                <$s> =~ /\A224 / or _exit(1);
                                <$s> =~ /\A1/ or _exit(2);
                                <$s> eq ".\r\n" or _exit(3);
                        }
+                       my %sums;
+                       for (1..$nart) {
+                               <$s> =~ /\A220 / or _exit(4);
+                               my $dig = Digest::SHA->new(1);
+                               while (my $l = <$s>) {
+                                       last if $l eq ".\r\n";
+                                       $dig->add($l);
+                               }
+                               $dig = $dig->hexdigest;
+                               $sums{$dig}++;
+                       }
+                       if ($nart) {
+                               scalar(keys(%sums)) == 1 or _exit(5);
+                               (values(%sums))[0] == $nart or _exit(6);
+                       }
                        _exit(0);
                }
                for (1..$nreq) {
                        syswrite($s, "XOVER 1\r\n");
                }
+               syswrite($s, "ARTICLE 1\r\n" x $nart);
                is($rdr, waitpid($rdr, 0), 'reader done');
                is($? >> 8, 0, 'no errors');
        }
@@ -339,6 +352,7 @@ Date: Fri, 02 Oct 1993 00:00:00 +0000
                my @of = xqx([$lsof, '-p', $td->{pid}], undef, $noerr);
                is(scalar(grep(/\(deleted\)/, @of)), 0, 'no deleted files');
        };
+       SKIP: { test_watch($tmpdir, $sock, $group) };
        {
                setsockopt($s, IPPROTO_TCP, TCP_NODELAY, 1);
                syswrite($s, 'HDR List-id 1-');
@@ -378,4 +392,55 @@ sub read_til_dot {
        $buf;
 }
 
+sub test_watch {
+       my ($tmpdir, $sock, $group) = @_;
+       use_ok 'PublicInbox::WatchMaildir';
+       use_ok 'PublicInbox::InboxIdle';
+       require_git('1.8.5', 1) or skip('git 1.8.5+ needed for --urlmatch', 4);
+       my $old_env = { HOME => $ENV{HOME} };
+       my $home = "$tmpdir/watch_home";
+       mkdir $home or BAIL_OUT $!;
+       mkdir "$home/.public-inbox" or BAIL_OUT $!;
+       local $ENV{HOME} = $home;
+       my $name = 'watchnntp';
+       my $addr = "i1\@example.com";
+       my $url = "http://example.com/i1";
+       my $inboxdir = "$tmpdir/watchnntp";
+       my $cmd = ['-init', '-V1', '-Lbasic', $name, $inboxdir, $url, $addr];
+       my ($ihost, $iport) = ($sock->sockhost, $sock->sockport);
+       my $nntpurl = "nntp://$ihost:$iport/$group";
+       run_script($cmd) or BAIL_OUT("init $name");
+       xsys(qw(git config), "--file=$home/.public-inbox/config",
+                       "publicinbox.$name.watch",
+                       $nntpurl) == 0 or BAIL_OUT "git config $?";
+       # try again with polling
+       xsys(qw(git config), "--file=$home/.public-inbox/config",
+               'nntp.PollInterval', 0.11) == 0
+               or BAIL_OUT "git config $?";
+       my $cfg = PublicInbox::Config->new;
+       PublicInbox::DS->Reset;
+       my $ii = PublicInbox::InboxIdle->new($cfg);
+       my $cb = sub { PublicInbox::DS->SetPostLoopCallback(sub {}) };
+       my $obj = bless \$cb, 'PublicInbox::TestCommon::InboxWakeup';
+       $cfg->each_inbox(sub { $_[0]->subscribe_unlock('ident', $obj) });
+       my $watcherr = "$tmpdir/watcherr";
+       open my $err_wr, '>', $watcherr or BAIL_OUT $!;
+       open my $err, '<', $watcherr or BAIL_OUT $!;
+       my $w = start_script(['-watch'], undef, { 2 => $err_wr });
+
+       diag 'waiting for initial fetch...';
+       PublicInbox::DS->EventLoop;
+       diag 'inbox unlocked on initial fetch';
+       $w->kill;
+       $w->join;
+       is($?, 0, 'no error in exited -watch process');
+       $cfg->each_inbox(sub { shift->unsubscribe_unlock('ident') });
+       $ii->close;
+       PublicInbox::DS->Reset;
+       my @err = grep(!/^I:/, <$err>);
+       is(@err, 0, 'no warnings/errors from -watch'.join(' ', @err));
+       my @ls = xqx(['git', "--git-dir=$inboxdir", qw(ls-tree -r HEAD)]);
+       isnt(scalar(@ls), 0, 'imported something');
+}
+
 1;