Skip to content

Commit 3bb3f3b

Browse files
minor symfony#17070 [Process] Make tests more deterministic (nicolas-grekas)
This PR was merged into the 2.7 branch. Discussion ---------- [Process] Make tests more deterministic | Q | A | ------------- | --- | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | symfony#16988 | License | MIT | Doc PR | - This makes running the Process tests faster and more deterministic. Commits ------- 0dc9389 [Process] Make tests more deterministic
2 parents 53b0d77 + 0dc9389 commit 3bb3f3b

File tree

2 files changed

+37
-37
lines changed

2 files changed

+37
-37
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: 36 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -828,8 +828,8 @@ 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
}
834834

835835
$this->assertLessThan(15, microtime(true) - $start);
@@ -839,18 +839,18 @@ public function testCheckTimeoutOnStartedProcess()
839839

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

846846
try {
847847
$process->run();
848848

849849
$this->fail('A timeout exception was expected.');
850-
} catch (ProcessTimedOutException $ex) {
851-
$this->assertTrue($ex->isIdleTimeout());
852-
$this->assertFalse($ex->isGeneralTimeout());
853-
$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());
854854
}
855855
}
856856

@@ -859,17 +859,23 @@ public function testIdleTimeoutNotExceededWhenOutputIsSent()
859859
if ('\\' === DIRECTORY_SEPARATOR) {
860860
$this->markTestIncomplete('This test fails with a timeout on Windows, can someone investigate please?');
861861
}
862-
$process = $this->getProcess(sprintf('%s -r %s', self::$phpBin, escapeshellarg('$n = 30; while ($n--) {echo "foo\n"; usleep(100000); }')));
863-
$process->setTimeout(2);
864-
$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);
865871

866872
try {
867-
$process->run();
873+
$process->wait();
868874
$this->fail('A timeout exception was expected.');
869875
} catch (ProcessTimedOutException $ex) {
870876
$this->assertTrue($ex->isGeneralTimeout(), 'A general timeout is expected.');
871877
$this->assertFalse($ex->isIdleTimeout(), 'No idle timeout is expected.');
872-
$this->assertEquals(2, $ex->getExceededTimeout());
878+
$this->assertEquals(1, $ex->getExceededTimeout());
873879
}
874880
}
875881

@@ -884,11 +890,12 @@ public function testStartAfterATimeout()
884890

885891
try {
886892
$process->run();
887-
$this->fail('A RuntimeException should have been raised.');
888-
} catch (RuntimeException $e) {
893+
$this->fail('A ProcessTimedOutException should have been raised.');
894+
} catch (ProcessTimedOutException $e) {
889895
}
890896
$this->assertFalse($process->isRunning());
891897
$process->start();
898+
$this->assertTrue($process->isRunning());
892899
$process->stop(0);
893900

894901
throw $e;
@@ -1046,7 +1053,7 @@ public function provideWrongSignal()
10461053

10471054
public function testDisableOutputDisablesTheOutput()
10481055
{
1049-
$p = $this->getProcess(self::$phpBin.' -r "usleep(500000);"');
1056+
$p = $this->getProcess('foo');
10501057
$this->assertFalse($p->isOutputDisabled());
10511058
$p->disableOutput();
10521059
$this->assertTrue($p->isOutputDisabled());
@@ -1060,7 +1067,7 @@ public function testDisableOutputDisablesTheOutput()
10601067
*/
10611068
public function testDisableOutputWhileRunningThrowsException()
10621069
{
1063-
$p = $this->getProcess(self::$phpBin.' -r "usleep(500000);"');
1070+
$p = $this->getProcess(self::$phpBin.' -r "sleep(39);"');
10641071
$p->start();
10651072
$p->disableOutput();
10661073
}
@@ -1071,20 +1078,20 @@ public function testDisableOutputWhileRunningThrowsException()
10711078
*/
10721079
public function testEnableOutputWhileRunningThrowsException()
10731080
{
1074-
$p = $this->getProcess(self::$phpBin.' -r "usleep(500000);"');
1081+
$p = $this->getProcess(self::$phpBin.' -r "sleep(40);"');
10751082
$p->disableOutput();
10761083
$p->start();
10771084
$p->enableOutput();
10781085
}
10791086

10801087
public function testEnableOrDisableOutputAfterRunDoesNotThrowException()
10811088
{
1082-
$p = $this->getProcess(self::$phpBin.' -r "usleep(500000);"');
1089+
$p = $this->getProcess('echo foo');
10831090
$p->disableOutput();
1084-
$p->start();
1085-
$p->wait();
1091+
$p->run();
10861092
$p->enableOutput();
10871093
$p->disableOutput();
1094+
$this->assertTrue($p->isOutputDisabled());
10881095
}
10891096

10901097
/**
@@ -1093,7 +1100,7 @@ public function testEnableOrDisableOutputAfterRunDoesNotThrowException()
10931100
*/
10941101
public function testDisableOutputWhileIdleTimeoutIsSet()
10951102
{
1096-
$process = $this->getProcess('sleep 3');
1103+
$process = $this->getProcess('foo');
10971104
$process->setIdleTimeout(1);
10981105
$process->disableOutput();
10991106
}
@@ -1104,24 +1111,24 @@ public function testDisableOutputWhileIdleTimeoutIsSet()
11041111
*/
11051112
public function testSetIdleTimeoutWhileOutputIsDisabled()
11061113
{
1107-
$process = $this->getProcess('sleep 3');
1114+
$process = $this->getProcess('foo');
11081115
$process->disableOutput();
11091116
$process->setIdleTimeout(1);
11101117
}
11111118

11121119
public function testSetNullIdleTimeoutWhileOutputIsDisabled()
11131120
{
1114-
$process = $this->getProcess('sleep 3');
1121+
$process = $this->getProcess('foo');
11151122
$process->disableOutput();
1116-
$process->setIdleTimeout(null);
1123+
$this->assertSame($process, $process->setIdleTimeout(null));
11171124
}
11181125

11191126
/**
11201127
* @dataProvider provideStartMethods
11211128
*/
11221129
public function testStartWithACallbackAndDisabledOutput($startMethod, $exception, $exceptionMessage)
11231130
{
1124-
$p = $this->getProcess(self::$phpBin.' -r "usleep(500000);"');
1131+
$p = $this->getProcess('foo');
11251132
$p->disableOutput();
11261133
$this->setExpectedException($exception, $exceptionMessage);
11271134
if ('mustRun' === $startMethod) {
@@ -1146,7 +1153,7 @@ public function provideStartMethods()
11461153
*/
11471154
public function testGetOutputWhileDisabled($fetchMethod)
11481155
{
1149-
$p = $this->getProcess(self::$phpBin.' -r "usleep(500000);"');
1156+
$p = $this->getProcess(self::$phpBin.' -r "sleep(41);"');
11501157
$p->disableOutput();
11511158
$p->start();
11521159
$p->{$fetchMethod}();
@@ -1161,7 +1168,7 @@ public function provideOutputFetchingMethods()
11611168
array('getIncrementalErrorOutput'),
11621169
);
11631170
}
1164-
1171+
11651172
public function testStopTerminatesProcessCleanly()
11661173
{
11671174
$process = $this->getProcess(self::$phpBin.' -r "echo 123; sleep(42);"');

0 commit comments

Comments
 (0)