|
20 | 20 | use Symfony\Component\Lock\Lock;
|
21 | 21 | use Symfony\Component\Lock\PersistingStoreInterface;
|
22 | 22 | use Symfony\Component\Lock\SharedLockStoreInterface;
|
| 23 | +use Symfony\Component\Lock\Store\ExpiringStoreTrait; |
23 | 24 |
|
24 | 25 | /**
|
25 | 26 | * @author Jérémy Derussé <jeremy@derusse.com>
|
@@ -504,4 +505,50 @@ public function testAcquireReadBlockingWithPersistingStoreInterface()
|
504 | 505 |
|
505 | 506 | $this->assertTrue($lock->acquireRead(true));
|
506 | 507 | }
|
| 508 | + |
| 509 | + /** |
| 510 | + * @group time-sensitive |
| 511 | + */ |
| 512 | + public function testAcquireTwiceWithExpiration() |
| 513 | + { |
| 514 | + $key = new Key(uniqid(__METHOD__, true)); |
| 515 | + $store = new class() implements PersistingStoreInterface { |
| 516 | + use ExpiringStoreTrait; |
| 517 | + private $keys = []; |
| 518 | + private $initialTtl = 30; |
| 519 | + |
| 520 | + public function save(Key $key) |
| 521 | + { |
| 522 | + $key->reduceLifetime($this->initialTtl); |
| 523 | + $this->keys[spl_object_hash($key)] = $key; |
| 524 | + $this->checkNotExpired($key); |
| 525 | + |
| 526 | + return true; |
| 527 | + } |
| 528 | + |
| 529 | + public function delete(Key $key) |
| 530 | + { |
| 531 | + unset($this->keys[spl_object_hash($key)]); |
| 532 | + } |
| 533 | + |
| 534 | + public function exists(Key $key) |
| 535 | + { |
| 536 | + return isset($this->keys[spl_object_hash($key)]); |
| 537 | + } |
| 538 | + |
| 539 | + public function putOffExpiration(Key $key, float $ttl) |
| 540 | + { |
| 541 | + $key->reduceLifetime($ttl); |
| 542 | + $this->checkNotExpired($key); |
| 543 | + } |
| 544 | + }; |
| 545 | + $ttl = 1; |
| 546 | + $lock = new Lock($key, $store, $ttl); |
| 547 | + |
| 548 | + $this->assertTrue($lock->acquire()); |
| 549 | + $lock->release(); |
| 550 | + sleep($ttl + 1); |
| 551 | + $this->assertTrue($lock->acquire()); |
| 552 | + $lock->release(); |
| 553 | + } |
507 | 554 | }
|
0 commit comments