mirror of
https://github.com/predis/predis.git
synced 2026-11-16 15:47:18 +00:00
45b6bcc6d9
Retrying one of its commands on a new connection runs it outside of the transaction: with Sentinel even without the retry option, since SentinelReplication retries on its own. A transaction block then threw with one command already written, and a CAS transaction overwrote a change made after WATCH, because MULTI and EXEC ran on a connection that watched nothing. MultiExec::unwatch() now goes through the strategy before MULTI, a held connection that is no longer open fails instead of reconnecting outside the transaction, a StreamInitException releases it too, and replication holds getCurrent(). In CAS mode the first command initializes the transaction without sending MULTI, so MultiExec::unwatch() now checks for CAS too.
104 lines
3.1 KiB
PHP
104 lines
3.1 KiB
PHP
<?php
|
|
|
|
/*
|
|
* This file is part of the Predis package.
|
|
*
|
|
* (c) 2009-2020 Daniele Alessandri
|
|
* (c) 2021-2026 Till Krüss
|
|
*
|
|
* For the full copyright and license information, please view the LICENSE
|
|
* file that was distributed with this source code.
|
|
*/
|
|
|
|
namespace Predis\Transaction\Strategy;
|
|
|
|
use PHPUnit\Framework\TestCase;
|
|
use Predis\Command\CommandInterface;
|
|
use Predis\Command\Redis\SET;
|
|
use Predis\Connection\NodeConnectionInterface;
|
|
use Predis\Connection\Parameters;
|
|
use Predis\Connection\Replication\ReplicationInterface;
|
|
use Predis\Transaction\MultiExecState;
|
|
|
|
class ReplicationConnectionStrategyTest extends TestCase
|
|
{
|
|
/**
|
|
* @var ReplicationInterface
|
|
*/
|
|
private $mockConnection;
|
|
|
|
/**
|
|
* @var CommandInterface
|
|
*/
|
|
private $mockCommand;
|
|
|
|
protected function setUp(): void
|
|
{
|
|
$this->mockConnection = $this->getMockBuilder(ReplicationInterface::class)->getMock();
|
|
$this->mockCommand = $this->getMockBuilder(CommandInterface::class)->getMock();
|
|
}
|
|
|
|
/**
|
|
* @return void
|
|
*/
|
|
public function testExecuteCommand(): void
|
|
{
|
|
$this->mockConnection
|
|
->expects($this->once())
|
|
->method('executeCommand')
|
|
->with($this->mockCommand)
|
|
->willReturn('OK');
|
|
|
|
$this->mockConnection
|
|
->expects($this->any())
|
|
->method('getParameters')
|
|
->willReturn(new Parameters());
|
|
|
|
$strategy = new ReplicationConnectionStrategy($this->mockConnection, new MultiExecState());
|
|
|
|
$this->assertEquals('OK', $strategy->executeCommand($this->mockCommand));
|
|
}
|
|
|
|
/**
|
|
* @return void
|
|
*/
|
|
public function testCommandsAfterMultiGoOnlyToTheConnectionHoldingTheTransaction(): void
|
|
{
|
|
$master = $this->getMockBuilder(NodeConnectionInterface::class)->getMock();
|
|
$master
|
|
->expects($this->exactly(2))
|
|
->method('executeCommand')
|
|
->willReturnCallback(static function (CommandInterface $command) {
|
|
return $command->getId() === 'EXEC' ? ['OK'] : 'QUEUED';
|
|
});
|
|
$master
|
|
->method('isConnected')
|
|
->willReturn(true);
|
|
|
|
$this->mockConnection
|
|
->expects($this->once())
|
|
->method('executeCommand')
|
|
->with($this->callback(static function (CommandInterface $command) {
|
|
return $command->getId() === 'MULTI';
|
|
}))
|
|
->willReturn('OK');
|
|
|
|
// The node that has just run MULTI.
|
|
$this->mockConnection
|
|
->expects($this->once())
|
|
->method('getCurrent')
|
|
->willReturn($master);
|
|
|
|
$this->mockConnection
|
|
->expects($this->any())
|
|
->method('getParameters')
|
|
->willReturn(new Parameters());
|
|
|
|
$strategy = new ReplicationConnectionStrategy($this->mockConnection, new MultiExecState());
|
|
|
|
$this->assertTrue($strategy->initializeTransaction());
|
|
$this->assertSame('QUEUED', $strategy->executeCommand(new SET()));
|
|
$this->assertSame(['OK'], $strategy->executeTransaction());
|
|
}
|
|
}
|