Do not load DBIC::Optional::Dependencies at runtime unless we need to
[dbsrgits/DBIx-Class.git] / t / 52leaks.t
CommitLineData
c0329273 1BEGIN { do "./t/lib/ANFANG.pm" or die ( $@ || $! ) }
2
66917da3 3# work around brain damage in PPerl (yes, it has to be a global)
4$SIG{__WARN__} = sub {
5 warn @_ unless $_[0] =~ /\QUse of "goto" to jump into a construct is deprecated/
6} if ($ENV{DBICTEST_IN_PERSISTENT_ENV});
7
8# the persistent environments run with this flag first to see if
9# we will run at all (e.g. it will fail if $^X doesn't match)
10exit 0 if $ENV{DBICTEST_PERSISTENT_ENV_BAIL_EARLY};
11
f05edfd1 12# Do the override as early as possible so that CORE::bless doesn't get compiled away
13# We will replace $bless_override only if we are in author mode
14my $bless_override;
15BEGIN {
16 $bless_override = sub {
17 CORE::bless( $_[0], (@_ > 1) ? $_[1] : caller() );
18 };
19 *CORE::GLOBAL::bless = sub { goto $bless_override };
20}
21
50261284 22use strict;
23use warnings;
a917fb06 24use Test::More;
d5e5fb4b 25
08a8d8f1 26BEGIN {
27 require DBICTest::Util;
28 plan skip_all => "Your perl version $] appears to leak like a sieve - skipping test"
29 if DBICTest::Util::PEEPEENESS();
30}
c0329273 31
30c81ba9 32use DBICTest::RunMode;
ee20935b 33use DBICTest::Util::LeakTracer qw(populate_weakregistry assert_empty_weakregistry visit_refs);
34use Scalar::Util qw(weaken blessed reftype);
35use DBIx::Class::_Util qw(hrefaddr sigwarn_silencer modver_gt_or_eq modver_gt_or_eq_and_lt);
18a2903b 36use DBIx::Class::Optional::Dependencies;
30c81ba9 37
66917da3 38my $TB = Test::More->builder;
39if ($ENV{DBICTEST_IN_PERSISTENT_ENV}) {
ee20935b 40 # without this explicit close TB warns in END after a ->reset
41 close ($TB->$_) for qw(output failure_output todo_output);
66917da3 42
ee20935b 43 # newer TB does not auto-reopen handles
44 if ( modver_gt_or_eq( 'Test::More', '1.200' ) ) {
45 open ($TB->$_, '>&', *STDERR)
46 for qw( failure_output todo_output );
7be5717e 47 open ($TB->output, '>&', *STDOUT);
48 }
49
50 # so done_testing can work on every persistent pass
51 $TB->reset;
66917da3 52}
53
a8c2c746 54# this is what holds all weakened refs to be checked for leakage
55my $weak_registry = {};
56
6a43bc0c 57# whether or to invoke IC::DT
58my $has_dt;
59
a8c2c746 60# Skip the heavy-duty leak tracing when just doing an install
8b60b921 61# or when having Moose crap all over everything
62if ( !$ENV{DBICTEST_VIA_REPLICATED} and !DBICTest::RunMode->is_plain ) {
f05edfd1 63
eb7aa960 64 # redefine the bless override so that we can catch each and every object created
a8c2c746 65 no warnings qw/redefine once/;
66 no strict qw/refs/;
67
f05edfd1 68 $bless_override = sub {
69
a8c2c746 70 my $obj = CORE::bless(
71 $_[0], (@_ > 1) ? $_[1] : do {
72 my ($class, $fn, $line) = caller();
73 fail ("bless() of $_[0] into $class without explicit class specification at $fn line $line")
74 if $class =~ /^ (?: DBIx\:\:Class | DBICTest ) /x;
75 $class;
76 }
77 );
78
8d6b1478 79 # unicode is tricky, and now we happen to invoke it early via a
80 # regex in connection()
81 return $obj if (ref $obj) =~ /^utf8/;
82
7be5717e 83 # Test Builder is now making a new object for every pass/fail (que bloat?)
84 # and as such we can't really store any of its objects (since it will
85 # re-populate the registry while checking it, ewwww!)
ee20935b 86 return $obj if (ref $obj) =~ /^TB2::|^Test::Stream/;
7be5717e 87
5dc4301c 88 # populate immediately to avoid weird side effects
65d35121 89 return populate_weakregistry ($weak_registry, $obj );
a8c2c746 90 };
91
eb7aa960 92 require Try::Tiny;
a8c2c746 93 for my $func (qw/try catch finally/) {
94 my $orig = \&{"Try::Tiny::$func"};
95 *{"Try::Tiny::$func"} = sub (&;@) {
65d35121 96 populate_weakregistry( $weak_registry, $_[0] );
a8c2c746 97 goto $orig;
98 }
99 }
eb7aa960 100
101 # Some modules are known to install singletons on-load
102 # Load them and empty the registry
103
104 # this loads the DT armada
54a9a088 105 $has_dt = DBIx::Class::Optional::Dependencies->req_ok_for([qw( test_rdbms_sqlite ic_dt )]);
eb7aa960 106
eb7aa960 107 require DBI;
108 require DBD::SQLite;
e6ff3658 109 require Moo;
eb7aa960 110
111 %$weak_registry = ();
a8c2c746 112}
113
114{
66917da3 115 use_ok ('DBICTest');
a917fb06 116
a8c2c746 117 my $schema = DBICTest->init_schema;
118 my $rs = $schema->resultset ('Artist');
119 my $storage = $schema->storage;
a917fb06 120
052b8ce2 121 my $row_obj = $rs->search({}, { rows => 1})->next; # so that commits/rollbacks work
a8c2c746 122 ok ($row_obj, 'row from db');
123
052b8ce2 124 # txn_do to invoke more codepaths
a8c2c746 125 my ($mc_row_obj, $pager, $pager_explicit_count) = $schema->txn_do (sub {
126
9345b14c 127 my $artist = $schema->resultset('Artist')->create ({
a8c2c746 128 name => 'foo artist',
129 cds => [{
130 title => 'foo cd',
131 year => 1984,
187ec69a 132 tracks => [
133 { title => 't1' },
134 { title => 't2' },
135 ],
136 genre => { name => 'mauve' },
a8c2c746 137 }],
138 });
139
140 my $pg = $rs->search({}, { rows => 1})->page(2)->pager;
141
142 my $pg_wcount = $rs->page(4)->pager->total_entries (66);
143
144 return ($artist, $pg, $pg_wcount);
145 });
146
9345b14c 147 # more codepaths - error handling in txn_do
148 {
149 eval { $schema->txn_do ( sub {
150 $storage->_dbh->begin_work;
151 fail ('how did we get so far?!');
152 } ) };
153
154 eval { $schema->txn_do ( sub {
155 $schema->txn_do ( sub {
156 die "It's called EXCEPTION";
157 fail ('how did we get so far?!');
158 } );
159 fail ('how did we get so far?!');
160 } ) };
161 like( $@, qr/It\'s called EXCEPTION/, 'Exception correctly propagated in nested txn_do' );
162 }
163
164 # dbh_do codepath
187ec69a 165 my ($rs_bind_circref, $cond_rowobj) = $schema->storage->dbh_do ( sub {
166 my $row = $_[0]->schema->resultset('Artist')->new({});
167 my $rs = $_[0]->schema->resultset('Artist')->search({
168 name => $row, # this is deliberately bogus, see FIXME below!
169 });
170 return ($rs, $row);
171 });
172
a8c2c746 173 is ($pager->next_page, 3, 'There is one more page available');
174
175 # based on 66 per 10 pages
176 is ($pager_explicit_count->last_page, 7, 'Correct last page');
551e711a 177
052b8ce2 178 # do some population (invokes some extra codepaths)
179 # also exercise the guard code and the manual txn control
180 {
181 my $guard = $schema->txn_scope_guard;
182 # populate with bindvars
183 $rs->populate([{ name => 'James Bound' }]);
184 $guard->commit;
185
186 $schema->txn_begin;
187 # populate mixed
188 $rs->populate([{ name => 'James Rebound', rank => \ '11' }]);
189 $schema->txn_commit;
190
191 $schema->txn_begin;
192 # and without bindvars
193 $rs->populate([{ name => \ '"James Unbound"' }]);
194 $schema->txn_rollback;
195 }
196
0a03206a 197 # prefetching
198 my $cds_rs = $schema->resultset('CD');
199 my $cds_with_artist = $cds_rs->search({}, { prefetch => 'artist' });
200 my $cds_with_tracks = $cds_rs->search({}, { prefetch => 'tracks' });
201 my $cds_with_stuff = $cds_rs->search({}, { prefetch => [ 'genre', { artist => { cds => { tracks => 'cd_single' } } } ] });
202
203 # implicit pref
204 my $cds_with_impl_artist = $cds_rs->search({}, { columns => [qw/me.title artist.name/], join => 'artist' });
205
206 # get_column
207 my $getcol_rs = $cds_rs->get_column('me.cdid');
208 my $pref_getcol_rs = $cds_with_stuff->get_column('me.cdid');
209
a8c2c746 210 my $base_collection = {
a8c2c746 211 resultset => $rs,
307ab4c5 212
0a03206a 213 pref_precursor => $cds_rs,
214
215 pref_rs_single => $cds_with_artist,
216 pref_rs_multi => $cds_with_tracks,
217 pref_rs_nested => $cds_with_stuff,
218
219 pref_rs_implicit => $cds_with_impl_artist,
220
221 pref_row_single => $cds_with_artist->next,
222 pref_row_multi => $cds_with_tracks->next,
223 pref_row_nested => $cds_with_stuff->next,
224
225 # even though this does not leak Storable croaks on it :(((
226 #pref_row_implicit => $cds_with_impl_artist->next,
227
228 get_column_rs_plain => $getcol_rs,
229 get_column_rs_pref => $pref_getcol_rs,
230
37aafa2e 231 # twice so that we make sure only one H::M object spawned
b35500f1 232 chained_resultset => $rs->search_rs ({}, { '+columns' => { foo => 'artistid' } } ),
233 chained_resultset2 => $rs->search_rs ({}, { '+columns' => { bar => 'artistid' } } ),
37aafa2e 234
a8c2c746 235 row_object => $row_obj,
551e711a 236
187ec69a 237 mc_row_object => $mc_row_obj,
238
a8c2c746 239 result_source => $rs->result_source,
551e711a 240
4376a157 241 result_source_handle => $rs->result_source->handle,
242
a8c2c746 243 pager_explicit_count => $pager_explicit_count,
187ec69a 244
245 leaky_resultset => $rs_bind_circref,
246 leaky_resultset_cond => $cond_rowobj,
a8c2c746 247 };
574d9b69 248
6ae62c5c 249 # fire all resultsets multiple times, once here, more below
250 # some of these can't find anything (notably leaky_resultset)
251 my @rsets = grep {
252 blessed $_
253 and
254 (
255 $_->isa('DBIx::Class::ResultSet')
256 or
257 $_->isa('DBIx::Class::ResultSetColumn')
258 )
259 } values %$base_collection;
260
261
262 my $fire_resultsets = sub {
263 local $ENV{DBIC_COLUMNS_INCLUDE_FILTER_RELS} = 1;
264 local $SIG{__WARN__} = sigwarn_silencer(
265 qr/Unable to deflate 'filter'-type relationship 'artist'.+related object primary key not retrieved/
266 );
267
268 map
269 { $_, (blessed($_) ? { $_->get_columns } : ()) }
270 map
271 { $_->all }
272 @rsets
273 ;
274 };
b35500f1 275
6ae62c5c 276 push @{$base_collection->{random_results}}, $fire_resultsets->();
b35500f1 277
278 # FIXME - something throws a Storable for a spin if we keep
279 # the results in-collection. The same problem is seen above,
280 # swept under the rug back in 0a03206a, damned lazy ribantainer
281{
282 local $base_collection->{random_results};
218b7c12 283
eb7aa960 284 require Storable;
4376a157 285 %$base_collection = (
286 %$base_collection,
287 refrozen => Storable::dclone( $base_collection ),
288 rerefrozen => Storable::dclone( Storable::dclone( $base_collection ) ),
0a03206a 289 pref_row_implicit => $cds_with_impl_artist->next,
4376a157 290 schema => $schema,
291 storage => $storage,
292 sql_maker => $storage->sql_maker,
293 dbh => $storage->_dbh,
cd122820 294 fresh_pager => $rs->page(5)->pager,
295 pager => $pager,
4376a157 296 );
b35500f1 297}
4376a157 298
21aa86aa 299 # FIXME - ideally this kind of collector ought to be global, but attempts
300 # with an invasive debugger-based tracer did not quite work out... yet
301 # Manually scan the innards of everything we have in the base collection
302 # we assembled so far (skip the DT madness below) *recursively*
303 #
304 # Only do this when we do have the bits to look inside CVs properly,
305 # without it we are liable to pick up object defaults that are locked
306 # in method closures
307 if (DBICTest::Util::LeakTracer::CV_TRACING) {
308 visit_refs(
309 refs => [ $base_collection ],
310 action => sub {
311 populate_weakregistry ($weak_registry, $_[0]);
312 1; # true means "keep descending"
313 },
314 );
6ae62c5c 315
316 # do a heavy-duty fire-and-compare loop on all resultsets
317 # this is expensive - not running on install
318 my $typecounts = {};
014846ee 319 if (
320 ! DBICTest::RunMode->is_plain
321 and
322 ! $ENV{DBICTEST_IN_PERSISTENT_ENV}
014846ee 323 ) {
6ae62c5c 324
325 # FIXME - ideally we should be able to just populate an alternative
326 # registry, subtract everything from the main one, and arrive at
327 # an "empty" resulting hash
328 # However due to gross inefficiencies in the ::ResultSet code we
329 # end up recalculating a new set of aliasmaps which could have very
330 # well been cached if it wasn't for... anyhow
331 # What we do here for the time being is similar to the lazy approach
332 # of Devel::LeakTrace - we just make sure we do not end up with more
333 # reftypes than when we started. At least we are not blanket-counting
334 # SVs like D::LT does, but going by reftype... sigh...
335
336 for (values %$weak_registry) {
337 if ( my $r = reftype($_->{weakref}) ) {
338 $typecounts->{$r}--;
339 }
340 }
341
342 # For now we can only reuse the same registry, see FIXME above/below
343 #for my $interim_wr ({}, {}) {
344 for my $interim_wr ( ($weak_registry) x 4 ) {
345
346 visit_refs(
347 refs => [ $fire_resultsets->(), @rsets ],
348 action => sub {
349 populate_weakregistry ($interim_wr, $_[0]);
350 1; # true means "keep descending"
351 },
352 );
353
354 # FIXME - this is what *should* be here
355 #
356 ## anything we have seen so far is cool
357 #delete @{$interim_wr}{keys %$weak_registry};
358 #
6ae62c5c 359 ## moment of truth - the rest ought to be gone
360 #assert_empty_weakregistry($interim_wr);
361 }
362
363 for (values %$weak_registry) {
364 if ( my $r = reftype($_->{weakref}) ) {
365 $typecounts->{$r}++;
366 }
367 }
368 }
369
370 for (keys %$typecounts) {
371 fail ("Amount of $_ refs changed by $typecounts->{$_} during resultset mass-execution")
372 if ( abs ($typecounts->{$_}) > 1 ); # there is a pad caught somewhere, the +1/-1 can be ignored
373 }
21aa86aa 374 }
375
6a43bc0c 376 if ($has_dt) {
377 my $rs = $base_collection->{icdt_rs} = $schema->resultset('Event');
378
379 my $now = DateTime->now;
380 for (1..5) {
381 $base_collection->{"icdt_row_$_"} = $rs->create({
382 created_on => DateTime->new(year => 2011, month => 1, day => $_, time_zone => "-0${_}00" ),
383 starts_at => $now->clone->add(days => $_),
384 });
385 }
386
387 # re-search
388 my @dummy = $rs->all;
389 }
390
eb7aa960 391 # dbh's are created in XS space, so pull them separately
392 for ( grep { defined } map { @{$_->{ChildHandles}} } values %{ {DBI->installed_drivers()} } ) {
393 $base_collection->{"DBI handle $_"} = $_;
394 }
395
65d35121 396 populate_weakregistry ($weak_registry, $base_collection->{$_}, "basic $_")
397 for keys %$base_collection;
551e711a 398}
399
50261284 400# check that "phantom-chaining" works - we never lose track of the original $schema
401# and have access to the entire tree without leaking anything
402{
403 my $phantom;
404 for (
39b80a73 405 sub { DBICTest->init_schema( sqlite_use_file => 0 ) },
50261284 406 sub { shift->source('Artist') },
407 sub { shift->resultset },
408 sub { shift->result_source },
409 sub { shift->schema },
410 sub { shift->resultset('Artist') },
411 sub { shift->find_or_create({ name => 'detachable' }) },
412 sub { shift->result_source },
413 sub { shift->schema },
414 sub { shift->clone },
187ec69a 415 sub { shift->resultset('CD') },
416 sub { shift->next },
417 sub { shift->artist },
418 sub { shift->search_related('cds') },
50261284 419 sub { shift->next },
187ec69a 420 sub { shift->search_related('artist') },
50261284 421 sub { shift->result_source },
422 sub { shift->resultset },
423 sub { shift->create({ name => 'detached' }) },
424 sub { shift->update({ name => 'reattached' }) },
425 sub { shift->discard_changes },
426 sub { shift->delete },
427 sub { shift->insert },
428 ) {
65d35121 429 $phantom = populate_weakregistry ( $weak_registry, scalar $_->($phantom) );
50261284 430 }
431
432 ok( $phantom->in_storage, 'Properly deleted/reinserted' );
433 is( $phantom->name, 'reattached', 'Still correct name' );
434}
a8c2c746 435
307ab4c5 436# Naturally we have some exceptions
437my $cleared;
96577657 438for my $addr (keys %$weak_registry) {
439 my $names = join "\n", keys %{$weak_registry->{$addr}{slot_names}};
440
441 if ($names =~ /^Test::Builder/m) {
c8194884 442 # T::B 2.0 has result objects and other fancyness
96577657 443 delete $weak_registry->{$addr};
c8194884 444 }
e48635f7 445 # remove this when IO::Dir is gone from SQLT
446 elsif ($INC{"IO/Dir.pm"} and $names =~ /^Class::Struct::Tie_ISA/m) {
439a7283 447 delete $weak_registry->{$addr};
448 }
96577657 449 elsif ($names =~ /^Hash::Merge/m) {
37aafa2e 450 # only clear one object of a specific behavior - more would indicate trouble
96577657 451 delete $weak_registry->{$addr}
452 unless $cleared->{hash_merge_singleton}{$weak_registry->{$addr}{weakref}{behavior}}++;
307ab4c5 453 }
e9d552de 454 elsif ($names =~ /^B::Hooks::EndOfScope::PP::_TieHintHashFieldHash/m) {
455 # there is one tied lexical which stays alive until GC time
456 # https://metacpan.org/source/ETHER/B-Hooks-EndOfScope-0.15/lib/B/Hooks/EndOfScope/PP/FieldHash.pm#L24
457 # simply ignore it here, instead of teaching the leaktracer to examine ties
458 # the latter is possible yet terrible: https://github.com/dbsrgits/dbix-class/blob/v0.082820/t/lib/DBICTest/Util/LeakTracer.pm#L113-L117
459 delete $weak_registry->{$addr}
460 unless $cleared->{bheos_pptiehinthashfieldhash}++;
461 }
3619783b 462 elsif ($names =~ /^DateTime::TimeZone::UTC/m) {
463 # DT is going through a refactor it seems - let it leak zones for now
464 delete $weak_registry->{$addr};
465 }
c0fe9332 466 elsif (
75c23ff0 467# # if we can look at closed over pieces - we will register it as a global
468# !DBICTest::Util::LeakTracer::CV_TRACING
469# and
c0fe9332 470 $names =~ /^SQL::Translator::Generator::DDL::SQLite/m
471 ) {
472 # SQLT::Producer::SQLite keeps global generators around for quoted
473 # and non-quoted DDL, allow one for each quoting style
474 delete $weak_registry->{$addr}
475 unless $cleared->{sqlt_ddl_sqlite}->{@{$weak_registry->{$addr}{weakref}->quote_chars}}++;
476 }
307ab4c5 477}
478
187ec69a 479# FIXME !!!
480# There is an actual strong circular reference taking place here, but because
5dc4301c 481# half of it is in XS, so it is a bit harder to track down (it stumps D::FR)
482# (our tracker does not yet do it, but it'd be nice)
483# The problem is:
187ec69a 484#
728f32b5 485# $cond_object --> result_source --> schema --> storage --> $dbh --> {CachedKids}
187ec69a 486# ^ /
487# \-------- bound value on prepared/cached STH <-----------/
488#
728f32b5 489{
5dc4301c 490 my @circreffed;
491
492 for my $r (map
493 { $_->{weakref} }
494 grep
495 { $_->{slot_names}{'basic leaky_resultset_cond'} }
496 values %$weak_registry
497 ) {
498 local $TODO = 'Needs Data::Entangled or somesuch - see RT#82942';
728f32b5 499 ok(! defined $r, 'Self-referential RS conditions no longer leak!')
5dc4301c 500 or push @circreffed, $r;
501 }
502
503 if (@circreffed) {
504 is (scalar @circreffed, 1, 'One resultset expected to leak');
505
506 # this is useless on its own, it is to showcase the circref-diag
507 # and eventually test it when it is operational
508 local $TODO = 'Needs Data::Entangled or somesuch - see RT#82942';
509 while (@circreffed) {
510 weaken (my $r = shift @circreffed);
511
512 populate_weakregistry( (my $mini_registry = {}), $r );
513 assert_empty_weakregistry( $mini_registry );
514
515 $r->result_source(undef);
516 }
728f32b5 517 }
187ec69a 518}
519
65d35121 520assert_empty_weakregistry ($weak_registry);
551e711a 521
66917da3 522# we got so far without a failure - this is a good thing
523# now let's try to rerun this script under a "persistent" environment
524# this is ugly and dirty but we do not yet have a Test::Embedded or
525# similar
526
7617dcc4 527my $persistence_tests;
66917da3 528SKIP: {
529 skip 'Test already in a persistent loop', 1
530 if $ENV{DBICTEST_IN_PERSISTENT_ENV};
531
66917da3 532 skip 'Main test failed - skipping persistent env tests', 1
533 unless $TB->is_passing;
534
ee20935b 535 skip "Test::Builder\@@{[ Test::Builder->VERSION ]} known to break persistence tests", 1
536 if modver_gt_or_eq_and_lt( 'Test::More', '1.200', '1.301001_099' );
537
66917da3 538 local $ENV{DBICTEST_IN_PERSISTENT_ENV} = 1;
539
aff5e9c1 540 require File::Spec;
541
7617dcc4 542 $persistence_tests = {
543 PPerl => {
544 cmd => [qw/pperl --prefork=1/, __FILE__],
545 },
546 'CGI::SpeedyCGI' => {
547 cmd => [qw/speedy -- -t5/, __FILE__],
548 },
549 };
550
551 # scgi is smart and will auto-reap after -t amount of seconds
552 # pperl needs an actual killer :(
553 $persistence_tests->{PPerl}{termcmd} = [
554 $persistence_tests->{PPerl}{cmd}[0],
555 '--kill',
556 @{$persistence_tests->{PPerl}{cmd}}[ 1 .. $#{$persistence_tests->{PPerl}{cmd}} ],
557 ];
558
4fb8d74c 559 # set up -I
560 require Config;
561 $ENV{PERL5LIB} = join ($Config::Config{path_sep}, @INC);
562
563 # adjust PATH for -T
564 if (length $ENV{PATH}) {
565 ( $ENV{PATH} ) = join ( $Config::Config{path_sep},
566 map { length($_) ? File::Spec->rel2abs($_) : () }
567 split /\Q$Config::Config{path_sep}/, $ENV{PATH}
568 ) =~ /\A(.+)\z/;
569 }
7be5717e 570
571 for my $type (keys %$persistence_tests) { SKIP: {
53a5200d 572 unless (eval "require $type") {
573 # Don't terminate what we didn't start
574 delete $persistence_tests->{$type}{termcmd};
575 skip "$type module not found", 1;
576 }
7be5717e 577
578 my @cmd = @{$persistence_tests->{$type}{cmd}};
66917da3 579
580 # since PPerl is racy and sucks - just prime the "server"
581 {
582 local $ENV{DBICTEST_PERSISTENT_ENV_BAIL_EARLY} = 1;
7be5717e 583 system(@cmd);
66917da3 584 sleep 1;
585
7be5717e 586 # see if the thing actually runs, if not - might as well bail now
587 skip "Something is wrong with $type ($!)", 1
588 if system(@cmd);
66917da3 589 }
590
4fb8d74c 591 require IPC::Open2;
592
66917da3 593 for (1,2,3) {
7be5717e 594 note ("Starting run in persistent env ($type pass $_)");
595 IPC::Open2::open2(my $out, undef, @cmd);
596 my @out_lines;
597 while (my $ln = <$out>) {
598 next if $ln =~ /^\s*$/;
599 push @out_lines, " $ln";
600 last if $ln =~ /^\d+\.\.\d+$/; # this is persistence, we need to terminate reading on our end
601 }
602 print $_ for @out_lines;
603 close $out;
604 wait;
605 ok (!$?, "Run in persistent env ($type pass $_): exit $?");
606 ok (scalar @out_lines, "Run in persistent env ($type pass $_): got output");
66917da3 607 }
608
7be5717e 609 ok (! system (@{$persistence_tests->{$type}{termcmd}}), "killed $type server instance")
610 if $persistence_tests->{$type}{termcmd};
611 }}
66917da3 612}
613
551e711a 614done_testing;
66917da3 615
616# just an extra precaution in case we blew away from the SKIP - since there are no
617# PID files to go by (man does pperl really suck :(
618END {
7617dcc4 619 if ($persistence_tests->{PPerl}{termcmd}) {
66917da3 620 local $?; # otherwise test will inherit $? of the system()
7617dcc4 621 require IPC::Open3;
622 open my $null, ">", File::Spec->devnull;
623 waitpid(
624 IPC::Open3::open3(undef, $null, $null, @{$persistence_tests->{PPerl}{termcmd}}),
625 0,
626 );
66917da3 627 }
628}