ConsumerTest.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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
  28. ->expects($this->once())
  29. ->method('supportsCommand')
  30. ->with('MONITOR')
  31. ->will($this->returnValue(false));
  32. $client = new Client(null, array('commands' => $commands));
  33. new MonitorConsumer($client);
  34. }
  35. /**
  36. * @group disconnected
  37. * @expectedException \Predis\NotSupportedException
  38. * @expectedExceptionMessage Cannot initialize a monitor consumer over aggregate connections.
  39. */
  40. public function testMonitorConsumerDoesNotWorkOnClusters()
  41. {
  42. $cluster = $this->getMock('Predis\Connection\AggregateConnectionInterface');
  43. $client = new Client($cluster);
  44. new MonitorConsumer($client);
  45. }
  46. /**
  47. * @group disconnected
  48. */
  49. public function testConstructorStartsConsumer()
  50. {
  51. $cmdMonitor = $this->getCommandFactory()->createCommand('monitor');
  52. $connection = $this->getMock('Predis\Connection\NodeConnectionInterface');
  53. $client = $this->getMock('Predis\Client', array('createCommand', 'executeCommand'), array($connection));
  54. $client
  55. ->expects($this->once())
  56. ->method('createCommand')
  57. ->with('MONITOR', array())
  58. ->will($this->returnValue($cmdMonitor));
  59. $client
  60. ->expects($this->once())
  61. ->method('executeCommand')
  62. ->with($cmdMonitor);
  63. new MonitorConsumer($client);
  64. }
  65. /**
  66. * @group disconnected
  67. *
  68. * @todo Investigate why disconnect() is invoked 2 times in this test, but
  69. * the reason is probably that the GC invokes __destruct() on monitor
  70. * thus calling disconnect() a second time at the end of the test.
  71. */
  72. public function testStoppingConsumerClosesConnection()
  73. {
  74. $connection = $this->getMock('Predis\Connection\NodeConnectionInterface');
  75. $client = $this->getMock('Predis\Client', array('disconnect'), array($connection));
  76. $client
  77. ->expects($this->exactly(2))
  78. ->method('disconnect');
  79. $monitor = new MonitorConsumer($client);
  80. $monitor->stop();
  81. }
  82. /**
  83. * @group disconnected
  84. */
  85. public function testGarbageCollectorRunStopsConsumer()
  86. {
  87. $connection = $this->getMock('Predis\Connection\NodeConnectionInterface');
  88. $client = $this->getMock('Predis\Client', array('disconnect'), array($connection));
  89. $client
  90. ->expects($this->once())
  91. ->method('disconnect');
  92. $monitor = new MonitorConsumer($client);
  93. unset($monitor);
  94. }
  95. /**
  96. * @group disconnected
  97. */
  98. public function testReadsMessageFromConnectionToRedis24()
  99. {
  100. $message = '1323367530.939137 (db 15) "MONITOR"';
  101. $connection = $this->getMock('Predis\Connection\NodeConnectionInterface');
  102. $connection
  103. ->expects($this->once())
  104. ->method('read')
  105. ->will($this->returnValue($message));
  106. $client = new Client($connection);
  107. $monitor = new MonitorConsumer($client);
  108. $payload = $monitor->current();
  109. $this->assertSame(1323367530, (int) $payload->timestamp);
  110. $this->assertSame(15, $payload->database);
  111. $this->assertNull($payload->client);
  112. $this->assertSame('MONITOR', $payload->command);
  113. $this->assertNull($payload->arguments);
  114. }
  115. /**
  116. * @group disconnected
  117. */
  118. public function testReadsMessageFromConnectionToRedis26()
  119. {
  120. $message = '1323367530.939137 [15 127.0.0.1:37265] "MONITOR"';
  121. $connection = $this->getMock('Predis\Connection\NodeConnectionInterface');
  122. $connection
  123. ->expects($this->once())
  124. ->method('read')
  125. ->will($this->returnValue($message));
  126. $client = new Client($connection);
  127. $monitor = new MonitorConsumer($client);
  128. $payload = $monitor->current();
  129. $this->assertSame(1323367530, (int) $payload->timestamp);
  130. $this->assertSame(15, $payload->database);
  131. $this->assertSame('127.0.0.1:37265', $payload->client);
  132. $this->assertSame('MONITOR', $payload->command);
  133. $this->assertNull($payload->arguments);
  134. }
  135. // ******************************************************************** //
  136. // ---- INTEGRATION TESTS --------------------------------------------- //
  137. // ******************************************************************** //
  138. /**
  139. * @group connected
  140. */
  141. public function testMonitorAgainstRedisServer()
  142. {
  143. $parameters = array(
  144. 'host' => REDIS_SERVER_HOST,
  145. 'port' => REDIS_SERVER_PORT,
  146. 'database' => REDIS_SERVER_DBNUM,
  147. // Prevents suite from handing on broken test
  148. 'read_write_timeout' => 2,
  149. );
  150. $echoed = array();
  151. $producer = new Client($parameters);
  152. $producer->connect();
  153. $consumer = new Client($parameters);
  154. $consumer->connect();
  155. $monitor = new MonitorConsumer($consumer);
  156. $producer->echo('message1');
  157. $producer->echo('message2');
  158. $producer->echo('QUIT');
  159. foreach ($monitor as $message) {
  160. if ($message->command == 'ECHO') {
  161. $echoed[] = $arguments = trim($message->arguments, '"');
  162. if ($arguments == 'QUIT') {
  163. $monitor->stop();
  164. }
  165. }
  166. }
  167. $this->assertSame(array('message1', 'message2', 'QUIT'), $echoed);
  168. $this->assertFalse($monitor->valid());
  169. $this->assertEquals('PONG', $consumer->ping());
  170. }
  171. }