ServerSlowlogTest.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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\Command\Redis;
  11. /**
  12. * In order to support the output of SLOWLOG, the backend connection must be
  13. * able to parse nested multibulk responses deeper than 2 levels.
  14. *
  15. * @group commands
  16. * @group realm-server
  17. */
  18. class ServerSlowlogTest extends PredisCommandTestCase
  19. {
  20. /**
  21. * {@inheritdoc}
  22. */
  23. protected function getExpectedCommand()
  24. {
  25. return 'Predis\Command\Redis\ServerSlowlog';
  26. }
  27. /**
  28. * {@inheritdoc}
  29. */
  30. protected function getExpectedId()
  31. {
  32. return 'SLOWLOG';
  33. }
  34. /**
  35. * @group disconnected
  36. */
  37. public function testFilterArguments()
  38. {
  39. $arguments = array('GET', '2');
  40. $expected = array('GET', '2');
  41. $command = $this->getCommand();
  42. $command->setArguments($arguments);
  43. $this->assertSame($expected, $command->getArguments());
  44. }
  45. /**
  46. * @group disconnected
  47. */
  48. public function testParseResponse()
  49. {
  50. $raw = array(array(0, 1323163469, 12451, array('SORT', 'list:unordered')));
  51. $expected = array(
  52. array(
  53. 'id' => 0,
  54. 'timestamp' => 1323163469,
  55. 'duration' => 12451,
  56. 'command' => array('SORT', 'list:unordered'),
  57. ),
  58. );
  59. $command = $this->getCommand();
  60. $this->assertSame($expected, $command->parseResponse($raw));
  61. }
  62. /**
  63. * @group connected
  64. * @requiresRedisVersion >= 2.2.12
  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. * @requiresRedisVersion >= 2.2.12
  85. */
  86. public function testCanResetTheLog()
  87. {
  88. $redis = $this->getClient();
  89. $this->assertEquals('OK', $redis->slowlog('RESET'));
  90. }
  91. /**
  92. * @group connected
  93. * @requiresRedisVersion >= 2.2.12
  94. * @expectedException \Predis\Response\ServerException
  95. */
  96. public function testThrowsExceptionOnInvalidSubcommand()
  97. {
  98. $redis = $this->getClient();
  99. $redis->slowlog('INVALID');
  100. }
  101. }