diff --git a/src/Lock/FlockLock.php b/src/Lock/FlockLock.php index a646a6c..043c0c0 100644 --- a/src/Lock/FlockLock.php +++ b/src/Lock/FlockLock.php @@ -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 * diff --git a/src/Lock/LockAbstract.php b/src/Lock/LockAbstract.php index d20dc65..02242d1 100644 --- a/src/Lock/LockAbstract.php +++ b/src/Lock/LockAbstract.php @@ -9,8 +9,6 @@ */ namespace NinjaMutex\Lock; -use NinjaMutex\UnrecoverableMutexException; - /** * Abstract lock implementor * @@ -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 * diff --git a/src/Lock/MySQLPDOLock.php b/src/Lock/MySQLPDOLock.php index 32c6c8c..89cee94 100644 --- a/src/Lock/MySQLPDOLock.php +++ b/src/Lock/MySQLPDOLock.php @@ -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]); - } - } } diff --git a/src/Mutex.php b/src/Mutex.php index 5d19c1c..08df685 100644 --- a/src/Mutex.php +++ b/src/Mutex.php @@ -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 * diff --git a/src/UnrecoverableMutexException.php b/src/UnrecoverableMutexException.php deleted file mode 100644 index 236d3cc..0000000 --- a/src/UnrecoverableMutexException.php +++ /dev/null @@ -1,22 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ -namespace NinjaMutex; - -/** - * Unrecoverable Mutex exception - * - * You shouldn't try to catch it unless you really know what are you doing - * This kind of exception suggest you messed up your code and you should fix it - * - * @author Kamil Dziedzic - */ -class UnrecoverableMutexException extends MutexException -{ -} diff --git a/tests/Lock/LockTest.php b/tests/Lock/LockTest.php index 77eeebe..8bba135 100644 --- a/tests/Lock/LockTest.php +++ b/tests/Lock/LockTest.php @@ -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 @@ -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 diff --git a/tests/Mock/MockLock.php b/tests/Mock/MockLock.php index 80e281f..1760d09 100644 --- a/tests/Mock/MockLock.php +++ b/tests/Mock/MockLock.php @@ -87,8 +87,4 @@ public function setAvailable($available) { $this->available = (bool)$available; } - - public function __destruct() - { - } } diff --git a/tests/Mock/MockPDO.php b/tests/Mock/MockPDO.php index 18c0213..8c3aac0 100644 --- a/tests/Mock/MockPDO.php +++ b/tests/Mock/MockPDO.php @@ -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]); - } - } } diff --git a/tests/MutexTest.php b/tests/MutexTest.php deleted file mode 100644 index f8f79e5..0000000 --- a/tests/MutexTest.php +++ /dev/null @@ -1,57 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace NinjaMutex\Tests; - -use NinjaMutex\Mutex; -use NinjaMutex\Tests\Mock\MockLock; -use NinjaMutex\UnrecoverableMutexException; -use PHPUnit\Framework\TestCase; - -/** - * Tests for Mutex - * - * @author Kamil Dziedzic - */ -class MutexTest extends TestCase -{ - /** - * @issue https://github.com/arvenil/ninja-mutex/pull/4 - */ - public function testIfMutexDestructorThrowsWhenBackendIsUnavailable() - { - $lockImplementor = new MockLock(); - $mutex = new Mutex('forfiter', $lockImplementor); - - $this->assertFalse($mutex->isAcquired()); - $this->assertTrue($mutex->acquireLock()); - $this->assertTrue($mutex->isAcquired()); - $this->assertTrue($mutex->acquireLock()); - $this->assertTrue($mutex->isAcquired()); - - // make backend unavailable - $lockImplementor->setAvailable(false); - - try { - // explicit __destructor() call, should throw UnrecoverableMutexException - $mutex->__destruct(); - } catch (UnrecoverableMutexException $e) { - // make backend available again - $lockImplementor->setAvailable(true); - // release lock - $this->assertTrue($mutex->releaseLock()); - $this->assertFalse($mutex->releaseLock()); - - return; - } - - $this->fail('An expected exception has not been raised.'); - } -}