Skip to content

Commit

Permalink
tests
Browse files Browse the repository at this point in the history
  • Loading branch information
fprochazka committed Sep 4, 2016
1 parent 5a04658 commit 88c097e
Showing 1 changed file with 57 additions and 8 deletions.
65 changes: 57 additions & 8 deletions tests/KdybyTests/AdvisoryLock.phpt
Expand Up @@ -23,16 +23,31 @@ require_once __DIR__ . '/bootstrap.php';
class AdvisoryLockTest extends Tester\TestCase
{

public function testLockExpired()
public function testIncreaseDuration()
{
Assert::exception(function () {
$first = new AdvisoryLock('foo:bar', $this->createClient());
$lock = new AdvisoryLock('foo:bar', $this->createClient());

Assert::true($first->lock(1));
sleep(3);
$lock->lock(2);
$remainingTimeout = $lock->calculateRemainingTimeout();
Assert::true($remainingTimeout >= 1 && $remainingTimeout <= 3);

$first->increaseDuration();
$lock->increaseDuration(5);
$remainingTimeout = $lock->calculateRemainingTimeout();
Assert::true($remainingTimeout >= 4 && $remainingTimeout <= 6);

$lock->release();
}



public function testIncreaseDurationLockExpiredException()
{
$first = new AdvisoryLock('foo:bar', $this->createClient());
$first->lock(1);
sleep(3);

Assert::exception(function () use ($first) {
$first->increaseDuration();
}, 'Kdyby\RedisActiveLock\LockException', 'Process ran too long. Increase lock duration, or extend lock regularly.');
}

Expand All @@ -43,16 +58,50 @@ class AdvisoryLockTest extends Tester\TestCase
$first = new AdvisoryLock('foo:bar', $this->createClient());
$second = new AdvisoryLock('foo:bar', $this->createClient());

Assert::true($first->lock(1));
$first->lock(1);
sleep(3); // first died?
Assert::true($second->lock(1));
$second->lock(1);
Assert::true($second->release());
Assert::false($first->release());
}



public function testInvalidDurationException()
{
$lock = new AdvisoryLock('foo:bar', $this->createClient());

Assert::exception(function () use ($lock) {
$lock->lock(-1);
}, 'Kdyby\RedisActiveLock\InvalidArgumentException');

Assert::exception(function () use ($lock) {
$lock->increaseDuration(-1);
}, 'Kdyby\RedisActiveLock\InvalidArgumentException');
}



public function testIncreaseNotLockedKeyException()
{
$lock = new AdvisoryLock('foo:bar', $this->createClient());

Assert::exception(function () use ($lock) {
$lock->increaseDuration(1);
}, 'Kdyby\RedisActiveLock\LockException', 'The key "foo:bar" has not yet been locked');
}



public function testReleaseNotLocked()
{
$lock = new AdvisoryLock('foo:bar', $this->createClient());
Assert::false($lock->release());
}



/**
* @return Redis
*/
Expand Down

0 comments on commit 88c097e

Please sign in to comment.