WWW-Mechanize-Chrome
view release on metacpan or search on metacpan
t/helper.pm view on Meta::CPAN
package # hide from CPAN indexer
t::helper;
use 5.020;
use feature 'postderef';
use Test::More;
use File::Glob qw(bsd_glob);
use Config '%Config';
use File::Spec;
use Carp qw(croak);
use File::Temp 'tempdir';
use WWW::Mechanize::Chrome;
use Config;
use Time::HiRes qw(sleep time);
use POSIX qw(:sys_wait_h);
use Log::Log4perl ':easy';
delete $ENV{HTTP_PROXY};
delete $ENV{HTTPS_PROXY};
$ENV{PERL_FUTURE_DEBUG} = 1
if not exists $ENV{PERL_FUTURE_DEBUG};
# Global PID tracking for fail-safe cleanup
our %all_spawned_pids;
{
my $org_new = \&WWW::Mechanize::Chrome::new;
no warnings 'redefine';
*WWW::Mechanize::Chrome::new = sub {
my $self = $org_new->(@_);
if (ref $self && $self->{pid}) {
for my $pid ($self->{pid}->@*) {
$all_spawned_pids{$pid} = 1 if $pid;
}
}
return $self;
};
# Override kill_child to be more aggressive and non-blocking in tests.
# This prevents hangs during the cleanup phase of tests, especially with
# modern Chromium versions that may not exit promptly on SIGTERM.
*WWW::Mechanize::Chrome::kill_child = sub {
my ($self, $signal, $pids, $wait_file) = @_;
return unless $pids;
my @p = ref $pids eq 'ARRAY' ? @$pids : ($pids);
for my $pid (@p) {
next unless $pid && kill(0, $pid);
# Use SIGKILL in tests to ensure swift termination and avoid hangs
kill('KILL', $pid);
# Non-blocking wait with a short timeout
my $timeout = Time::HiRes::time() + 2;
while (Time::HiRes::time() < $timeout) {
my $res = waitpid($pid, WNOHANG);
last if $res == -1 || $res == $pid;
Time::HiRes::sleep(0.1);
}
delete $all_spawned_pids{$pid};
}
return;
};
}
END {
# Final fail-safe cleanup of all PIDs spawned during this test process
for my $pid (keys %all_spawned_pids) {
if ($pid && kill(0, $pid)) {
kill('KILL', $pid);
waitpid($pid, WNOHANG);
}
}
}
sub need_minimum_chrome_version {
( run in 0.911 second using v1.01-cache-2.11-cpan-d7a12ab2c7f )