From: Nicholas Clark <nick@ccl4.org>
Date: Wed, 6 Aug 2008 17:51:10 +0000 (+0000)
Subject: If TEST_JOBS is set to something non-zero, use TAP::Harness to run the
X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=9ae5a6c323cea172e440bd71782fdef16f8f20b1;p=p5sagit%2Fp5-mst-13.2.git

If TEST_JOBS is set to something non-zero, use TAP::Harness to run the
tests in parallel. The tests aren't fully parallelisable yet to the
level we'd like, but one needs to start somewhere.

p4raw-id: //depot/perl@34174
---

diff --git a/t/harness b/t/harness
index 1c6bcf1..d137701 100644
--- a/t/harness
+++ b/t/harness
@@ -46,7 +46,7 @@ foreach (keys %datahandle) {
      unlink "$_.t";
 }
 
-my (@tests, $re);
+my (@tests, $rules, $re);
 
 # [.VMS]TEST.COM calls harness with empty arguments, so clean-up @ARGV
 @ARGV = grep $_ && length( $_ ) => @ARGV;
@@ -55,6 +55,51 @@ sub _populate_hash {
     return map {$_, 1} split /\s+/, $_[0];
 }
 
+sub _glob_and_parallelise {
+    my @dirs;
+    # Run the tests in each of these directories in sequence, but the
+    # directories themselves can be parallelised.
+    foreach (@_) {
+	push @dirs, { seq => [ glob "$_/*.t" ] };
+    }
+    { par =>  \@dirs };
+}
+
+# Generate T::H schedule rules that run the contents of each directory
+# sequentially.
+sub _seq_dir_rules {
+    my @tests = @_;
+    my %dir;
+    for (@tests) {
+        s{[^/]+$}{\*};
+        $dir{$_}++;
+    }
+
+    return { par => [ map { { seq => $_ } } sort keys %dir ] };
+}
+
+sub _extract_tests;
+sub _extract_tests {
+    # This can probably be done more tersely with a map, but I doubt that it
+    # would be as clear
+    my @results;
+    foreach (@_) {
+	my $ref = ref $_;
+	if ($ref) {
+	    if ($ref eq 'ARRAY') {
+		push @results, _extract_tests @$_;
+	    } elsif ($ref eq 'HASH') {
+		push @results, _extract_tests values %$_;
+	    } else {
+		die "Unknown reference type $ref";
+	    }
+	} else {
+	    push @results, $_;
+	}
+    }
+    @results;
+}
+
 if ($ARGV[0] && $ARGV[0]=~/^-re/) {
     if ($ARGV[0]!~/=/) {
         shift;
@@ -73,18 +118,31 @@ if (@ARGV) {
 	@tests = @ARGV;
     }
 } else {
+    # Ideally we'd get somewhere close to Tux's Oslo rules
+    # my $rules = {
+    #     par => [
+    #         { seq => '../ext/DB_File/t/*' },
+    #         { seq => '../ext/IO_Compress_Zlib/t/*' },
+    #         { seq => '../lib/CPANPLUS/*' },
+    #         { seq => '../lib/ExtUtils/t/*' },
+    #         '*'
+    #     ]
+    # };
+
+    # but for now, run all directories in sequence. In particular, it would be
+    # nice to get the tests in t/op/*.t able to run in parallel.
+
     unless (@tests) {
-	push @tests, <base/*.t>;
-        push @tests, <comp/*.t>;
-        push @tests, <cmd/*.t>;
-        push @tests, <run/*.t>;
-        push @tests, <io/*.t>;
-        push @tests, <op/*.t>;
-        push @tests, <uni/*.t>;
-        push @tests, <mro/*.t>;
-        push @tests, <lib/*.t>;
-        push @tests, <japh/*.t> if $torture;
-	push @tests, <win32/*.t> if $^O eq 'MSWin32';
+	my @seq;
+	push @seq, <base/*.t>;
+
+	push @seq, _glob_and_parallelise qw(comp cmd run io);
+	my @next = qw(op uni mro lib);
+	push @next, 'japh' if $torture;
+	push @next, 'win32' if $^O eq 'MSWin32';
+	push @seq, _glob_and_parallelise @next;
+
+	my @last;
 	use Config;
 	my %skip;
 	{
@@ -114,13 +172,20 @@ if (@ARGV) {
 	    close MANI;
 	    # Sort the list of test files read from MANIFEST into a sensible
 	    # order instead of using the order in which they are listed there
-	    push @tests, sort { lc $a cmp lc $b } @manitests;
+	    push @last, sort { lc $a cmp lc $b } @manitests;
 	} else {
 	    warn "$0: cannot open $mani: $!\n";
 	}
-	push @tests, <Module_Pluggable/*.t>;
-	push @tests, <pod/*.t>;
-	push @tests, <x2p/*.t>;
+	push @last, <Module_Pluggable/*.t>;
+	push @last, <pod/*.t>;
+	push @last, <x2p/*.t>;
+
+	@tests = (_extract_tests (@seq), @last);
+
+	push @seq, _seq_dir_rules @last;
+
+	$rules = { seq => \@seq };
+
     }
 }
 if ($^O eq 'MSWin32') {
@@ -128,5 +193,13 @@ if ($^O eq 'MSWin32') {
 }
 @tests=grep /$re/, @tests 
     if $re;
-Test::Harness::runtests @tests;
+
+my $jobs = $ENV{TEST_JOBS};
+if ($jobs) {
+    eval 'use TAP::Harness 3.13; 1' or die $@;
+    my $h = TAP::Harness->new({ jobs => $jobs, rules => $rules});
+    $h->runtests(@tests);
+} else {
+    Test::Harness::runtests @tests;
+}
 exit(0);