1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
|
# Client push a lot of data to server, server got several EINBUFLIMIT
# errors, dynamically increase {in_buf_limit}, and finally receive all data.
# - EINBUFLIMIT
# - dynamic tuning of {in_buf_limit}
# - custom fields /^[A-Z]/
# - class/method names instead of CODE ref in {cb}
use warnings;
use strict;
use lib 't';
use share;
my $SIZE = 204800;
plan tests =>
1 # accept client
+ 3*4 # server got EINBUFLIMIT
+ 5 # server got EOF
;
my $srv_sock = tcp_server('127.0.0.1', 0);
my $srv_w = EV::io($srv_sock, EV::READ, sub {
if (my $paddr = accept my $sock, $srv_sock) {
my ($port,$iaddr) = sockaddr_in($paddr);
my $ip = inet_ntoa($iaddr);
is($ip, '127.0.0.1', 'ip correct');
IO::Stream->new({
fh => $sock,
cb => 'Server',
wait_for => EOF,
in_buf_limit=> 1024,
Prev_bytes => 0,
LimitErrs => 3,
});
}
elsif ($! != EAGAIN) {
die "accept: $!\n";
}
});
my $io = IO::Stream->new({
host => '127.0.0.1',
port => sockport($srv_sock),
cb => 'Client',
method => 'IO_client',
wait_for => SENT,
out_buf => 'x' x $SIZE,
out_pos => 0,
});
EV::loop();
package Server;
use Test::More;
use IO::Stream;
sub IO {
my ($self, $io, $e, $err) = @_;
if ($err) {
if ($err == EINBUFLIMIT) {
ok($io->{LimitErrs} > 0, 'got <in_buf_limit reached> error');
$io->{LimitErrs}--;
$io->{in_buf_limit} *= 10;
}
else {
die $err;
}
}
ok($io->{in_bytes} > $io->{Prev_bytes}, ' in_bytes incremented, good');
is(length($io->{in_buf}), $io->{in_bytes}, ' in_bytes correct');
$io->{Prev_bytes} = $io->{in_bytes};
if ($io->{in_bytes} < $SIZE) {
ok(!$io->{is_eof}, 'no eof yet');
} else {
ok($io->{LimitErrs} == 0, 'got ALL <in_buf_limit reached> errors');
ok($io->{is_eof}, 'now got {is_eof}!!!');
is($io->{in_bytes}, $SIZE, 'All data received');
exit;
}
}
package Client;
use Test::More;
use IO::Stream;
sub IO_client {
my ($self, $io, $e, $err) = @_;
die $err if $err;
shutdown $io->{fh}, 1;
}
|