Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 0dc9389

Browse files
[Process] Make tests more deterministic
1 parent 5dc2bb3 commit 0dc9389

File tree

2 files changed

+38
-39
lines changed

2 files changed

+38
-39
lines changed

src/Symfony/Component/Process/Process.php

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1225,14 +1225,7 @@ public static function isPtySupported()
12251225
return $result = false;
12261226
}
12271227

1228-
$proc = @proc_open('echo 1', array(array('pty'), array('pty'), array('pty')), $pipes);
1229-
if (is_resource($proc)) {
1230-
proc_close($proc);
1231-
1232-
return $result = true;
1233-
}
1234-
1235-
return $result = false;
1228+
return $result = (bool) @proc_open('echo 1', array(array('pty'), array('pty'), array('pty')), $pipes);
12361229
}
12371230

12381231
/**

src/Symfony/Component/Process/Tests/ProcessTest.php

Lines changed: 37 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -828,30 +828,29 @@ public function testCheckTimeoutOnStartedProcess()
828828
$process->checkTimeout();
829829
usleep(100000);
830830
}
831-
$this->fail('A RuntimeException should have been raised');
832-
} catch (RuntimeException $e) {
831+
$this->fail('A ProcessTimedOutException should have been raised');
832+
} catch (ProcessTimedOutException $e) {
833833
}
834-
$duration = microtime(true) - $start;
835834

836-
$this->assertLessThan(3, $duration);
835+
$this->assertLessThan(15, microtime(true) - $start);
837836

838837
throw $e;
839838
}
840839

841840
public function testIdleTimeout()
842841
{
843-
$process = $this->getProcess(self::$phpBin.' -r "sleep(3);"');
844-
$process->setTimeout(10);
845-
$process->setIdleTimeout(0.5);
842+
$process = $this->getProcess(self::$phpBin.' -r "sleep(34);"');
843+
$process->setTimeout(60);
844+
$process->setIdleTimeout(0.1);
846845

847846
try {
848847
$process->run();
849848

850849
$this->fail('A timeout exception was expected.');
851-
} catch (ProcessTimedOutException $ex) {
852-
$this->assertTrue($ex->isIdleTimeout());
853-
$this->assertFalse($ex->isGeneralTimeout());
854-
$this->assertEquals(0.5, $ex->getExceededTimeout());
850+
} catch (ProcessTimedOutException $e) {
851+
$this->assertTrue($e->isIdleTimeout());
852+
$this->assertFalse($e->isGeneralTimeout());
853+
$this->assertEquals(0.1, $e->getExceededTimeout());
855854
}
856855
}
857856

@@ -860,17 +859,23 @@ public function testIdleTimeoutNotExceededWhenOutputIsSent()
860859
if ('\\' === DIRECTORY_SEPARATOR) {
861860
$this->markTestIncomplete('This test fails with a timeout on Windows, can someone investigate please?');
862861
}
863-
$process = $this->getProcess(sprintf('%s -r %s', self::$phpBin, escapeshellarg('$n = 30; while ($n--) {echo "foo\n"; usleep(100000); }')));
864-
$process->setTimeout(2);
865-
$process->setIdleTimeout(1);
862+
$process = $this->getProcess(sprintf('%s -r %s', self::$phpBin, escapeshellarg('while (true) {echo "foo\n"; usleep(10000);}')));
863+
$process->setTimeout(1);
864+
$process->start();
865+
866+
while (false === strpos($process->getOutput(), 'foo')) {
867+
usleep(1000);
868+
}
869+
870+
$process->setIdleTimeout(0.1);
866871

867872
try {
868-
$process->run();
873+
$process->wait();
869874
$this->fail('A timeout exception was expected.');
870875
} catch (ProcessTimedOutException $ex) {
871876
$this->assertTrue($ex->isGeneralTimeout(), 'A general timeout is expected.');
872877
$this->assertFalse($ex->isIdleTimeout(), 'No idle timeout is expected.');
873-
$this->assertEquals(2, $ex->getExceededTimeout());
878+
$this->assertEquals(1, $ex->getExceededTimeout());
874879
}
875880
}
876881

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

886891
try {
887892
$process->run();
888-
$this->fail('A RuntimeException should have been raised.');
889-
} catch (RuntimeException $e) {
893+
$this->fail('A ProcessTimedOutException should have been raised.');
894+
} catch (ProcessTimedOutException $e) {
890895
}
891896
$this->assertFalse($process->isRunning());
892897
$process->start();
898+
$this->assertTrue($process->isRunning());
893899
$process->stop(0);
894900

895901
throw $e;
@@ -1047,7 +1053,7 @@ public function provideWrongSignal()
10471053

10481054
public function testDisableOutputDisablesTheOutput()
10491055
{
1050-
$p = $this->getProcess(self::$phpBin.' -r "usleep(500000);"');
1056+
$p = $this->getProcess('foo');
10511057
$this->assertFalse($p->isOutputDisabled());
10521058
$p->disableOutput();
10531059
$this->assertTrue($p->isOutputDisabled());
@@ -1061,7 +1067,7 @@ public function testDisableOutputDisablesTheOutput()
10611067
*/
10621068
public function testDisableOutputWhileRunningThrowsException()
10631069
{
1064-
$p = $this->getProcess(self::$phpBin.' -r "usleep(500000);"');
1070+
$p = $this->getProcess(self::$phpBin.' -r "sleep(39);"');
10651071
$p->start();
10661072
$p->disableOutput();
10671073
}
@@ -1072,20 +1078,20 @@ public function testDisableOutputWhileRunningThrowsException()
10721078
*/
10731079
public function testEnableOutputWhileRunningThrowsException()
10741080
{
1075-
$p = $this->getProcess(self::$phpBin.' -r "usleep(500000);"');
1081+
$p = $this->getProcess(self::$phpBin.' -r "sleep(40);"');
10761082
$p->disableOutput();
10771083
$p->start();
10781084
$p->enableOutput();
10791085
}
10801086

10811087
public function testEnableOrDisableOutputAfterRunDoesNotThrowException()
10821088
{
1083-
$p = $this->getProcess(self::$phpBin.' -r "usleep(500000);"');
1089+
$p = $this->getProcess('echo foo');
10841090
$p->disableOutput();
1085-
$p->start();
1086-
$p->wait();
1091+
$p->run();
10871092
$p->enableOutput();
10881093
$p->disableOutput();
1094+
$this->assertTrue($p->isOutputDisabled());
10891095
}
10901096

10911097
/**
@@ -1094,7 +1100,7 @@ public function testEnableOrDisableOutputAfterRunDoesNotThrowException()
10941100
*/
10951101
public function testDisableOutputWhileIdleTimeoutIsSet()
10961102
{
1097-
$process = $this->getProcess('sleep 3');
1103+
$process = $this->getProcess('foo');
10981104
$process->setIdleTimeout(1);
10991105
$process->disableOutput();
11001106
}
@@ -1105,24 +1111,24 @@ public function testDisableOutputWhileIdleTimeoutIsSet()
11051111
*/
11061112
public function testSetIdleTimeoutWhileOutputIsDisabled()
11071113
{
1108-
$process = $this->getProcess('sleep 3');
1114+
$process = $this->getProcess('foo');
11091115
$process->disableOutput();
11101116
$process->setIdleTimeout(1);
11111117
}
11121118

11131119
public function testSetNullIdleTimeoutWhileOutputIsDisabled()
11141120
{
1115-
$process = $this->getProcess('sleep 3');
1121+
$process = $this->getProcess('foo');
11161122
$process->disableOutput();
1117-
$process->setIdleTimeout(null);
1123+
$this->assertSame($process, $process->setIdleTimeout(null));
11181124
}
11191125

11201126
/**
11211127
* @dataProvider provideStartMethods
11221128
*/
11231129
public function testStartWithACallbackAndDisabledOutput($startMethod, $exception, $exceptionMessage)
11241130
{
1125-
$p = $this->getProcess(self::$phpBin.' -r "usleep(500000);"');
1131+
$p = $this->getProcess('foo');
11261132
$p->disableOutput();
11271133
$this->setExpectedException($exception, $exceptionMessage);
11281134
if ('mustRun' === $startMethod) {
@@ -1147,7 +1153,7 @@ public function provideStartMethods()
11471153
*/
11481154
public function testGetOutputWhileDisabled($fetchMethod)
11491155
{
1150-
$p = $this->getProcess(self::$phpBin.' -r "usleep(500000);"');
1156+
$p = $this->getProcess(self::$phpBin.' -r "sleep(41);"');
11511157
$p->disableOutput();
11521158
$p->start();
11531159
$p->{$fetchMethod}();
@@ -1162,7 +1168,7 @@ public function provideOutputFetchingMethods()
11621168
array('getIncrementalErrorOutput'),
11631169
);
11641170
}
1165-
1171+
11661172
public function testStopTerminatesProcessCleanly()
11671173
{
11681174
$process = $this->getProcess(self::$phpBin.' -r "echo 123; sleep(42);"');

0 commit comments

Comments
 (0)