Back out constructor/prefetch rewrite introduced mainly by 43245ada4a
[dbsrgits/DBIx-Class.git] / t / 52leaks.t
1 # work around brain damage in PPerl (yes, it has to be a global)
2 $SIG{__WARN__} = sub {
3   warn @_ unless $_[0] =~ /\QUse of "goto" to jump into a construct is deprecated/
4 } if ($ENV{DBICTEST_IN_PERSISTENT_ENV});
5
6 # the persistent environments run with this flag first to see if
7 # we will run at all (e.g. it will fail if $^X doesn't match)
8 exit 0 if $ENV{DBICTEST_PERSISTENT_ENV_BAIL_EARLY};
9
10 # Do the override as early as possible so that CORE::bless doesn't get compiled away
11 # We will replace $bless_override only if we are in author mode
12 my $bless_override;
13 BEGIN {
14   $bless_override = sub {
15     CORE::bless( $_[0], (@_ > 1) ? $_[1] : caller() );
16   };
17   *CORE::GLOBAL::bless = sub { goto $bless_override };
18 }
19
20 use strict;
21 use warnings;
22 use Test::More;
23
24 my $TB = Test::More->builder;
25 if ($ENV{DBICTEST_IN_PERSISTENT_ENV}) {
26   # without this explicit close older TBs warn in END after a ->reset
27   if ($TB->VERSION < 1.005) {
28     close ($TB->$_) for (qw/output failure_output todo_output/);
29   }
30
31   # if I do not do this, I get happy sigpipes on new TB, no idea why
32   # (the above close-and-forget doesn't work - new TB does *not* reopen
33   # its handles automatically anymore)
34   else {
35     for (qw/failure_output todo_output/) {
36       close $TB->$_;
37       open ($TB->$_, '>&', *STDERR);
38     }
39
40     close $TB->output;
41     open ($TB->output, '>&', *STDOUT);
42   }
43
44   # so done_testing can work on every persistent pass
45   $TB->reset;
46 }
47
48 use lib qw(t/lib);
49 use DBICTest::RunMode;
50 use DBICTest::Util qw/populate_weakregistry assert_empty_weakregistry/;
51 use DBIx::Class;
52 use B 'svref_2object';
53 BEGIN {
54   plan skip_all => "Your perl version $] appears to leak like a sieve - skipping test"
55     if DBIx::Class::_ENV_::PEEPEENESS;
56 }
57
58 # this is what holds all weakened refs to be checked for leakage
59 my $weak_registry = {};
60
61 # whether or to invoke IC::DT
62 my $has_dt;
63
64 # Skip the heavy-duty leak tracing when just doing an install
65 unless (DBICTest::RunMode->is_plain) {
66
67   # redefine the bless override so that we can catch each and every object created
68   no warnings qw/redefine once/;
69   no strict qw/refs/;
70
71   $bless_override = sub {
72
73     my $obj = CORE::bless(
74       $_[0], (@_ > 1) ? $_[1] : do {
75         my ($class, $fn, $line) = caller();
76         fail ("bless() of $_[0] into $class without explicit class specification at $fn line $line")
77           if $class =~ /^ (?: DBIx\:\:Class | DBICTest ) /x;
78         $class;
79       }
80     );
81
82     # unicode is tricky, and now we happen to invoke it early via a
83     # regex in connection()
84     return $obj if (ref $obj) =~ /^utf8/;
85
86     # Test Builder is now making a new object for every pass/fail (que bloat?)
87     # and as such we can't really store any of its objects (since it will
88     # re-populate the registry while checking it, ewwww!)
89     return $obj if (ref $obj) =~ /^TB2::/;
90
91     # weaken immediately to avoid weird side effects
92     return populate_weakregistry ($weak_registry, $obj );
93   };
94
95   require Try::Tiny;
96   for my $func (qw/try catch finally/) {
97     my $orig = \&{"Try::Tiny::$func"};
98     *{"Try::Tiny::$func"} = sub (&;@) {
99       populate_weakregistry( $weak_registry, $_[0] );
100       goto $orig;
101     }
102   }
103
104   # Some modules are known to install singletons on-load
105   # Load them and empty the registry
106
107   # this loads the DT armada
108   $has_dt = DBIx::Class::Optional::Dependencies->req_ok_for('test_dt_sqlite');
109
110   require Errno;
111   require DBI;
112   require DBD::SQLite;
113   require FileHandle;
114   require Moo;
115
116   %$weak_registry = ();
117 }
118
119 my @compose_ns_classes;
120 {
121   use_ok ('DBICTest');
122
123   my $schema = DBICTest->init_schema;
124   my $rs = $schema->resultset ('Artist');
125   my $storage = $schema->storage;
126
127   @compose_ns_classes = map { "DBICTest::${_}" } keys %{$schema->source_registrations};
128
129   ok ($storage->connected, 'we are connected');
130
131   my $row_obj = $rs->search({}, { rows => 1})->next;  # so that commits/rollbacks work
132   ok ($row_obj, 'row from db');
133
134   # txn_do to invoke more codepaths
135   my ($mc_row_obj, $pager, $pager_explicit_count) = $schema->txn_do (sub {
136
137     my $artist = $schema->resultset('Artist')->create ({
138       name => 'foo artist',
139       cds => [{
140         title => 'foo cd',
141         year => 1984,
142         tracks => [
143           { title => 't1' },
144           { title => 't2' },
145         ],
146         genre => { name => 'mauve' },
147       }],
148     });
149
150     my $pg = $rs->search({}, { rows => 1})->page(2)->pager;
151
152     my $pg_wcount = $rs->page(4)->pager->total_entries (66);
153
154     return ($artist, $pg, $pg_wcount);
155   });
156
157   # more codepaths - error handling in txn_do
158   {
159     eval { $schema->txn_do ( sub {
160       $storage->_dbh->begin_work;
161       fail ('how did we get so far?!');
162     } ) };
163
164     eval { $schema->txn_do ( sub {
165       $schema->txn_do ( sub {
166         die "It's called EXCEPTION";
167         fail ('how did we get so far?!');
168       } );
169       fail ('how did we get so far?!');
170     } ) };
171     like( $@, qr/It\'s called EXCEPTION/, 'Exception correctly propagated in nested txn_do' );
172   }
173
174   # dbh_do codepath
175   my ($rs_bind_circref, $cond_rowobj) = $schema->storage->dbh_do ( sub {
176     my $row = $_[0]->schema->resultset('Artist')->new({});
177     my $rs = $_[0]->schema->resultset('Artist')->search({
178       name => $row,  # this is deliberately bogus, see FIXME below!
179     });
180     return ($rs, $row);
181   });
182
183   is ($pager->next_page, 3, 'There is one more page available');
184
185   # based on 66 per 10 pages
186   is ($pager_explicit_count->last_page, 7, 'Correct last page');
187
188   # do some population (invokes some extra codepaths)
189   # also exercise the guard code and the manual txn control
190   {
191     my $guard = $schema->txn_scope_guard;
192     # populate with bindvars
193     $rs->populate([{ name => 'James Bound' }]);
194     $guard->commit;
195
196     $schema->txn_begin;
197     # populate mixed
198     $rs->populate([{ name => 'James Rebound', rank => \ '11'  }]);
199     $schema->txn_commit;
200
201     $schema->txn_begin;
202     # and without bindvars
203     $rs->populate([{ name => \ '"James Unbound"' }]);
204     $schema->txn_rollback;
205   }
206
207   # prefetching
208   my $cds_rs = $schema->resultset('CD');
209   my $cds_with_artist = $cds_rs->search({}, { prefetch => 'artist' });
210   my $cds_with_tracks = $cds_rs->search({}, { prefetch => 'tracks' });
211   my $cds_with_stuff = $cds_rs->search({}, { prefetch => [ 'genre', { artist => { cds => { tracks => 'cd_single' } } } ] });
212
213   # implicit pref
214   my $cds_with_impl_artist = $cds_rs->search({}, { columns => [qw/me.title artist.name/], join => 'artist' });
215
216   # get_column
217   my $getcol_rs = $cds_rs->get_column('me.cdid');
218   my $pref_getcol_rs = $cds_with_stuff->get_column('me.cdid');
219
220   # fire the column getters
221   my @throwaway = $pref_getcol_rs->all;
222
223   my $base_collection = {
224     resultset => $rs,
225
226     pref_precursor => $cds_rs,
227
228     pref_rs_single => $cds_with_artist,
229     pref_rs_multi => $cds_with_tracks,
230     pref_rs_nested => $cds_with_stuff,
231
232     pref_rs_implicit => $cds_with_impl_artist,
233
234     pref_row_single => $cds_with_artist->next,
235     pref_row_multi => $cds_with_tracks->next,
236     pref_row_nested => $cds_with_stuff->next,
237
238     # even though this does not leak Storable croaks on it :(((
239     #pref_row_implicit => $cds_with_impl_artist->next,
240
241     get_column_rs_plain => $getcol_rs,
242     get_column_rs_pref => $pref_getcol_rs,
243
244     # twice so that we make sure only one H::M object spawned
245     chained_resultset => $rs->search_rs ({}, { '+columns' => [ 'foo' ] } ),
246     chained_resultset2 => $rs->search_rs ({}, { '+columns' => [ 'bar' ] } ),
247
248     row_object => $row_obj,
249
250     mc_row_object => $mc_row_obj,
251
252     result_source => $rs->result_source,
253
254     result_source_handle => $rs->result_source->handle,
255
256     pager_explicit_count => $pager_explicit_count,
257
258     leaky_resultset => $rs_bind_circref,
259     leaky_resultset_cond => $cond_rowobj,
260     leaky_resultset_member => $rs_bind_circref->next,
261   };
262
263   require Storable;
264   %$base_collection = (
265     %$base_collection,
266     refrozen => Storable::dclone( $base_collection ),
267     rerefrozen => Storable::dclone( Storable::dclone( $base_collection ) ),
268     pref_row_implicit => $cds_with_impl_artist->next,
269     schema => $schema,
270     storage => $storage,
271     sql_maker => $storage->sql_maker,
272     dbh => $storage->_dbh,
273     fresh_pager => $rs->page(5)->pager,
274     pager => $pager,
275   );
276
277   if ($has_dt) {
278     my $rs = $base_collection->{icdt_rs} = $schema->resultset('Event');
279
280     my $now = DateTime->now;
281     for (1..5) {
282       $base_collection->{"icdt_row_$_"} = $rs->create({
283         created_on => DateTime->new(year => 2011, month => 1, day => $_, time_zone => "-0${_}00" ),
284         starts_at => $now->clone->add(days => $_),
285       });
286     }
287
288     # re-search
289     my @dummy = $rs->all;
290   }
291
292   # dbh's are created in XS space, so pull them separately
293   for ( grep { defined } map { @{$_->{ChildHandles}} } values %{ {DBI->installed_drivers()} } ) {
294     $base_collection->{"DBI handle $_"} = $_;
295   }
296
297   SKIP: {
298     if ( DBIx::Class::Optional::Dependencies->req_ok_for ('test_leaks') ) {
299       Test::Memory::Cycle::memory_cycle_ok ($base_collection, 'No cycles in the object collection')
300     }
301     else {
302       skip 'Circular ref test needs ' .  DBIx::Class::Optional::Dependencies->req_missing_for ('test_leaks'), 1;
303     }
304   }
305
306   populate_weakregistry ($weak_registry, $base_collection->{$_}, "basic $_")
307     for keys %$base_collection;
308 }
309
310 # check that "phantom-chaining" works - we never lose track of the original $schema
311 # and have access to the entire tree without leaking anything
312 {
313   my $phantom;
314   for (
315     sub { DBICTest->init_schema( sqlite_use_file => 0 ) },
316     sub { shift->source('Artist') },
317     sub { shift->resultset },
318     sub { shift->result_source },
319     sub { shift->schema },
320     sub { shift->resultset('Artist') },
321     sub { shift->find_or_create({ name => 'detachable' }) },
322     sub { shift->result_source },
323     sub { shift->schema },
324     sub { shift->clone },
325     sub { shift->resultset('CD') },
326     sub { shift->next },
327     sub { shift->artist },
328     sub { shift->search_related('cds') },
329     sub { shift->next },
330     sub { shift->search_related('artist') },
331     sub { shift->result_source },
332     sub { shift->resultset },
333     sub { shift->create({ name => 'detached' }) },
334     sub { shift->update({ name => 'reattached' }) },
335     sub { shift->discard_changes },
336     sub { shift->delete },
337     sub { shift->insert },
338   ) {
339     $phantom = populate_weakregistry ( $weak_registry, scalar $_->($phantom) );
340   }
341
342   ok( $phantom->in_storage, 'Properly deleted/reinserted' );
343   is( $phantom->name, 'reattached', 'Still correct name' );
344 }
345
346 # Naturally we have some exceptions
347 my $cleared;
348 for my $slot (keys %$weak_registry) {
349   if ($slot =~ /^Test::Builder/) {
350     # T::B 2.0 has result objects and other fancyness
351     delete $weak_registry->{$slot};
352   }
353   elsif ($slot =~ /^Method::Generate::(?:Accessor|Constructor)/) {
354     # Moo keeps globals around, this is normal
355     delete $weak_registry->{$slot};
356   }
357   elsif ($slot =~ /^SQL::Translator/) {
358     # SQLT is a piece of shit, leaks all over
359     delete $weak_registry->{$slot};
360   }
361   elsif ($slot =~ /^Hash::Merge/) {
362     # only clear one object of a specific behavior - more would indicate trouble
363     delete $weak_registry->{$slot}
364       unless $cleared->{hash_merge_singleton}{$weak_registry->{$slot}{weakref}{behavior}}++;
365   }
366   elsif (DBIx::Class::_ENV_::INVISIBLE_DOLLAR_AT and $slot =~ /^__TxnScopeGuard__FIXUP__/) {
367     delete $weak_registry->{$slot}
368   }
369   elsif ($slot =~ /^DateTime::TimeZone/) {
370     # DT is going through a refactor it seems - let it leak zones for now
371     delete $weak_registry->{$slot};
372   }
373 }
374
375 # every result class has a result source instance as classdata
376 # make sure these are all present and distinct before ignoring
377 # (distinct means only 1 reference)
378 for my $rs_class (
379   'DBICTest::BaseResult',
380   @compose_ns_classes,
381   map { DBICTest::Schema->class ($_) } DBICTest::Schema->sources
382 ) {
383   # need to store the SVref and examine it separately, to push the rsrc instance off the pad
384   my $SV = svref_2object($rs_class->result_source_instance);
385   is( $SV->REFCNT, 1, "Source instance of $rs_class referenced exactly once" );
386
387   # ignore it
388   delete $weak_registry->{$rs_class->result_source_instance};
389 }
390
391 # Schema classes also hold sources, but these are clones, since
392 # each source contains the schema (or schema class name in this case)
393 # Hence the clone so that the same source can be registered with
394 # multiple schemas
395 for my $moniker ( keys %{DBICTest::Schema->source_registrations || {}} ) {
396
397   my $SV = svref_2object(DBICTest::Schema->source($moniker));
398   is( $SV->REFCNT, 1, "Source instance registered under DBICTest::Schema as $moniker referenced exactly once" );
399
400   delete $weak_registry->{DBICTest::Schema->source($moniker)};
401 }
402
403 # FIXME !!!
404 # There is an actual strong circular reference taking place here, but because
405 # half of it is in XS no leaktracer sees it, and Devel::FindRef is equally
406 # stumped when trying to trace the origin. The problem is:
407 #
408 # $cond_object --> result_source --> schema --> storage --> $dbh --> {cached_kids}
409 #          ^                                                           /
410 #           \-------- bound value on prepared/cached STH  <-----------/
411 #
412 TODO: {
413   local $TODO = 'Not sure how to fix this yet, an entanglment could be an option';
414   my $r = $weak_registry->{'basic leaky_resultset_cond'}{weakref};
415   ok(! defined $r, 'We no longer leak!')
416     or $r->result_source(undef);
417 }
418
419 assert_empty_weakregistry ($weak_registry);
420
421 # we got so far without a failure - this is a good thing
422 # now let's try to rerun this script under a "persistent" environment
423 # this is ugly and dirty but we do not yet have a Test::Embedded or
424 # similar
425
426 # set up -I
427 require Config;
428 $ENV{PERL5LIB} = join ($Config::Config{path_sep}, @INC);
429 ($ENV{PATH}) = $ENV{PATH} =~ /(.+)/;
430
431
432 my $persistence_tests = {
433   PPerl => {
434     cmd => [qw/pperl --prefork=1/, __FILE__],
435   },
436   'CGI::SpeedyCGI' => {
437     cmd => [qw/speedy -- -t5/, __FILE__],
438   },
439 };
440
441 # scgi is smart and will auto-reap after -t amount of seconds
442 # pperl needs an actual killer :(
443 $persistence_tests->{PPerl}{termcmd} = [
444   $persistence_tests->{PPerl}{cmd}[0],
445   '--kill',
446   @{$persistence_tests->{PPerl}{cmd}}[ 1 .. $#{$persistence_tests->{PPerl}{cmd}} ],
447 ];
448
449 SKIP: {
450   skip 'Test already in a persistent loop', 1
451     if $ENV{DBICTEST_IN_PERSISTENT_ENV};
452
453   skip 'Main test failed - skipping persistent env tests', 1
454     unless $TB->is_passing;
455
456   local $ENV{DBICTEST_IN_PERSISTENT_ENV} = 1;
457
458   require IPC::Open2;
459
460   for my $type (keys %$persistence_tests) { SKIP: {
461       skip "$type module not found", 1
462         unless eval "require $type";
463
464     my @cmd = @{$persistence_tests->{$type}{cmd}};
465
466     # since PPerl is racy and sucks - just prime the "server"
467     {
468       local $ENV{DBICTEST_PERSISTENT_ENV_BAIL_EARLY} = 1;
469       system(@cmd);
470       sleep 1;
471
472       # see if the thing actually runs, if not - might as well bail now
473       skip "Something is wrong with $type ($!)", 1
474         if system(@cmd);
475     }
476
477     for (1,2,3) {
478       note ("Starting run in persistent env ($type pass $_)");
479       IPC::Open2::open2(my $out, undef, @cmd);
480       my @out_lines;
481       while (my $ln = <$out>) {
482         next if $ln =~ /^\s*$/;
483         push @out_lines, "   $ln";
484         last if $ln =~ /^\d+\.\.\d+$/;  # this is persistence, we need to terminate reading on our end
485       }
486       print $_ for @out_lines;
487       close $out;
488       wait;
489       ok (!$?, "Run in persistent env ($type pass $_): exit $?");
490       ok (scalar @out_lines, "Run in persistent env ($type pass $_): got output");
491     }
492
493     ok (! system (@{$persistence_tests->{$type}{termcmd}}), "killed $type server instance")
494       if $persistence_tests->{$type}{termcmd};
495   }}
496 }
497
498 done_testing;
499
500 # just an extra precaution in case we blew away from the SKIP - since there are no
501 # PID files to go by (man does pperl really suck :(
502 END {
503   unless ($ENV{DBICTEST_IN_PERSISTENT_ENV}) {
504     close $_ for (*STDIN, *STDOUT, *STDERR);
505     local $?; # otherwise test will inherit $? of the system()
506     system (@{$persistence_tests->{PPerl}{termcmd}});
507   }
508 }