8000 Merge pull request #88 from clue-labs/fix-close · PaulRotmann/reactphp-stdio@5ec6440 · GitHub
[go: up one dir, main page]

Skip to content

Commit 5ec6440

Browse files
authored
Merge pull request clue#88 from clue-labs/fix-close
Fix closing to emit final close event and clean up all listeners
2 parents 71800d2 + 403bd19 commit 5ec6440

File tree

4 files changed

+26
-7
lines changed

4 files changed

+26
-7
lines changed

src/Readline.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1012,5 +1012,6 @@ public function close()
10121012
$this->input->close();
10131013

10141014
$this->emit('close');
1015+
$this->removeAllListeners();
10151016
}
10161017
}

src/Stdio.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -192,10 +192,11 @@ public function close()
192192
$this->ending = true;
193193
$this->closed = true;
194194

195-
// clear readline output and then close
196-
$this->readline->setInput('')->setPrompt('');
197195
$this->input->close();
198196
$this->output->close();
197+
198+
$this->emit('close');
199+
$this->removeAllListeners();
199200
}
200201

201202
/**

tests/ReadlineTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,16 @@ public function testEndInputWithIncompleteLineOnCtrlD()
286286
$this->input->emit('data', array("hello\x04"));
287287
}
288288

289+
public function testCloseWillEmitCloseEventAndCloseInputStream()
290+
{
291+
$this->input->on('close', $this->expectCallableOnce());
292+
$this->readline->on('close', $this->expectCallableOnce());
293+
294+
$this->readline->close();
295+
296+
$this->assertEquals(array(), $this->readline->listeners('close'));
297+
}
298+
289299
public function testWriteSimpleCharWritesOnce()
290300
{
291301
$this->output->expects($this->once())->method('write')->with($this->equalTo("\r\033[K" . "k"));

tests/StdioTest.php

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,7 @@ public function testWritableWillBeForwardedToOutput()
285285
$this->assertTrue($stdio->isWritable());
286286
}
287287

288-
public function testCloseWillCloseInputAndOutput()
288+
public function testCloseWillEmitCloseEventAndCloseInputAndOutput()
289289
{
290290
$input = $this->getMockBuilder('React\Stream\ReadableStreamInterface')->getMock();
291291
$output = $this->getMockBuilder('React\Stream\WritableStreamInterface')->getMock();
@@ -295,13 +295,17 @@ public function testCloseWillCloseInputAndOutput()
295295

8000
296296
$stdio = new Stdio($this->loop, $input, $output, $readline);
297297

298+
$stdio->on('close', $this->expectCallableOnce());
299+
298300
$input->expects($this->once())->method('close');
299301
$output->expects($this->once())->method('close');
300302

301303
$stdio->close();
304+
305+
$this->assertEquals(array(), $stdio->listeners('close'));
302306
}
303307

304-
public function testCloseTwiceWillCloseInputAndOutputOnlyOnce()
308+
public function testCloseTwiceWillEmitCloseEventAndCloseInputAndOutputOnlyOnce()
305309
{
306310
$input = $this->getMockBuilder('React\Stream\ReadableStreamInterface')->getMock();
307311
$output = $this->getMockBuilder('React\Stream\WritableStreamInterface')->getMock();
@@ -311,20 +315,23 @@ public function testCloseTwiceWillCloseInputAndOutputOnlyOnce()
311315

312316
$stdio = new Stdio($this->loop, $input, $output, $readline);
313317

318+
$stdio->on('close', $this->expectCallableOnce());
319+
314320
$input->expects($this->once())->method('close');
315321
$output->expects($this->once())->method('close');
316322

317323
$stdio->close();
318324
$stdio->close();
319325
}
320326

321-
public function testEndWillCloseInputAndEndOutput()
327+
public function testEndWillClearReadlineAndCloseInputAndEndOutput()
322328
{
323329
$input = $this->getMockBuilder('React\Stream\ReadableStreamInterface')->getMock();
324330
$output = $this->getMockBuilder('React\Stream\WritableStreamInterface')->getMock();
325331

326-
//$readline = $this->getMockBuilder('Clue\React\Stdio\Readline')->disableOriginalConstructor()->getMock();
327-
$readline = new Readline($input, $output);
332+
$readline = $this->getMockBuilder('Clue\React\Stdio\Readline')->disableOriginalConstructor()->getMock();
333+
$readline->expects($this->once())->method('setPrompt')->with('')->willReturnSelf();
334+
$readline->expects($this->once())->method('setInput')->with('')->willReturnSelf();
328335

329336
$stdio = new Stdio($this->loop, $input, $output, $readline);
330337

0 commit comments

Comments
 (0)
0