Skip to content

Commit c83a0dd

Browse files
committed
Merge pull request clue#20 from clue/close
Emit "close" event once when invoking close()
2 parents 8052864 + 62b4c75 commit c83a0dd

File tree

3 files changed

+23
-8
lines changed

3 files changed

+23
-8
lines changed

src/Client.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
* Simple interface for executing redis commands
1111
*
1212
* @event message(ModelInterface $model, Client $thisClient)
13+
* @event close()
1314
*/
1415
interface Client extends EventEmitterInterface
1516
{
@@ -43,6 +44,8 @@ public function end();
4344
/**
4445
* close connection immediately
4546
*
47+
* This will emit the "close" event.
48+
*
4649
* @see self::end() for closing the connection once the client is idle
4750
*/
4851
public function close();

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)