1 # Do the override as early as possible so that CORE::bless doesn't get compiled away
2 # We will replace $bless_override only if we are in author mode
5 $bless_override = sub {
6 CORE::bless( $_[0], (@_ > 1) ? $_[1] : caller() );
8 *CORE::GLOBAL::bless = sub { goto $bless_override };
16 use DBICTest::RunMode;
18 plan skip_all => "Your perl version $] appears to leak like a sieve - skipping test"
19 if DBICTest::RunMode->peepeeness;
22 use Scalar::Util qw/refaddr reftype weaken/;
23 use Carp qw/longmess/;
28 require DBIx::Class::Optional::Dependencies;
29 $have_test_cycle = DBIx::Class::Optional::Dependencies->req_ok_for ('test_leaks')
30 and import Test::Memory::Cycle;
33 # this is what holds all weakened refs to be checked for leakage
34 my $weak_registry = {};
36 # Skip the heavy-duty leak tracing when just doing an install
37 unless (DBICTest::RunMode->is_plain) {
39 # Some modules are known to install singletons on-load
40 # Load them before we swap out $bless_override
44 require Class::Struct;
48 no warnings qw/redefine once/;
51 # redefine the bless override so that we can catch each and every object created
52 $bless_override = sub {
54 my $obj = CORE::bless(
55 $_[0], (@_ > 1) ? $_[1] : do {
56 my ($class, $fn, $line) = caller();
57 fail ("bless() of $_[0] into $class without explicit class specification at $fn line $line")
58 if $class =~ /^ (?: DBIx\:\:Class | DBICTest ) /x;
63 my $slot = (sprintf '%s=%s(0x%x)', # so we don't trigger stringification
69 # weaken immediately to avoid weird side effects
70 $weak_registry->{$slot} = { weakref => $obj, strace => longmess() };
71 weaken $weak_registry->{$slot}{weakref};
76 for my $func (qw/try catch finally/) {
77 my $orig = \&{"Try::Tiny::$func"};
78 *{"Try::Tiny::$func"} = sub (&;@) {
80 my $slot = sprintf ('CODE(0x%x)', refaddr $_[0]);
82 $weak_registry->{$slot} = { weakref => $_[0], strace => longmess() };
83 weaken $weak_registry->{$slot}{weakref};
93 my $schema = DBICTest->init_schema;
94 my $rs = $schema->resultset ('Artist');
95 my $storage = $schema->storage;
97 ok ($storage->connected, 'we are connected');
99 my $row_obj = $rs->search({}, { rows => 1})->next; # so that commits/rollbacks work
100 ok ($row_obj, 'row from db');
102 # txn_do to invoke more codepaths
103 my ($mc_row_obj, $pager, $pager_explicit_count) = $schema->txn_do (sub {
105 my $artist = $rs->create ({
106 name => 'foo artist',
113 my $pg = $rs->search({}, { rows => 1})->page(2)->pager;
115 my $pg_wcount = $rs->page(4)->pager->total_entries (66);
117 return ($artist, $pg, $pg_wcount);
120 is ($pager->next_page, 3, 'There is one more page available');
122 # based on 66 per 10 pages
123 is ($pager_explicit_count->last_page, 7, 'Correct last page');
125 # do some population (invokes some extra codepaths)
126 # also exercise the guard code and the manual txn control
128 my $guard = $schema->txn_scope_guard;
129 # populate with bindvars
130 $rs->populate([{ name => 'James Bound' }]);
135 $rs->populate([{ name => 'James Rebound', rank => \ '11' }]);
139 # and without bindvars
140 $rs->populate([{ name => \ '"James Unbound"' }]);
141 $schema->txn_rollback;
144 my $base_collection = {
150 # twice so that we make sure only one H::M object spawned
151 chained_resultset => $rs->search_rs ({}, { '+columns' => [ 'foo' ] } ),
152 chained_resultset2 => $rs->search_rs ({}, { '+columns' => [ 'bar' ] } ),
154 row_object => $row_obj,
156 result_source => $rs->result_source,
158 fresh_pager => $rs->page(5)->pager,
160 pager_explicit_count => $pager_explicit_count,
162 sql_maker => $storage->sql_maker,
163 dbh => $storage->_dbh
166 memory_cycle_ok ($base_collection, 'No cycles in the object collection')
169 for (keys %$base_collection) {
170 $weak_registry->{"basic $_"} = { weakref => $base_collection->{$_} };
171 weaken $weak_registry->{"basic $_"}{weakref};
175 # check that "phantom-chaining" works - we never lose track of the original $schema
176 # and have access to the entire tree without leaking anything
180 sub { DBICTest->init_schema },
181 sub { shift->source('Artist') },
182 sub { shift->resultset },
183 sub { shift->result_source },
184 sub { shift->schema },
185 sub { shift->resultset('Artist') },
186 sub { shift->find_or_create({ name => 'detachable' }) },
187 sub { shift->result_source },
188 sub { shift->schema },
189 sub { shift->clone },
190 sub { shift->resultset('Artist') },
192 sub { shift->result_source },
193 sub { shift->resultset },
194 sub { shift->create({ name => 'detached' }) },
195 sub { shift->update({ name => 'reattached' }) },
196 sub { shift->discard_changes },
197 sub { shift->delete },
198 sub { shift->insert },
200 $phantom = $_->($phantom);
202 my $slot = (sprintf 'phantom %s=%s(0x%x)', # so we don't trigger stringification
207 $weak_registry->{$slot} = $phantom;
208 weaken $weak_registry->{$slot};
211 ok( $phantom->in_storage, 'Properly deleted/reinserted' );
212 is( $phantom->name, 'reattached', 'Still correct name' );
215 # Naturally we have some exceptions
217 for my $slot (keys %$weak_registry) {
218 if ($slot =~ /^\QTest::Builder/) {
219 # T::B 2.0 has result objects and other fancyness
220 delete $weak_registry->{$slot};
222 elsif ($slot =~ /^\QSQL::Translator/) {
223 # SQLT is a piece of shit, leaks all over
224 delete $weak_registry->{$slot};
226 elsif ($slot =~ /^\QHash::Merge/) {
227 # only clear one object of a specific behavior - more would indicate trouble
228 delete $weak_registry->{$slot}
229 unless $cleared->{hash_merge_singleton}{$weak_registry->{$slot}{weakref}{behavior}}++;
231 elsif ($slot =~ /^__TxnScopeGuard__FIXUP__/) {
232 die 'The $@ debacle should have been fixed by now!!!' if $] >= 5.013008;
233 delete $weak_registry->{$slot};
239 # For reasons I can not yet fully understand the table() god-method (located in
240 # ::ResultSourceProxy::Table) attaches an actual source instance to each class
241 # as virtually *immortal* class-data.
242 # For now just blow away these instances manually but there got to be a saner way
243 $_->result_source_instance(undef) for (
244 'DBICTest::BaseResult',
245 map { DBICTest::Schema->class ($_) } DBICTest::Schema->sources
249 # same problem goes for the schema - its classdata contains live result source
250 # objects, which to add insult to the injury are *different* instances from the
251 # ones we destroyed above
252 DBICTest::Schema->source_registrations(undef);
254 my $tb = Test::More->builder;
255 for my $slot (keys %$weak_registry) {
257 ok (! defined $weak_registry->{$slot}{weakref}, "No leaks of $slot") or do {
260 $diag .= Devel::FindRef::track ($weak_registry->{$slot}{weakref}, 20) . "\n"
261 if ( $ENV{TEST_VERBOSE} && try { require Devel::FindRef });
263 if (my $stack = $weak_registry->{$slot}{strace}) {
264 $diag .= " Reference first seen$stack";