Skip to content

Commit

Permalink
Mutex for memcached (#42)
Browse files Browse the repository at this point in the history
* Mutex for memcached

* Remove unused imports
  • Loading branch information
Dyktus authored Dec 5, 2018
1 parent 44851df commit 4472c09
Show file tree
Hide file tree
Showing 5 changed files with 199 additions and 3 deletions.
4 changes: 2 additions & 2 deletions src/DistributedMemcacheMutex.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class DistributedMemcacheMutex
/**
* @var Memcache
*/
private $memcache;
protected $memcache;

/**
* @param Memcache $memcache
Expand Down Expand Up @@ -44,7 +44,7 @@ public function lock($name, $expirationTimeInSeconds = self::FIVE_MINUTES_SECOND
/**
* @throws InvalidArgumentException
*/
private function assertServersAddedToMemcache()
protected function assertServersAddedToMemcache()
{
if ($this->memcache->getversion() === false) {
throw new InvalidArgumentException(self::MEMCACHE_ERR_MSG);
Expand Down
28 changes: 28 additions & 0 deletions src/DistributedMemcachedMutex.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php
declare(strict_types=1);

namespace Assertis\Util;

use Memcached;

/**
* This class should be used when we will migrate to PHP 7.2
* It extends DistributedMemcacheMutex because all other methods like add, remove etc.
* are available in memcached also so there is no reason to overwrite methods.
*
* @author Łukasz Nowak <[email protected]>
* Class DistributedMemcachedMutex
* @package Assertis\Util
*/
class DistributedMemcachedMutex extends DistributedMemcacheMutex
{

/**
* DistributedMemcachedMutex constructor.
* @param Memcached $memcached
*/
public function __construct(Memcached $memcached)
{
$this->memcache = $memcached;
}
}
104 changes: 104 additions & 0 deletions tests/DistributedMemcachedMutexTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
<?php

namespace Assertis\Util;

use InvalidArgumentException;
use PHPUnit_Framework_TestCase;

class DistributedMemcachedMutexTest extends PHPUnit_Framework_TestCase
{
/**
* @var DistributedMemcachedMutex
*/
private $mutex;
/**
* @var DistributedMemcachedMutex
*/
private $serverlessMutex;

/**
* {@inheritdoc}
*/
protected function setUp()
{
parent::setUp();
$memcached = new MemcachedStub();
$memcachedWithoutServers = new MemcachedStub();
$this->mutex = new DistributedMemcachedMutex($memcached->withServersAdded());
$this->serverlessMutex = new DistributedMemcachedMutex($memcachedWithoutServers);
}

/**
* @test
*/
public function lockWhenMemcacheKeyDoesNotExist()
{
try {
$this->mutex->lock('some_key');
$this->success();
} catch (AlreadyLockedException $exception) {
$this->fail($exception->getMessage());
}
}

/**
* @test
*/
public function lockThrowsExceptionWhenMemcacheKeyExists()
{
try {
$this->mutex->lock('some_key');
$this->mutex->lock('some_key');
$this->fail('DistributedMemcachedMutex::lock method should throw AlreadyLockedException.');
} catch (AlreadyLockedException $exception) {
$this->success();
}
}

/**
* @test
*/
public function unlockWhenMemcacheKeyExists()
{
try {
$this->mutex->lock('some_key');
$this->mutex->unlock('some_key');
$this->mutex->lock('some_key');
$this->success();
} catch (AlreadyLockedException $exception) {
$this->fail($exception->getMessage(). ' It should not exists because it was unlocked.');
}
}

/**
* @test
*/
public function unlockWhenMemcacheKeyDoesNotExist()
{
try {
$this->mutex->unlock('some_key');
$this->mutex->lock('some_key');
$this->success();
} catch (AlreadyLockedException $exception) {
$this->fail($exception->getMessage(). ' It should not exists because it was not present.');
}
}

/**
* @test
*/
public function lockWhenNoServers()
{
try {
$this->serverlessMutex->lock('some_key');
$this->fail('DistributedMemcachedMutex::lock method should throw InvalidArgumentException.');
} catch (InvalidArgumentException $exception) {
$this->success();
}
}

private function success()
{
$this->assertTrue(true);
}
}
2 changes: 1 addition & 1 deletion tests/MemcacheStub.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public function withServersAdded()
/**
* {@inheritdoc}
*/
public function add($key, $var, $flag, $expire)
public function add($key, $var, $expire)
{
if (array_key_exists($key, $this->cache)) {
return false;
Expand Down
64 changes: 64 additions & 0 deletions tests/MemcachedStub.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php

namespace Assertis\Util;

use Memcached;

/**
* @author Mateusz Angulski <[email protected]>
*/
class MemcachedStub extends Memcached
{
/**
* @var array
*/
private $cache = [];
/**
* @var bool
*/
private $areServersAdded = false;

/**
* @return MemcachedStub
*/
public function withServersAdded()
{
$this->areServersAdded = true;

return $this;
}

/**
* {@inheritdoc}
*/
public function add($key, $var, $expire = NULL)
{
if (array_key_exists($key, $this->cache)) {
return false;
}
$this->cache[$key] = $var;

return $this->cache;
}

/**
* {@inheritdoc}
*/
public function delete($key, $timeout = 0)
{
if (!array_key_exists($key, $this->cache)) {
return false;
}
unset($this->cache[$key]);

return true;
}

/**
* {@inheritdoc}
*/
public function getversion()
{
return $this->areServersAdded;
}
}

0 comments on commit 4472c09

Please sign in to comment.