Skip to content

Commit

Permalink
[Process] Avoid failure because of a slow process
Browse files Browse the repository at this point in the history
  • Loading branch information
romainneutron committed Mar 14, 2014
1 parent 173f8c5 commit 238565e
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 7 deletions.
6 changes: 4 additions & 2 deletions src/Symfony/Component/Process/ProcessPipes.php
Expand Up @@ -29,6 +29,8 @@ class ProcessPipes
/** @var Boolean */
private $ttyMode;

const CHUNK_SIZE = 16384;

public function __construct($useFiles, $ttyMode)
{
$this->useFiles = (Boolean) $useFiles;
Expand Down Expand Up @@ -233,7 +235,7 @@ private function readFileHandles($close = false)
$data = '';
$dataread = null;
while (!feof($fileHandle)) {
if (false !== $dataread = fread($fileHandle, 16392)) {
if (false !== $dataread = fread($fileHandle, self::CHUNK_SIZE)) {
$data .= $dataread;
}
}
Expand Down Expand Up @@ -291,7 +293,7 @@ private function readStreams($blocking, $close = false)
$type = array_search($pipe, $this->pipes);

$data = '';
while ($dataread = fread($pipe, 8192)) {
while ($dataread = fread($pipe, self::CHUNK_SIZE)) {
$data .= $dataread;
}

Expand Down
13 changes: 8 additions & 5 deletions src/Symfony/Component/Process/Tests/AbstractProcessTest.php
Expand Up @@ -13,6 +13,7 @@

use Symfony\Component\Process\Process;
use Symfony\Component\Process\Exception\RuntimeException;
use Symfony\Component\Process\ProcessPipes;

/**
* @author Robert Schönthal <seroscho@googlemail.com>
Expand Down Expand Up @@ -87,18 +88,20 @@ public function testAllOutputIsActuallyReadOnTermination()
// 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
// 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
// never be read.
$expectedOutputSize = 16385;
// a possibility. therefore we need 2 * ProcessPipes::CHUNK_SIZE plus
// another byte which will never be read.
$expectedOutputSize = ProcessPipes::CHUNK_SIZE * 2 + 2;

$code = sprintf('echo str_repeat(\'*\', %d);', $expectedOutputSize);
$p = $this->getProcess(sprintf('php -r %s', escapeshellarg($code)));

$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()) {
$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();
Expand Down

0 comments on commit 238565e

Please sign in to comment.