Skip to content
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
3 changes: 3 additions & 0 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
* Simple interface for executing redis commands
*
* @event message(ModelInterface $model, Client $thisClient)
* @event close()
*/
interface Client extends EventEmitterInterface
{
Expand Down Expand Up @@ -43,6 +44,8 @@ public function end();
/**
* close connection immediately
*
* This will emit the "close" event.
*
* @see self::end() for closing the connection once the client is idle
*/
public function close();
Expand Down
15 changes: 11 additions & 4 deletions src/StreamingClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class StreamingClient extends EventEmitter implements Client
private $serializer;
private $requests = array();
private $ending = false;
private $closed = false;

public function __construct(Stream $stream, ParserInterface $parser = null, SerializerInterface $serializer = null)
{
Expand Down Expand Up @@ -57,11 +58,10 @@ public function __construct(Stream $stream, ParserInterface $parser = null, Seri
}
}
});
$stream->on('close', function () use ($that) {
$that->close();
$that->emit('close');
});

$stream->on('close', array($this, 'close'));
$stream->resume();

$this->stream = $stream;
$this->parser = $parser;
$this->serializer = $serializer;
Expand Down Expand Up @@ -119,10 +119,17 @@ public function end()

public function close()
{
if ($this->closed) {
return;
}

$this->ending = true;
$this->closed = true;

$this->stream->close();

$this->emit('close');

// reject all remaining requests in the queue
while($this->requests) {
$request = array_shift($this->requests);
Expand Down
13 changes: 9 additions & 4 deletions tests/StreamingClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,6 @@ public function testClosedClientRejectsAllNewRequests()

public function testEndingNonBusyClosesClient()
{
$this->markTestIncomplete();

$this->assertFalse($this->client->isBusy());

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

public function testEndingBusyClosesClientWhenNotBusyAnymore()
{
$this->markTestIncomplete();

// count how often the "close" method has been called
$closed = 0;
$this->client->on('close', function() use (&$closed) {
Expand All @@ -150,9 +146,18 @@ public function testEndingBusyClosesClientWhenNotBusyAnymore()
$this->assertEquals(0, $closed);

$this->client->handleMessage(new BulkReply('PONG'));
$promise->then($this->expectCallableOnce('PONG'));
$this->assertEquals(1, $closed);
}

public function testClosingMultipleTimesEmitsOnce()
{
$this->client->on('close', $this->expectCallableOnce());

$this->client->close();
$this->client->close();
}

public function testReceivingUnexpectedMessageThrowsException()
{
$this->setExpectedException('UnderflowException');
Expand Down