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 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
|
#!/usr/bin/perl -w
use strict;
use File::Spec;
use File::Temp qw( tempdir );
use Test::More;
use HTML::Mason::Interp;
BEGIN
{
unless ( eval { require Test::Memory::Cycle;
Test::Memory::Cycle->import(); 1 } )
{
plan skip_all => 'These tests require Test::Memory::Cycle to run.';
}
}
plan tests => 8;
SIMPLE_OBJECTS:
{
my $interp = HTML::Mason::Interp->new( out_method => sub {} );
memory_cycle_ok( $interp, 'Interp before making a request' );
my $comp = $interp->make_component( comp_source => 'Comp' );
$interp->exec( $comp, foo => 1 );
memory_cycle_ok( $interp, 'Interp after making a request with in-memory comp' );
}
our $Destroyed = 0;
COMP_ON_DISK:
{
my $dir = tempdir( CLEANUP => 1 );
make_comp( $dir, 'comp1', <<'EOF' );
This is component 1.
<&| comp2, object => $object &>
content
</&>
<%args>
$object
</%args>
EOF
make_comp( $dir, 'comp2', <<'EOF' );
This is component 2.
EOF
my $interp = HTML::Mason::Interp->new( out_method => sub {},
comp_root => $dir,
);
$interp->exec( '/comp1', object => Object->new() );
memory_cycle_ok( $interp, 'Interp after making a request with on-disk comp' );
is( $Destroyed, 1, 'object passed into request was destroyed' );
my $req = $interp->make_request( comp => '/comp1', args => [ object => Object->new() ] );
memory_cycle_ok( $req, 'Request object' );
undef $req;
is( $Destroyed, 2, 'object passed into make_request was destroyed' );
}
# See http://marc.theaimsgroup.com/?l=mason&m=115883578111647&w=2 for
# details.
OBJECTS_CREATED_IN_COMP:
{
my $dir = tempdir( CLEANUP => 1 );
make_comp( $dir, 'comp1', <<'EOF' );
<& /comp2, object => Object->new() &>
Destroyed: <% Object->DestroyCount() %>
EOF
make_comp( $dir, 'comp2', 'Comp 2' );
my $output = '';
my $interp = HTML::Mason::Interp->new( out_method => \$output,
comp_root => $dir,
);
$Destroyed = 0;
$interp->exec('/comp1');
like( $output, qr/Destroyed: 1/,
'one object was destroyed in comp1' );
}
# See http://marc.theaimsgroup.com/?l=mason&m=111769803701028&w=2 for
# details. It actually has nothing to do with %ARGS, it's seems that
# anything referred to inside nested comp-with-content calls never
# gets destroyed.
TWO_COMP_WITH_CONTENT_CALLS:
{
my $dir = tempdir( CLEANUP => 1 );
make_comp( $dir, 'comp1', <<'EOF' );
<%init>
my $object = Object->new();
</%init>
<&| .sub &>
%# <% $object %> - with this here the object doesn't leak!
<&| .sub &>
<% $object %>
</&>
</&>
else
<%def .sub>
<% $m->content() %>
</%def>
EOF
my $output = '';
my $interp = HTML::Mason::Interp->new( out_method => sub {},
comp_root => $dir,
);
$Destroyed = 0;
$interp->exec('/comp1');
# See
# http://www.xray.mpe.mpg.de/mailing-lists/perl5-porters/2006-10/msg00189.html
# for further details.
local $TODO = 'This seems to be a bug in Perl (< 5.10.0), not Mason.'
if $] < 5.010000;
is( $Destroyed, 1, 'object was destroyed - 2 layers of comp-with-content' );
}
sub make_comp
{
my $dir = shift;
my $file = shift;
my $content = shift;
open my $fh, '>', File::Spec->catfile( $dir, $file )
or die $!;
print $fh $content
or die $!;
close $fh;
}
package Object;
sub new { return bless {}, $_[0] }
sub DESTROY { $Destroyed++ }
sub DestroyCount { $Destroyed }
|