]> git.sur5r.net Git - i3/i3/blobdiff - testcases/lib/StartXDummy.pm
Merge branch 'master' into next
[i3/i3] / testcases / lib / StartXDummy.pm
index 9dfdedd29b7bcff425e9aec546b172d40d7c366f..d9d2fd129cc6a1e99620291222f44d4b8af02570 100644 (file)
@@ -23,8 +23,11 @@ the file ./Xdummy) and returns two arrayrefs: a list of X11 display numbers to
 the Xdummy processes and a list of PIDs of the processes.
 
 =cut
+
+my $x_socketpath = '/tmp/.X11-unix/X';
+
 sub start_xdummy {
-    my ($parallel) = @_;
+    my ($parallel, $numtests) = @_;
 
     my @displays = ();
     my @childpids = ();
@@ -38,14 +41,17 @@ sub start_xdummy {
 
     $parallel ||= $num_cores * 2;
 
+    # If we are running a small number of tests, don’t over-parallelize.
+    $parallel = $numtests if $numtests < $parallel;
+
     # First get the last used display number, then increment it by one.
     # Effectively falls back to 1 if no X server is running.
-    my ($displaynum) = reverse ('0', sort </tmp/.X11-unix/X*>);
-    $displaynum =~ s/.*(\d)$/$1/;
+    my ($displaynum) = map { /(\d+)$/ } reverse sort glob($x_socketpath . '*');
     $displaynum++;
 
     say "Starting $parallel Xdummy instances, starting at :$displaynum...";
 
+    my @sockets_waiting;
     for my $idx (0 .. ($parallel-1)) {
         my $pid = fork();
         die "Could not fork: $!" unless defined($pid);
@@ -53,30 +59,30 @@ sub start_xdummy {
             # Child, close stdout/stderr, then start Xdummy.
             close STDOUT;
             close STDERR;
+            # make sure this display isn’t in use yet
+            $displaynum++ while -e ($x_socketpath . $displaynum);
+
             # We use -config /dev/null to prevent Xdummy from using the system
             # Xorg configuration. The tests should be independant from the
             # actual system X configuration.
             exec './Xdummy', ":$displaynum", '-config', '/dev/null';
             exit 1;
         }
-        push(@childpids, $pid);
+        push(@complete_run::CLEANUP, sub { kill(15, $pid) });
         push(@displays, ":$displaynum");
+        push(@sockets_waiting, $x_socketpath . $displaynum);
         $displaynum++;
     }
 
     # Wait until the X11 sockets actually appear. Pretty ugly solution, but as
     # long as we can’t socket-activate X11…
-    my $sockets_ready;
-    do {
-        $sockets_ready = 1;
-        for (@displays) {
-            my $path = "/tmp/.X11-unix/X" . substr($_, 1);
-            $sockets_ready = 0 unless -S $path;
-        }
+    while (1) {
+        @sockets_waiting = grep { ! -S $_ } @sockets_waiting;
+        last unless @sockets_waiting;
         sleep 0.1;
-    } until $sockets_ready;
+    }
 
-    return \@displays, \@childpids;
+    return @displays;
 }
 
 1