MonitorContextTest.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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\ServerProfile;
  14. /**
  15. * @group realm-monitor
  16. */
  17. class MonitorContextTest extends PredisTestCase
  18. {
  19. /**
  20. * @group disconnected
  21. * @expectedException Predis\NotSupportedException
  22. * @expectedExceptionMessage The current profile does not support the MONITOR command
  23. */
  24. public function testMonitorContextRequireMonitorCommand()
  25. {
  26. $profile = $this->getMock('Predis\Profile\ServerProfileInterface');
  27. $profile->expects($this->once())
  28. ->method('supportsCommand')
  29. ->with('monitor')
  30. ->will($this->returnValue(false));
  31. $client = new Client(null, array('profile' => $profile));
  32. $monitor = new MonitorContext($client);
  33. }
  34. /**
  35. * @group disconnected
  36. * @expectedException Predis\NotSupportedException
  37. * @expectedExceptionMessage Cannot initialize a monitor context when using aggregated connections
  38. */
  39. public function testMonitorContextDoesNotWorkOnClusters()
  40. {
  41. $cluster = $this->getMock('Predis\Connection\ClusterConnectionInterface');
  42. $client = new Client($cluster);
  43. $monitor = new MonitorContext($client);
  44. }
  45. /**
  46. * @group disconnected
  47. */
  48. public function testConstructorOpensContext()
  49. {
  50. $cmdMonitor = ServerProfile::getDefault()->createCommand('monitor');
  51. $connection = $this->getMock('Predis\Connection\SingleConnectionInterface');
  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. $monitor = new MonitorContext($client);
  61. }
  62. /**
  63. * @group disconnected
  64. * @todo We should investigate why disconnect is invoked 2 times in this test,
  65. * but the reason is probably that the GC invokes __destruct() on monitor
  66. * thus calling $client->disconnect() a second time at the end of the test.
  67. */
  68. public function testClosingContextClosesConnection()
  69. {
  70. $connection = $this->getMock('Predis\Connection\SingleConnectionInterface');
  71. $client = $this->getMock('Predis\Client', array('disconnect'), array($connection));
  72. $client->expects($this->exactly(2))->method('disconnect');
  73. $monitor = new MonitorContext($client);
  74. $monitor->closeContext();
  75. }
  76. /**
  77. * @group disconnected
  78. */
  79. public function testGarbageCollectorRunClosesContext()
  80. {
  81. $connection = $this->getMock('Predis\Connection\SingleConnectionInterface');
  82. $client = $this->getMock('Predis\Client', array('disconnect'), array($connection));
  83. $client->expects($this->once())->method('disconnect');
  84. $monitor = new MonitorContext($client);
  85. unset($monitor);
  86. }
  87. /**
  88. * @group disconnected
  89. */
  90. public function testReadsMessageFromConnectionToRedis24()
  91. {
  92. $message = '1323367530.939137 (db 15) "MONITOR"';
  93. $connection = $this->getMock('Predis\Connection\SingleConnectionInterface');
  94. $connection->expects($this->once())
  95. ->method('read')
  96. ->will($this->returnValue($message));
  97. $client = new Client($connection);
  98. $monitor = new MonitorContext($client);
  99. $payload = $monitor->current();
  100. $this->assertSame(1323367530, (int) $payload->timestamp);
  101. $this->assertSame(15, $payload->database);
  102. $this->assertNull($payload->client);
  103. $this->assertSame('MONITOR', $payload->command);
  104. $this->assertNull($payload->arguments);
  105. }
  106. /**
  107. * @group disconnected
  108. */
  109. public function testReadsMessageFromConnectionToRedis26()
  110. {
  111. $message = '1323367530.939137 [15 127.0.0.1:37265] "MONITOR"';
  112. $connection = $this->getMock('Predis\Connection\SingleConnectionInterface');
  113. $connection->expects($this->once())
  114. ->method('read')
  115. ->will($this->returnValue($message));
  116. $client = new Client($connection);
  117. $monitor = new MonitorContext($client);
  118. $payload = $monitor->current();
  119. $this->assertSame(1323367530, (int) $payload->timestamp);
  120. $this->assertSame(15, $payload->database);
  121. $this->assertSame('127.0.0.1:37265', $payload->client);
  122. $this->assertSame('MONITOR', $payload->command);
  123. $this->assertNull($payload->arguments);
  124. }
  125. // ******************************************************************** //
  126. // ---- INTEGRATION TESTS --------------------------------------------- //
  127. // ******************************************************************** //
  128. /**
  129. * @group connected
  130. */
  131. public function testMonitorAgainstRedisServer()
  132. {
  133. $parameters = array(
  134. 'host' => REDIS_SERVER_HOST,
  135. 'port' => REDIS_SERVER_PORT,
  136. 'database' => REDIS_SERVER_DBNUM,
  137. // Prevents suite from handing on broken test
  138. 'read_write_timeout' => 2,
  139. );
  140. $options = array('profile' => REDIS_SERVER_VERSION);
  141. $echoed = array();
  142. $producer = new Client($parameters, $options);
  143. $producer->connect();
  144. $consumer = new Client($parameters, $options);
  145. $consumer->connect();
  146. $monitor = new MonitorContext($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->closeContext();
  155. }
  156. }
  157. }
  158. $this->assertSame(array('message1', 'message2', 'QUIT'), $echoed);
  159. $this->assertFalse($monitor->valid());
  160. $this->assertTrue($consumer->ping());
  161. }
  162. }