MonitorContextTest.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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 \PHPUnit_Framework_TestCase as StandardTestCase;
  12. use Predis\Client;
  13. use Predis\Profile\ServerProfile;
  14. /**
  15. * @group realm-monitor
  16. */
  17. class MonitorContextTest extends StandardTestCase
  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 over a cluster of 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 testCurrentReadsMessageFromConnection()
  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->assertSame('MONITOR', $payload->command);
  103. $this->assertNull($payload->arguments);
  104. }
  105. // ******************************************************************** //
  106. // ---- INTEGRATION TESTS --------------------------------------------- //
  107. // ******************************************************************** //
  108. /**
  109. * @group connected
  110. */
  111. public function testMonitorAgainstRedisServer()
  112. {
  113. $parameters = array(
  114. 'host' => REDIS_SERVER_HOST,
  115. 'port' => REDIS_SERVER_PORT,
  116. 'database' => REDIS_SERVER_DBNUM,
  117. // Prevents suite from handing on broken test
  118. 'read_write_timeout' => 2,
  119. );
  120. $echoed = array();
  121. $producer = new Client($parameters, REDIS_SERVER_VERSION);
  122. $producer->connect();
  123. $consumer = new Client($parameters, REDIS_SERVER_VERSION);
  124. $consumer->connect();
  125. $monitor = new MonitorContext($consumer);
  126. $producer->echo('message1');
  127. $producer->echo('message2');
  128. $producer->echo('QUIT');
  129. foreach ($monitor as $message) {
  130. if ($message->command == 'ECHO') {
  131. $echoed[] = $arguments = trim($message->arguments, '"');
  132. if ($arguments == 'QUIT') {
  133. $monitor->closeContext();
  134. }
  135. }
  136. }
  137. $this->assertSame(array('message1', 'message2', 'QUIT'), $echoed);
  138. $this->assertFalse($monitor->valid());
  139. $this->assertTrue($consumer->ping());
  140. }
  141. }