mark some tests as requiring no preloads when run with yath
[catagits/Catalyst-Runtime.git] / t / optional_memleak.t
1 use strict;
2 use warnings;
3
4 use Test::More;
5 BEGIN {
6     plan skip_all => 'set TEST_MEMLEAK to enable this test'
7         unless $ENV{TEST_MEMLEAK};
8 }
9
10 use FindBin;
11 use lib "$FindBin::Bin/lib";
12 use Catalyst::Test 'TestApp';
13
14 eval "use Proc::ProcessTable";
15 plan skip_all => 'Proc::ProcessTable required for this test' if $@;
16
17 use JSON::MaybeXS qw(decode_json);
18
19 our $t = Proc::ProcessTable->new( cache_ttys => 1 );
20 our ( $initial, $final ) = ( 0, 0 );
21 my $test_data = do {
22   open my $fh, '<:raw', "$FindBin::Bin/optional_stress.json" or die "$!";
23   local $/;
24   <$fh>;
25 };
26
27 our $tests = decode_json($test_data);
28
29 my $total_tests = 0;
30
31 # let the user specify a single uri to test
32 my $user_test = shift;
33 if ( $user_test ) {
34     plan tests => 1;
35     run_test( $user_test );
36 }
37 # otherwise, run all tests
38 else {
39     map { $total_tests += scalar @{ $tests->{$_} } } keys %{$tests};
40     plan tests => $total_tests;
41
42     foreach my $test_group ( keys %{$tests} ) {
43         foreach my $test ( @{ $tests->{$test_group} } ) {
44             run_test( $test );
45         }
46     }
47 }
48
49 sub run_test {
50     my $uri = shift || die 'No URI given for test';
51
52     print "TESTING $uri\n";
53
54     # make a few requests to set initial memory size
55     for ( 1 .. 3 ) {
56         request( $uri );
57     }
58
59     $initial = size_of($$);
60     print "Initial Size: $initial\n";
61
62     for ( 1 .. 500 ) {
63         request( $uri );
64     }
65
66     $final = size_of($$);
67     print "Final Size:   $final\n";
68
69     if ( $final > $initial ) {
70         print "Leaked:       " . ($final - $initial) . "K\n";
71     }
72
73     is( $final, $initial, "'$uri' memory is not leaking" );
74 }
75
76 sub size_of {
77     my $pid = shift;
78
79     foreach my $p ( @{ $t->table } ) {
80         if ( $p->pid == $pid ) {
81             return $p->rss;
82         }
83     }
84
85     die "Pid $pid not found?";
86 }
87