You are viewing a plain text version of this content. The canonical link for it is here.
Posted to modperl-cvs@perl.apache.org by st...@apache.org on 2003/01/12 05:03:03 UTC
cvs commit: modperl-2.0/t/filter context_stream.t context.t
stas 2003/01/11 20:03:03
Added: t/filter/TestFilter context_stream.pm context.pm
t/filter context_stream.t context.t
Log:
add two tests that exercise the filter context, one using bbs, the other
using streaming interface
Revision Changes Path
1.1 modperl-2.0/t/filter/TestFilter/context_stream.pm
Index: context_stream.pm
===================================================================
package TestFilter::context_stream;
use strict;
use warnings;# FATAL => 'all';
use Apache::RequestRec ();
use Apache::RequestIO ();
use APR::Brigade ();
use APR::Bucket ();
use base qw(Apache::Filter);
use Apache::Const -compile => qw(OK M_POST);
use APR::Const -compile => ':common';
use constant BLOCK_SIZE => 5003;
use constant READ_SIZE => 1024;
sub handler {
my $filter = shift;
my $ctx = $filter->ctx;
my $data = exists $ctx->{data} ? $ctx->{data} : '';
$ctx->{invoked}++;
while ($filter->read(my $bdata, READ_SIZE)) {
$data .= $bdata;
my $len = length $data;
my $blocks = 0;
if ($len >= BLOCK_SIZE) {
$blocks = int($len / BLOCK_SIZE);
$len = $len % BLOCK_SIZE;
$data = substr $data, $blocks*BLOCK_SIZE, $len;
$ctx->{blocks} += $blocks;
}
if ($blocks) {
$filter->print("#" x $blocks);
}
}
# flush the remaining data and add a statistics signature
if ($filter->seen_eos) {
$filter->print("$data\n") if $data;
my $sig = join "\n", "received $ctx->{blocks} complete blocks",
"filter invoked $ctx->{invoked} times\n";
$filter->print($sig);
}
else {
# no need to store context, since it was the last invocation
$ctx->{data} = $data;
$filter->ctx($ctx);
}
return Apache::OK;
}
sub response {
my $r = shift;
$r->content_type('text/plain');
# make sure that
# - we send big enough data so it won't fit into one buffer
# - use chunk size which doesn't nicely fit into a buffer size, so
# we have something to store in the context between filter calls
my $blocks = 11;
my $block_size = BLOCK_SIZE + 1;
# embed \n, so there will be several buckets
my $block = "x" x $block_size;
for (1..$blocks) {
$r->print($block);
$r->rflush; # so the filter reads a chunk at a time
}
return Apache::OK;
}
1;
__DATA__
SetHandler modperl
PerlResponseHandler TestFilter::context::response
1.1 modperl-2.0/t/filter/TestFilter/context.pm
Index: context.pm
===================================================================
package TestFilter::context;
use strict;
use warnings;# FATAL => 'all';
use Apache::RequestRec ();
use Apache::RequestIO ();
use APR::Brigade ();
use APR::Bucket ();
use base qw(Apache::Filter);
use Apache::Const -compile => qw(OK M_POST);
use APR::Const -compile => ':common';
use constant BLOCK_SIZE => 5003;
sub handler {
my($filter, $bb) = @_;
my $c = $filter->c;
my $bb_ctx = APR::Brigade->new($c->pool, $c->bucket_alloc);
my $ctx = $filter->ctx;
$ctx->{invoked}++;
my $data = exists $ctx->{data} ? $ctx->{data} : '';
while (my $bucket = $bb->first) {
$bucket->remove;
if ($bucket->is_eos) {
# flush the remainings and send a stats signature
$bb_ctx->insert_tail(APR::Bucket->new("$data\n")) if $data;
my $sig = join "\n", "received $ctx->{blocks} complete blocks",
"filter invoked $ctx->{invoked} times\n";
$bb_ctx->insert_tail(APR::Bucket->new($sig));
$bb_ctx->insert_tail($bucket);
last;
}
my $status = $bucket->read(my $bdata);
return $status unless $status == APR::SUCCESS;
if (defined $bdata) {
$data .= $bdata;
my $len = length $data;
my $blocks = 0;
if ($len >= BLOCK_SIZE) {
$blocks = int($len / BLOCK_SIZE);
$len = $len % BLOCK_SIZE;
$data = substr $data, $blocks*BLOCK_SIZE, $len;
$ctx->{blocks} += $blocks;
}
if ($blocks) {
$bucket = APR::Bucket->new("#" x $blocks);
$bb_ctx->insert_tail($bucket);
}
}
}
$ctx->{data} = $data;
$filter->ctx($ctx);
my $rv = $filter->next->pass_brigade($bb_ctx);
return $rv unless $rv == APR::SUCCESS;
return Apache::OK;
}
sub handler1 {
my $filter = shift;
my $ctx = $filter->ctx;
$ctx->{invoked}++;
my ($data, $len) = (exists $ctx->{leftover} && length $ctx->{leftover})
? ($ctx->{leftover}, $ctx->{leftover_len})
: ('', 0);
while (my $read_len = $filter->read(my $buffer, 1024)) {
$len += $read_len;
$data .= $buffer;
if ($len >= BLOCK_SIZE) {
my $blocks = int($len / BLOCK_SIZE);
warn "len $len, $read_len, blocks $blocks\n";
$len = $len % BLOCK_SIZE;
warn "len $len ", length($data), "\n";
$data = substr $data, $blocks*BLOCK_SIZE, $len;
$ctx->{blocks} += $blocks;
$filter->print("#" x $blocks);
}
}
#
# # there shouldn't be any leftovers in our test
# if ($filter->eos) {
# if ($len) {
# $filter->print("?");
# }
# $filter->print("sig: $blocks blocks");
# }
# else {
$ctx->{leftover} = $data;
$ctx->{leftover_len} = $len;
$filter->ctx($ctx);
# }
return Apache::OK;
}
sub response {
my $r = shift;
$r->content_type('text/plain');
# make sure that
# - we send big enough data so it won't fit into one buffer
# - use chunk size which doesn't nicely fit into a buffer size, so
# we have something to store in the context between filter calls
my $blocks = 11;
my $block_size = BLOCK_SIZE + 1;
# embed \n, so there will be several buckets
my $block = "x" x $block_size;
for (1..$blocks) {
$r->print($block);
$r->rflush; # so the filter reads a chunk at a time
}
return Apache::OK;
}
1;
__DATA__
SetHandler modperl
PerlResponseHandler TestFilter::context::response
1.1 modperl-2.0/t/filter/context_stream.t
Index: context_stream.t
===================================================================
use strict;
use warnings FATAL => 'all';
use Apache::Test;
use Apache::TestRequest;
use Apache::TestUtil;
plan tests => 1;
my $blocks = 11;
my $invoked = 34;
my $sig = join "\n", "received $blocks complete blocks",
"filter invoked $invoked times\n";
my $data = "#" x $blocks . "x" x $blocks;
my $expected = join "\n", $data, $sig;
{
# test the filtering of the mod_perl response handler
my $location = '/TestFilter::context_stream';
my $response = GET_BODY $location;
ok t_cmp($expected, $response, "context stream filter");
}
1.1 modperl-2.0/t/filter/context.t
Index: context.t
===================================================================
use strict;
use warnings FATAL => 'all';
use Apache::Test;
use Apache::TestRequest;
use Apache::TestUtil;
plan tests => 1;
my $blocks = 11;
my $invoked = 34;
my $sig = join "\n", "received $blocks complete blocks",
"filter invoked $invoked times\n";
my $data = "#" x $blocks . "x" x $blocks;
my $expected = join "\n", $data, $sig;
{
# test the filtering of the mod_perl response handler
my $location = '/TestFilter::context';
my $response = GET_BODY $location;
ok t_cmp($expected, $response, "context filter");
}