mirror of
https://github.com/predis/predis.git
synced 2026-09-19 06:47:39 +00:00
Merge branch 'v0.9/rename-monitorcontext'
This commit is contained in:
@@ -11,7 +11,7 @@
|
||||
|
||||
require 'SharedConfigurations.php';
|
||||
|
||||
// This is a basic example on how to use the Predis\MonitorContext class.
|
||||
// This is a basic example on how to use the Predis\Monitor\Consumer class.
|
||||
// You can use redis-cli to send commands to the same Redis instance your client is
|
||||
// connected to, and then type "ECHO QUIT_MONITOR" in redis-cli when you want to
|
||||
// exit the monitor loop and terminate this script in a graceful way.
|
||||
@@ -25,11 +25,11 @@ $timestamp = new DateTime();
|
||||
foreach (($monitor = $client->monitor()) as $event) {
|
||||
$timestamp->setTimestamp((int) $event->timestamp);
|
||||
|
||||
// If we notice a ECHO command with the message QUIT_MONITOR, we close the
|
||||
// monitor context and then break the loop.
|
||||
// If we notice a ECHO command with the message QUIT_MONITOR, we stop the
|
||||
// monitor consumer and then break the loop.
|
||||
if ($event->command === 'ECHO' && $event->arguments === '"QUIT_MONITOR"') {
|
||||
echo "Exiting the monitor loop...\n";
|
||||
$monitor->closeContext();
|
||||
$monitor->stop();
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -20,7 +20,7 @@ use Predis\Configuration\OptionsInterface;
|
||||
use Predis\Connection\AggregatedConnectionInterface;
|
||||
use Predis\Connection\ConnectionInterface;
|
||||
use Predis\Connection\ConnectionParametersInterface;
|
||||
use Predis\Monitor\MonitorContext;
|
||||
use Predis\Monitor;
|
||||
use Predis\Pipeline\PipelineContext;
|
||||
use Predis\Profile\ServerProfile;
|
||||
use Predis\PubSub;
|
||||
@@ -451,12 +451,12 @@ class Client implements ClientInterface
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new monitor context and returns it.
|
||||
* Creates a new monitor consumer and returns it.
|
||||
*
|
||||
* @return MonitorContext
|
||||
* @return Monitor\Consumer
|
||||
*/
|
||||
public function monitor()
|
||||
{
|
||||
return new MonitorContext($this);
|
||||
return new Monitor\Consumer($this);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,73 +11,80 @@
|
||||
|
||||
namespace Predis\Monitor;
|
||||
|
||||
use Iterator;
|
||||
use Predis\ClientInterface;
|
||||
use Predis\NotSupportedException;
|
||||
use Predis\Connection\AggregatedConnectionInterface;
|
||||
|
||||
/**
|
||||
* Client-side abstraction of a Redis MONITOR context.
|
||||
* Redis MONITOR consumer.
|
||||
*
|
||||
* @author Daniele Alessandri <suppakilla@gmail.com>
|
||||
*/
|
||||
class MonitorContext implements \Iterator
|
||||
class Consumer implements Iterator
|
||||
{
|
||||
private $client;
|
||||
private $isValid;
|
||||
private $valid;
|
||||
private $position;
|
||||
|
||||
/**
|
||||
* @param ClientInterface $client Client instance used by the context.
|
||||
* @param ClientInterface $client Client instance used by the consumer.
|
||||
*/
|
||||
public function __construct(ClientInterface $client)
|
||||
{
|
||||
$this->checkCapabilities($client);
|
||||
$this->client = $client;
|
||||
$this->openContext();
|
||||
|
||||
$this->start();
|
||||
}
|
||||
|
||||
/**
|
||||
* Automatically closes the context when PHP's garbage collector kicks in.
|
||||
* Automatically stops the consumer when PHP's garbage collector kicks in.
|
||||
*/
|
||||
public function __destruct()
|
||||
{
|
||||
$this->closeContext();
|
||||
$this->stop();
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the passed client instance satisfies the required conditions
|
||||
* needed to initialize a monitor context.
|
||||
* needed to initialize a monitor consumer.
|
||||
*
|
||||
* @param ClientInterface $client Client instance used by the context.
|
||||
* @param ClientInterface $client Client instance used by the consumer.
|
||||
*/
|
||||
private function checkCapabilities(ClientInterface $client)
|
||||
{
|
||||
if ($client->getConnection() instanceof AggregatedConnectionInterface) {
|
||||
throw new NotSupportedException('Cannot initialize a monitor context when using aggregated connections');
|
||||
throw new NotSupportedException(
|
||||
'Cannot initialize a monitor consumer when using aggregated connections'
|
||||
);
|
||||
}
|
||||
|
||||
if ($client->getProfile()->supportsCommand('monitor') === false) {
|
||||
throw new NotSupportedException('The current profile does not support the MONITOR command');
|
||||
throw new NotSupportedException(
|
||||
'The current profile does not support the MONITOR command'
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Initializes the context and sends the MONITOR command to the server.
|
||||
* Initializes the consumer and sends the MONITOR command to the server.
|
||||
*/
|
||||
protected function openContext()
|
||||
protected function start()
|
||||
{
|
||||
$this->isValid = true;
|
||||
$this->valid = true;
|
||||
$monitor = $this->client->createCommand('monitor');
|
||||
$this->client->executeCommand($monitor);
|
||||
}
|
||||
|
||||
/**
|
||||
* Closes the context. Internally this is done by disconnecting from server
|
||||
* Stops the consumer. Internally this is done by disconnecting from server
|
||||
* since there is no way to terminate the stream initialized by MONITOR.
|
||||
*/
|
||||
public function closeContext()
|
||||
public function stop()
|
||||
{
|
||||
$this->client->disconnect();
|
||||
$this->isValid = false;
|
||||
$this->valid = false;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -115,13 +122,13 @@ class MonitorContext implements \Iterator
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the the context is still in a valid state to continue.
|
||||
* Checks if the the consumer is still in a valid state to continue.
|
||||
*
|
||||
* @return Boolean
|
||||
*/
|
||||
public function valid()
|
||||
{
|
||||
return $this->isValid;
|
||||
return $this->valid;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -742,12 +742,12 @@ class ClientTest extends StandardTestCase
|
||||
/**
|
||||
* @group disconnected
|
||||
*/
|
||||
public function testMonitorReturnsMonitorContext()
|
||||
public function testMonitorReturnsMonitorConsumer()
|
||||
{
|
||||
$connection = $this->getMock('Predis\Connection\SingleConnectionInterface');
|
||||
$client = new Client($connection);
|
||||
|
||||
$this->assertInstanceOf('Predis\Monitor\MonitorContext', $monitor = $client->monitor());
|
||||
$this->assertInstanceOf('Predis\Monitor\Consumer', $monitor = $client->monitor());
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
+18
-17
@@ -15,18 +15,19 @@ use PHPUnit_Framework_TestCase as StandardTestCase;
|
||||
|
||||
use Predis\Client;
|
||||
use Predis\Profile\ServerProfile;
|
||||
use Predis\Monitor\Consumer as MonitorConsumer;
|
||||
|
||||
/**
|
||||
* @group realm-monitor
|
||||
*/
|
||||
class MonitorContextTest extends StandardTestCase
|
||||
class ConsumerTest extends StandardTestCase
|
||||
{
|
||||
/**
|
||||
* @group disconnected
|
||||
* @expectedException Predis\NotSupportedException
|
||||
* @expectedExceptionMessage The current profile does not support the MONITOR command
|
||||
*/
|
||||
public function testMonitorContextRequireMonitorCommand()
|
||||
public function testMonitorConsumerRequireMonitorCommand()
|
||||
{
|
||||
$profile = $this->getMock('Predis\Profile\ServerProfileInterface');
|
||||
$profile->expects($this->once())
|
||||
@@ -35,26 +36,26 @@ class MonitorContextTest extends StandardTestCase
|
||||
->will($this->returnValue(false));
|
||||
|
||||
$client = new Client(null, array('profile' => $profile));
|
||||
$monitor = new MonitorContext($client);
|
||||
$monitor = new MonitorConsumer($client);
|
||||
}
|
||||
|
||||
/**
|
||||
* @group disconnected
|
||||
* @expectedException Predis\NotSupportedException
|
||||
* @expectedExceptionMessage Cannot initialize a monitor context when using aggregated connections
|
||||
* @expectedExceptionMessage Cannot initialize a monitor consumer when using aggregated connections
|
||||
*/
|
||||
public function testMonitorContextDoesNotWorkOnClusters()
|
||||
public function testMonitorConsumerDoesNotWorkOnClusters()
|
||||
{
|
||||
$cluster = $this->getMock('Predis\Connection\ClusterConnectionInterface');
|
||||
|
||||
$client = new Client($cluster);
|
||||
$monitor = new MonitorContext($client);
|
||||
$monitor = new MonitorConsumer($client);
|
||||
}
|
||||
|
||||
/**
|
||||
* @group disconnected
|
||||
*/
|
||||
public function testConstructorOpensContext()
|
||||
public function testConstructorStartsConsumer()
|
||||
{
|
||||
$cmdMonitor = ServerProfile::getDefault()->createCommand('monitor');
|
||||
|
||||
@@ -69,7 +70,7 @@ class MonitorContextTest extends StandardTestCase
|
||||
->method('executeCommand')
|
||||
->with($cmdMonitor);
|
||||
|
||||
$monitor = new MonitorContext($client);
|
||||
$monitor = new MonitorConsumer($client);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -78,28 +79,28 @@ class MonitorContextTest extends StandardTestCase
|
||||
* but the reason is probably that the GC invokes __destruct() on monitor
|
||||
* thus calling $client->disconnect() a second time at the end of the test.
|
||||
*/
|
||||
public function testClosingContextClosesConnection()
|
||||
public function testStoppingConsumerClosesConnection()
|
||||
{
|
||||
$connection = $this->getMock('Predis\Connection\SingleConnectionInterface');
|
||||
|
||||
$client = $this->getMock('Predis\Client', array('disconnect'), array($connection));
|
||||
$client->expects($this->exactly(2))->method('disconnect');
|
||||
|
||||
$monitor = new MonitorContext($client);
|
||||
$monitor->closeContext();
|
||||
$monitor = new MonitorConsumer($client);
|
||||
$monitor->stop();
|
||||
}
|
||||
|
||||
/**
|
||||
* @group disconnected
|
||||
*/
|
||||
public function testGarbageCollectorRunClosesContext()
|
||||
public function testGarbageCollectorRunStopsConsumer()
|
||||
{
|
||||
$connection = $this->getMock('Predis\Connection\SingleConnectionInterface');
|
||||
|
||||
$client = $this->getMock('Predis\Client', array('disconnect'), array($connection));
|
||||
$client->expects($this->once())->method('disconnect');
|
||||
|
||||
$monitor = new MonitorContext($client);
|
||||
$monitor = new MonitorConsumer($client);
|
||||
unset($monitor);
|
||||
}
|
||||
|
||||
@@ -116,7 +117,7 @@ class MonitorContextTest extends StandardTestCase
|
||||
->will($this->returnValue($message));
|
||||
|
||||
$client = new Client($connection);
|
||||
$monitor = new MonitorContext($client);
|
||||
$monitor = new MonitorConsumer($client);
|
||||
|
||||
$payload = $monitor->current();
|
||||
$this->assertSame(1323367530, (int) $payload->timestamp);
|
||||
@@ -139,7 +140,7 @@ class MonitorContextTest extends StandardTestCase
|
||||
->will($this->returnValue($message));
|
||||
|
||||
$client = new Client($connection);
|
||||
$monitor = new MonitorContext($client);
|
||||
$monitor = new MonitorConsumer($client);
|
||||
|
||||
$payload = $monitor->current();
|
||||
$this->assertSame(1323367530, (int) $payload->timestamp);
|
||||
@@ -175,7 +176,7 @@ class MonitorContextTest extends StandardTestCase
|
||||
$consumer = new Client($parameters, $options);
|
||||
$consumer->connect();
|
||||
|
||||
$monitor = new MonitorContext($consumer);
|
||||
$monitor = new MonitorConsumer($consumer);
|
||||
|
||||
$producer->echo('message1');
|
||||
$producer->echo('message2');
|
||||
@@ -185,7 +186,7 @@ class MonitorContextTest extends StandardTestCase
|
||||
if ($message->command == 'ECHO') {
|
||||
$echoed[] = $arguments = trim($message->arguments, '"');
|
||||
if ($arguments == 'QUIT') {
|
||||
$monitor->closeContext();
|
||||
$monitor->stop();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user