Added support for FUNCTION KILL, FUNCTION LIST, FUNCTION STATS commands (#1334)

* Added support for FUNCTION KILL, FUNCTION LIST, FUNCTION STATS commands

* Marked tests as relay-incompatible
This commit is contained in:
Vladyslav Vildanov
2023-07-12 09:58:00 +03:00
committed by GitHub
parent 0b6ab4dea4
commit 7c2e8e01d8
8 changed files with 354 additions and 2 deletions
@@ -15,11 +15,14 @@ namespace Predis\Command\Redis\Container;
use Predis\Response\Status;
/**
* @method string load(string $functionCode, bool $replace = 'false')
* @method Status delete(string $libraryName)
* @method string dump()
* @method Status flush(?string $mode = null)
* @method Status restore(string $value, ?string $policy = null)
* @method Status kill()
* @method array list(string $libraryNamePattern = null, bool $withCode = false)
* @method string load(string $functionCode, bool $replace = 'false')
* @method Status restore(string $value, string $policy = null)
* @method array stats()
*/
class FunctionContainer extends AbstractContainer
{
@@ -0,0 +1,26 @@
<?php
/*
* This file is part of the Predis package.
*
* (c) 2009-2020 Daniele Alessandri
* (c) 2021-2023 Till Krüss
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Predis\Command\Strategy\ContainerCommands\Functions;
use Predis\Command\Strategy\SubcommandStrategyInterface;
class KillStrategy implements SubcommandStrategyInterface
{
/**
* {@inheritDoc}
*/
public function processArguments(array $arguments): array
{
return $arguments;
}
}
@@ -0,0 +1,36 @@
<?php
/*
* This file is part of the Predis package.
*
* (c) 2009-2020 Daniele Alessandri
* (c) 2021-2023 Till Krüss
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Predis\Command\Strategy\ContainerCommands\Functions;
use Predis\Command\Strategy\SubcommandStrategyInterface;
class ListStrategy implements SubcommandStrategyInterface
{
/**
* {@inheritDoc}
*/
public function processArguments(array $arguments): array
{
$processedArguments = [$arguments[0]];
if (array_key_exists(1, $arguments) && null !== $arguments[1]) {
array_push($processedArguments, 'LIBRARYNAME', $arguments[1]);
}
if (array_key_exists(2, $arguments) && true === $arguments[2]) {
$processedArguments[] = 'WITHCODE';
}
return $processedArguments;
}
}
@@ -0,0 +1,26 @@
<?php
/*
* This file is part of the Predis package.
*
* (c) 2009-2020 Daniele Alessandri
* (c) 2021-2023 Till Krüss
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Predis\Command\Strategy\ContainerCommands\Functions;
use Predis\Command\Strategy\SubcommandStrategyInterface;
class StatsStrategy implements SubcommandStrategyInterface
{
/**
* {@inheritDoc}
*/
public function processArguments(array $arguments): array
{
return $arguments;
}
}
@@ -84,6 +84,34 @@ class FUNCTIONS_Test extends PredisCommandTestCase
$this->assertSameValues($expected, $command->getArguments());
}
/**
* @group disconnected
*/
public function testKillFilterArguments(): void
{
$arguments = ['KILL'];
$expected = ['KILL'];
$command = $this->getCommand();
$command->setArguments($arguments);
$this->assertSameValues($expected, $command->getArguments());
}
/**
* @group disconnected
*/
public function testStatsFilterArguments(): void
{
$arguments = ['STATS'];
$expected = ['STATS'];
$command = $this->getCommand();
$command->setArguments($arguments);
$this->assertSameValues($expected, $command->getArguments());
}
/**
* @dataProvider flushArgumentsProvider
* @group disconnected
@@ -108,6 +136,18 @@ class FUNCTIONS_Test extends PredisCommandTestCase
$this->assertSameValues($expectedResponse, $command->getArguments());
}
/**
* @dataProvider listArgumentsProvider
* @group disconnected
*/
public function testListFilterArguments(array $actualArguments, array $expectedResponse): void
{
$command = $this->getCommand();
$command->setArguments($actualArguments);
$this->assertSameValues($expectedResponse, $command->getArguments());
}
/**
* @group disconnected
*/
@@ -267,6 +307,53 @@ class FUNCTIONS_Test extends PredisCommandTestCase
$this->assertEquals('OK', $redis->function->restore($serializedPayload));
}
/**
* @group connected
* @group relay-incompatible
* @return void
* @requiresRedisVersion >= 7.0.0
*/
public function testListReturnsListOfAvailableFunctions(): void
{
$redis = $this->getClient();
$redis->function->flush();
$expectedResponse = [
[
'library_name', 'mylib', 'engine', 'LUA', 'functions',
[
['name', 'myfunc', 'description', null, 'flags', []],
],
],
];
$libName = $redis->function->load(
"#!lua name={$this->libName} \n redis.register_function('myfunc', function(keys, args) return args[1] end)"
);
$this->assertEquals($this->libName, $libName);
$this->assertSame($expectedResponse, $redis->function->list());
}
/**
* @group connected
* @group relay-incompatible
* @return void
* @requiresRedisVersion >= 7.0.0
*/
public function testStatsReturnsInformationAboutRunningScript(): void
{
$redis = $this->getClient();
$redis->function->flush();
$expectedResponse = ['running_script', null, 'engines', ['LUA', ['libraries_count', 1, 'functions_count', 1]]];
$libName = $redis->function->load(
"#!lua name={$this->libName} \n redis.register_function('myfunc', function(keys, args) return args[1] end)"
);
$this->assertEquals($this->libName, $libName);
$this->assertSame($expectedResponse, $redis->function->stats());
}
/**
* @group connected
* @return void
@@ -283,6 +370,22 @@ class FUNCTIONS_Test extends PredisCommandTestCase
$redis->function->delete($this->libName);
}
/**
* @group connected
* @return void
* @requiresRedisVersion >= 7.0.0
*/
public function testKillThrowsExceptionOnNonExistingRunningScript(): void
{
$redis = $this->getClient();
$redis->function->flush();
$this->expectException(ServerException::class);
$this->expectExceptionMessage('NOTBUSY No scripts in execution right now.');
$redis->function->kill();
}
public function flushArgumentsProvider(): array
{
return [
@@ -310,4 +413,26 @@ class FUNCTIONS_Test extends PredisCommandTestCase
],
];
}
public function listArgumentsProvider(): array
{
return [
'with default arguments' => [
['LIST', null, false],
['LIST'],
],
'with LIBRARYNAME modifier' => [
['LIST', 'libraryname', false],
['LIST', 'LIBRARYNAME', 'libraryname'],
],
'with WITHCODE modifier' => [
['LIST', null, true],
['LIST', 'WITHCODE'],
],
'with all arguments' => [
['LIST', 'libraryname', true],
['LIST', 'LIBRARYNAME', 'libraryname', 'WITHCODE'],
],
];
}
}
@@ -0,0 +1,37 @@
<?php
/*
* This file is part of the Predis package.
*
* (c) 2009-2020 Daniele Alessandri
* (c) 2021-2023 Till Krüss
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Predis\Command\Strategy\ContainerCommands\Functions;
use PredisTestCase;
class KillStrategyTest extends PredisTestCase
{
/**
* @var KillStrategy
*/
private $strategy;
protected function setUp(): void
{
$this->strategy = new KillStrategy();
}
/**
* @group disconnected
* @return void
*/
public function testProcessArguments(): void
{
$this->assertSame(['arg1', 'arg2'], $this->strategy->processArguments(['arg1', 'arg2']));
}
}
@@ -0,0 +1,62 @@
<?php
/*
* This file is part of the Predis package.
*
* (c) 2009-2020 Daniele Alessandri
* (c) 2021-2023 Till Krüss
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Predis\Command\Strategy\ContainerCommands\Functions;
use PredisTestCase;
class ListStrategyTest extends PredisTestCase
{
/**
* @var ListStrategy
*/
private $strategy;
protected function setUp(): void
{
$this->strategy = new ListStrategy();
}
/**
* @dataProvider argumentsProvider
* @group disconnected
* @param array $actualArguments
* @param array $expectedResponse
* @return void
*/
public function testProcessArguments(array $actualArguments, array $expectedResponse): void
{
$this->assertSame($expectedResponse, $this->strategy->processArguments($actualArguments));
}
public function argumentsProvider(): array
{
return [
'with default arguments' => [
['LIST', null, false],
['LIST'],
],
'with LIBRARYNAME modifier' => [
['LIST', 'libraryname', false],
['LIST', 'LIBRARYNAME', 'libraryname'],
],
'with WITHCODE modifier' => [
['LIST', null, true],
['LIST', 'WITHCODE'],
],
'with all arguments' => [
['LIST', 'libraryname', true],
['LIST', 'LIBRARYNAME', 'libraryname', 'WITHCODE'],
],
];
}
}
@@ -0,0 +1,37 @@
<?php
/*
* This file is part of the Predis package.
*
* (c) 2009-2020 Daniele Alessandri
* (c) 2021-2023 Till Krüss
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Predis\Command\Strategy\ContainerCommands\Functions;
use PredisTestCase;
class StatsStrategyTest extends PredisTestCase
{
/**
* @var StatsStrategy
*/
private $strategy;
protected function setUp(): void
{
$this->strategy = new StatsStrategy();
}
/**
* @group disconnected
* @return void
*/
public function testProcessArguments(): void
{
$this->assertSame(['arg1', 'arg2'], $this->strategy->processArguments(['arg1', 'arg2']));
}
}