-
-
Notifications
You must be signed in to change notification settings - Fork 85
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(PidBasedController) check PID maps to running process
This fixes the issue where one of the managed processes (Chromedriver, PHP built-in server or MySQL server) would be killed by the system or user outside of wp-browser control leaving an orphaned PID file pointing at a no-more-running process.
- Loading branch information
Showing
7 changed files
with
209 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
88 changes: 88 additions & 0 deletions
88
tests/unit/lucatume/WPBrowser/Extension/PidBasedControllerTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
<?php | ||
|
||
|
||
namespace Unit\lucatume\WPBrowser\Extension; | ||
|
||
use lucatume\WPBrowser\Extension\PidBasedController; | ||
use lucatume\WPBrowser\Exceptions\RuntimeException; | ||
use Symfony\Component\Process\Process; | ||
|
||
class PidBasedControllerTest extends \Codeception\Test\Unit | ||
{ | ||
public function test_isProcessRunning_on_posix():void{ | ||
$testClass = new class { | ||
use PidBasedController; | ||
|
||
public function openIsProcessRunning(string $pidFile):bool{ | ||
return $this->isProcessRunning($pidFile); | ||
} | ||
}; | ||
$hash = md5(microtime()); | ||
$pidFile = sys_get_temp_dir()."/test-{$hash}.pid"; | ||
$pid = posix_getpid(); | ||
if(!file_put_contents($pidFile,$pid)){ | ||
$this->fail('Could not write pid to file '.$pidFile); | ||
} | ||
|
||
$this->assertTrue($testClass->openIsProcessRunning($pidFile)); | ||
$this->assertFileExists($pidFile); | ||
} | ||
|
||
public function test_isProcessRunning_returns_false_if_pid_file_not_exists():void{ | ||
$testClass = new class { | ||
use PidBasedController; | ||
|
||
public function openIsProcessRunning(string $pidFile):bool{ | ||
return $this->isProcessRunning($pidFile); | ||
} | ||
}; | ||
$pid = posix_getpid(); | ||
|
||
$this->assertFalse($testClass->openIsProcessRunning(__DIR__ .'/test.pid')); | ||
} | ||
|
||
public function test_isProcessRunning_throws_if_pid_file_cannot_be_read():void{ | ||
$testClass = new class { | ||
use PidBasedController; | ||
|
||
public function openIsProcessRunning(string $pidFile):bool{ | ||
return $this->isProcessRunning($pidFile); | ||
} | ||
}; | ||
$pid = posix_getpid(); | ||
$hash = md5(microtime()); | ||
$pidFile = sys_get_temp_dir()."/test-{$hash}.pid"; | ||
$pid = posix_getpid(); | ||
if(!file_put_contents($pidFile,$pid)){ | ||
$this->fail('Could not write pid to file '.$pidFile); | ||
} | ||
// Change the file mode to not be readable by the current user. | ||
chmod($pidFile,0000); | ||
|
||
$this->expectException(RuntimeException::class); | ||
|
||
$testClass->openIsProcessRunning($pidFile); | ||
} | ||
|
||
public function test_isProcessRunning_returns_false_if_process_is_not_running():void{ | ||
$testClass = new class { | ||
use PidBasedController; | ||
|
||
public function openIsProcessRunning(string $pidFile):bool{ | ||
return $this->isProcessRunning($pidFile); | ||
} | ||
}; | ||
$hash = md5(microtime()); | ||
$pidFile = sys_get_temp_dir()."/test-{$hash}.pid"; | ||
$process = new Process(['echo', '23']); | ||
$process->start(); | ||
$pid = $process->getPid(); | ||
$process->wait(); | ||
if(!file_put_contents($pidFile,$pid)){ | ||
$this->fail('Could not write pid to file '.$pidFile); | ||
} | ||
|
||
$this->assertFalse($testClass->openIsProcessRunning($pidFile)); | ||
$this->assertFileNotExists($pidFile); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters