Skip to content

[Process] Make tests more deterministic #17070

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 23, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 1 addition & 8 deletions src/Symfony/Component/Process/Process.php
Original file line number Diff line number Diff line change
Expand Up @@ -1225,14 +1225,7 @@ public static function isPtySupported()
return $result = false;
}

$proc = @proc_open('echo 1', array(array('pty'), array('pty'), array('pty')), $pipes);
if (is_resource($proc)) {
proc_close($proc);

return $result = true;
}

return $result = false;
return $result = (bool) @proc_open('echo 1', array(array('pty'), array('pty'), array('pty')), $pipes);
}

/**
Expand Down
68 changes: 37 additions & 31 deletions src/Symfony/Component/Process/Tests/ProcessTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -828,30 +828,29 @@ public function testCheckTimeoutOnStartedProcess()
$process->checkTimeout();
usleep(100000);
}
$this->fail('A RuntimeException should have been raised');
} catch (RuntimeException $e) {
$this->fail('A ProcessTimedOutException should have been raised');
} catch (ProcessTimedOutException $e) {
}
$duration = microtime(true) - $start;

$this->assertLessThan(3, $duration);
$this->assertLessThan(15, microtime(true) - $start);

throw $e;
}

public function testIdleTimeout()
{
$process = $this->getProcess(self::$phpBin.' -r "sleep(3);"');
$process->setTimeout(10);
$process->setIdleTimeout(0.5);
$process = $this->getProcess(self::$phpBin.' -r "sleep(34);"');
$process->setTimeout(60);
$process->setIdleTimeout(0.1);

try {
$process->run();

$this->fail('A timeout exception was expected.');
} catch (ProcessTimedOutException $ex) {
$this->assertTrue($ex->isIdleTimeout());
$this->assertFalse($ex->isGeneralTimeout());
$this->assertEquals(0.5, $ex->getExceededTimeout());
} catch (ProcessTimedOutException $e) {
$this->assertTrue($e->isIdleTimeout());
$this->assertFalse($e->isGeneralTimeout());
$this->assertEquals(0.1, $e->getExceededTimeout());
}
}

Expand All @@ -860,17 +859,23 @@ public function testIdleTimeoutNotExceededWhenOutputIsSent()
if ('\\' === DIRECTORY_SEPARATOR) {
$this->markTestIncomplete('This test fails with a timeout on Windows, can someone investigate please?');
}
$process = $this->getProcess(sprintf('%s -r %s', self::$phpBin, escapeshellarg('$n = 30; while ($n--) {echo "foo\n"; usleep(100000); }')));
$process->setTimeout(2);
$process->setIdleTimeout(1);
$process = $this->getProcess(sprintf('%s -r %s', self::$phpBin, escapeshellarg('while (true) {echo "foo\n"; usleep(10000);}')));
$process->setTimeout(1);
$process->start();

while (false === strpos($process->getOutput(), 'foo')) {
usleep(1000);
}

$process->setIdleTimeout(0.1);

try {
$process->run();
$process->wait();
$this->fail('A timeout exception was expected.');
} catch (ProcessTimedOutException $ex) {
$this->assertTrue($ex->isGeneralTimeout(), 'A general timeout is expected.');
$this->assertFalse($ex->isIdleTimeout(), 'No idle timeout is expected.');
$this->assertEquals(2, $ex->getExceededTimeout());
$this->assertEquals(1, $ex->getExceededTimeout());
}
}

Expand All @@ -885,11 +890,12 @@ public function testStartAfterATimeout()

try {
$process->run();
$this->fail('A RuntimeException should have been raised.');
} catch (RuntimeException $e) {
$this->fail('A ProcessTimedOutException should have been raised.');
} catch (ProcessTimedOutException $e) {
}
$this->assertFalse($process->isRunning());
$process->start();
$this->assertTrue($process->isRunning());
$process->stop(0);

throw $e;
Expand Down Expand Up @@ -1047,7 +1053,7 @@ public function provideWrongSignal()

public function testDisableOutputDisablesTheOutput()
{
$p = $this->getProcess(self::$phpBin.' -r "usleep(500000);"');
$p = $this->getProcess('foo');
$this->assertFalse($p->isOutputDisabled());
$p->disableOutput();
$this->assertTrue($p->isOutputDisabled());
Expand All @@ -1061,7 +1067,7 @@ public function testDisableOutputDisablesTheOutput()
*/
public function testDisableOutputWhileRunningThrowsException()
{
$p = $this->getProcess(self::$phpBin.' -r "usleep(500000);"');
$p = $this->getProcess(self::$phpBin.' -r "sleep(39);"');
$p->start();
$p->disableOutput();
}
Expand All @@ -1072,20 +1078,20 @@ public function testDisableOutputWhileRunningThrowsException()
*/
public function testEnableOutputWhileRunningThrowsException()
{
$p = $this->getProcess(self::$phpBin.' -r "usleep(500000);"');
$p = $this->getProcess(self::$phpBin.' -r "sleep(40);"');
$p->disableOutput();
$p->start();
$p->enableOutput();
}

public function testEnableOrDisableOutputAfterRunDoesNotThrowException()
{
$p = $this->getProcess(self::$phpBin.' -r "usleep(500000);"');
$p = $this->getProcess('echo foo');
$p->disableOutput();
$p->start();
$p->wait();
$p->run();
$p->enableOutput();
$p->disableOutput();
$this->assertTrue($p->isOutputDisabled());
}

/**
Expand All @@ -1094,7 +1100,7 @@ public function testEnableOrDisableOutputAfterRunDoesNotThrowException()
*/
public function testDisableOutputWhileIdleTimeoutIsSet()
{
$process = $this->getProcess('sleep 3');
$process = $this->getProcess('foo');
$process->setIdleTimeout(1);
$process->disableOutput();
}
Expand All @@ -1105,24 +1111,24 @@ public function testDisableOutputWhileIdleTimeoutIsSet()
*/
public function testSetIdleTimeoutWhileOutputIsDisabled()
{
$process = $this->getProcess('sleep 3');
$process = $this->getProcess('foo');
$process->disableOutput();
$process->setIdleTimeout(1);
}

public function testSetNullIdleTimeoutWhileOutputIsDisabled()
{
$process = $this->getProcess('sleep 3');
$process = $this->getProcess('foo');
$process->disableOutput();
$process->setIdleTimeout(null);
$this->assertSame($process, $process->setIdleTimeout(null));
}

/**
* @dataProvider provideStartMethods
*/
public function testStartWithACallbackAndDisabledOutput($startMethod, $exception, $exceptionMessage)
{
$p = $this->getProcess(self::$phpBin.' -r "usleep(500000);"');
$p = $this->getProcess('foo');
$p->disableOutput();
$this->setExpectedException($exception, $exceptionMessage);
if ('mustRun' === $startMethod) {
Expand All @@ -1147,7 +1153,7 @@ public function provideStartMethods()
*/
public function testGetOutputWhileDisabled($fetchMethod)
{
$p = $this->getProcess(self::$phpBin.' -r "usleep(500000);"');
$p = $this->getProcess(self::$phpBin.' -r "sleep(41);"');
$p->disableOutput();
$p->start();
$p->{$fetchMethod}();
Expand All @@ -1162,7 +1168,7 @@ public function provideOutputFetchingMethods()
array('getIncrementalErrorOutput'),
);
}

public function testStopTerminatesProcessCleanly()
{
$process = $this->getProcess(self::$phpBin.' -r "echo 123; sleep(42);"');
Expand Down