make the missing data handler exception slightly more useful
[catagits/Catalyst-Runtime.git] / t / optional_memleak.t
CommitLineData
7c40a30f 1use strict;
2use warnings;
3
4853fb50 4use Test::More;
5BEGIN {
6 plan skip_all => 'set TEST_MEMLEAK to enable this test'
7 unless $ENV{TEST_MEMLEAK};
8}
9
7c40a30f 10use FindBin;
a2e038a1 11use lib "$FindBin::Bin/lib";
7c40a30f 12use Catalyst::Test 'TestApp';
7c40a30f 13
15f0ede8 14eval "use Proc::ProcessTable";
9b984642 15plan skip_all => 'Proc::ProcessTable required for this test' if $@;
7c40a30f 16
3eca4f18 17eval "use HTTP::Body 0.03";
18plan skip_all => 'HTTP::Body >= 0.03 required for this test' if $@;
19
15f0ede8 20eval "use YAML";
21plan skip_all => 'YAML required for this test' if $@;
22
9b984642 23our $t = Proc::ProcessTable->new( cache_ttys => 1 );
010c814d 24our ( $initial, $final ) = ( 0, 0 );
e63e8323 25our $tests = YAML::LoadFile("$FindBin::Bin/optional_stress.yml");
7c40a30f 26
010c814d 27my $total_tests = 0;
010c814d 28
13af225c 29# let the user specify a single uri to test
30my $user_test = shift;
31if ( $user_test ) {
32 plan tests => 1;
33 run_test( $user_test );
34}
35# otherwise, run all tests
36else {
37 map { $total_tests += scalar @{ $tests->{$_} } } keys %{$tests};
38 plan tests => $total_tests;
39
40 foreach my $test_group ( keys %{$tests} ) {
41 foreach my $test ( @{ $tests->{$test_group} } ) {
42 run_test( $test );
43 }
010c814d 44 }
45}
46
47sub run_test {
48 my $uri = shift || die 'No URI given for test';
7c40a30f 49
010c814d 50 print "TESTING $uri\n";
51
52 # make a few requests to set initial memory size
53 for ( 1 .. 3 ) {
54 request( $uri );
55 }
56
9b984642 57 $initial = size_of($$);
58 print "Initial Size: $initial\n";
7c40a30f 59
010c814d 60 for ( 1 .. 500 ) {
61 request( $uri );
7c40a30f 62 }
63
9b984642 64 $final = size_of($$);
65 print "Final Size: $final\n";
7c40a30f 66
67 if ( $final > $initial ) {
0d9dfa69 68 print "Leaked: " . ($final - $initial) . "K\n";
7c40a30f 69 }
70
010c814d 71 is( $final, $initial, "'$uri' memory is not leaking" );
7c40a30f 72}
010c814d 73
9b984642 74sub size_of {
75 my $pid = shift;
76
77 foreach my $p ( @{ $t->table } ) {
78 if ( $p->pid == $pid ) {
fe796093 79 return $p->rss;
9b984642 80 }
81 }
82
83 die "Pid $pid not found?";
84}
85