Subject: | Zero-length writes raise an exception |
Hi,
As mentioned briefly in IRC:
Calling IO::Async::Stream->write('') will raise 'Internal consistency
problem - empty writequeue item without a gensub', which is perfectly
reasonable but the wording is perhaps not immediately obvious.
Simple test case attached to demonstrate the current behaviour.
For a normal ->write with no other parameters, ignoring the request
might be acceptable, although if the on_flush callback is provided then
perhaps it should raise an error ("on_flush callback given but no data
was provided to ->write"?), or include the on_flush with the previous
->write entry.
Since I was just using on_flush as a way of tracking buffer status,
ended up replacing the write with this instead:
$stream->write($buffer, on_flush => ...) if length $buffer;
Alternatively, maybe could just include a short documentation note for
IO::Async::Stream->write explaining that an empty write buffer will
raise an error?
thanks,
Tom
Subject: | empty-write.t |
use strict;
use warnings;
use Test::More tests => 2;
use IO::Async::Loop;
use IO::Async::Stream;
use Test::Exception;
my $loop = IO::Async::Loop->new;
my $stream = IO::Async::Stream->new_for_stdout;
$loop->add($stream);
$stream->write('');
$loop->later(sub { $loop->loop_stop; });
# Fails with 'Internal consistency problem - empty writequeue item without a gensub'
lives_ok { $loop->loop_forever; } 'loop_forever runs without complaints';
$stream->write(
'',
on_flush => sub {
pass('Flushed OK');
$loop->loop_stop;
}
);
$loop->later(sub { $loop->loop_stop; });
# Fails with 'Internal consistency problem - empty writequeue item without a gensub'
lives_ok { $loop->loop_forever; } 'loop_forever works with on_flush as well';