-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Mutex for memcached * Remove unused imports
- Loading branch information
Showing
5 changed files
with
199 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |