]> Sergey Matveev's repositories - public-inbox.git/blobdiff - t/nntpd.t
t/nntpd: avoid fork+exec for search indexing
[public-inbox.git] / t / nntpd.t
index d8c2e7b305f4563aefea6e3ab32885a51c89c1b5..a9df36dc7818764767e48bd5bb996745885b2a1a 100644 (file)
--- a/t/nntpd.t
+++ b/t/nntpd.t
@@ -16,26 +16,19 @@ use Fcntl qw(FD_CLOEXEC F_SETFD F_GETFD);
 use Socket qw(SO_KEEPALIVE IPPROTO_TCP TCP_NODELAY);
 use File::Temp qw/tempdir/;
 use Net::NNTP;
-use IPC::Run qw(run);
 
-my $tmpdir = tempdir(CLEANUP => 1);
+my $tmpdir = tempdir('pi-nntpd-XXXXXX', TMPDIR => 1, CLEANUP => 1);
 my $home = "$tmpdir/pi-home";
 my $err = "$tmpdir/stderr.log";
 my $out = "$tmpdir/stdout.log";
-my $pi_home = "$home/.public-inbox";
-my $pi_config = "$pi_home/config";
 my $maindir = "$tmpdir/main.git";
-my $main_bin = getcwd()."/t/main-bin";
-my $main_path = "$main_bin:$ENV{PATH}"; # for spamc ham mock
 my $group = 'test-nntpd';
 my $addr = $group . '@example.com';
 my $cfgpfx = "publicinbox.$group";
-my $failbox = "$home/fail.mbox";
-local $ENV{PI_EMERGENCY} = $failbox;
-my $mda = 'blib/script/public-inbox-mda';
 my $nntpd = 'blib/script/public-inbox-nntpd';
 my $init = 'blib/script/public-inbox-init';
-my $index = 'blib/script/public-inbox-index';
+use_ok 'PublicInbox::Import';
+use_ok 'PublicInbox::Git';
 
 my %opts = (
        LocalAddr => '127.0.0.1',
@@ -46,15 +39,16 @@ my %opts = (
 );
 my $sock = IO::Socket::INET->new(%opts);
 my $pid;
+my $len;
 END { kill 'TERM', $pid if defined $pid };
 {
        local $ENV{HOME} = $home;
        system($init, $group, $maindir, 'http://example.com/', $addr);
+       my $len;
 
        # ensure successful message delivery
        {
-               local $ENV{ORIGINAL_RECIPIENT} = $addr;
-               my $simple = Email::Simple->new(<<EOF);
+               my $mime = Email::MIME->new(<<EOF);
 From: Me <me\@example.com>
 To: You <you\@example.com>
 Cc: $addr
@@ -64,11 +58,14 @@ Date: Thu, 01 Jan 1970 06:06:06 +0000
 
 nntp
 EOF
-               my $in = $simple->as_string;
-               local $ENV{PATH} = $main_path;
-               IPC::Run::run([$mda], \$in);
-               is(0, $?, 'ran MDA correctly');
-               is(0, system($index, $maindir), 'indexed git dir');
+               $mime->header_set('List-Id', "<$addr>");
+               $len = length($mime->as_string);
+               my $git = PublicInbox::Git->new($maindir);
+               my $im = PublicInbox::Import->new($git, 'test', $addr);
+               $im->add($mime);
+               $im->done;
+               my $s = PublicInbox::SearchIdx->new($maindir, 1);
+               $s->index_sync;
        }
 
        ok($sock, 'sock created');
@@ -151,7 +148,7 @@ EOF
                        'Thu, 01 Jan 1970 06:06:06 +0000',
                        '<nntp@example.com>',
                        '',
-                       '202',
+                       $len,
                        '1' ] }, "XOVER range works");
 
        is_deeply($n->xover('1'), {
@@ -160,7 +157,7 @@ EOF
                        'Thu, 01 Jan 1970 06:06:06 +0000',
                        '<nntp@example.com>',
                        '',
-                       '202',
+                       $len,
                        '1' ] }, "XOVER by article works");
 
        {
@@ -173,7 +170,7 @@ EOF
                like($r[0], qr/^224 /, 'got 224 response for OVER');
                is($r[1], "0\thihi\tMe <me\@example.com>\t" .
                        "Thu, 01 Jan 1970 06:06:06 +0000\t" .
-                       "$mid\t\t202\t1", 'OVER by Message-ID works');
+                       "$mid\t\t$len\t1", 'OVER by Message-ID works');
                is($r[2], '.', 'correctly terminated response');
        }
 
@@ -185,7 +182,12 @@ EOF
                 'XHDR on invalid header returns empty');
 
        {
-               syswrite($s, "HDR List-id 1-\r\n");
+               setsockopt($s, IPPROTO_TCP, TCP_NODELAY, 1);
+               syswrite($s, 'HDR List-id 1-');
+               select(undef, undef, undef, 0.15);
+               ok(kill('TERM', $pid), 'killed nntpd');
+               select(undef, undef, undef, 0.15);
+               syswrite($s, "\r\n");
                $buf = '';
                do {
                        sysread($s, $buf, 4096, length($buf));
@@ -196,9 +198,8 @@ EOF
                is(scalar @r, 1, 'only one response line');
        }
 
-       ok(kill('TERM', $pid), 'killed nntpd');
-       $pid = undef;
-       waitpid(-1, 0);
+       is($pid, waitpid($pid, 0), 'nntpd exited successfully');
+       is($?, 0, 'no error in exited process');
 }
 
 done_testing();