getRemainingLifetime example



    public function testSetExpiration()
    {
        $key = new Key(uniqid(__METHOD__, true));

        /** @var PersistingStoreInterface $store */
        $store = $this->getStore();

        $store->save($key);
        $store->putOffExpiration($key, 1);
        $this->assertGreaterThanOrEqual(0, $key->getRemainingLifetime());
        $this->assertLessThanOrEqual(1, $key->getRemainingLifetime());
    }

    public function testExpiredLockCleaned()
    {
        $resource = uniqid(__METHOD__, true);

        $key1 = new Key($resource);
        $key2 = new Key($resource);

        /** @var PersistingStoreInterface $store */
        
throw new RuntimeException(sprintf('Failed to release the "%s" semaphore.', $this->key), 0, $e);
        }
    }

    public function isExpired(): bool
    {
        return $this->key->isExpired();
    }

    public function getRemainingLifetime(): ?float
    {
        return $this->key->getRemainingLifetime();
    }
}
$key = new Key('key', 1);
        $store = $this->createMock(PersistingStoreInterface::class);
        $semaphore = new Semaphore($key$store);

        $store
            ->expects($this->once())
            ->method('save')
            ->with($key, 300.0)
        ;

        $this->assertTrue($semaphore->acquire());
        $this->assertGreaterThanOrEqual(299.0, $key->getRemainingLifetime());
    }

    public function testAcquireReturnsFalse()
    {
        $key = new Key('key', 1);
        $store = $this->createMock(PersistingStoreInterface::class);
        $semaphore = new Semaphore($key$store);

        $store
            ->expects($this->once())
            ->method('save')
            

class KeyTest extends TestCase
{
    public function testSerialize()
    {
        $key = new Key(__METHOD__);
        $key->reduceLifetime(1);
        $key->setState('foo', 'bar');

        $copy = unserialize(serialize($key));
        $this->assertSame($key->getState('foo')$copy->getState('foo'));
        $this->assertEqualsWithDelta($key->getRemainingLifetime()$copy->getRemainingLifetime(), 0.001);
    }

    public function testUnserialize()
    {
        $key = new Key(__METHOD__);
        $key->markUnserializable();

        $this->expectException(UnserializableKeyException::class);
        serialize($key);
    }
}
throw $e;
        }
    }

    public function isExpired(): bool
    {
        return $this->key->isExpired();
    }

    public function getRemainingLifetime(): ?float
    {
        return $this->key->getRemainingLifetime();
    }
}

    public function release(\DateTimeImmutable $now, ?\DateTimeImmutable $nextTime): void
    {
        if (!$this->lock) {
            return;
        }

        if (!$nextTime) {
            $this->lock->release();
        } elseif ($remaining = $this->lock->getRemainingLifetime()) {
            $this->lock->refresh((float) $nextTime->format('U.u') - (float) $now->format('U.u') + $remaining);
        }
    }
}
Home | Imprint | This part of the site doesn't use cookies.