5 use Scalar::Util qw/refaddr reftype weaken/;
10 use DBICTest::RunMode;
14 require DBIx::Class::Optional::Dependencies;
15 $have_test_cycle = DBIx::Class::Optional::Dependencies->req_ok_for ('test_leaks')
16 and import Test::Memory::Cycle;
19 # preload stuff so that we don't trap globals
26 # this is what holds all weakened refs to be checked for leakage
27 my $weak_registry = {};
29 # Skip the heavy-duty leak tracing when just doing an install
30 unless (DBICTest::RunMode->is_plain) {
31 no warnings qw/redefine once/;
34 # override bless so that we can catch each and every object created
35 *CORE::GLOBAL::bless = sub {
36 my $obj = CORE::bless(
37 $_[0], (@_ > 1) ? $_[1] : do {
38 my ($class, $fn, $line) = caller();
39 fail ("bless() of $_[0] into $class without explicit class specification at $fn line $line")
40 if $class =~ /^ (?: DBIx\:\:Class | DBICTest ) /x;
45 my $slot = (sprintf '%s=%s(0x%x)', # so we don't trigger stringification
51 # weaken immediately to avoid weird side effects
52 $weak_registry->{$slot} = { weakref => $obj, strace => longmess() };
53 weaken $weak_registry->{$slot}{weakref};
58 for my $func (qw/try catch finally/) {
59 my $orig = \&{"Try::Tiny::$func"};
60 *{"Try::Tiny::$func"} = sub (&;@) {
62 my $slot = sprintf ('CODE(0x%x)', refaddr $_[0]);
64 $weak_registry->{$slot} = { weakref => $_[0], strace => longmess() };
65 weaken $weak_registry->{$slot}{weakref};
75 my $schema = DBICTest->init_schema;
76 my $rs = $schema->resultset ('Artist');
77 my $storage = $schema->storage;
79 ok ($storage->connected, 'we are connected');
81 my $row_obj = $rs->next;
82 ok ($row_obj, 'row from db');
84 my ($mc_row_obj, $pager, $pager_explicit_count) = $schema->txn_do (sub {
86 my $artist = $rs->create ({
94 my $pg = $rs->search({}, { rows => 1})->page(2)->pager;
96 my $pg_wcount = $rs->page(4)->pager->total_entries (66);
98 return ($artist, $pg, $pg_wcount);
101 is ($pager->next_page, 3, 'There is one more page available');
103 # based on 66 per 10 pages
104 is ($pager_explicit_count->last_page, 7, 'Correct last page');
106 my $base_collection = {
111 row_object => $row_obj,
113 result_source => $rs->result_source,
115 fresh_pager => $rs->page(5)->pager,
117 pager_explicit_count => $pager_explicit_count,
119 sql_maker => $storage->sql_maker,
120 dbh => $storage->_dbh
123 memory_cycle_ok ($base_collection, 'No cycles in the object collection')
126 for (keys %$base_collection) {
127 $weak_registry->{"basic $_"} = { weakref => $base_collection->{$_} };
128 weaken $weak_registry->{"basic $_"}{weakref};
133 memory_cycle_ok($weak_registry, 'No cycles in the weakened object collection')
137 # For reasons I can not yet fully understand the table() god-method (located in
138 # ::ResultSourceProxy::Table) attaches an actual source instance to each class
139 # as virtually *immortal* class-data.
140 # For now just blow away these instances manually but there got to be a saner way
141 $_->result_source_instance(undef) for (
142 'DBICTest::BaseResult',
143 map { DBICTest::Schema->class ($_) } DBICTest::Schema->sources
147 # same problem goes for the schema - its classdata contains live result source
148 # objects, which to add insult to the injury are *different* instances from the
149 # ones we destroyed above
150 DBICTest::Schema->source_registrations(undef);
152 my $tb = Test::More->builder;
153 for my $slot (keys %$weak_registry) {
154 # SQLT is a piece of shit, leaks all over
155 next if $slot =~ /^SQL\:\:Translator/;
157 ok (! defined $weak_registry->{$slot}{weakref}, "No leaks of $slot") or do {
160 $diag .= Devel::FindRef::track ($weak_registry->{$slot}{weakref}, 20) . "\n"
161 if ( $ENV{TEST_VERBOSE} && try { require Devel::FindRef });
163 if (my $stack = $weak_registry->{$slot}{strace}) {
164 $diag .= " Reference first seen$stack";