123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209 |
- <?php
- namespace Predis\Monitor;
- use Predis\Client;
- use Predis\Monitor\Consumer as MonitorConsumer;
- use PredisTestCase;
- class ConsumerTest extends PredisTestCase
- {
-
- public function testMonitorConsumerRequireMonitorCommand()
- {
- $commands = $this->getMock('Predis\Command\FactoryInterface');
- $commands
- ->expects($this->once())
- ->method('supportsCommand')
- ->with('MONITOR')
- ->will($this->returnValue(false));
- $client = new Client(null, array('commands' => $commands));
- new MonitorConsumer($client);
- }
-
- public function testMonitorConsumerDoesNotWorkOnClusters()
- {
- $cluster = $this->getMock('Predis\Connection\AggregateConnectionInterface');
- $client = new Client($cluster);
- new MonitorConsumer($client);
- }
-
- public function testConstructorStartsConsumer()
- {
- $cmdMonitor = $this->getCommandFactory()->createCommand('monitor');
- $connection = $this->getMock('Predis\Connection\NodeConnectionInterface');
- $client = $this->getMock('Predis\Client', array('createCommand', 'executeCommand'), array($connection));
- $client
- ->expects($this->once())
- ->method('createCommand')
- ->with('MONITOR', array())
- ->will($this->returnValue($cmdMonitor));
- $client
- ->expects($this->once())
- ->method('executeCommand')
- ->with($cmdMonitor);
- new MonitorConsumer($client);
- }
-
- public function testStoppingConsumerClosesConnection()
- {
- $connection = $this->getMock('Predis\Connection\NodeConnectionInterface');
- $client = $this->getMock('Predis\Client', array('disconnect'), array($connection));
- $client
- ->expects($this->exactly(2))
- ->method('disconnect');
- $monitor = new MonitorConsumer($client);
- $monitor->stop();
- }
-
- public function testGarbageCollectorRunStopsConsumer()
- {
- $connection = $this->getMock('Predis\Connection\NodeConnectionInterface');
- $client = $this->getMock('Predis\Client', array('disconnect'), array($connection));
- $client
- ->expects($this->once())
- ->method('disconnect');
- $monitor = new MonitorConsumer($client);
- unset($monitor);
- }
-
- public function testReadsMessageFromConnectionToRedis24()
- {
- $message = '1323367530.939137 (db 15) "MONITOR"';
- $connection = $this->getMock('Predis\Connection\NodeConnectionInterface');
- $connection
- ->expects($this->once())
- ->method('read')
- ->will($this->returnValue($message));
- $client = new Client($connection);
- $monitor = new MonitorConsumer($client);
- $payload = $monitor->current();
- $this->assertSame(1323367530, (int) $payload->timestamp);
- $this->assertSame(15, $payload->database);
- $this->assertNull($payload->client);
- $this->assertSame('MONITOR', $payload->command);
- $this->assertNull($payload->arguments);
- }
-
- public function testReadsMessageFromConnectionToRedis26()
- {
- $message = '1323367530.939137 [15 127.0.0.1:37265] "MONITOR"';
- $connection = $this->getMock('Predis\Connection\NodeConnectionInterface');
- $connection
- ->expects($this->once())
- ->method('read')
- ->will($this->returnValue($message));
- $client = new Client($connection);
- $monitor = new MonitorConsumer($client);
- $payload = $monitor->current();
- $this->assertSame(1323367530, (int) $payload->timestamp);
- $this->assertSame(15, $payload->database);
- $this->assertSame('127.0.0.1:37265', $payload->client);
- $this->assertSame('MONITOR', $payload->command);
- $this->assertNull($payload->arguments);
- }
-
-
-
-
- public function testMonitorAgainstRedisServer()
- {
- $parameters = array(
- 'host' => REDIS_SERVER_HOST,
- 'port' => REDIS_SERVER_PORT,
- 'database' => REDIS_SERVER_DBNUM,
-
- 'read_write_timeout' => 2,
- );
- $echoed = array();
- $producer = new Client($parameters);
- $producer->connect();
- $consumer = new Client($parameters);
- $consumer->connect();
- $monitor = new MonitorConsumer($consumer);
- $producer->echo('message1');
- $producer->echo('message2');
- $producer->echo('QUIT');
- foreach ($monitor as $message) {
- if ($message->command == 'ECHO') {
- $echoed[] = $arguments = trim($message->arguments, '"');
- if ($arguments == 'QUIT') {
- $monitor->stop();
- }
- }
- }
- $this->assertSame(array('message1', 'message2', 'QUIT'), $echoed);
- $this->assertFalse($monitor->valid());
- $this->assertEquals('PONG', $consumer->ping());
- }
- }
|