/
githubmirror
/
framework
Обзор
Документация
Войти
/
githubmirror
/
framework
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
13.x
tests/Integration/Cache/RedisCacheLockTest.php
226 строк
7 KB
Mior Muhammad Zaki
Merge branch 'origin/12.x' into 13.x
22 июн 2026, 15:59
22 июн 2026, 15:59
a732f41
Код
Авторство
О чём код?
<?php namespace Illuminate\Tests\Integration\Cache; use Exception; use Illuminate\Foundation\Testing\Concerns\InteractsWithRedis; use Illuminate\Support\Facades\Cache; use Orchestra\Testbench\TestCase; class RedisCacheLockTest extends TestCase { use InteractsWithRedis; protected function setUp(): void { parent::setUp(); $this->setUpRedis(); } protected function tearDown(): void { $this->tearDownRedis(); parent::tearDown(); } public function testRedisLocksCanBeAcquiredAndReleased() { Cache::store('redis')->lock('foo')->forceRelease(); $lock = Cache::store('redis')->lock('foo', 10); $this->assertTrue($lock->get()); $this->assertFalse(Cache::store('redis')->lock('foo', 10)->get()); $lock->release(); $lock = Cache::store('redis')->lock('foo', 10); $this->assertTrue($lock->get()); $this->assertFalse(Cache::store('redis')->lock('foo', 10)->get()); Cache::store('redis')->lock('foo')->release(); } public function testRedisLockCanHaveASeparateConnection() { $this->app['config']->set('cache.stores.redis.lock_connection', 'default'); $this->assertSame('default', Cache::store('redis')->lock('foo')->getConnectionName()); } public function testRedisLocksCanBlockForSeconds() { Cache::store('redis')->lock('foo')->forceRelease(); $this->assertSame('taylor', Cache::store('redis')->lock('foo', 10)->block(1, function () { return 'taylor'; })); Cache::store('redis')->lock('foo')->forceRelease(); $this->assertTrue(Cache::store('redis')->lock('foo', 10)->block(1)); } public function testConcurrentRedisLocksAreReleasedSafely() { Cache::store('redis')->lock('foo')->forceRelease(); $firstLock = Cache::store('redis')->lock('foo', 1); $this->assertTrue($firstLock->get()); sleep(2); $secondLock = Cache::store('redis')->lock('foo', 10); $this->assertTrue($secondLock->get()); $firstLock->release(); $this->assertFalse(Cache::store('redis')->lock('foo')->get()); } public function testRedisLocksWithFailedBlockCallbackAreReleased() { Cache::store('redis')->lock('foo')->forceRelease(); $firstLock = Cache::store('redis')->lock('foo', 10); try { $firstLock->block(1, function () { throw new Exception('failed'); }); } catch (Exception) { // Not testing the exception, just testing the lock // is released regardless of the how the exception // thrown by the callback was handled. } $secondLock = Cache::store('redis')->lock('foo', 1); $this->assertTrue($secondLock->get()); } public function testRedisLocksCanBeReleasedUsingOwnerToken() { Cache::store('redis')->lock('foo')->forceRelease(); $firstLock = Cache::store('redis')->lock('foo', 10); $this->assertTrue($firstLock->get()); $owner = $firstLock->owner(); $secondLock = Cache::store('redis')->restoreLock('foo', $owner); $secondLock->release(); $this->assertTrue(Cache::store('redis')->lock('foo')->get()); } public function testOwnerStatusCanBeCheckedAfterRestoringLock() { Cache::store('redis')->lock('foo')->forceRelease(); $firstLock = Cache::store('redis')->lock('foo', 10); $this->assertTrue($firstLock->get()); $owner = $firstLock->owner(); $secondLock = Cache::store('redis')->restoreLock('foo', $owner); $this->assertTrue($secondLock->isOwnedByCurrentProcess()); } public function testIsLocked() { Cache::store('redis')->lock('foo')->forceRelease(); $lock = Cache::store('redis')->lock('foo', 10); $this->assertFalse($lock->isLocked()); $lock->get(); $this->assertTrue($lock->isLocked()); $lock->release(); $this->assertFalse($lock->isLocked()); } public function testOtherOwnerDoesNotOwnLockAfterRestore() { Cache::store('redis')->lock('foo')->forceRelease(); $firstLock = Cache::store('redis')->lock('foo', 10); $this->assertTrue($firstLock->isOwnedBy(null)); $this->assertTrue($firstLock->get()); $this->assertTrue($firstLock->isOwnedBy($firstLock->owner())); $secondLock = Cache::store('redis')->restoreLock('foo', 'other_owner'); $this->assertTrue($secondLock->isOwnedBy($firstLock->owner())); $this->assertFalse($secondLock->isOwnedByCurrentProcess()); } public function testRedisLockCanBeRefreshed() { Cache::store('redis')->lock('foo')->forceRelease(); $lock = Cache::store('redis')->lock('foo', 10); $this->assertTrue($lock->get()); // Refresh the lock for another 20 seconds $this->assertTrue($lock->refresh(20)); // Lock should still be held $this->assertFalse(Cache::store('redis')->lock('foo', 10)->get()); $lock->release(); } public function testRedisLockCannotBeRefreshedByAnotherOwner() { Cache::store('redis')->lock('foo')->forceRelease(); $firstLock = Cache::store('redis')->lock('foo', 10); $this->assertTrue($firstLock->get()); // Create a new lock with a different owner $secondLock = Cache::store('redis')->restoreLock('foo', 'other_owner'); // Second lock should not be able to refresh $this->assertFalse($secondLock->refresh(20)); // Original lock should still be able to refresh $this->assertTrue($firstLock->refresh(20)); $firstLock->release(); } public function testRedisLockRefreshWithDefaultSeconds() { Cache::store('redis')->lock('foo')->forceRelease(); $lock = Cache::store('redis')->lock('foo', 10); $this->assertTrue($lock->get()); // Refresh without specifying seconds should use the original duration $this->assertTrue($lock->refresh()); // Lock should still be held $this->assertFalse(Cache::store('redis')->lock('foo', 10)->get()); $lock->release(); } public function testRedisLockRefreshWithNoExpirationDoesNotReleaseLock() { Cache::store('redis')->lock('foo')->forceRelease(); $lock = Cache::store('redis')->lock('foo'); $this->assertTrue($lock->get()); $this->assertTrue($lock->refresh()); $this->assertFalse(Cache::store('redis')->lock('foo')->get()); $lock->release(); } public function testRedisLockRefreshWithZeroSecondsRemovesExpirationWithoutReleasingLock() { Cache::store('redis')->lock('foo')->forceRelease(); $lock = Cache::store('redis')->lock('foo', 10); $this->assertTrue($lock->get()); $this->assertTrue($lock->refresh(0)); $this->assertFalse(Cache::store('redis')->lock('foo')->get()); $lock->release(); } }