8000 Merge pull request #2 from clue-labs/terminate · SimonFrings/reactphp-shell@d7450f8 · GitHub
[go: up one dir, main page]

Skip to content

Commit d7450f8

Browse files
committed
Merge pull request clue#2 from clue-labs/terminate
Forcefully terminate Process if its Stream closes
2 parents baa7273 + bd2b6cc commit d7450f8

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

src/ProcessLauncher.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,13 @@ public function createDeferredShell($process)
3838

3939
$stream = new CompositeStream($process->stdout, $process->stdin);
4040

41+
// forcefully terminate process when stream closes
42+
$stream->on('close', function () use ($process) {
43+
if ($process->isRunning()) {
44+
$process->terminate(SIGKILL);
45+
}
46+
});
47+
4148
return new DeferredShell($stream);
4249
}
4350
}

tests/ProcessLauncherTest.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php
22

33
use Clue\React\Shell\ProcessLauncher;
4+
use React\Stream\ReadableStream;
45

56
class ProcessLauncherTest extends TestCase
67
{
@@ -25,4 +26,32 @@ public function testProcessWillBeStarted()
2526

2627
$this->assertInstanceOf('Clue\React\Shell\DeferredShell', $shell);
2728
}
29+
30+
public function testClosingStreamTerminatesRunningProcess()
31+
{
32+
$process = $this->getMockBuilder('React\ChildProcess\Process')->disableOriginalConstructor()->getMock();
33+
$process->stdout = new ReadableStream();
34+
$process->stdin = $this->getMock('React\Stream\WritableStreamInterface');
35+
36+
$process->expects($this->once())->method('isRunning')->will($this->returnValue(true));
37+
$process->expects($this->once())->method('terminate')->with($this->equalTo(SIGKILL));
38+
39+
$shell = $this->processLauncher->createDeferredShell($process);
40+
41+
$shell->close();
42+
}
43+
44+
public function testClosingStreamOfNonRunningProcessWillNotTerminate()
45+
{
46+
$process = $this->getMockBuilder('React\ChildProcess\Process')->disableOriginalConstructor()->getMock();
47+
$process->stdout = new ReadableStream();
48+
$process->stdin = $this->getMock('React\Stream\WritableStreamInterface');
49+
50+
$process->expects($this->once())->method('isRunning')->will($this->returnValue(false));
51+
$process->expects($this->never())->method('terminate');
52+
53+
$shell = $this->processLauncher->createDeferredShell($process);
54+
55+
$shell->close();
56+
}
2857
}

0 commit comments

Comments
 (0)
0