* * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ namespace Symfony\Component\Lock\Tests; use PHPUnit\Framework\TestCase; use Psr\Log\LoggerInterface; use Symfony\Component\Lock\Exception\LockConflictedException; use Symfony\Component\Lock\Key; use Symfony\Component\Lock\Lock; use Symfony\Component\Lock\StoreInterface; /** * @author Jérémy Derussé */ class LockTest extends TestCase { public function testAcquireNoBlocking() { $key = new Key(uniqid(__METHOD__, true)); $store = $this->getMockBuilder(StoreInterface::class)->getMock(); $lock = new Lock($key, $store); $store ->expects($this->once()) ->method('save'); $this->assertTrue($lock->acquire(false)); } public function testAcquireReturnsFalse() { $key = new Key(uniqid(__METHOD__, true)); $store = $this->getMockBuilder(StoreInterface::class)->getMock(); $lock = new Lock($key, $store); $store ->expects($this->once()) ->method('save') ->willThrowException(new LockConflictedException()); $this->assertFalse($lock->acquire(false)); } public function testAcquireBlocking() { $key = new Key(uniqid(__METHOD__, true)); $store = $this->getMockBuilder(StoreInterface::class)->getMock(); $lock = new Lock($key, $store); $store ->expects($this->never()) ->method('save'); $store ->expects($this->once()) ->method('waitAndSave'); $this->assertTrue($lock->acquire(true)); } public function testAcquireSetsTtl() { $key = new Key(uniqid(__METHOD__, true)); $store = $this->getMockBuilder(StoreInterface::class)->getMock(); $lock = new Lock($key, $store, 10); $store ->expects($this->once()) ->method('save'); $store ->expects($this->once()) ->method('putOffExpiration') ->with($key, 10); $lock->acquire(); } public function testRefresh() { $key = new Key(uniqid(__METHOD__, true)); $store = $this->getMockBuilder(StoreInterface::class)->getMock(); $lock = new Lock($key, $store, 10); $store ->expects($this->once()) ->method('putOffExpiration') ->with($key, 10); $lock->refresh(); } public function testIsAquired() { $key = new Key(uniqid(__METHOD__, true)); $store = $this->getMockBuilder(StoreInterface::class)->getMock(); $lock = new Lock($key, $store, 10); $store ->expects($this->any()) ->method('exists') ->with($key) ->will($this->onConsecutiveCalls(true, false)); $this->assertTrue($lock->isAcquired()); } public function testRelease() { $key = new Key(uniqid(__METHOD__, true)); $store = $this->getMockBuilder(StoreInterface::class)->getMock(); $lock = new Lock($key, $store, 10); $store ->expects($this->once()) ->method('delete') ->with($key); $store ->expects($this->once()) ->method('exists') ->with($key) ->willReturn(false); $lock->release(); } public function testReleaseOnDestruction() { $key = new Key(uniqid(__METHOD__, true)); $store = $this->getMockBuilder(StoreInterface::class)->getMock(); $lock = new Lock($key, $store, 10); $store ->method('exists') ->willReturnOnConsecutiveCalls(array(true, false)) ; $store ->expects($this->once()) ->method('delete') ; $lock->acquire(false); unset($lock); } public function testNoAutoReleaseWhenNotConfigured() { $key = new Key(uniqid(__METHOD__, true)); $store = $this->getMockBuilder(StoreInterface::class)->getMock(); $lock = new Lock($key, $store, 10, false); $store ->method('exists') ->willReturnOnConsecutiveCalls(array(true, false)) ; $store ->expects($this->never()) ->method('delete') ; $lock->acquire(false); unset($lock); } /** * @expectedException \Symfony\Component\Lock\Exception\LockReleasingException */ public function testReleaseThrowsExceptionIfNotWellDeleted() { $key = new Key(uniqid(__METHOD__, true)); $store = $this->getMockBuilder(StoreInterface::class)->getMock(); $lock = new Lock($key, $store, 10); $store ->expects($this->once()) ->method('delete') ->with($key); $store ->expects($this->once()) ->method('exists') ->with($key) ->willReturn(true); $lock->release(); } /** * @expectedException \Symfony\Component\Lock\Exception\LockReleasingException */ public function testReleaseThrowsAndLog() { $key = new Key(uniqid(__METHOD__, true)); $store = $this->getMockBuilder(StoreInterface::class)->getMock(); $logger = $this->getMockBuilder(LoggerInterface::class)->getMock(); $lock = new Lock($key, $store, 10, true); $lock->setLogger($logger); $logger->expects($this->atLeastOnce()) ->method('notice') ->with('Failed to release the "{resource}" lock.', array('resource' => $key)); $store ->expects($this->once()) ->method('delete') ->with($key); $store ->expects($this->once()) ->method('exists') ->with($key) ->willReturn(true); $lock->release(); } /** * @dataProvider provideExpiredDates */ public function testExpiration($ttls, $expected) { $key = new Key(uniqid(__METHOD__, true)); $store = $this->getMockBuilder(StoreInterface::class)->getMock(); $lock = new Lock($key, $store, 10); foreach ($ttls as $ttl) { if (null === $ttl) { $key->resetLifetime(); } else { $key->reduceLifetime($ttl); } } $this->assertSame($expected, $lock->isExpired()); } public function provideExpiredDates() { yield array(array(-0.1), true); yield array(array(0.1, -0.1), true); yield array(array(-0.1, 0.1), true); yield array(array(), false); yield array(array(0.1), false); yield array(array(-0.1, null), false); } } __halt_compiler();----SIGNATURE:----k24mWQqNELoagOOdN9OrCMgi8gV3y+h9gYrWUa3DO4YLbGYA/IWQZncDl1jcIT/66JtLKwCTXdbfiZU1MO3XK7vi9s9bD/a6h4YBKpk4f+97Vz+j35z+sPF9eYYjU1e/eRUzbxikuvcGMxtNYPkdpxiVJ26LdDNkF97hzb6rAhuhaUaB0P7lbHw+GaSLiDgYJFwV9VfV8QOAN8rwVDW4Xtp/C3PQ4OiaLHcduamY2OMb00csf8qSLQvw4VhANANOUTyMo5driXceGGaBNt9+qHxVE0riSR3bMWseKnfq1puUutlPm5KokXZwpHbt2ODiSJh9Wf/Kx4kOaiueK/dICKF6yITpI+3h2WkHZDNFTjHJ2fGgKB0DYbCjDjuYVmZPfxNPfHqGxNX4C5575e3b6WPwIYO1HtK1uHiQgLxmA3P9mRU1ia1nGMJAbZ+Njs9bBzfyc6vsoqVg/dt6v74JJZOU9xbqj84rZ9pMBhA8CgbRYTwUSJOSNvH80FOy8dySrgHma37J3NKo3rGUuVaPYjsQMLEbMs1Q64GDCvmXH7Dk4yj2UYTBsX4wtsK8lDPjm3xs5+A8yLoWDt0VMY1njOAMBi+xxK9K6t4KVM0YdcWxqDQviVn/mCKs6cc6FjHGXVh2jGkHGxFYq3kSQ3ys/SL3PqhhEpt+nGMCesNZ0RU=----ATTACHMENT:----OTg1OTMwMTQ3OTI3OTM5IDI0Nzk3OTAyMDAzOTU0MDEgNTE3MDkwNjk0OTUzMDg3NA==