Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## 2.1.0
### Changed
- Renamed existing `ttl` parameter to `ttw` for lock time to wait
- Added new `ttl` parameter for lock time to live

## 2.0.0
### Removed
- Symfony 2.* support
Expand Down
3 changes: 2 additions & 1 deletion src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ public function getConfigTreeBuilder()

$rootNode
->children()
->scalarNode('ttl')->defaultValue(5)->end()
->scalarNode('ttw')->defaultValue(5)->end()
->scalarNode('ttl')->defaultNull()->end()
->scalarNode('redis_client')->isRequired()->cannotBeEmpty()->end()
->end()
;
Expand Down
3 changes: 2 additions & 1 deletion src/DependencyInjection/PayseraLockExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ public function load(array $configs, ContainerBuilder $container)
$configuration = new Configuration();
$config = $this->processConfiguration($configuration, $configs);

$container->setParameter('paysera_lock.ttl', (int) $config['ttl']);
$container->setParameter('paysera_lock.ttw', $config['ttw']);
$container->setParameter('paysera_lock.ttl', $config['ttl']);

$loader = new Loader\XmlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config'));
$loader->load('services.xml');
Expand Down
1 change: 1 addition & 0 deletions src/Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
<service id="paysera_lock.lock_manager"
class="Paysera\Bundle\LockBundle\Service\LockManager">
<argument type="service" id="paysera_lock.lock_factory"/>
<argument>%paysera_lock.ttw%</argument>
<argument>%paysera_lock.ttl%</argument>
</service>
</services>
Expand Down
11 changes: 7 additions & 4 deletions src/Service/LockManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,29 +11,32 @@
class LockManager
{
private $lockFactory;
private $ttw;
private $ttl;

public function __construct(
Factory $lockFactory,
int $ttl
int $ttw,
int $ttl = null
) {
$this->lockFactory = $lockFactory;
$this->ttw = $ttw;
$this->ttl = $ttl;
}

public function createLock(string $resource): LockInterface
{
return $this->lockFactory->createLock($resource, null);
return $this->lockFactory->createLock($resource, $this->ttl);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not clear why we even need to add ttl here. Why it was bad that it was null? Redis lock is not the same as resource lock

}

public function acquire(LockInterface $lock): bool
{
foreach (range(1, $this->ttl) as $waited) {
foreach (range(1, $this->ttw) as $waited) {
if ($lock->acquire()) {
return true;
}

if ($waited >= $this->ttl) {
if ($waited >= $this->ttw) {
throw new LockAcquiringException('Failed to acquire lock, wait time expired');
}

Expand Down
5 changes: 4 additions & 1 deletion test/BundleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,11 @@ protected function tearDown()

public function testLockManagerConfiguration()
{
$ttw = $this->container->getParameter('paysera_lock.ttw');
$this->assertEquals(10, $ttw);

$ttl = $this->container->getParameter('paysera_lock.ttl');
$this->assertEquals(10, $ttl);
$this->assertNull($ttl);

$lockManager = $this->container->get('paysera_lock.lock_manager');
$this->assertInstanceOf(LockManager::class, $lockManager);
Expand Down
108 changes: 108 additions & 0 deletions test/Service/LockManagerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
<?php

declare(strict_types=1);

namespace Paysera\Bundle\LockBundle\Test\Service;

use Paysera\Bundle\LockBundle\Service\LockManager;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Lock\Exception\LockAcquiringException;
use Symfony\Component\Lock\Factory;
use Symfony\Component\Lock\LockInterface;

class LockManagerTest extends TestCase
{
/**
* @var LockManager
*/
private $lockManager;

/**
* @var MockObject|Factory
*/
private $lockFactory;

public function setUp()
{
$this->lockFactory = $this->createMock(Factory::class);
$this->lockManager = new LockManager(
$this->lockFactory,
2,
1
);
}

public function testCreateLock()
{
$lock = $this->createMock(LockInterface::class);
$this->lockFactory
->expects($this->once())
->method('createLock')
->with('resource', 1)
->willReturn($lock)
;

$createdLock = $this->lockManager->createLock('resource');

$this->assertEquals($lock, $createdLock);
}

public function testAcquire()
{
$lock = $this->createMock(LockInterface::class);
$lock
->expects($this->exactly(2))
->method('acquire')
->willReturnOnConsecutiveCalls(false, true)
;

$this->lockManager->acquire($lock);
}

public function testAcquireTtw()
{
$lock = $this->createMock(LockInterface::class);
$lock
->expects($this->exactly(2))
->method('acquire')
->willReturnOnConsecutiveCalls(false, false)
;

$this->expectException(LockAcquiringException::class);
$this->expectExceptionMessage('Failed to acquire lock, wait time expired');

$this->lockManager->acquire($lock);
}

public function testCreateAcquired()
{
$lock = $this->createMock(LockInterface::class);
$lock
->expects($this->once())
->method('acquire')
->willReturn(true)
;

$this->lockFactory
->expects($this->once())
->method('createLock')
->with('resource', 1)
->willReturn($lock)
;

$created = $this->lockManager->createAcquired('resource');

$this->assertEquals($lock, $created);
}

public function testRelease()
{
$lock = $this->createMock(LockInterface::class);
$lock
->expects($this->once())
->method('release')
;
$this->lockManager->release($lock);
}
}
2 changes: 1 addition & 1 deletion test/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@ snc_redis:
dsn: redis://localhost

paysera_lock:
ttl: 10
ttw: 10
redis_client: 'snc_redis.default'