Refactor server startup and shutdown
Matt S Trout [Wed, 22 Jun 2011 12:13:08 +0000 (13:13 +0100)]
- split start and start_xvnc to allow for only starting selenium
- switch from system to exec in child processes so destructors don't run twice
- handle not invoking ssh when starting things on localhost
- wrap an eval round the shutdown get() in case of failure

lib/Test/Harness/Selenium.pm

index cd1d929..da496c3 100644 (file)
@@ -49,20 +49,31 @@ sub new {
 
 sub start_selenium_server {
   my($self) = @_;
-  if($self->{selenium_rc}{start}) {
-    my $child = Child->new(sub {
-        system('ssh', $self->{selenium_rc}{host}, 'vncserver',
-          ":$self->{selenium_rc}{xvnc_display}");
-      }
-    );
-    $self->{selenium_rc}{xvnc_server_proc} = $child->start;
-    $child = Child->new(sub {
-        system('ssh', $self->{selenium_rc}{host}, 'env',
-          "DISPLAY=:$self->{selenium_rc}{xvnc_display}", 'selenium-rc', '-port',
-          $self->{selenium_rc}{port} );
+  my $selrc = $self->{selenium_rc};
+  if($selrc->{start}) {
+    my ($host, $display, $port) = @{$selrc}{qw(host xvnc_display port)};
+    my @do_ssh = $host eq 'localhost' ? () : ('ssh', $host);
+    if ($selrc->{start_xvnc}) {
+      $selrc->{xvnc_server_proc} = Child->new(
+          sub {
+            exec(
+              @do_ssh,
+              'vncserver', ":${display}",
+            );
+          }
+        )->start;
+      $selrc->{xvnc_started} = 1;
+      sleep 3;
+    }
+    $selrc->{selenium_server_proc} = Child->new(
+      sub {
+        exec(
+          @do_ssh,
+          'env', "DISPLAY=:${display}", 'selenium-rc', '-port', $port
+        )
       }
-    );
-    $self->{selenium_rc}{selenium_server_proc} = $child->start;
+    )->start;
+    sleep 1;
   }
   my $tries = 0;
   while($tries < 5) {
@@ -91,20 +102,22 @@ sub start_selenium_server {
 
 sub stop_selenium_server {
   my($self) = @_;
-  # okay, we're done, kill the server.
-  my $url = sprintf
-    "http://%s:%s/selenium-server/driver/?cmd=shutDownSeleniumServer",
-    $self->{selenium_rc}{host}, $self->{selenium_rc}{port};
-  get($url);
-  delete $self->{src};
-  $self->{selenium_rc}{selenium_server_proc}->kill("KILL");
-  my $child = Child->new(sub {
-      system('ssh', $self->{selenium_rc}{host}, 'vncserver', '-kill',
+  if (my $proc = delete $self->{selenium_rc}{selenium_server_proc}) {
+    my $url = sprintf
+      "http://%s:%s/selenium-server/driver/?cmd=shutDownSeleniumServer",
+      $self->{selenium_rc}{host}, $self->{selenium_rc}{port};
+    eval { get($url); }; # will fail if it never started
+    delete $self->{src};
+    $proc->kill("KILL");
+  }
+  if (delete $self->{selenium_rc}{xvnc_started}) {
+    my $host = $self->{selenium_rc}{host};
+    my @do_ssh = $host eq 'localhost' ? () : ('ssh', $host);
+    Child->new(sub {
+      exec(@do_ssh, 'vncserver', '-kill',
         ":$self->{selenium_rc}{xvnc_display}");
-    }
-  );
-  my $proc = $child->start;
-  $proc->wait;
+    })->start->wait;
+  }
 }
 
 sub start_app_server {