Skip to content
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

Fix closing to emit final close event and clean up all listeners #88

Merged
merged 1 commit into from
Aug 19, 2019
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
1 change: 1 addition & 0 deletions src/Readline.php
Original file line number Diff line number Diff line change
Expand Up @@ -1012,5 +1012,6 @@ public function close()
$this->input->close();

$this->emit('close');
$this->removeAllListeners();
}
}
5 changes: 3 additions & 2 deletions src/Stdio.php
Original file line number Diff line number Diff line change
Expand Up @@ -192,10 +192,11 @@ public function close()
$this->ending = true;
$this->closed = true;

// clear readline output and then close
$this->readline->setInput('')->setPrompt('');
$this->input->close();
$this->output->close();

$this->emit('close');
$this->removeAllListeners();
}

/**
Expand Down
10 changes: 10 additions & 0 deletions tests/ReadlineTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,16 @@ public function testEndInputWithIncompleteLineOnCtrlD()
$this->input->emit('data', array("hello\x04"));
}

public function testCloseWillEmitCloseEventAndCloseInputStream()
{
$this->input->on('close', $this->expectCallableOnce());
$this->readline->on('close', $this->expectCallableOnce());

$this->readline->close();

$this->assertEquals(array(), $this->readline->listeners('close'));
}

public function testWriteSimpleCharWritesOnce()
{
$this->output->expects($this->once())->method('write')->with($this->equalTo("\r\033[K" . "k"));
Expand Down
17 changes: 12 additions & 5 deletions tests/StdioTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ public function testWritableWillBeForwardedToOutput()
$this->assertTrue($stdio->isWritable());
}

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

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

$stdio->on('close', $this->expectCallableOnce());

$input->expects($this->once())->method('close');
$output->expects($this->once())->method('close');

$stdio->close();

$this->assertEquals(array(), $stdio->listeners('close'));
}

public function testCloseTwiceWillCloseInputAndOutputOnlyOnce()
public function testCloseTwiceWillEmitCloseEventAndCloseInputAndOutputOnlyOnce()
{
$input = $this->getMockBuilder('React\Stream\ReadableStreamInterface')->getMock();
$output = $this->getMockBuilder('React\Stream\WritableStreamInterface')->getMock();
Expand All @@ -311,20 +315,23 @@ public function testCloseTwiceWillCloseInputAndOutputOnlyOnce()

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

$stdio->on('close', $this->expectCallableOnce());

$input->expects($this->once())->method('close');
$output->expects($this->once())->method('close');

$stdio->close();
$stdio->close();
}

public function testEndWillCloseInputAndEndOutput()
public function testEndWillClearReadlineAndCloseInputAndEndOutput()
{
$input = $this->getMockBuilder('React\Stream\ReadableStreamInterface')->getMock();
$output = $this->getMockBuilder('React\Stream\WritableStreamInterface')->getMock();

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

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

Expand Down