]> Sergey Matveev's repositories - public-inbox.git/blob - t/gzip_filter.t
www: drop --subject from "git send-email" instructions
[public-inbox.git] / t / gzip_filter.t
1 # Copyright (C) 2020-2021 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 Test::More;
5 use IO::Handle (); # autoflush
6 use Fcntl qw(SEEK_SET);
7 use PublicInbox::TestCommon;
8 require_mods(qw(Compress::Zlib IO::Uncompress::Gunzip));
9 require_ok 'PublicInbox::GzipFilter';
10
11 {
12         open my $fh, '+>', undef or die "open: $!";
13         open my $dup, '>&', $fh or die "dup $!";
14         $dup->autoflush(1);
15         my $filter = PublicInbox::GzipFilter->new->attach($dup);
16         ok($filter->write("hello"), 'wrote something');
17         ok($filter->write("world"), 'wrote more');
18         $filter->close;
19         seek($fh, 0, SEEK_SET) or die;
20         IO::Uncompress::Gunzip::gunzip($fh => \(my $buf));
21         is($buf, 'helloworld', 'buffer matches');
22 }
23
24 {
25         pipe(my ($r, $w)) or die "pipe: $!";
26         $w->autoflush(1);
27         close $r or die;
28         my $filter = PublicInbox::GzipFilter->new->attach($w);
29         my $sigpipe;
30         local $SIG{PIPE} = sub { $sigpipe = 1 };
31         open my $fh, '<', 'COPYING' or die "open(COPYING): $!";
32         my $buf = do { local $/; <$fh> };
33         while ($filter->write($buf .= rand)) {}
34         ok($sigpipe, 'got SIGPIPE');
35         close $w;
36 }
37 done_testing;