Files
predis/lib/Predis/PubSub/PubSubContext.php
T
Daniele Alessandri 371ac91777 [tests] Rewrite the whole test suite to allow more granular testing.
Fix also a few bugs found while rewriting the test suite.

In order to be able to run integration tests, the test suite requires
a version of Redis >= 2.4.0.

The units have been splitted into several different groups using
PHPUnit @group annotation to allow developers to enable, disable
and combine certain types of tests. The available groups are:

  - disconnected: can run without a Redis server online
  - connected: active connection to a Redis server is required
  - commands: test dedicated to a specific Redis command
  - slow: performs operations that can slow down execution;

A list of the available groups can be obtained by running

  phpunit --list-groups

Groups of tests can be disabled or enabled via the XML configuration
file or the standard command-line test runner. Please note that due
to a bug in PHPUnit, older versions ignore the --group option when
the group is excluded in the XML configuration file. Please refer to
http://github.com/sebastianbergmann/phpunit/issues/320 for details

Integration tests in the @connected group check if the command being
tested is defined in the selected server profile (see the value of
the TEST_SERVER_VERSION constant in phpunit.xml). If the command is
not defined in the target server profile, the integration test is
automatically marked as skipped.

We also provide an helper script in the bin directory that can be
used to automatically generate a file with the scheleton of a test
case for a Redis command by specifying the name of the class in the
Predis\Commands namespace. For example, to generate a test case for
SET (represented by the Predis\Commands\StringSet class):

  ./bin/generate-command-test.php --class=StringSet

The realm of a command is automatically inferred from the name of the
class, but it can be set using the --realm option.
2011-12-10 15:26:00 +01:00

291 lines
7.8 KiB
PHP

<?php
/*
* This file is part of the Predis package.
*
* (c) Daniele Alessandri <suppakilla@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Predis\PubSub;
use Predis\Client;
use Predis\Helpers;
use Predis\ClientException;
use Predis\NotSupportedException;
/**
* Client-side abstraction of a Publish / Subscribe context.
*
* @author Daniele Alessandri <suppakilla@gmail.com>
*/
class PubSubContext implements \Iterator
{
const SUBSCRIBE = 'subscribe';
const UNSUBSCRIBE = 'unsubscribe';
const PSUBSCRIBE = 'psubscribe';
const PUNSUBSCRIBE = 'punsubscribe';
const MESSAGE = 'message';
const PMESSAGE = 'pmessage';
const STATUS_VALID = 0x0001;
const STATUS_SUBSCRIBED = 0x0010;
const STATUS_PSUBSCRIBED = 0x0100;
private $client;
private $position;
private $options;
/**
* @param Client Client instance used by the context.
* @param array Options for the context initialization.
*/
public function __construct(Client $client, Array $options = null)
{
$this->checkCapabilities($client);
$this->options = $options ?: array();
$this->client = $client;
$this->statusFlags = self::STATUS_VALID;
$this->genericSubscribeInit('subscribe');
$this->genericSubscribeInit('psubscribe');
}
/**
* Automatically closes the context when PHP's garbage collector kicks in.
*/
public function __destruct()
{
$this->closeContext(true);
}
/**
* Checks if the passed client instance satisfies the required conditions
* needed to initialize a Publish / Subscribe context.
*
* @param Client Client instance used by the context.
*/
private function checkCapabilities(Client $client)
{
if (Helpers::isCluster($client->getConnection())) {
throw new NotSupportedException('Cannot initialize a PUB/SUB context over a cluster of connections');
}
$commands = array('publish', 'subscribe', 'unsubscribe', 'psubscribe', 'punsubscribe');
if ($client->getProfile()->supportsCommands($commands) === false) {
throw new NotSupportedException('The current profile does not support PUB/SUB related commands');
}
}
/**
* This method shares the logic to handle both SUBSCRIBE and PSUBSCRIBE.
*
* @param string $subscribeAction Type of subscription.
*/
private function genericSubscribeInit($subscribeAction)
{
if (isset($this->options[$subscribeAction])) {
$this->$subscribeAction($this->options[$subscribeAction]);
}
}
/**
* Checks if the specified flag is valid in the state of the context.
*
* @param int $value Flag.
* @return Boolean
*/
private function isFlagSet($value)
{
return ($this->statusFlags & $value) === $value;
}
/**
* Subscribes to the specified channels.
*
* @param mixed $arg,... One or more channel names.
*/
public function subscribe(/* arguments */)
{
$this->writeCommand(self::SUBSCRIBE, func_get_args());
$this->statusFlags |= self::STATUS_SUBSCRIBED;
}
/**
* Unsubscribes from the specified channels.
*
* @param mixed $arg,... One or more channel names.
*/
public function unsubscribe(/* arguments */)
{
$this->writeCommand(self::UNSUBSCRIBE, func_get_args());
}
/**
* Subscribes to the specified channels using a pattern.
*
* @param mixed $arg,... One or more channel name patterns.
*/
public function psubscribe(/* arguments */)
{
$this->writeCommand(self::PSUBSCRIBE, func_get_args());
$this->statusFlags |= self::STATUS_PSUBSCRIBED;
}
/**
* Unsubscribes from the specified channels using a pattern.
*
* @param mixed $arg,... One or more channel name patterns.
*/
public function punsubscribe(/* arguments */)
{
$this->writeCommand(self::PUNSUBSCRIBE, func_get_args());
}
/**
* Closes the context by unsubscribing from all the subscribed channels.
* Optionally, the context can be forcefully closed by dropping the
* underlying connection.
*
* @param Boolean $force Forcefully close the context by closing the connection.
* @return Boolean Returns false if there are no pending messages.
*/
public function closeContext($force = false)
{
if (!$this->valid()) {
return false;
}
if ($force) {
$this->invalidate();
$this->client->disconnect();
}
else {
if ($this->isFlagSet(self::STATUS_SUBSCRIBED)) {
$this->unsubscribe();
}
if ($this->isFlagSet(self::STATUS_PSUBSCRIBED)) {
$this->punsubscribe();
}
}
return !$force;
}
/**
* Writes a Redis command on the underlying connection.
*
* @param string $method ID of the command.
* @param array $arguments List of arguments.
*/
private function writeCommand($method, $arguments)
{
$arguments = Helpers::filterArrayArguments($arguments);
$command = $this->client->createCommand($method, $arguments);
$this->client->getConnection()->writeCommand($command);
}
/**
* {@inheritdoc}
*/
public function rewind()
{
// NOOP
}
/**
* Returns the last message payload retrieved from the server and generated
* by one of the active subscriptions.
*
* @return array
*/
public function current()
{
return $this->getValue();
}
/**
* {@inheritdoc}
*/
public function key()
{
return $this->position;
}
/**
* {@inheritdoc}
*/
public function next()
{
if ($this->valid()) {
$this->position++;
}
return $this->position;
}
/**
* Checks if the the context is still in a valid state to continue.
*
* @return Boolean
*/
public function valid()
{
$isValid = $this->isFlagSet(self::STATUS_VALID);
$subscriptionFlags = self::STATUS_SUBSCRIBED | self::STATUS_PSUBSCRIBED;
$hasSubscriptions = ($this->statusFlags & $subscriptionFlags) > 0;
return $isValid && $hasSubscriptions;
}
/**
* Resets the state of the context.
*/
private function invalidate()
{
$this->statusFlags = 0x0000;
}
/**
* Waits for a new message from the server generated by one of the active
* subscriptions and returns it when available.
*
* @return array
*/
private function getValue()
{
$response = $this->client->getConnection()->read();
switch ($response[0]) {
case self::SUBSCRIBE:
case self::UNSUBSCRIBE:
case self::PSUBSCRIBE:
case self::PUNSUBSCRIBE:
if ($response[2] === 0) {
$this->invalidate();
}
case self::MESSAGE:
return (object) array(
'kind' => $response[0],
'channel' => $response[1],
'payload' => $response[2],
);
case self::PMESSAGE:
return (object) array(
'kind' => $response[0],
'pattern' => $response[1],
'channel' => $response[2],
'payload' => $response[3],
);
default:
$message = "Received an unknown message type {$response[0]} inside of a pubsub context";
throw new ClientException($message);
}
}
}