File: optional_memleak.t

package info (click to toggle)
libcatalyst-perl 5.90132-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,016 kB
  • sloc: perl: 11,061; makefile: 7
file content (87 lines) | stat: -rw-r--r-- 1,797 bytes parent folder | download | duplicates (4)
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
use strict;
use warnings;

use Test::More;
BEGIN {
    plan skip_all => 'set TEST_MEMLEAK to enable this test'
        unless $ENV{TEST_MEMLEAK};
}

use FindBin;
use lib "$FindBin::Bin/lib";
use Catalyst::Test 'TestApp';

eval "use Proc::ProcessTable";
plan skip_all => 'Proc::ProcessTable required for this test' if $@;

use JSON::MaybeXS qw(decode_json);

our $t = Proc::ProcessTable->new( cache_ttys => 1 );
our ( $initial, $final ) = ( 0, 0 );
my $test_data = do {
  open my $fh, '<:raw', "$FindBin::Bin/optional_stress.json" or die "$!";
  local $/;
  <$fh>;
};

our $tests = decode_json($test_data);

my $total_tests = 0;

# let the user specify a single uri to test
my $user_test = shift;
if ( $user_test ) {
    plan tests => 1;
    run_test( $user_test );
}
# otherwise, run all tests
else {
    map { $total_tests += scalar @{ $tests->{$_} } } keys %{$tests};
    plan tests => $total_tests;

    foreach my $test_group ( keys %{$tests} ) {
        foreach my $test ( @{ $tests->{$test_group} } ) {
            run_test( $test );
        }
    }
}

sub run_test {
    my $uri = shift || die 'No URI given for test';

    print "TESTING $uri\n";

    # make a few requests to set initial memory size
    for ( 1 .. 3 ) {
        request( $uri );
    }

    $initial = size_of($$);
    print "Initial Size: $initial\n";

    for ( 1 .. 500 ) {
        request( $uri );
    }

    $final = size_of($$);
    print "Final Size:   $final\n";

    if ( $final > $initial ) {
        print "Leaked:       " . ($final - $initial) . "K\n";
    }

    is( $final, $initial, "'$uri' memory is not leaking" );
}

sub size_of {
    my $pid = shift;

    foreach my $p ( @{ $t->table } ) {
        if ( $p->pid == $pid ) {
            return $p->rss;
        }
    }

    die "Pid $pid not found?";
}