From 13647c1938664a3d51f42d8139c6a94abf7c9004 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michael=20Vo=C5=99=C3=AD=C5=A1ek?= Date: Fri, 14 Mar 2025 12:59:18 +0100 Subject: [PATCH 1/4] Upgrade "php-mock/php-mock-phpunit" dev package --- composer.json | 2 +- tests/Mutex/AbstractSpinlockMutexTest.php | 10 ++-------- tests/Mutex/AbstractSpinlockWithTokenMutexTest.php | 10 ++-------- tests/Mutex/DistributedMutexTest.php | 10 ++-------- tests/Util/LoopTest.php | 10 ++-------- 5 files changed, 9 insertions(+), 33 deletions(-) diff --git a/composer.json b/composer.json index e5850ee..b4027f9 100644 --- a/composer.json +++ b/composer.json @@ -52,7 +52,7 @@ "ergebnis/phpunit-slow-test-detector": "^2.9", "friendsofphp/php-cs-fixer": "^3.0", "mikey179/vfsstream": "^1.6.11", - "php-mock/php-mock-phpunit": "^2.1", + "php-mock/php-mock-phpunit": "^2.12", "phpstan/extension-installer": "^1.1", "phpstan/phpstan": "^2.0", "phpstan/phpstan-deprecation-rules": "^2.0", diff --git a/tests/Mutex/AbstractSpinlockMutexTest.php b/tests/Mutex/AbstractSpinlockMutexTest.php index 9faa11e..f1241a0 100644 --- a/tests/Mutex/AbstractSpinlockMutexTest.php +++ b/tests/Mutex/AbstractSpinlockMutexTest.php @@ -9,7 +9,6 @@ use Malkusch\Lock\Exception\LockReleaseException; use Malkusch\Lock\Mutex\AbstractSpinlockMutex; use phpmock\environment\SleepEnvironmentBuilder; -use phpmock\MockEnabledException; use phpmock\phpunit\PHPMock; use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\TestCase; @@ -28,13 +27,8 @@ protected function setUp(): void $sleepBuilder->addNamespace('Malkusch\Lock\Mutex'); $sleepBuilder->addNamespace('Malkusch\Lock\Util'); $sleep = $sleepBuilder->build(); - try { - $sleep->enable(); - $this->registerForTearDown($sleep); - } catch (MockEnabledException $e) { - // workaround for burn testing - \assert($e->getMessage() === 'microtime is already enabled. Call disable() on the existing mock.'); - } + $sleep->enable(); + $this->registerForTearDown($sleep); } /** diff --git a/tests/Mutex/AbstractSpinlockWithTokenMutexTest.php b/tests/Mutex/AbstractSpinlockWithTokenMutexTest.php index 34cb69a..b2ecf63 100644 --- a/tests/Mutex/AbstractSpinlockWithTokenMutexTest.php +++ b/tests/Mutex/AbstractSpinlockWithTokenMutexTest.php @@ -7,7 +7,6 @@ use Malkusch\Lock\Exception\ExecutionOutsideLockException; use Malkusch\Lock\Mutex\AbstractSpinlockWithTokenMutex; use phpmock\environment\SleepEnvironmentBuilder; -use phpmock\MockEnabledException; use phpmock\phpunit\PHPMock; use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\TestCase; @@ -26,13 +25,8 @@ protected function setUp(): void $sleepBuilder->addNamespace('Malkusch\Lock\Mutex'); $sleepBuilder->addNamespace('Malkusch\Lock\Util'); $sleep = $sleepBuilder->build(); - try { - $sleep->enable(); - $this->registerForTearDown($sleep); - } catch (MockEnabledException $e) { - // workaround for burn testing - \assert($e->getMessage() === 'microtime is already enabled. Call disable() on the existing mock.'); - } + $sleep->enable(); + $this->registerForTearDown($sleep); } /** diff --git a/tests/Mutex/DistributedMutexTest.php b/tests/Mutex/DistributedMutexTest.php index 883c85c..815aa9e 100644 --- a/tests/Mutex/DistributedMutexTest.php +++ b/tests/Mutex/DistributedMutexTest.php @@ -13,7 +13,6 @@ use Malkusch\Lock\Mutex\DistributedMutex; use Malkusch\Lock\Util\LockUtil; use phpmock\environment\SleepEnvironmentBuilder; -use phpmock\MockEnabledException; use phpmock\phpunit\PHPMock; use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\MockObject\MockObject; @@ -35,13 +34,8 @@ protected function setUp(): void $sleepBuilder->addNamespace('Malkusch\Lock\Mutex'); $sleepBuilder->addNamespace('Malkusch\Lock\Util'); $sleep = $sleepBuilder->build(); - try { - $sleep->enable(); - $this->registerForTearDown($sleep); - } catch (MockEnabledException $e) { - // workaround for burn testing - \assert($e->getMessage() === 'microtime is already enabled. Call disable() on the existing mock.'); - } + $sleep->enable(); + $this->registerForTearDown($sleep); } /** diff --git a/tests/Util/LoopTest.php b/tests/Util/LoopTest.php index df08c90..74fe7e8 100644 --- a/tests/Util/LoopTest.php +++ b/tests/Util/LoopTest.php @@ -8,7 +8,6 @@ use Malkusch\Lock\Util\LockUtil; use Malkusch\Lock\Util\Loop; use phpmock\environment\SleepEnvironmentBuilder; -use phpmock\MockEnabledException; use phpmock\phpunit\PHPMock; use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\Attributes\DoesNotPerformAssertions; @@ -27,13 +26,8 @@ protected function setUp(): void $sleepBuilder->addNamespace(__NAMESPACE__); $sleepBuilder->addNamespace('Malkusch\Lock\Util'); $sleep = $sleepBuilder->build(); - try { - $sleep->enable(); - $this->registerForTearDown($sleep); - } catch (MockEnabledException $e) { - // workaround for burn testing - \assert($e->getMessage() === 'microtime is already enabled. Call disable() on the existing mock.'); - } + $sleep->enable(); + $this->registerForTearDown($sleep); } /** From e93848a150f55dcdbfa474daf47380d08bf166cd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michael=20Vo=C5=99=C3=AD=C5=A1ek?= Date: Fri, 14 Mar 2025 13:04:30 +0100 Subject: [PATCH 2/4] fix cs --- tests/Mutex/DistributedMutexTest.php | 30 ++++---- tests/Mutex/MutexTest.php | 108 +++++++++++++-------------- tests/Mutex/RedisMutexTest.php | 36 ++++----- 3 files changed, 87 insertions(+), 87 deletions(-) diff --git a/tests/Mutex/DistributedMutexTest.php b/tests/Mutex/DistributedMutexTest.php index 815aa9e..23909d4 100644 --- a/tests/Mutex/DistributedMutexTest.php +++ b/tests/Mutex/DistributedMutexTest.php @@ -228,6 +228,21 @@ public function testAcquireWithMajority(int $count, int $available): void $mutex->synchronized(static function () {}); } + /** + * Provides test cases with enough. + * + * @return iterable> + */ + public static function provideMajorityCases(): iterable + { + yield [1, 1]; + yield [2, 2]; + yield [3, 2]; + yield [3, 3]; + yield [4, 3]; + yield [5, 3]; + } + /** * Tests releasing fails because too few servers are available. * @@ -306,21 +321,6 @@ public static function provideMinorityCases(): iterable yield [6, 3]; } - /** - * Provides test cases with enough. - * - * @return iterable> - */ - public static function provideMajorityCases(): iterable - { - yield [1, 1]; - yield [2, 2]; - yield [3, 2]; - yield [3, 3]; - yield [4, 3]; - yield [5, 3]; - } - public function testAcquireMutexLogger(): void { $mutex = $this->createDistributedMutexMock(3); diff --git a/tests/Mutex/MutexTest.php b/tests/Mutex/MutexTest.php index 84880d2..4cad726 100644 --- a/tests/Mutex/MutexTest.php +++ b/tests/Mutex/MutexTest.php @@ -40,6 +40,60 @@ public static function setUpBeforeClass(): void vfsStream::setup('test'); } + /** + * Tests synchronized() executes the code and returns its result. + * + * @param \Closure(): Mutex $mutexFactory + * + * @dataProvider provideMutexFactoriesCases + */ + #[DataProvider('provideMutexFactoriesCases')] + public function testSynchronizedDelegates(\Closure $mutexFactory): void + { + $mutex = $mutexFactory(); + $result = $mutex->synchronized(static function () { + return 'test'; + }); + self::assertSame('test', $result); + } + + /** + * Tests that synchronized() released the lock. + * + * @param \Closure(): Mutex $mutexFactory + * + * @doesNotPerformAssertions + * + * @dataProvider provideMutexFactoriesCases + */ + #[DoesNotPerformAssertions] + #[DataProvider('provideMutexFactoriesCases')] + public function testRelease(\Closure $mutexFactory): void + { + $mutex = $mutexFactory(); + $mutex->synchronized(static function () {}); + + $mutex->synchronized(static function () {}); + } + + /** + * Tests synchronized() rethrows the exception of the code. + * + * @param \Closure(): Mutex $mutexFactory + * + * @dataProvider provideMutexFactoriesCases + */ + #[DataProvider('provideMutexFactoriesCases')] + public function testSynchronizedPassesExceptionThrough(\Closure $mutexFactory): void + { + $mutex = $mutexFactory(); + + $this->expectException(\DomainException::class); + $mutex->synchronized(static function () { + throw new \DomainException(); + }); + } + /** * Provides Mutex factories. * @@ -188,58 +242,4 @@ static function ($uri) { }]; } } - - /** - * Tests synchronized() executes the code and returns its result. - * - * @param \Closure(): Mutex $mutexFactory - * - * @dataProvider provideMutexFactoriesCases - */ - #[DataProvider('provideMutexFactoriesCases')] - public function testSynchronizedDelegates(\Closure $mutexFactory): void - { - $mutex = $mutexFactory(); - $result = $mutex->synchronized(static function () { - return 'test'; - }); - self::assertSame('test', $result); - } - - /** - * Tests that synchronized() released the lock. - * - * @param \Closure(): Mutex $mutexFactory - * - * @doesNotPerformAssertions - * - * @dataProvider provideMutexFactoriesCases - */ - #[DoesNotPerformAssertions] - #[DataProvider('provideMutexFactoriesCases')] - public function testRelease(\Closure $mutexFactory): void - { - $mutex = $mutexFactory(); - $mutex->synchronized(static function () {}); - - $mutex->synchronized(static function () {}); - } - - /** - * Tests synchronized() rethrows the exception of the code. - * - * @param \Closure(): Mutex $mutexFactory - * - * @dataProvider provideMutexFactoriesCases - */ - #[DataProvider('provideMutexFactoriesCases')] - public function testSynchronizedPassesExceptionThrough(\Closure $mutexFactory): void - { - $mutex = $mutexFactory(); - - $this->expectException(\DomainException::class); - $mutex->synchronized(static function () { - throw new \DomainException(); - }); - } } diff --git a/tests/Mutex/RedisMutexTest.php b/tests/Mutex/RedisMutexTest.php index a49136b..e63c753 100644 --- a/tests/Mutex/RedisMutexTest.php +++ b/tests/Mutex/RedisMutexTest.php @@ -258,24 +258,6 @@ public function testSerializersAndCompressors(int $serializer, int $compressor): })); } - public function testResistantToPartialClusterFailuresForAcquiringLock(): void - { - $this->closeMinorityConnections(); - - self::assertSame('test', $this->mutex->synchronized(static function () { - return 'test'; - })); - } - - public function testResistantToPartialClusterFailuresForReleasingLock(): void - { - self::assertNull($this->mutex->synchronized(function () { // @phpstan-ignore staticMethod.alreadyNarrowedType - $this->closeMinorityConnections(); - - return null; - })); - } - /** * @return iterable> */ @@ -313,4 +295,22 @@ public static function provideSerializersAndCompressorsCases(): iterable } } } + + public function testResistantToPartialClusterFailuresForAcquiringLock(): void + { + $this->closeMinorityConnections(); + + self::assertSame('test', $this->mutex->synchronized(static function () { + return 'test'; + })); + } + + public function testResistantToPartialClusterFailuresForReleasingLock(): void + { + self::assertNull($this->mutex->synchronized(function () { // @phpstan-ignore staticMethod.alreadyNarrowedType + $this->closeMinorityConnections(); + + return null; + })); + } } From 3e23f388ca99ebc9f472d414eb57e767988812e8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michael=20Vo=C5=99=C3=AD=C5=A1ek?= Date: Fri, 14 Mar 2025 13:08:46 +0100 Subject: [PATCH 3/4] fix PHP 8.4 --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index b4027f9..3c96fd1 100644 --- a/composer.json +++ b/composer.json @@ -58,7 +58,7 @@ "phpstan/phpstan-deprecation-rules": "^2.0", "phpstan/phpstan-strict-rules": "^2.0", "phpunit/phpunit": "^9.5.25 || ^10.0 || ^11.0", - "predis/predis": "^1.1.8", + "predis/predis": "^1.1.8 || ^2.0", "spatie/async": "^1.5" }, "suggest": { From 1deaad0175773096f912250b704a4c144d6521a1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michael=20Vo=C5=99=C3=AD=C5=A1ek?= Date: Fri, 14 Mar 2025 13:15:02 +0100 Subject: [PATCH 4/4] revert burn workaround, not fixed --- tests/Mutex/AbstractSpinlockMutexTest.php | 10 ++++++++-- tests/Mutex/AbstractSpinlockWithTokenMutexTest.php | 10 ++++++++-- tests/Mutex/DistributedMutexTest.php | 10 ++++++++-- tests/Util/LoopTest.php | 10 ++++++++-- 4 files changed, 32 insertions(+), 8 deletions(-) diff --git a/tests/Mutex/AbstractSpinlockMutexTest.php b/tests/Mutex/AbstractSpinlockMutexTest.php index f1241a0..9faa11e 100644 --- a/tests/Mutex/AbstractSpinlockMutexTest.php +++ b/tests/Mutex/AbstractSpinlockMutexTest.php @@ -9,6 +9,7 @@ use Malkusch\Lock\Exception\LockReleaseException; use Malkusch\Lock\Mutex\AbstractSpinlockMutex; use phpmock\environment\SleepEnvironmentBuilder; +use phpmock\MockEnabledException; use phpmock\phpunit\PHPMock; use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\TestCase; @@ -27,8 +28,13 @@ protected function setUp(): void $sleepBuilder->addNamespace('Malkusch\Lock\Mutex'); $sleepBuilder->addNamespace('Malkusch\Lock\Util'); $sleep = $sleepBuilder->build(); - $sleep->enable(); - $this->registerForTearDown($sleep); + try { + $sleep->enable(); + $this->registerForTearDown($sleep); + } catch (MockEnabledException $e) { + // workaround for burn testing + \assert($e->getMessage() === 'microtime is already enabled. Call disable() on the existing mock.'); + } } /** diff --git a/tests/Mutex/AbstractSpinlockWithTokenMutexTest.php b/tests/Mutex/AbstractSpinlockWithTokenMutexTest.php index b2ecf63..34cb69a 100644 --- a/tests/Mutex/AbstractSpinlockWithTokenMutexTest.php +++ b/tests/Mutex/AbstractSpinlockWithTokenMutexTest.php @@ -7,6 +7,7 @@ use Malkusch\Lock\Exception\ExecutionOutsideLockException; use Malkusch\Lock\Mutex\AbstractSpinlockWithTokenMutex; use phpmock\environment\SleepEnvironmentBuilder; +use phpmock\MockEnabledException; use phpmock\phpunit\PHPMock; use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\TestCase; @@ -25,8 +26,13 @@ protected function setUp(): void $sleepBuilder->addNamespace('Malkusch\Lock\Mutex'); $sleepBuilder->addNamespace('Malkusch\Lock\Util'); $sleep = $sleepBuilder->build(); - $sleep->enable(); - $this->registerForTearDown($sleep); + try { + $sleep->enable(); + $this->registerForTearDown($sleep); + } catch (MockEnabledException $e) { + // workaround for burn testing + \assert($e->getMessage() === 'microtime is already enabled. Call disable() on the existing mock.'); + } } /** diff --git a/tests/Mutex/DistributedMutexTest.php b/tests/Mutex/DistributedMutexTest.php index 23909d4..5bb3b47 100644 --- a/tests/Mutex/DistributedMutexTest.php +++ b/tests/Mutex/DistributedMutexTest.php @@ -13,6 +13,7 @@ use Malkusch\Lock\Mutex\DistributedMutex; use Malkusch\Lock\Util\LockUtil; use phpmock\environment\SleepEnvironmentBuilder; +use phpmock\MockEnabledException; use phpmock\phpunit\PHPMock; use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\MockObject\MockObject; @@ -34,8 +35,13 @@ protected function setUp(): void $sleepBuilder->addNamespace('Malkusch\Lock\Mutex'); $sleepBuilder->addNamespace('Malkusch\Lock\Util'); $sleep = $sleepBuilder->build(); - $sleep->enable(); - $this->registerForTearDown($sleep); + try { + $sleep->enable(); + $this->registerForTearDown($sleep); + } catch (MockEnabledException $e) { + // workaround for burn testing + \assert($e->getMessage() === 'microtime is already enabled. Call disable() on the existing mock.'); + } } /** diff --git a/tests/Util/LoopTest.php b/tests/Util/LoopTest.php index 74fe7e8..df08c90 100644 --- a/tests/Util/LoopTest.php +++ b/tests/Util/LoopTest.php @@ -8,6 +8,7 @@ use Malkusch\Lock\Util\LockUtil; use Malkusch\Lock\Util\Loop; use phpmock\environment\SleepEnvironmentBuilder; +use phpmock\MockEnabledException; use phpmock\phpunit\PHPMock; use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\Attributes\DoesNotPerformAssertions; @@ -26,8 +27,13 @@ protected function setUp(): void $sleepBuilder->addNamespace(__NAMESPACE__); $sleepBuilder->addNamespace('Malkusch\Lock\Util'); $sleep = $sleepBuilder->build(); - $sleep->enable(); - $this->registerForTearDown($sleep); + try { + $sleep->enable(); + $this->registerForTearDown($sleep); + } catch (MockEnabledException $e) { + // workaround for burn testing + \assert($e->getMessage() === 'microtime is already enabled. Call disable() on the existing mock.'); + } } /**