Преглед на файлове

Fix #280: Search and destroy driver processes in direct-use modules

George S. Baugh преди 7 години
родител
ревизия
920889dc4e
променени са 3 файла, в които са добавени 45 реда и са изтрити 3 реда
  1. 10 2
      at/chrome.test
  2. 10 1
      at/firefox.test
  3. 25 0
      lib/Selenium/CanStartBinary.pm

+ 10 - 2
at/chrome.test

@@ -2,10 +2,18 @@ use strict;
 use warnings;
 
 use Selenium::Chrome;
-use Test::More tests => 2;
+use Test::More tests => 3;
 use Test::Fatal;
 
-$ENV{MOZ_HEADLESS} = 1;
 my $driver;
 is( exception { $driver = Selenium::Chrome->new(); }, undef, "can spawn new Selenium::Chrome");
+
+my $port = $driver->port();
+
 is( exception { $driver->shutdown_binary; }, undef, "can shutdown binary correctly");
+sleep 2;
+
+my $cmd = "lsof -t -i :$port";
+my $pid = `$cmd`;
+chomp $pid;
+is($pid,'',"Destructor appears to have run shutdown_binary and whacked the driver process");

+ 10 - 1
at/firefox.test

@@ -2,7 +2,7 @@ use strict;
 use warnings;
 
 use Selenium::Firefox;
-use Test::More tests => 2;
+use Test::More tests => 3;
 use Test::Fatal;
 
 my $driver;
@@ -13,4 +13,13 @@ is( exception { $driver = Selenium::Firefox->new(
         },
     },
 ); }, undef, "can spawn new Selenium::Firefox");
+
+my $port = $driver->port();
+
 is( exception { $driver->shutdown_binary; }, undef, "can shutdown binary correctly");
+sleep 2;
+
+my $cmd = "lsof -t -i :$port";
+my $pid = `$cmd`;
+chomp $pid;
+is($pid,'',"Destructor appears to have run shutdown_binary and whacked the driver process");

+ 25 - 0
lib/Selenium/CanStartBinary.pm

@@ -354,6 +354,15 @@ sub _build_binary_mode {
     }
 }
 
+sub _run_binary {
+    my ($self,@command) = @_;
+
+    $self->{pid} = fork() or die "could not fork to run driver process!";
+    return if $self->{pid};
+    #We don't care about the output.
+    capture_merged { system @command };
+}
+
 sub _handle_firefox_setup {
     my ($self, $port) = @_;
 
@@ -399,6 +408,22 @@ sub shutdown_binary {
 
         # Close the orphaned command windows on windows
         $self->shutdown_windows_binary;
+        $self->shutdown_unix_binary;
+    }
+
+}
+
+sub shutdown_unix_binary {
+    my ($self) = @_;
+    if (!IS_WIN) {
+        my $cmd = "lsof -t -i :".$self->port();
+        my $pid = `$cmd`;
+        chomp $pid;
+        if ($pid) {
+            print "Killing Driver PID $pid listening on port ".$self->port."...\n";
+            eval { kill 'KILL', $pid };
+            warn "Could not kill driver process! you may have to clean up manually." if $@;
+        }
     }
 }