|
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 |
|
22 | 23 | /** |
23 | 24 | * @author Jérémy Derussé <jeremy@derusse.com> |
@@ -372,4 +373,50 @@ public function provideExpiredDates() |
372 | 373 | yield [[0.1], false]; |
373 | 374 | yield [[-0.1, null], false]; |
374 | 375 | } |
| 376 | + |
| 377 | + /** |
| 378 | + * @group time-sensitive |
| 379 | + */ |
| 380 | + public function testAcquireTwiceWithExpiration() |
| 381 | + { |
| 382 | + $key = new Key(uniqid(__METHOD__, true)); |
| 383 | + $store = new class() implements PersistingStoreInterface { |
| 384 | + use ExpiringStoreTrait; |
| 385 | + private $keys = []; |
| 386 | + private $initialTtl = 30; |
| 387 | + |
| 388 | + public function save(Key $key) |
| 389 | + { |
| 390 | + $key->reduceLifetime($this->initialTtl); |
| 391 | + $this->keys[spl_object_hash($key)] = $key; |
| 392 | + $this->checkNotExpired($key); |
| 393 | + |
| 394 | + return true; |
| 395 | + } |
| 396 | + |
| 397 | + public function delete(Key $key) |
| 398 | + { |
| 399 | + unset($this->keys[spl_object_hash($key)]); |
| 400 | + } |
| 401 | + |
| 402 | + public function exists(Key $key) |
| 403 | + { |
| 404 | + return isset($this->keys[spl_object_hash($key)]); |
| 405 | + } |
| 406 | + |
| 407 | + public function putOffExpiration(Key $key, $ttl) |
| 408 | + { |
| 409 | + $key->reduceLifetime($ttl); |
| 410 | + $this->checkNotExpired($key); |
| 411 | + } |
| 412 | + }; |
| 413 | + $ttl = 1; |
| 414 | + $lock = new Lock($key, $store, $ttl); |
| 415 | + |
| 416 | + $this->assertTrue($lock->acquire()); |
| 417 | + $lock->release(); |
| 418 | + sleep($ttl + 1); |
| 419 | + $this->assertTrue($lock->acquire()); |
| 420 | + $lock->release(); |
| 421 | + } |
375 | 422 | } |
0 commit comments