|
18 | 18 | use Symfony\Component\Lock\Key;
|
19 | 19 | use Symfony\Component\Lock\Lock;
|
20 | 20 | use Symfony\Component\Lock\PersistingStoreInterface;
|
| 21 | +use Symfony\Component\Lock\Store\ExpiringStoreTrait; |
21 | 22 | use Symfony\Component\Lock\StoreInterface;
|
22 | 23 |
|
23 | 24 | /**
|
@@ -392,4 +393,50 @@ public function provideExpiredDates()
|
392 | 393 | yield [[0.1], false];
|
393 | 394 | yield [[-0.1, null], false];
|
394 | 395 | }
|
| 396 | + |
| 397 | + /** |
| 398 | + * @group time-sensitive |
| 399 | + */ |
| 400 | + public function testAcquireTwiceWithExpiration() |
| 401 | + { |
| 402 | + $key = new Key(uniqid(__METHOD__, true)); |
| 403 | + $store = new class() implements PersistingStoreInterface { |
| 404 | + use ExpiringStoreTrait; |
| 405 | + private $keys = []; |
| 406 | + private $initialTtl = 30; |
| 407 | + |
| 408 | + public function save(Key $key) |
| 409 | + { |
| 410 | + $key->reduceLifetime($this->initialTtl); |
| 411 | + $this->keys[spl_object_hash($key)] = $key; |
| 412 | + $this->checkNotExpired($key); |
| 413 | + |
| 414 | + return true; |
| 415 | + } |
| 416 | + |
| 417 | + public function delete(Key $key) |
| 418 | + { |
| 419 | + unset($this->keys[spl_object_hash($key)]); |
| 420 | + } |
| 421 | + |
| 422 | + public function exists(Key $key) |
| 423 | + { |
| 424 | + return isset($this->keys[spl_object_hash($key)]); |
| 425 | +
95D6
} |
| 426 | + |
| 427 | + public function putOffExpiration(Key $key, $ttl) |
| 428 | + { |
| 429 | + $key->reduceLifetime($ttl); |
| 430 | + $this->checkNotExpired($key); |
| 431 | + } |
| 432 | + }; |
| 433 | + $ttl = 1; |
| 434 | + $lock = new Lock($key, $store, $ttl); |
| 435 | + |
| 436 | + $this->assertTrue($lock->acquire()); |
| 437 | + $lock->release(); |
| 438 | + sleep($ttl + 1); |
| 439 | + $this->assertTrue($lock->acquire()); |
| 440 | + $lock->release(); |
| 441 | + } |
395 | 442 | }
|
0 commit comments