Add Hash::Merge exception to the leak test
[dbsrgits/DBIx-Class.git] / t / 52leaks.t
CommitLineData
a917fb06 1use strict;
2use warnings;
226d1c35 3
f05edfd1 4# Do the override as early as possible so that CORE::bless doesn't get compiled away
5# We will replace $bless_override only if we are in author mode
6my $bless_override;
7BEGIN {
8 $bless_override = sub {
9 CORE::bless( $_[0], (@_ > 1) ? $_[1] : caller() );
10 };
11 *CORE::GLOBAL::bless = sub { goto $bless_override };
12}
13
a917fb06 14use Test::More;
a8c2c746 15use Scalar::Util qw/refaddr reftype weaken/;
16use Carp qw/longmess/;
17use Try::Tiny;
a917fb06 18
19use lib qw(t/lib);
a8c2c746 20use DBICTest::RunMode;
a917fb06 21
a8c2c746 22my $have_test_cycle;
a917fb06 23BEGIN {
226d1c35 24 require DBIx::Class::Optional::Dependencies;
a8c2c746 25 $have_test_cycle = DBIx::Class::Optional::Dependencies->req_ok_for ('test_leaks')
26 and import Test::Memory::Cycle;
a917fb06 27}
28
a8c2c746 29# this is what holds all weakened refs to be checked for leakage
30my $weak_registry = {};
31
32# Skip the heavy-duty leak tracing when just doing an install
33unless (DBICTest::RunMode->is_plain) {
f05edfd1 34
35 # Some modules are known to install singletons on-load
36 # Load them before we swap out $bless_override
37 require DBI;
38 require DBD::SQLite;
39 require Errno;
40 require Class::Struct;
41 require FileHandle;
307ab4c5 42 require Hash::Merge;
f05edfd1 43
a8c2c746 44 no warnings qw/redefine once/;
45 no strict qw/refs/;
46
f05edfd1 47 # redefine the bless override so that we can catch each and every object created
48 $bless_override = sub {
49
a8c2c746 50 my $obj = CORE::bless(
51 $_[0], (@_ > 1) ? $_[1] : do {
52 my ($class, $fn, $line) = caller();
53 fail ("bless() of $_[0] into $class without explicit class specification at $fn line $line")
54 if $class =~ /^ (?: DBIx\:\:Class | DBICTest ) /x;
55 $class;
56 }
57 );
58
59 my $slot = (sprintf '%s=%s(0x%x)', # so we don't trigger stringification
60 ref $obj,
61 reftype $obj,
62 refaddr $obj,
63 );
64
65 # weaken immediately to avoid weird side effects
66 $weak_registry->{$slot} = { weakref => $obj, strace => longmess() };
67 weaken $weak_registry->{$slot}{weakref};
68
69 return $obj;
70 };
71
72 for my $func (qw/try catch finally/) {
73 my $orig = \&{"Try::Tiny::$func"};
74 *{"Try::Tiny::$func"} = sub (&;@) {
75
76 my $slot = sprintf ('CODE(0x%x)', refaddr $_[0]);
77
78 $weak_registry->{$slot} = { weakref => $_[0], strace => longmess() };
79 weaken $weak_registry->{$slot}{weakref};
80
81 goto $orig;
82 }
83 }
84}
85
86{
87 require DBICTest;
a917fb06 88
a8c2c746 89 my $schema = DBICTest->init_schema;
90 my $rs = $schema->resultset ('Artist');
91 my $storage = $schema->storage;
a917fb06 92
a8c2c746 93 ok ($storage->connected, 'we are connected');
a917fb06 94
a8c2c746 95 my $row_obj = $rs->next;
96 ok ($row_obj, 'row from db');
97
98 my ($mc_row_obj, $pager, $pager_explicit_count) = $schema->txn_do (sub {
99
100 my $artist = $rs->create ({
101 name => 'foo artist',
102 cds => [{
103 title => 'foo cd',
104 year => 1984,
105 }],
106 });
107
108 my $pg = $rs->search({}, { rows => 1})->page(2)->pager;
109
110 my $pg_wcount = $rs->page(4)->pager->total_entries (66);
111
112 return ($artist, $pg, $pg_wcount);
113 });
114
115 is ($pager->next_page, 3, 'There is one more page available');
116
117 # based on 66 per 10 pages
118 is ($pager_explicit_count->last_page, 7, 'Correct last page');
551e711a 119
a8c2c746 120 my $base_collection = {
121 schema => $schema,
122 storage => $storage,
dc35bbe6 123
a8c2c746 124 resultset => $rs,
307ab4c5 125
a8c2c746 126 row_object => $row_obj,
551e711a 127
a8c2c746 128 result_source => $rs->result_source,
551e711a 129
a8c2c746 130 fresh_pager => $rs->page(5)->pager,
131 pager => $pager,
132 pager_explicit_count => $pager_explicit_count,
551e711a 133
a8c2c746 134 sql_maker => $storage->sql_maker,
135 dbh => $storage->_dbh
136 };
574d9b69 137
a8c2c746 138 memory_cycle_ok ($base_collection, 'No cycles in the object collection')
139 if $have_test_cycle;
574d9b69 140
a8c2c746 141 for (keys %$base_collection) {
142 $weak_registry->{"basic $_"} = { weakref => $base_collection->{$_} };
143 weaken $weak_registry->{"basic $_"}{weakref};
574d9b69 144 }
145
551e711a 146}
147
a8c2c746 148memory_cycle_ok($weak_registry, 'No cycles in the weakened object collection')
149 if $have_test_cycle;
150
307ab4c5 151# Naturally we have some exceptions
152my $cleared;
153for my $slot (keys %$weak_registry) {
154 if ($slot =~ /^SQL\:\:Translator/) {
155 # SQLT is a piece of shit, leaks all over
156 delete $weak_registry->{$slot};
157 }
158 elsif ($slot =~ /^Hash\:\:Merge/) {
159 # only clear one object - more would indicate trouble
160 delete $weak_registry->{$slot}
161 unless $cleared->{hash_merge_singleton}{$weak_registry->{$slot}{weakref}{behavior}}++;
162 }
163}
164
165
a8c2c746 166# FIXME
167# For reasons I can not yet fully understand the table() god-method (located in
168# ::ResultSourceProxy::Table) attaches an actual source instance to each class
169# as virtually *immortal* class-data.
170# For now just blow away these instances manually but there got to be a saner way
171$_->result_source_instance(undef) for (
172 'DBICTest::BaseResult',
173 map { DBICTest::Schema->class ($_) } DBICTest::Schema->sources
174);
175
176# FIXME
177# same problem goes for the schema - its classdata contains live result source
178# objects, which to add insult to the injury are *different* instances from the
179# ones we destroyed above
180DBICTest::Schema->source_registrations(undef);
181
182my $tb = Test::More->builder;
183for my $slot (keys %$weak_registry) {
a8c2c746 184
185 ok (! defined $weak_registry->{$slot}{weakref}, "No leaks of $slot") or do {
186 my $diag = '';
187
188 $diag .= Devel::FindRef::track ($weak_registry->{$slot}{weakref}, 20) . "\n"
189 if ( $ENV{TEST_VERBOSE} && try { require Devel::FindRef });
190
191 if (my $stack = $weak_registry->{$slot}{strace}) {
192 $diag .= " Reference first seen$stack";
193 }
194
195 diag $diag if $diag;
196 };
551e711a 197}
198
199done_testing;