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 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
|
use lib 't/lib';
use TestHelp;
use Net::Stomp::Frame;
my ($s,$fh)=mkstomp_testsocket(timeout=>1);
subtest 'one frame' => sub {
my $timeout_in_call;
my $orig = \&Net::Stomp::_read_data;
no warnings 'redefine';
local *Net::Stomp::_read_data = sub {
my ($self,$timeout) = @_;
$timeout_in_call=$timeout;
$self->$orig($timeout);
};
my $frame = Net::Stomp::Frame->new({
command=>'MESSAGE',
headers=>{'message-id'=>1},
body=>'string',
});
$fh->{to_read}=$frame->as_string;
my $received = $s->receive_frame;
cmp_deeply($received,$frame,'received and parsed');
is($timeout_in_call,1,'correct timeout passed');
$fh->{to_read}=$frame->as_string;
$received = $s->receive_frame({timeout=>3});
is($timeout_in_call,3,'correct timeout passed');
};
subtest 'two frames' => sub {
my @frames = map {Net::Stomp::Frame->new({
command=>'MESSAGE',
headers=>{'message-id'=>$_},
body=>'string',
})} (1,2);
$fh->{to_read}=join '',map {$_->as_string} @frames;
my $received = $s->receive_frame;
cmp_deeply($received,$frames[0],'received and parsed');
$received = $s->receive_frame;
cmp_deeply($received,$frames[1],'received and parsed');
};
subtest 'a few bytes at a time' => sub {
my $frame = Net::Stomp::Frame->new({
command=>'MESSAGE',
headers=>{'message-id'=>1},
body=>'string',
});
my $frame_string = $frame->as_string;
$fh->{to_read} = sub {
return substr($frame_string,0,2,'');
};
my $received = $s->receive_frame;
cmp_deeply($received,$frame,'received and parsed');
};
subtest 'one frame, with content-length' => sub {
my $str = "string\0with\0zeroes\0";
my $frame = Net::Stomp::Frame->new({
command=>'MESSAGE',
body=>$str,
headers=>{
'message-id'=>1,
'content-length'=>length($str),
},
});
$fh->{to_read}=$frame->as_string;
my $received = $s->receive_frame;
cmp_deeply($received,$frame,'received and parsed');
};
subtest 'a few bytes at a time, with content-length' => sub {
my $str = "string\0with\0zeroes\0";
my $frame = Net::Stomp::Frame->new({
command=>'MESSAGE',
body=>$str,
headers=>{
'message-id'=>1,
'content-length'=>length($str),
},
});
my $frame_string = $frame->as_string;
$fh->{to_read} = sub {
return substr($frame_string,0,2,'');
};
my $received = $s->receive_frame;
cmp_deeply($received,$frame,'received and parsed');
};
subtest 'buffer boundary condition' => sub {
# this is a regression test for RT #105500, thanks Dryapak Grigory
# for reporting it
my $str = "string\0with\0zeroes\0";
my $frame = Net::Stomp::Frame->new({
command=>'MESSAGE',
body=>$str,
headers=>{
'message-id'=>1,
'content-length'=>length($str),
},
});
my $frame_string = $frame->as_string;
# the first read gets the entire frame *minus the terminating 0*
# the second read gets the zero
my $bufsize = length($frame_string)-1;
$s->bufsize($bufsize);
# let's add another frame
$frame_string .= $frame_string;
$fh->{to_read} = sub {
return substr($frame_string,0,$bufsize,'');
};
my $received = $s->receive_frame;
cmp_deeply($received,$frame,'received and parsed');
$received = $s->receive_frame;
cmp_deeply($received,$frame,'received and parsed, twice');
};
done_testing;
|