1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
|
<?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\Monitor;
use Predis\Client;
use Predis\Monitor\Consumer as MonitorConsumer;
use Predis\Profile;
use PredisTestCase;
/**
* @group realm-monitor
*/
class ConsumerTest extends PredisTestCase
{
/**
* @group disconnected
*/
public function testMonitorConsumerRequireMonitorCommand()
{
$this->expectException('\Predis\NotSupportedException');
$this->expectExceptionMessage('The current profile does not support \'MONITOR\'.');
$profile = $this->createMock('Predis\Profile\ProfileInterface');
$profile->expects($this->once())
->method('supportsCommand')
->with('MONITOR')
->will($this->returnValue(false));
$client = new Client(null, array('profile' => $profile));
new MonitorConsumer($client);
}
/**
* @group disconnected
*/
public function testMonitorConsumerDoesNotWorkOnClusters()
{
$this->expectException('\Predis\NotSupportedException');
$this->expectExceptionMessage('Cannot initialize a monitor consumer over aggregate connections.');
$cluster = $this->createMock('Predis\Connection\Aggregate\ClusterInterface');
$client = new Client($cluster);
new MonitorConsumer($client);
}
/**
* @group disconnected
* @group wip
*/
public function testConstructorStartsConsumer()
{
$cmdMonitor = Profile\Factory::getDefault()->createCommand('monitor');
$connection = $this->createMock('Predis\Connection\NodeConnectionInterface');
$client = $this->getMockBuilder('Predis\Client', array('createCommand', 'executeCommand'))->setMethods([$connection])->getMock();
$client->expects($this->once())
->method('createCommand')
->with('MONITOR', array())
->will($this->returnValue($cmdMonitor));
$client->expects($this->once())
->method('executeCommand')
->with($cmdMonitor);
new MonitorConsumer($client);
}
/**
* @group disconnected
* @group wip
*
* @todo Investigate why disconnect() is invoked 2 times in this test, but
* the reason is probably that the GC invokes __destruct() on monitor
* thus calling disconnect() a second time at the end of the test.
*/
public function testStoppingConsumerClosesConnection()
{
$connection = $this->createMock('Predis\Connection\NodeConnectionInterface');
$client = $this->getMockBuilder('Predis\Client', array('disconnect'))->setMethods([$connection])->getMock();
$client->expects($this->exactly(2))->method('disconnect');
$monitor = new MonitorConsumer($client);
$monitor->stop();
}
/**
* @group disconnected
* @group wip
*/
public function testGarbageCollectorRunStopsConsumer()
{
$connection = $this->createMock('Predis\Connection\NodeConnectionInterface');
$client = $this->getMockBuilder('Predis\Client', array('disconnect'))->setMethods([$connection])->getMock();
$client->expects($this->once())->method('disconnect');
$monitor = new MonitorConsumer($client);
unset($monitor);
}
/**
* @group disconnected
*/
public function testReadsMessageFromConnectionToRedis24()
{
$message = '1323367530.939137 (db 15) "MONITOR"';
$connection = $this->createMock('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);
}
/**
* @group disconnected
*/
public function testReadsMessageFromConnectionToRedis26()
{
$message = '1323367530.939137 [15 127.0.0.1:37265] "MONITOR"';
$connection = $this->createMock('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);
}
// ******************************************************************** //
// ---- INTEGRATION TESTS --------------------------------------------- //
// ******************************************************************** //
/**
* @group connected
*/
public function testMonitorAgainstRedisServer()
{
$parameters = array(
'host' => REDIS_SERVER_HOST,
'port' => REDIS_SERVER_PORT,
'database' => REDIS_SERVER_DBNUM,
// Prevents suite from handing on broken test
'read_write_timeout' => 2,
);
$options = array('profile' => REDIS_SERVER_VERSION);
$echoed = array();
$producer = new Client($parameters, $options);
$producer->connect();
$consumer = new Client($parameters, $options);
$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());
}
}
|