ConsumerTest.php 6.3 KB

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