[Process] Avoid failure because of a slow process

See example of failure here https://travis-ci.org/symfony/symfony/jobs/20761834.
This commit is contained in:
Romain Neutron 2014-03-14 17:03:41 +01:00
parent 173f8c5fd2
commit 238565e93a
2 changed files with 12 additions and 7 deletions

View File

@ -29,6 +29,8 @@ class ProcessPipes
/** @var Boolean */ /** @var Boolean */
private $ttyMode; private $ttyMode;
const CHUNK_SIZE = 16384;
public function __construct($useFiles, $ttyMode) public function __construct($useFiles, $ttyMode)
{ {
$this->useFiles = (Boolean) $useFiles; $this->useFiles = (Boolean) $useFiles;
@ -233,7 +235,7 @@ class ProcessPipes
$data = ''; $data = '';
$dataread = null; $dataread = null;
while (!feof($fileHandle)) { while (!feof($fileHandle)) {
if (false !== $dataread = fread($fileHandle, 16392)) { if (false !== $dataread = fread($fileHandle, self::CHUNK_SIZE)) {
$data .= $dataread; $data .= $dataread;
} }
} }
@ -291,7 +293,7 @@ class ProcessPipes
$type = array_search($pipe, $this->pipes); $type = array_search($pipe, $this->pipes);
$data = ''; $data = '';
while ($dataread = fread($pipe, 8192)) { while ($dataread = fread($pipe, self::CHUNK_SIZE)) {
$data .= $dataread; $data .= $dataread;
} }

View File

@ -13,6 +13,7 @@ namespace Symfony\Component\Process\Tests;
use Symfony\Component\Process\Process; use Symfony\Component\Process\Process;
use Symfony\Component\Process\Exception\RuntimeException; use Symfony\Component\Process\Exception\RuntimeException;
use Symfony\Component\Process\ProcessPipes;
/** /**
* @author Robert Schönthal <seroscho@googlemail.com> * @author Robert Schönthal <seroscho@googlemail.com>
@ -87,18 +88,20 @@ abstract class AbstractProcessTest extends \PHPUnit_Framework_TestCase
// has terminated so the internal pipes array is already empty. normally // has terminated so the internal pipes array is already empty. normally
// the call to start() will not read any data as the process will not have // the call to start() will not read any data as the process will not have
// generated output, but this is non-deterministic so we must count it as // generated output, but this is non-deterministic so we must count it as
// a possibility. therefore we need 2 * 8192 plus another byte which will // a possibility. therefore we need 2 * ProcessPipes::CHUNK_SIZE plus
// never be read. // another byte which will never be read.
$expectedOutputSize = 16385; $expectedOutputSize = ProcessPipes::CHUNK_SIZE * 2 + 2;
$code = sprintf('echo str_repeat(\'*\', %d);', $expectedOutputSize); $code = sprintf('echo str_repeat(\'*\', %d);', $expectedOutputSize);
$p = $this->getProcess(sprintf('php -r %s', escapeshellarg($code))); $p = $this->getProcess(sprintf('php -r %s', escapeshellarg($code)));
$p->start(); $p->start();
usleep(250000); // Let's wait enough time for process to finish...
// Here we don't call Process::run or Process::wait to avoid any read of pipes
usleep(500000);
if ($p->isRunning()) { if ($p->isRunning()) {
$this->fail('Process execution did not complete in the required time frame'); $this->markTestSkipped('Process execution did not complete in the required time frame');
} }
$o = $p->getOutput(); $o = $p->getOutput();