ConsumerTest.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. <?php
  2. /*
  3. * This file is part of the Predis package.
  4. *
  5. * (c) Daniele Alessandri <suppakilla@gmail.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Predis\Monitor;
  11. use Predis\Client;
  12. use Predis\Monitor\Consumer as MonitorConsumer;
  13. use PredisTestCase;
  14. /**
  15. * @group realm-monitor
  16. */
  17. class ConsumerTest extends PredisTestCase
  18. {
  19. /**
  20. * @group disconnected
  21. * @expectedException \Predis\NotSupportedException
  22. * @expectedExceptionMessage 'MONITOR' is not supported by the current command factory.
  23. */
  24. public function testMonitorConsumerRequireMonitorCommand()
  25. {
  26. $commands = $this->getMock('Predis\Command\FactoryInterface');
  27. $commands->expects($this->once())
  28. ->method('supportsCommand')
  29. ->with('MONITOR')
  30. ->will($this->returnValue(false));
  31. $client = new Client(null, array('commands' => $commands));
  32. new MonitorConsumer($client);
  33. }
  34. /**
  35. * @group disconnected
  36. * @expectedException \Predis\NotSupportedException
  37. * @expectedExceptionMessage Cannot initialize a monitor consumer over aggregate connections.
  38. */
  39. public function testMonitorConsumerDoesNotWorkOnClusters()
  40. {
  41. $cluster = $this->getMock('Predis\Connection\AggregateConnectionInterface');
  42. $client = new Client($cluster);
  43. new MonitorConsumer($client);
  44. }
  45. /**
  46. * @group disconnected
  47. */
  48. public function testConstructorStartsConsumer()
  49. {
  50. $cmdMonitor = $this->getCommandFactory()->createCommand('monitor');
  51. $connection = $this->getMock('Predis\Connection\NodeConnectionInterface');
  52. $client = $this->getMock('Predis\Client', array('createCommand', 'executeCommand'), array($connection));
  53. $client->expects($this->once())
  54. ->method('createCommand')
  55. ->with('MONITOR', array())
  56. ->will($this->returnValue($cmdMonitor));
  57. $client->expects($this->once())
  58. ->method('executeCommand')
  59. ->with($cmdMonitor);
  60. new MonitorConsumer($client);
  61. }
  62. /**
  63. * @group disconnected
  64. *
  65. * @todo Investigate why disconnect() is invoked 2 times in this test, but
  66. * the reason is probably that the GC invokes __destruct() on monitor
  67. * thus calling disconnect() a second time at the end of the test.
  68. */
  69. public function testStoppingConsumerClosesConnection()
  70. {
  71. $connection = $this->getMock('Predis\Connection\NodeConnectionInterface');
  72. $client = $this->getMock('Predis\Client', array('disconnect'), array($connection));
  73. $client->expects($this->exactly(2))->method('disconnect');
  74. $monitor = new MonitorConsumer($client);
  75. $monitor->stop();
  76. }
  77. /**
  78. * @group disconnected
  79. */
  80. public function testGarbageCollectorRunStopsConsumer()
  81. {
  82. $connection = $this->getMock('Predis\Connection\NodeConnectionInterface');
  83. $client = $this->getMock('Predis\Client', array('disconnect'), array($connection));
  84. $client->expects($this->once())->method('disconnect');
  85. $monitor = new MonitorConsumer($client);
  86. unset($monitor);
  87. }
  88. /**
  89. * @group disconnected
  90. */
  91. public function testReadsMessageFromConnectionToRedis24()
  92. {
  93. $message = '1323367530.939137 (db 15) "MONITOR"';
  94. $connection = $this->getMock('Predis\Connection\NodeConnectionInterface');
  95. $connection->expects($this->once())
  96. ->method('read')
  97. ->will($this->returnValue($message));
  98. $client = new Client($connection);
  99. $monitor = new MonitorConsumer($client);
  100. $payload = $monitor->current();
  101. $this->assertSame(1323367530, (int) $payload->timestamp);
  102. $this->assertSame(15, $payload->database);
  103. $this->assertNull($payload->client);
  104. $this->assertSame('MONITOR', $payload->command);
  105. $this->assertNull($payload->arguments);
  106. }
  107. /**
  108. * @group disconnected
  109. */
  110. public function testReadsMessageFromConnectionToRedis26()
  111. {
  112. $message = '1323367530.939137 [15 127.0.0.1:37265] "MONITOR"';
  113. $connection = $this->getMock('Predis\Connection\NodeConnectionInterface');
  114. $connection->expects($this->once())
  115. ->method('read')
  116. ->will($this->returnValue($message));
  117. $client = new Client($connection);
  118. $monitor = new MonitorConsumer($client);
  119. $payload = $monitor->current();
  120. $this->assertSame(1323367530, (int) $payload->timestamp);
  121. $this->assertSame(15, $payload->database);
  122. $this->assertSame('127.0.0.1:37265', $payload->client);
  123. $this->assertSame('MONITOR', $payload->command);
  124. $this->assertNull($payload->arguments);
  125. }
  126. // ******************************************************************** //
  127. // ---- INTEGRATION TESTS --------------------------------------------- //
  128. // ******************************************************************** //
  129. /**
  130. * @group connected
  131. */
  132. public function testMonitorAgainstRedisServer()
  133. {
  134. $parameters = array(
  135. 'host' => REDIS_SERVER_HOST,
  136. 'port' => REDIS_SERVER_PORT,
  137. 'database' => REDIS_SERVER_DBNUM,
  138. // Prevents suite from handing on broken test
  139. 'read_write_timeout' => 2,
  140. );
  141. $echoed = array();
  142. $producer = new Client($parameters);
  143. $producer->connect();
  144. $consumer = new Client($parameters);
  145. $consumer->connect();
  146. $monitor = new MonitorConsumer($consumer);
  147. $producer->echo('message1');
  148. $producer->echo('message2');
  149. $producer->echo('QUIT');
  150. foreach ($monitor as $message) {
  151. if ($message->command == 'ECHO') {
  152. $echoed[] = $arguments = trim($message->arguments, '"');
  153. if ($arguments == 'QUIT') {
  154. $monitor->stop();
  155. }
  156. }
  157. }
  158. $this->assertSame(array('message1', 'message2', 'QUIT'), $echoed);
  159. $this->assertFalse($monitor->valid());
  160. $this->assertEquals('PONG', $consumer->ping());
  161. }
  162. }