ConsumerTest.php 6.4 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 PredisTestCase;
  12. use Predis\Client;
  13. use Predis\Profile;
  14. use Predis\Monitor\Consumer as MonitorConsumer;
  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 the MONITOR command
  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. $monitor = new MonitorConsumer($client);
  34. }
  35. /**
  36. * @group disconnected
  37. * @expectedException Predis\NotSupportedException
  38. * @expectedExceptionMessage Cannot initialize a monitor consumer when using aggregate connections
  39. */
  40. public function testMonitorConsumerDoesNotWorkOnClusters()
  41. {
  42. $cluster = $this->getMock('Predis\Connection\ClusterConnectionInterface');
  43. $client = new Client($cluster);
  44. $monitor = 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\SingleConnectionInterface');
  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. $monitor = new MonitorConsumer($client);
  62. }
  63. /**
  64. * @group disconnected
  65. * @todo We should investigate why disconnect is invoked 2 times in this test,
  66. * but the reason is probably that the GC invokes __destruct() on monitor
  67. * thus calling $client->disconnect() a second time at the end of the test.
  68. */
  69. public function testStoppingConsumerClosesConnection()
  70. {
  71. $connection = $this->getMock('Predis\Connection\SingleConnectionInterface');
  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\SingleConnectionInterface');
  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\SingleConnectionInterface');
  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\SingleConnectionInterface');
  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. $options = array('profile' => REDIS_SERVER_VERSION);
  142. $echoed = array();
  143. $producer = new Client($parameters, $options);
  144. $producer->connect();
  145. $consumer = new Client($parameters, $options);
  146. $consumer->connect();
  147. $monitor = new MonitorConsumer($consumer);
  148. $producer->echo('message1');
  149. $producer->echo('message2');
  150. $producer->echo('QUIT');
  151. foreach ($monitor as $message) {
  152. if ($message->command == 'ECHO') {
  153. $echoed[] = $arguments = trim($message->arguments, '"');
  154. if ($arguments == 'QUIT') {
  155. $monitor->stop();
  156. }
  157. }
  158. }
  159. $this->assertSame(array('message1', 'message2', 'QUIT'), $echoed);
  160. $this->assertFalse($monitor->valid());
  161. $this->assertEquals('PONG', $consumer->ping());
  162. }
  163. }