File: 10_memoryleak.t

package info (click to toggle)
libdata-streamdeserializer-perl 0.06-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 728 kB
  • sloc: perl: 660; makefile: 3
file content (91 lines) | stat: -rw-r--r-- 2,158 bytes parent folder | download | duplicates (3)
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
#!/usr/bin/perl

use warnings;
use strict;
use utf8;
use open qw(:std :utf8);
use lib qw(lib ../lib);

use lib qw(blib/lib ../blib/lib blib/arch ../blib/arch);
use Test::More;
use Data::Dumper;
use Time::HiRes qw(time);
use Encode qw(decode encode);
use Sys::Hostname;

BEGIN {
    if ( $ENV{AUTOMATED_TESTING} ) {
        plan skip_all => 'Development test';
    }
    else {
        plan tests => 3;
    }

    # Подготовка объекта тестирования для работы с utf8
    my $builder = Test::More->builder;
    binmode $builder->output,         ":utf8";
    binmode $builder->failure_output, ":utf8";
    binmode $builder->todo_output,    ":utf8";
    $Data::Dumper::Indent = 1;
    $Data::Dumper::Terse = 1;
    $Data::Dumper::Useqq = 1;
    $Data::Dumper::Deepcopy = 1;

    use_ok 'Data::StreamDeserializer';
}


sub gen_rand_object() {
    my $h = {};
    for (0 .. 20) {
        for (0 .. 20) {
            $h->{rand()} = [ map { "aa\\n" . rand } 0 .. 10 ];
        }
    }

    $h;
}

my $size;
my $size_end;
my $counter = 0;
my $i = 0;
my $len = 0;
my $pcount = 0;

my @tests = map { Dumper gen_rand_object  } 0 .. 5;
for(;;)
{
    my $str = $tests[rand @tests];
    my $ds = new Data::StreamDeserializer data => $str;

    $i++ until $ds->next;
    $len+= length $str;
    $pcount++;

    if ($pcount < 30) {
        $size = Data::StreamDeserializer::_memory_size;
    } elsif ($pcount < 100) {
        $size_end = Data::StreamDeserializer::_memory_size;
        last if $size_end > $size;
    } else {
        last;
    }
}

my $leak = $size_end - $size;
ok $size_end <= $size, "Check memory leak ($leak bytes)";
note "$pcount/$i iterations/subiterations were done, $len bytes were parsed";

SKIP: {
    skip "This test is only for my system", 1
        if hostname !~ /^(apache|marish|nbw)$/;
    $size = $size_end;
    for (1 .. 20_000_000 + int rand 50_000_000) {
        push @tests, rand rand 1000;
        $size_end = Data::StreamDeserializer::_memory_size;
        last if $size_end != $size;
    }
    ok $size_end != $size,
        sprintf "Check memory checker (size: %d elements) :)", scalar @tests;
}