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
8 $bless_override = sub {
9 CORE::bless( $_[0], (@_ > 1) ? $_[1] : caller() );
11 *CORE::GLOBAL::bless = sub { goto $bless_override };
17 use DBICTest::RunMode;
19 plan skip_all => "Your perl version $] appears to leak like a sieve - skipping test"
20 if DBICTest::RunMode->peepeeness;
23 use Scalar::Util qw/refaddr reftype weaken/;
24 use Carp qw/longmess/;
29 require DBIx::Class::Optional::Dependencies;
30 $have_test_cycle = DBIx::Class::Optional::Dependencies->req_ok_for ('test_leaks')
31 and import Test::Memory::Cycle;
34 # this is what holds all weakened refs to be checked for leakage
35 my $weak_registry = {};
37 # Skip the heavy-duty leak tracing when just doing an install
38 unless (DBICTest::RunMode->is_plain) {
40 # Some modules are known to install singletons on-load
41 # Load them before we swap out $bless_override
45 require Class::Struct;
49 no warnings qw/redefine once/;
52 # redefine the bless override so that we can catch each and every object created
53 $bless_override = sub {
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;
64 my $slot = (sprintf '%s=%s(0x%x)', # so we don't trigger stringification
70 # weaken immediately to avoid weird side effects
71 $weak_registry->{$slot} = { weakref => $obj, strace => longmess() };
72 weaken $weak_registry->{$slot}{weakref};
77 for my $func (qw/try catch finally/) {
78 my $orig = \&{"Try::Tiny::$func"};
79 *{"Try::Tiny::$func"} = sub (&;@) {
81 my $slot = sprintf ('CODE(0x%x)', refaddr $_[0]);
83 $weak_registry->{$slot} = { weakref => $_[0], strace => longmess() };
84 weaken $weak_registry->{$slot}{weakref};
94 my $schema = DBICTest->init_schema;
95 my $rs = $schema->resultset ('Artist');
96 my $storage = $schema->storage;
98 ok ($storage->connected, 'we are connected');
100 my $row_obj = $rs->search({}, { rows => 1})->next; # so that commits/rollbacks work
101 ok ($row_obj, 'row from db');
103 # txn_do to invoke more codepaths
104 my ($mc_row_obj, $pager, $pager_explicit_count) = $schema->txn_do (sub {
106 my $artist = $rs->create ({
107 name => 'foo artist',
114 my $pg = $rs->search({}, { rows => 1})->page(2)->pager;
116 my $pg_wcount = $rs->page(4)->pager->total_entries (66);
118 return ($artist, $pg, $pg_wcount);
121 is ($pager->next_page, 3, 'There is one more page available');
123 # based on 66 per 10 pages
124 is ($pager_explicit_count->last_page, 7, 'Correct last page');
126 # do some population (invokes some extra codepaths)
127 # also exercise the guard code and the manual txn control
129 my $guard = $schema->txn_scope_guard;
130 # populate with bindvars
131 $rs->populate([{ name => 'James Bound' }]);
136 $rs->populate([{ name => 'James Rebound', rank => \ '11' }]);
140 # and without bindvars
141 $rs->populate([{ name => \ '"James Unbound"' }]);
142 $schema->txn_rollback;
145 my $base_collection = {
151 # twice so that we make sure only one H::M object spawned
152 chained_resultset => $rs->search_rs ({}, { '+columns' => [ 'foo' ] } ),
153 chained_resultset2 => $rs->search_rs ({}, { '+columns' => [ 'bar' ] } ),
155 row_object => $row_obj,
157 result_source => $rs->result_source,
159 fresh_pager => $rs->page(5)->pager,
161 pager_explicit_count => $pager_explicit_count,
163 sql_maker => $storage->sql_maker,
164 dbh => $storage->_dbh
167 memory_cycle_ok ($base_collection, 'No cycles in the object collection')
170 for (keys %$base_collection) {
171 $weak_registry->{"basic $_"} = { weakref => $base_collection->{$_} };
172 weaken $weak_registry->{"basic $_"}{weakref};
177 memory_cycle_ok($weak_registry, 'No cycles in the weakened object collection')
180 # Naturally we have some exceptions
182 for my $slot (keys %$weak_registry) {
183 if ($slot =~ /^\QTest::Builder/) {
184 # T::B 2.0 has result objects and other fancyness
185 delete $weak_registry->{$slot};
187 elsif ($slot =~ /^\QSQL::Translator/) {
188 # SQLT is a piece of shit, leaks all over
189 delete $weak_registry->{$slot};
191 elsif ($slot =~ /^\QHash::Merge/) {
192 # only clear one object of a specific behavior - more would indicate trouble
193 delete $weak_registry->{$slot}
194 unless $cleared->{hash_merge_singleton}{$weak_registry->{$slot}{weakref}{behavior}}++;
196 elsif ($slot =~ /^__TxnScopeGuard__FIXUP__/) {
197 die 'The $@ debacle should have been fixed by now!!!' if $] >= 5.013008;
198 delete $weak_registry->{$slot};
204 # For reasons I can not yet fully understand the table() god-method (located in
205 # ::ResultSourceProxy::Table) attaches an actual source instance to each class
206 # as virtually *immortal* class-data.
207 # For now just blow away these instances manually but there got to be a saner way
208 $_->result_source_instance(undef) for (
209 'DBICTest::BaseResult',
210 map { DBICTest::Schema->class ($_) } DBICTest::Schema->sources
214 # same problem goes for the schema - its classdata contains live result source
215 # objects, which to add insult to the injury are *different* instances from the
216 # ones we destroyed above
217 DBICTest::Schema->source_registrations(undef);
219 my $tb = Test::More->builder;
220 for my $slot (keys %$weak_registry) {
222 ok (! defined $weak_registry->{$slot}{weakref}, "No leaks of $slot") or do {
225 $diag .= Devel::FindRef::track ($weak_registry->{$slot}{weakref}, 20) . "\n"
226 if ( $ENV{TEST_VERBOSE} && try { require Devel::FindRef });
228 if (my $stack = $weak_registry->{$slot}{strace}) {
229 $diag .= " Reference first seen$stack";