Added support for WAITAOF command (#1357)

* Added support for WAITAOF command

* Added call to parent setUp method
This commit is contained in:
Vladyslav Vildanov
2023-08-14 09:18:19 +03:00
committed by GitHub
parent ffd31cfdef
commit 311cd2b79e
5 changed files with 152 additions and 0 deletions
+41
View File
@@ -0,0 +1,41 @@
<?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.
*/
use Predis\Client;
require __DIR__ . '/../shared.php';
// Example of WAITAOF command usage:
// 1. Enable appendonly mode if it's not (command works only in appendonly mode)
$client = new Client($single_server);
$info = $client->info();
$enabled = false;
if ($info['Persistence']['aof_enabled'] === '0') {
$client->config('set', 'appendonly', 'yes');
$enabled = true;
}
// 2. Set key value pair
$response = $client->set('foo', 'bar');
echo "Key-value pair set status: {$response}\n";
// 3. Run WAITAOF command to make sure that all previous writes was fsynced
$response = $client->waitaof(1, 0, 0);
echo "Quantity of local instances that was fsynced - {$response[0]}, quantity of replicas - {$response[1]}";
// 4. Disable appendonly mode if it was enabled during script execution
if ($enabled) {
$client->config('set', 'appendonly', 'no');
}
+1
View File
@@ -304,6 +304,7 @@ use Predis\Command\Redis\Container\Search\FTCURSOR;
* @method $this exec()
* @method $this multi()
* @method $this unwatch()
* @method $this waitaof(int $numLocal, int $numReplicas, int $timeout)
* @method $this watch($key)
* @method $this eval($script, $numkeys, $keyOrArg1 = null, $keyOrArgN = null)
* @method $this eval_ro(string $script, array $keys, ...$argument)
+1
View File
@@ -322,6 +322,7 @@ use Predis\Response\Status;
* @method array|null exec()
* @method mixed multi()
* @method mixed unwatch()
* @method array waitaof(int $numLocal, int $numReplicas, int $timeout)
* @method mixed watch(string $key)
* @method mixed eval(string $script, int $numkeys, string ...$keyOrArg = null)
* @method mixed eval_ro(string $script, array $keys, ...$argument)
+29
View File
@@ -0,0 +1,29 @@
<?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\Redis;
use Predis\Command\Command as RedisCommand;
/**
* @see https://redis.io/commands/waitaof/
*
* This command blocks the current client until all the previous write commands are acknowledged
* as having been fsynced to the AOF of the local Redis and/or at least the specified number of replicas.
*/
class WAITAOF extends RedisCommand
{
public function getId()
{
return 'WAITAOF';
}
}
@@ -0,0 +1,80 @@
<?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\Redis;
class WAITAOF_Test extends PredisCommandTestCase
{
protected function setUp(): void
{
parent::setUp();
$redis = $this->getClient();
$this->assertEquals('OK', $redis->config('set', 'appendonly', 'yes'));
}
/**
* {@inheritDoc}
*/
protected function getExpectedCommand(): string
{
return WAITAOF::class;
}
/**
* {@inheritDoc}
*/
protected function getExpectedId(): string
{
return 'WAITAOF';
}
/**
* @group disconnected
*/
public function testFilterArguments(): void
{
$actualArguments = $expectedArguments = [1, 2, 3];
$command = $this->getCommand();
$command->setArguments($actualArguments);
$this->assertSameValues($expectedArguments, $command->getArguments());
}
/**
* @group disconnected
*/
public function testParseResponse(): void
{
$this->assertSame(1, $this->getCommand()->parseResponse(1));
}
/**
* @group connected
* @return void
* @requiresRedisVersion >= 7.2.0
*/
public function testReturnQuantityOfSyncedAOFInstances(): void
{
$redis = $this->getClient();
$this->assertEquals('OK', $redis->set('foo', 'bar'));
$this->assertSame([1, 0], $redis->waitaof(1, 0, 0));
}
protected function tearDown(): void
{
$redis = $this->getClient();
$this->assertEquals('OK', $redis->config('set', 'appendonly', 'no'));
}
}