|
| 1 | +<?php namespace Tests\Unit\Services; |
| 2 | +/** |
| 3 | + * Copyright 2026 OpenStack Foundation |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | + * Unless required by applicable law or agreed to in writing, software |
| 9 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | + * See the License for the specific language governing permissions and |
| 12 | + * limitations under the License. |
| 13 | + **/ |
| 14 | + |
| 15 | +use App\Services\Utils\Exceptions\UnacquiredLockException; |
| 16 | +use App\Services\Utils\LockManagerService; |
| 17 | +use libs\utils\ICacheService; |
| 18 | +use Mockery; |
| 19 | +use PHPUnit\Framework\TestCase; |
| 20 | +use ReflectionClass; |
| 21 | + |
| 22 | +/** |
| 23 | + * Regression tests for the four bugs fixed in LockManagerService: |
| 24 | + * |
| 25 | + * 1. Non-atomic acquisition (setnx + expire → SET NX EX) |
| 26 | + * 2. Missing ownership token (timestamp value → random token) |
| 27 | + * 3. Unconditional release in finally (guarded by $acquired flag) |
| 28 | + * 4. Broken exponential backoff (^ XOR → ** power) |
| 29 | + * |
| 30 | + * These tests use a mock ICacheService so they run without Redis. |
| 31 | + * The Alice/Bob scenario demonstrates that a failed acquirer never |
| 32 | + * deletes another process's lock key. |
| 33 | + */ |
| 34 | +class LockManagerServiceOwnershipTest extends TestCase |
| 35 | +{ |
| 36 | + protected function setUp(): void |
| 37 | + { |
| 38 | + parent::setUp(); |
| 39 | + \Illuminate\Support\Facades\Facade::clearResolvedInstances(); |
| 40 | + $container = new \Illuminate\Container\Container(); |
| 41 | + $container->instance('app', $container); |
| 42 | + $container->instance('log', new class { |
| 43 | + public function __call($name, $args) {} |
| 44 | + }); |
| 45 | + \Illuminate\Support\Facades\Facade::setFacadeApplication($container); |
| 46 | + } |
| 47 | + |
| 48 | + protected function tearDown(): void |
| 49 | + { |
| 50 | + \Illuminate\Support\Facades\Facade::clearResolvedInstances(); |
| 51 | + \Illuminate\Support\Facades\Facade::setFacadeApplication(null); |
| 52 | + Mockery::close(); |
| 53 | + parent::tearDown(); |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * Alice holds the lock. Bob's acquire exhausts retries and throws |
| 58 | + * UnacquiredLockException. Bob's lock() finally block must NOT call |
| 59 | + * deleteIfValueMatches — Bob never owned the key and must not delete it. |
| 60 | + * |
| 61 | + * On main (before fix) this test fails because releaseLock was called |
| 62 | + * unconditionally from the finally block. |
| 63 | + */ |
| 64 | + public function testBobsFailedAcquireNeverDeletesAlicesKey(): void |
| 65 | + { |
| 66 | + // Alice: acquires once, releases once via deleteIfValueMatches. |
| 67 | + $aliceCache = Mockery::mock(ICacheService::class); |
| 68 | + $aliceCache->shouldReceive('addSingleValue')->once()->andReturn(true); |
| 69 | + $aliceCache->shouldReceive('deleteIfValueMatches')->once()->andReturn(true); |
| 70 | + |
| 71 | + // Bob: fails to acquire on every retry; must never touch Redis for release. |
| 72 | + $bobCache = Mockery::mock(ICacheService::class); |
| 73 | + $bobCache->shouldReceive('addSingleValue') |
| 74 | + ->times(LockManagerService::MaxRetries) |
| 75 | + ->andReturn(false); |
| 76 | + $bobCache->shouldReceive('deleteIfValueMatches')->never(); |
| 77 | + |
| 78 | + $alice = new LockManagerService($aliceCache); |
| 79 | + $bob = new LockManagerService($bobCache); |
| 80 | + |
| 81 | + $alice->lock('resource.lock', function () { |
| 82 | + // Alice's critical section. |
| 83 | + }); |
| 84 | + |
| 85 | + $this->expectException(UnacquiredLockException::class); |
| 86 | + $bob->lock('resource.lock', function () { |
| 87 | + $this->fail('Bob must not enter the critical section.'); |
| 88 | + }); |
| 89 | + // Mockery tearDown asserts deleteIfValueMatches was never called on $bobCache. |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * Calling releaseLock on a name that was never acquired must be a |
| 94 | + * complete no-op — no Redis command issued, no exception thrown. |
| 95 | + */ |
| 96 | + public function testReleaseLockWithoutAcquireIsNoOp(): void |
| 97 | + { |
| 98 | + $cache = Mockery::mock(ICacheService::class); |
| 99 | + $cache->shouldReceive('deleteIfValueMatches')->never(); |
| 100 | + $cache->shouldReceive('delete')->never(); |
| 101 | + |
| 102 | + $service = new LockManagerService($cache); |
| 103 | + $service->releaseLock('never.acquired.lock'); |
| 104 | + |
| 105 | + // Tokens map must still be empty — the no-op must not corrupt state. |
| 106 | + $ref = new ReflectionClass($service); |
| 107 | + $prop = $ref->getProperty('tokens'); |
| 108 | + $prop->setAccessible(true); |
| 109 | + $this->assertEmpty($prop->getValue($service)); |
| 110 | + } |
| 111 | + |
| 112 | + /** |
| 113 | + * After a full acquire → callback → release cycle the internal tokens |
| 114 | + * map must be empty — no token leak that could cause a future |
| 115 | + * releaseLock call to issue a stale deleteIfValueMatches. |
| 116 | + */ |
| 117 | + public function testTokensClearedAfterSuccessfulLockCycle(): void |
| 118 | + { |
| 119 | + $cache = Mockery::mock(ICacheService::class); |
| 120 | + $cache->shouldReceive('addSingleValue')->once()->andReturn(true); |
| 121 | + $cache->shouldReceive('deleteIfValueMatches')->once()->andReturn(true); |
| 122 | + |
| 123 | + $service = new LockManagerService($cache); |
| 124 | + $service->lock('test.lock', function () {}, 3600); |
| 125 | + |
| 126 | + $ref = new ReflectionClass($service); |
| 127 | + $prop = $ref->getProperty('tokens'); |
| 128 | + $prop->setAccessible(true); |
| 129 | + $this->assertEmpty($prop->getValue($service), 'tokens map must be empty after release'); |
| 130 | + } |
| 131 | + |
| 132 | + /** |
| 133 | + * Structural assertion: addSingleValue is called exactly once per |
| 134 | + * acquisition attempt (not two separate calls for setnx + expire). |
| 135 | + * The call must carry the lock name, a string token, and the lifetime. |
| 136 | + */ |
| 137 | + public function testAddSingleValueCalledOnceWithTokenAndLifetime(): void |
| 138 | + { |
| 139 | + $cache = Mockery::mock(ICacheService::class); |
| 140 | + $cache->shouldReceive('addSingleValue') |
| 141 | + ->once() |
| 142 | + ->with('test.lock', Mockery::type('string'), 3600) |
| 143 | + ->andReturn(true); |
| 144 | + $cache->shouldReceive('deleteIfValueMatches')->once()->andReturn(true); |
| 145 | + |
| 146 | + $service = new LockManagerService($cache); |
| 147 | + $service->lock('test.lock', function () {}, 3600); |
| 148 | + |
| 149 | + // Tokens cleared — confirms the single addSingleValue call was paired |
| 150 | + // with exactly one deleteIfValueMatches (not a separate expire call). |
| 151 | + $ref = new ReflectionClass($service); |
| 152 | + $prop = $ref->getProperty('tokens'); |
| 153 | + $prop->setAccessible(true); |
| 154 | + $this->assertEmpty($prop->getValue($service)); |
| 155 | + } |
| 156 | +} |
0 commit comments