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