Files
predis/tests/Predis/Transaction/Strategy/ReplicationConnectionStrategyTest.php
T
Andrei Fedoseev 45b6bcc6d9 Keep a transaction on the connection that received WATCH or MULTI (#1756)
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.
2026-11-12 09:40:49 -07:00

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());
}
}