Skip to content

Commit 70d9b08

Browse files
committed
Emit "close" event once when invoking close()
1 parent 8052864 commit 70d9b08

File tree

2 files changed

+20
-8
lines changed

2 files changed

+20
-8
lines changed

src/StreamingClient.php

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ class StreamingClient extends EventEmitter implements Client
2222
private $serializer;
2323
private $requests = array();
2424
private $ending = false;
25+
private $closed = false;
2526

2627
public function __construct(Stream $stream, ParserInterface $parser = null, SerializerInterface $serializer = null)
2728
{
@@ -57,11 +58,10 @@ public function __construct(Stream $stream, ParserInterface $parser = null, Seri
5758
}
5859
}
5960
});
60-
$stream->on('close', function () use ($that) {
61-
$that->close();
62-
$that->emit('close');
63-
});
61+
62+
$stream->on('close', array($this, 'close'));
6463
$stream->resume();
64+
6565
$this->stream = $stream;
6666
$this->parser = $parser;
6767
$this->serializer = $serializer;
@@ -119,10 +119,17 @@ public function end()
119119

120120
public function close()
121121
{
122+
if ($this->closed) {
123+
return;
124+
}
125+
122126
$this->ending = true;
127+
$this->closed = true;
123128

124129
$this->stream->close();
125130

131+
$this->emit('close');
132+
126133
// reject all remaining requests in the queue
127134
while($this->requests) {
128135
$request = array_shift($this->requests);

tests/StreamingClientTest.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -125,8 +125,6 @@ public function testClosedClientRejectsAllNewRequests()
125125

126126
public function testEndingNonBusyClosesClient()
127127
{
128-
$this->markTestIncomplete();
129-
130128
$this->assertFalse($this->client->isBusy());
131129

132130
$this->client->on('close', $this->expectCallableOnce());
@@ -135,8 +133,6 @@ public function testEndingNonBusyClosesClient()
135133

136134
public function testEndingBusyClosesClientWhenNotBusyAnymore()
137135
{
138-
$this->markTestIncomplete();
139-
140136
// count how often the "close" method has been called
141137
$closed = 0;
142138
$this->client->on('close', function() use (&$closed) {
@@ -150,9 +146,18 @@ public function testEndingBusyClosesClientWhenNotBusyAnymore()
150146
$this->assertEquals(0, $closed);
151147

152148
$this->client->handleMessage(new BulkReply('PONG'));
149+
$promise->then($this->expectCallableOnce('PONG'));
153150
$this->assertEquals(1, $closed);
154151
}
155152

153+
public function testClosingMultipleTimesEmitsOnce()
154+
{
155+
$this->client->on('close', $this->expectCallableOnce());
156+
157+
$this->client->close();
158+
$this->client->close();
159+
}
160+
156161
public function testReceivingUnexpectedMessageThrowsException()
157162
{
158163
$this->setExpectedException('UnderflowException');

0 commit comments

Comments
 (0)