Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Removed destructors because they aren't reliable #45

Closed
Closed
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
17 changes: 0 additions & 17 deletions src/Lock/FlockLock.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,23 +108,6 @@ public function __clone()
$this->files = array();
}

/**
* Try to release any obtained locks when object is destroyed
*
* This is a safe guard for cases when your php script dies unexpectedly.
* It's not guaranteed it will work either.
*
* You should not depend on __destruct() to release your locks,
* instead release them with `$released = $this->releaseLock()`A
* and check `$released` if lock was properly released
*/
public function __destruct()
{
while (null !== $file = array_pop($this->files)) {
fclose($file);
}
}

/**
* Check if lock is locked
*
Expand Down
26 changes: 0 additions & 26 deletions src/Lock/LockAbstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@
*/
namespace NinjaMutex\Lock;

use NinjaMutex\UnrecoverableMutexException;

/**
* Abstract lock implementor
*
Expand Down Expand Up @@ -42,30 +40,6 @@ public function __clone()
$this->locks = array();
}

/**
* Try to release any obtained locks when object is destroyed
*
* This is a safe guard for cases when your php script dies unexpectedly.
* It's not guaranteed it will work either.
*
* You should not depend on __destruct() to release your locks,
* instead release them with `$released = $this->releaseLock()`A
* and check `$released` if lock was properly released
* @throws UnrecoverableMutexException
*/
public function __destruct()
{
foreach ($this->locks as $name => $v) {
$released = $this->releaseLock($name);
if (!$released) {
throw new UnrecoverableMutexException(sprintf(
'Cannot release lock in __destruct(): %s',
$name
));
}
}
}

/**
* Acquire lock
*
Expand Down
9 changes: 0 additions & 9 deletions src/Lock/MySQLPDOLock.php
Original file line number Diff line number Diff line change
Expand Up @@ -178,13 +178,4 @@ protected function setupPDO($name)

return true;
}

public function __destruct()
{
parent::__destruct();

foreach($this->pdo as $name => $pdo) {
unset($this->pdo[$name]);
}
}
}
24 changes: 0 additions & 24 deletions src/Mutex.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,30 +82,6 @@ public function releaseLock()
return false;
}

/**
* Try to release any obtained locks when object is destroyed
*
* This is a safe guard for cases when your php script dies unexpectedly.
* It's not guaranteed it will work either.
*
* You should not depend on __destruct() to release your locks,
* instead release them with `$released = $this->releaseLock()`A
* and check `$released` if lock was properly released
* @throws UnrecoverableMutexException
*/
public function __destruct()
{
while ($this->isAcquired()) {
$released = $this->releaseLock();
if (!$released) {
throw new UnrecoverableMutexException(sprintf(
'Cannot release lock in Mutex __destruct(): %s',
$this->name
));
}
}
}

/**
* Check if Mutex is acquired
*
Expand Down
22 changes: 0 additions & 22 deletions src/UnrecoverableMutexException.php

This file was deleted.

38 changes: 0 additions & 38 deletions tests/Lock/LockTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
use NinjaMutex\Tests\AbstractTest;
use NinjaMutex\Tests\Lock\Fabric\LockFabricWithExpirationInterface;
use NinjaMutex\Tests\Mock\PermanentServiceInterface;
use NinjaMutex\UnrecoverableMutexException;

/**
* Tests for Locks
Expand Down Expand Up @@ -128,43 +127,6 @@ public function testIfLockIsReleasedAfterLockImplementorIsDestroyed(LockInterfac
$lockImplementor->releaseLock($name);
}

/**
* @issue https://github.com/arvenil/ninja-mutex/pull/4
* It's not working for hhvm, see below link to understand limitation
* https://github.com/facebook/hhvm/blob/af329776c9f740cc1c8c4791f673ba5aa49042ce/hphp/doc/inconsistencies#L40-L45
*
* @dataProvider lockImplementorWithBackendProvider
* @param LockInterface $lockImplementor
* @param PermanentServiceInterface $backend
*/
public function testIfLockDestructorThrowsWhenBackendIsUnavailable(LockInterface $lockImplementor, PermanentServiceInterface $backend)
{
$name = "forfiter";

$this->assertFalse($lockImplementor->isLocked($name));
$this->assertTrue($lockImplementor->acquireLock($name, 0));
$this->assertTrue($lockImplementor->isLocked($name));

// make backend unavailable
$backend->setAvailable(false);

try {
// explicit __destructor() call, should throw UnrecoverableMutexException
$lockImplementor->__destruct();
} catch (UnrecoverableMutexException $e) {
// make backend available again
$backend->setAvailable(true);
// release lock
$this->assertTrue($lockImplementor->releaseLock($name));
$this->assertFalse($lockImplementor->releaseLock($name));
$this->assertFalse($lockImplementor->isLocked($name));

return;
}

$this->fail('An expected exception has not been raised.');
}

/**
* @issue https://github.com/arvenil/ninja-mutex/issues/12
* @medium Timeout for test increased to ~5s http://stackoverflow.com/a/10535787/916440
Expand Down
4 changes: 0 additions & 4 deletions tests/Mock/MockLock.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,4 @@ public function setAvailable($available)
{
$this->available = (bool)$available;
}

public function __destruct()
{
}
}
8 changes: 0 additions & 8 deletions tests/Mock/MockPDO.php
Original file line number Diff line number Diff line change
Expand Up @@ -150,12 +150,4 @@ public function quote($string, $type = PDO::PARAM_STR)
{
return $string;
}

public function __destruct()
{
foreach ($this->current as $k => $v) {
unset(self::$data[$k]);
unset($this->current[$k]);
}
}
}
57 changes: 0 additions & 57 deletions tests/MutexTest.php

This file was deleted.