Skip to content

Commit

Permalink
Implement PING in PUB/SUB loop abstraction for Redis 3.0.0.
Browse files Browse the repository at this point in the history
  • Loading branch information
nrk committed Jul 17, 2014
1 parent 18f5cea commit f6e97d4
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 1 deletion.
12 changes: 12 additions & 0 deletions lib/Predis/PubSub/AbstractPubSubContext.php
Expand Up @@ -24,6 +24,7 @@ abstract class AbstractPubSubContext implements \Iterator
const PUNSUBSCRIBE = 'punsubscribe';
const MESSAGE = 'message';
const PMESSAGE = 'pmessage';
const PONG = 'pong';

const STATUS_VALID = 1; // 0b0001
const STATUS_SUBSCRIBED = 2; // 0b0010
Expand Down Expand Up @@ -51,6 +52,17 @@ protected function isFlagSet($value)
return ($this->statusFlags & $value) === $value;
}

/**
* PING the server with an optional payload that will be echoed as a
* PONG message in the pub/sub loop.
*
* @param string $payload Optional PING payload.
*/
public function ping($payload = null)
{
$this->writeCommand('PING', array($payload));
}

/**
* Subscribes to the specified channels.
*
Expand Down
6 changes: 6 additions & 0 deletions lib/Predis/PubSub/PubSubContext.php
Expand Up @@ -133,6 +133,12 @@ protected function getValue()
'payload' => $response[3],
);

case self::PONG:
return (object) array(
'kind' => $response[0],
'payload' => $response[1],
);

default:
$message = "Received an unknown message type {$response[0]} inside of a pubsub context";
throw new ClientException($message);
Expand Down
35 changes: 34 additions & 1 deletion tests/Predis/PubSub/PubSubContextTest.php
Expand Up @@ -144,6 +144,39 @@ public function testIsNotValidWhenNotSubscribed()
$this->assertNull($pubsub->next());
}

public function testHandlesPongMessages()
{
$rawmessage = array('pong', '');

$connection = $this->getMock('Predis\Connection\SingleConnectionInterface');
$connection->expects($this->once())->method('read')->will($this->returnValue($rawmessage));

$client = new Client($connection);
$pubsub = new PubSubContext($client, array('subscribe' => 'channel:foo'));

$message = $pubsub->current();
$this->assertSame('pong', $message->kind);
$this->assertSame('', $message->payload);
}

/**
* @group disconnected
*/
public function testHandlesPongMessagesWithPayload()
{
$rawmessage = array('pong', 'foobar');

$connection = $this->getMock('Predis\Connection\SingleConnectionInterface');
$connection->expects($this->once())->method('read')->will($this->returnValue($rawmessage));

$client = new Client($connection);
$pubsub = new PubSubContext($client, array('subscribe' => 'channel:foo'));

$message = $pubsub->current();
$this->assertSame('pong', $message->kind);
$this->assertSame('foobar', $message->payload);
}

/**
* @group disconnected
*/
Expand Down Expand Up @@ -302,6 +335,6 @@ public function testPubSubAgainstRedisServer()

$this->assertSame(array('message1', 'message2', 'QUIT'), $messages);
$this->assertFalse($pubsub->valid());
$this->assertTrue($consumer->ping());
$this->assertEquals('ECHO', $consumer->echo('ECHO'));
}
}

0 comments on commit f6e97d4

Please sign in to comment.