From 0c5a9f9b92af1634dc60fa21e9ac86ed50e5d595 Mon Sep 17 00:00:00 2001
From: Paul Smith <psmith@gnu.org>
Date: Mon, 30 Oct 2017 12:53:49 -0400
Subject: [PATCH 056/104] * main.c (main): [SV 48274] Allow -j in makefile
 MAKEFLAGS variable.

* tests/jhelp.pl: New file to allow testing parallelism without sleep.
---
 tests/jhelp.pl                     | 63 +++++++++++++++++++++
 create mode 100755 tests/jhelp.pl

diff --git a/tests/jhelp.pl b/tests/jhelp.pl
new file mode 100755
index 0000000..b368099
--- /dev/null
+++ b/tests/jhelp.pl
@@ -0,0 +1,63 @@
+#!/usr/bin/env perl
+# -*-perl-*-
+#
+# This script helps us test jobserver/parallelism without a lot of unreliable
+# (and slow) sleep calls.  Written in Perl to get portable sub-second sleep.
+#
+# It can run the following steps based on arguments:
+#  -t <secs>  : maximum # of seconds the script can run; else we fail.
+#               Default is 4 seconds.
+#  -e <word>  : echo <word> to stdout
+#  -f <word>  : echo <word> to stdout AND create an (empty) file named <word>
+#  -w <word>  : wait for a file named <word> to exist
+
+# Force flush
+$| = 1;
+
+my $timeout = 4;
+
+sub op {
+    my ($op, $nm) = @_;
+
+    defined $nm or die "Missing value for $op\n";
+
+    if ($op eq '-e') {
+        print "$nm\n";
+        return 1;
+    }
+
+    if ($op eq '-f') {
+        print "$nm\n";
+        open(my $fh, '>', $nm) or die "$nm: open: $!\n";
+        close(my $fh);
+        return 1;
+    }
+
+    if ($op eq '-w') {
+        if (-f $nm) {
+            return 1;
+        }
+        select(undef, undef, undef, 0.1);
+        return 0;
+    }
+
+    if ($op eq '-t') {
+        $timeout = $nm;
+        return 1;
+    }
+
+    die("Invalid command: $op $nm\n");
+}
+
+my $start = time();
+while (@ARGV) {
+    if (op($ARGV[0], $ARGV[1])) {
+        shift;
+        shift;
+    }
+    if ($start + $timeout < time()) {
+        die("Timeout after ".(time()-$start-1)." seconds\n");
+    }
+}
+
+exit(0);
-- 
2.19.1

