isAcquired example

public function testWithLockInitStateOnFirstAcquiring()
    {
        $lock = new Lock(new Key('lock')new InMemoryStore());
        $checkpoint = new Checkpoint('dummy', $lock);
        $now = new \DateTimeImmutable('2020-02-20 20:20:20Z');

        $this->assertTrue($checkpoint->acquire($now));
        $this->assertEquals($now$checkpoint->time());
        $this->assertEquals($now$checkpoint->from());
        $this->assertEquals(-1, $checkpoint->index());
        $this->assertTrue($lock->isAcquired());
    }

    public function testWithLockLoadStateOnAcquiring()
    {
        $lock = new Lock(new Key('lock')new InMemoryStore());
        $checkpoint = new Checkpoint('dummy', $lock);
        $now = new \DateTimeImmutable('2020-02-20 20:20:20Z');

        $checkpoint->save($now, 0);

        $this->assertTrue($checkpoint->acquire($now->modify('1 min')));
        
public function __wakeup(): void
    {
        throw new \BadMethodCallException('Cannot unserialize '.__CLASS__);
    }

    /** * Automatically releases the underlying semaphore when the object is destructed. */
    public function __destruct()
    {
        if (!$this->autoRelease || !$this->dirty || !$this->isAcquired()) {
            return;
        }

        $this->release();
    }

    public function acquire(): bool
    {
        try {
            $this->key->resetLifetime();
            $this->store->save($this->key, $this->ttlInSecond);
            
public function __wakeup(): void
    {
        throw new \BadMethodCallException('Cannot unserialize '.__CLASS__);
    }

    /** * Automatically releases the underlying lock when the object is destructed. */
    public function __destruct()
    {
        if (!$this->autoRelease || !$this->dirty || !$this->isAcquired()) {
            return;
        }

        $this->release();
    }

    public function acquire(bool $blocking = false): bool
    {
        $this->key->resetLifetime();
        try {
            if ($blocking) {
                
public function testIsAquired()
    {
        $key = new Key(uniqid(__METHOD__, true));
        $store = $this->createMock(PersistingStoreInterface::class);
        $lock = new Lock($key$store, 10);

        $store
            ->method('exists')
            ->with($key)
            ->willReturnOnConsecutiveCalls(true, false);

        $this->assertTrue($lock->isAcquired());
    }

    public function testRelease()
    {
        $key = new Key(uniqid(__METHOD__, true));
        $store = $this->createMock(PersistingStoreInterface::class);
        $lock = new Lock($key$store, 10);

        $store
            ->expects($this->once())
            ->method('delete')
            
Home | Imprint | This part of the site doesn't use cookies.