Skip to content

Instantly share code, notes, and snippets.

@nicomen
Created October 30, 2017 19:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nicomen/8c01cc4e2c72af6c90cc580048c50d04 to your computer and use it in GitHub Desktop.
Save nicomen/8c01cc4e2c72af6c90cc580048c50d04 to your computer and use it in GitHub Desktop.
max 4 parallell jobs while testing syntax
#!/usr/bin/perl
use strict;
use warnings;
use Test::More;
use Test::Strict;
use Test2::AsyncSubtest;
use POSIX ":sys_wait_h";
use Time::HiRes qw/sleep/;
my $dir = 'bin';
my @files = Test::Strict::_all_perl_files($dir);
run_parallel_tests(4, [ map { my $file = $_; sub { all_perl_files_ok($file); }; } @files ], 'Syntax and strict checks for '. $dir);
sub run_parallel_tests {
my ($MAX_JOBS, $tests, $name) = @_;
my $ast = Test2::AsyncSubtest->new(name => $name);
my @tests = @{ $tests || [] };
my %children;
while (@tests) {
if (keys %children < $MAX_JOBS) {
my $pid = $ast->run_fork(shift @tests);
$children{$pid}++;
}
waitpid($_, WNOHANG) && delete $children{$_} for keys %children;
last if ! @tests;
sleep 0.01;
}
$ast->finish;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment