ServerSlowlogTest.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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;
  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\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. */
  65. public function testReturnsAnArrayOfLoggedCommands()
  66. {
  67. $redis = $this->getClient();
  68. $config = $redis->config('get', 'slowlog-log-slower-than');
  69. $threshold = array_pop($config);
  70. $redis->config('set', 'slowlog-log-slower-than', 0);
  71. $redis->set('foo', 'bar');
  72. $this->assertInternalType('array', $slowlog = $redis->slowlog('GET'));
  73. $this->assertGreaterThan(0, count($slowlog));
  74. $this->assertInternalType('array', $slowlog[0]);
  75. $this->assertGreaterThan(0, $slowlog[0]['id']);
  76. $this->assertGreaterThan(0, $slowlog[0]['timestamp']);
  77. $this->assertGreaterThan(0, $slowlog[0]['duration']);
  78. $this->assertInternalType('array', $slowlog[0]['command']);
  79. $redis->config('set', 'slowlog-log-slower-than', $threshold);
  80. }
  81. /**
  82. * @group connected
  83. */
  84. public function testCanResetTheLog()
  85. {
  86. $redis = $this->getClient();
  87. $this->assertEquals('OK', $redis->slowlog('RESET'));
  88. }
  89. /**
  90. * @group connected
  91. * @expectedException \Predis\Response\ServerException
  92. */
  93. public function testThrowsExceptionOnInvalidSubcommand()
  94. {
  95. $redis = $this->getClient();
  96. $redis->slowlog('INVALID');
  97. }
  98. }