/** * @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.'); }
/** * @expectedException NinjaMutex\MutexException */ public function testIfMutexDestructorThrowsWhenBackendIsUnavailable() { $predisMock = new MockPredisClient(); $lockImplementor = new PredisRedisLock($predisMock); $mutex = new Mutex('forfiter', $lockImplementor); $this->assertFalse($mutex->isAcquired()); $mutex->acquireLock(); $this->assertTrue($mutex->isAcquired()); $mutex->acquireLock(); $this->assertTrue($mutex->isAcquired()); // make backend unavailable $predisMock->setAvailable(false); // explicit dtor call, should throw MutexException $mutex->__destruct(); }
/** * @dataProvider lockImplementorProvider * @param LockInterface $lockImplementor */ public function testAcquireAndReleaseSecondMutexWithoutReleaseTheFirstMutex(LockInterface $lockImplementor) { $firstMutex = new Mutex('forfiter', $lockImplementor); $firstMutex->acquireLock(0); $secondMutex = new Mutex('gieraryhir', $lockImplementor); $this->assertTrue($secondMutex->acquireLock(0)); $this->assertTrue($secondMutex->isAcquired()); $this->assertTrue($secondMutex->isLocked()); $this->assertTrue($firstMutex->isAcquired()); $this->assertTrue($firstMutex->isLocked()); $this->assertTrue($secondMutex->releaseLock()); $this->assertTrue($firstMutex->isAcquired()); $this->assertTrue($firstMutex->isLocked()); }