MonitorContextTest.php 6.2 KB

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