Ensured Redis lock store is wrapped into a retry adapter

This commit is contained in:
Alejandro Celaya 2019-08-07 17:37:24 +02:00
parent 04389fc8b0
commit 73fd348490
3 changed files with 65 additions and 0 deletions

View File

@ -2,6 +2,7 @@
declare(strict_types=1);
use Shlinkio\Shlink\Common\Cache\RedisFactory;
use Shlinkio\Shlink\Common\Lock\RetryLockStoreDelegatorFactory;
use Symfony\Component\Lock;
use Zend\ServiceManager\AbstractFactory\ConfigAbstractFactory;
@ -22,6 +23,11 @@ return [
'lock_store' => Lock\Store\FlockStore::class,
'redis_lock_store' => Lock\Store\RedisStore::class,
],
'delegators' => [
Lock\Store\RedisStore::class => [
RetryLockStoreDelegatorFactory::class,
],
],
],
ConfigAbstractFactory::class => [

View File

@ -0,0 +1,18 @@
<?php
declare(strict_types=1);
namespace Shlinkio\Shlink\Common\Lock;
use Interop\Container\ContainerInterface;
use Symfony\Component\Lock\Store\RetryTillSaveStore;
use Symfony\Component\Lock\StoreInterface;
class RetryLockStoreDelegatorFactory
{
public function __invoke(ContainerInterface $container, $name, callable $callback): RetryTillSaveStore
{
/** @var StoreInterface $originalStore */
$originalStore = $callback();
return new RetryTillSaveStore($originalStore);
}
}

View File

@ -0,0 +1,41 @@
<?php
declare(strict_types=1);
namespace ShlinkioTest\Shlink\Common\Lock;
use PHPUnit\Framework\TestCase;
use Prophecy\Prophecy\ObjectProphecy;
use ReflectionObject;
use Shlinkio\Shlink\Common\Lock\RetryLockStoreDelegatorFactory;
use Symfony\Component\Lock\StoreInterface;
use Zend\ServiceManager\ServiceManager;
class RetryLockStoreDelegatorFactoryTest extends TestCase
{
/** @var RetryLockStoreDelegatorFactory */
private $delegator;
/** @var ObjectProphecy */
private $originalStore;
public function setUp(): void
{
$this->originalStore = $this->prophesize(StoreInterface::class)->reveal();
$this->delegator = new RetryLockStoreDelegatorFactory();
}
/** @test */
public function originalStoreIsWrappedInRetryStore(): void
{
$callback = function () {
return $this->originalStore;
};
$result = ($this->delegator)(new ServiceManager(), '', $callback);
$ref = new ReflectionObject($result);
$prop = $ref->getProperty('decorated');
$prop->setAccessible(true);
$this->assertSame($this->originalStore, $prop->getValue($result));
}
}