/** * {@inheritdoc} */ public function obtain($aggregateId) { if (!$this->lock->acquireLock($aggregateId)) { throw new ConcurrencyException(sprintf('Could not obtain lock for aggregate with id %s', $aggregateId)); } $this->lockedAggregateIds[$aggregateId] = true; }
/** * @param int|null $timeout * @return bool */ public function acquireLock($timeout = null) { if ($this->counter > 0 || $this->lockImplementor->acquireLock($this->name, $timeout)) { $this->counter++; return true; } return false; }
/** * @param string $resourceName * @return bool */ public function acquireLock($resourceName) { return $this->ninjaMutexLock->acquireLock($resourceName, $this->timeout); }
/** * @dataProvider lockImplementorProvider * @param LockInterface $lockImplementor */ public function testIfLockIsReleasedAfterLockImplementorIsDestroyed(LockInterface $lockImplementor) { $name = 'forfiter'; $duplicateLockImplementor = clone $lockImplementor; $duplicateLockImplementor->acquireLock($name, 0); unset($duplicateLockImplementor); $this->assertTrue($lockImplementor->acquireLock($name, 0)); $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.'); }