ServerSlowlogTest.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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\Commands;
  11. use \PHPUnit_Framework_TestCase as StandardTestCase;
  12. /**
  13. * In order to support the output of SLOWLOG, the backend connection
  14. * must be able to parse nested multibulk replies deeper than 2 levels.
  15. *
  16. * @group commands
  17. * @group realm-server
  18. */
  19. class ServerSlowlogTest extends CommandTestCase
  20. {
  21. /**
  22. * {@inheritdoc}
  23. */
  24. protected function getExpectedCommand()
  25. {
  26. return 'Predis\Commands\ServerSlowlog';
  27. }
  28. /**
  29. * {@inheritdoc}
  30. */
  31. protected function getExpectedId()
  32. {
  33. return 'SLOWLOG';
  34. }
  35. /**
  36. * @group disconnected
  37. */
  38. public function testFilterArguments()
  39. {
  40. $arguments = array('GET', '2');
  41. $expected = array('GET', '2');
  42. $command = $this->getCommand();
  43. $command->setArguments($arguments);
  44. $this->assertSame($expected, $command->getArguments());
  45. }
  46. /**
  47. * @group disconnected
  48. */
  49. public function testParseResponse()
  50. {
  51. $raw = array(array(0, 1323163469, 12451, array('SORT', 'list:unordered')));
  52. $expected = array(
  53. array(
  54. 'id' => 0,
  55. 'timestamp' => 1323163469,
  56. 'duration' => 12451,
  57. 'command' => array('SORT', 'list:unordered'),
  58. ),
  59. );
  60. $command = $this->getCommand();
  61. $this->assertSame($expected, $command->parseResponse($raw));
  62. }
  63. /**
  64. * @group connected
  65. */
  66. public function testReturnsAnArrayOfLoggedCommands()
  67. {
  68. $redis = $this->getClient();
  69. $config = $redis->config('get', 'slowlog-log-slower-than');
  70. $threshold = array_pop($config);
  71. $redis->config('set', 'slowlog-log-slower-than', 0);
  72. $redis->set('foo', 'bar');
  73. $this->assertInternalType('array', $slowlog = $redis->slowlog('GET'));
  74. $this->assertGreaterThan(0, count($slowlog));
  75. $this->assertInternalType('array', $slowlog[0]);
  76. $this->assertGreaterThan(0, $slowlog[0]['id']);
  77. $this->assertGreaterThan(0, $slowlog[0]['timestamp']);
  78. $this->assertGreaterThan(0, $slowlog[0]['duration']);
  79. $this->assertInternalType('array', $slowlog[0]['command']);
  80. $redis->config('set', 'slowlog-log-slower-than', $threshold);
  81. }
  82. /**
  83. * @group connected
  84. */
  85. public function testCanResetTheLog()
  86. {
  87. $redis = $this->getClient();
  88. $this->assertTrue($redis->slowlog('RESET'));
  89. }
  90. /**
  91. * @group connected
  92. * @expectedException Predis\ServerException
  93. */
  94. public function testThrowsExceptionOnInvalidSubcommand()
  95. {
  96. $redis = $this->getClient();
  97. $redis->slowlog('INVALID');
  98. }
  99. }