File: leaktest.t

package info (click to toggle)
libbson-xs-perl 0.8.4-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 1,712 kB
  • sloc: ansic: 9,734; perl: 1,046; makefile: 3
file content (143 lines) | stat: -rw-r--r-- 4,184 bytes parent folder | download
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
use 5.008001;
use strict;
use warnings;
use Test::More 0.96;
binmode( Test::More->builder->$_, ":utf8" )
  for qw/output failure_output todo_output/;

BEGIN {
    plan skip_all => "Test::LeakTrace not installed"
      unless eval { require Test::LeakTrace; 1 };
    Test::LeakTrace->import(qw/no_leaks_ok/);
}

use BSON;
use BSON::Types ':all';
use Tie::IxHash;

#--------------------------------------------------------------------------#
# Fixtures
#--------------------------------------------------------------------------#

# output caching hashes
my %encoded;

my %data = (
    string  => "abc",
    integer => 42,
    float   => 3.14,
    regex   => qr/abc/ixm,
    hash    => { foo => "bar" },
    array   => [qw/one two three/],
);

$data{stringref} = \$data{string};

my %generators = (
    bson_bool      => sub { bson_bool(1) },
    bson_bytes     => sub { bson_bytes("abc") },
    bson_code      => sub { bson_code("abc") },
    bson_codescope => sub { bson_code( "abc", { x => 1 } ) },
    bson_dbref      => sub { bson_dbref( bson_oid(), "test" ) },
    bson_decimal128 => sub { bson_decimal128("123e1234") },
    bson_doc    => sub { bson_doc( x => 1 ) },
    bson_double => sub { bson_double(3.14) },
    bson_int32  => sub { bson_int32(42) },
    bson_int64  => sub { bson_int64(23) },
    bson_maxkey => sub { bson_maxkey() },
    bson_minkey => sub { bson_minkey() },
    bson_oid    => sub { bson_oid() },
    bson_raw    => sub { bson_raw("\x05\x00\x00\x00\x00") },
    bson_regex     => sub { bson_regex( "abc",         "ixmu" ) },
    bson_string    => sub { bson_string("abc") },
    bson_time      => sub { bson_time() },
    bson_timestamp => sub { bson_timestamp( 123456789, 42 ) },
);

my %encoders = (
    default => BSON->new(),
    numeric => BSON->new( prefer_numeric => 1 ),
    ordered => BSON->new( ordered => 1 ),
);

my %decoders = (
    default => BSON->new(),
    wrapper => BSON->new( wrap_numbers => 1, wrap_strings => 1 ),
);

# XXX conditionally add decoders based on availability of dt_type support

#--------------------------------------------------------------------------#
# warm up
#--------------------------------------------------------------------------#

# Warm up generators so that Moo constructs all necessary subs, since those
# closures often count as "leaks".

for my $key ( sort keys %generators ) {
    $generators{$key}->();
}

#--------------------------------------------------------------------------#
# tests
#--------------------------------------------------------------------------#

subtest "Check wrappers for leaks" => sub {
    # Test for leaks
    for my $key ( sort keys %generators ) {
        no_leaks_ok {
            $data{$key} = $generators{$key}->();
        }
        $key;
    }
};

for my $codec ( sort keys %encoders ) {
    subtest "Check '$codec' encoding for leaks" => sub {
        # Test for leaks
        for my $key ( sort keys %data ) {
            my $case = "$key|$codec";
            # Cache encoded BSON doc from plain hashref case only
            no_leaks_ok {
                $encoded{$case} = $encoders{$codec}->encode_one( { a => $data{$key} } );
            }
            "$case (hashref)";
            no_leaks_ok {
                $encoded{$case} =
                  $encoders{$codec}->encode_one( Tie::IxHash->new( a => $data{$key} ) );
            }
            "$case (ixhash)";
            no_leaks_ok {
                $encoded{$case} =
                  $encoders{$codec}->encode_one( bson_doc( a => $data{$key} ) );
            }
            "$case (bsondoc)";
        }
    };
}

for my $codec ( sort keys %decoders ) {
    subtest "Check '$codec' decoding for leaks" => sub {
        # Test for leaks
        for my $key ( sort keys %encoded ) {
            my $case = "$key|$codec";
            no_leaks_ok {
                $decoders{$codec}->decode_one( $encoded{$key} );
            }
            $case;
        }
    };
}

done_testing;
#
# This file is part of BSON-XS
#
# This software is Copyright (c) 2020 by MongoDB, Inc.
#
# This is free software, licensed under:
#
#   The Apache License, Version 2.0, January 2004
#

# vim: set ts=4 sts=4 sw=4 et tw=75: