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