Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/Symfony/Component/Semaphore/Key.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,11 @@ public function getState(string $stateKey)
return $this->state[$stateKey];
}

public function resetLifetime(): void
{
$this->expiringTime = null;
}

public function reduceLifetime(float $ttlInSeconds)
{
$newTime = microtime(true) + $ttlInSeconds;
Expand Down
2 changes: 2 additions & 0 deletions src/Symfony/Component/Semaphore/Semaphore.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ public function __destruct()
public function acquire(): bool
{
try {
$this->key->resetLifetime();
$this->store->save($this->key, $this->ttlInSecond);
$this->key->reduceLifetime($this->ttlInSecond);
$this->dirty = true;
Expand Down Expand Up @@ -97,6 +98,7 @@ public function refresh(?float $ttlInSecond = null)
}

try {
$this->key->resetLifetime();
$this->store->putOffExpiration($this->key, $ttlInSecond);
$this->key->reduceLifetime($ttlInSecond);

Expand Down
20 changes: 20 additions & 0 deletions src/Symfony/Component/Semaphore/Tests/SemaphoreTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -252,4 +252,24 @@ public function testExpiration()
$semaphore = new Semaphore($key, $store);
$this->assertTrue($semaphore->isExpired());
}

/**
* @group time-sensitive
*/
public function testExpirationResetAfter()
{
$store = $this->getMockBuilder(PersistingStoreInterface::class)->getMock();

$key = new Key('key', 1);
$semaphore = new Semaphore($key, $store, 1);

$semaphore->acquire();
$this->assertFalse($semaphore->isExpired());
$semaphore->release();

sleep(2);

$semaphore->acquire();
$this->assertFalse($semaphore->isExpired());
}
}