kraih/mojo — Issue #248: stream->resume on file handle fails to produce read event
Author: jjYBdx4IL
Created: 2011-12-19 17:50:13+00:00
State: open

not good, because I don't know any clean handling of that situation (need to read file handle and stream it to the client, so the read should be paused while the client is still reading in order to avoid ever-growing buffers).

Test case:

# vim:set shiftwidth=4 tabstop=4 expandtab ai smartindent fileformat=unix fileencoding=utf-8 syntax=perl:
BEGIN { $ENV{MOJO_CHUNK_SIZE}=4; }

# testing generation of read-events after stream->resume...

use Test::More tests => 1;
use strict;
use warnings;
use FindBin;
use Mojo::IOLoop;
use Mojo::IOLoop::Stream;
require bytes;
no bytes;

open my $fh, '<:raw', $FindBin::Bin.'/'.$FindBin::Script or die $!;
my $s = Mojo::IOLoop::Stream->new($fh);
my $id = Mojo::IOLoop->stream($s);
my $cnt = 0;
my $paused = 0;
alarm 3;
$s->on(read => sub {
my($self,$chunk) = @_;
# print STDERR ">>>$chunk<<<".$/;
die if (bytes::length($chunk)!=4);
die if $paused;
$cnt++;
if($cnt==1) {
$s->pause;
$paused = 1;
Mojo::IOLoop->timer(1 => sub {$paused=0; $s->resume;});
}
if($cnt==2) {
Mojo::IOLoop->stop;
}
});

Mojo::IOLoop->start;

ok($cnt==2);