Files
yii2/tests/framework/mutex/RetryAcquireTraitTest.php
Pavel Ivanov e5ecea653f Fixed random fails of RetryAcquireTraitTest (#17024)
Property `RetryAcquireTrait::$retryDelay` sets the minimum delay, but not the exact delay. In fact, the delay may be longer. Because of this, sometimes less than 20 blocking attempts occur. Checking the exact number of locks sometimes leads to the following errors:
```
$ vendor/bin/phpunit --filter=testRetryAcquire
PHPUnit 4.8.34 by Sebastian Bergmann and contributors.

F

You should really fix these slow tests (>500ms)...
 1. 1050ms to run yiiunit\framework\mutex\RetryAcquireTraitTest:testRetryAcquire

Time: 4.08 seconds, Memory: 44.00MB

There was 1 failure:

1) yiiunit\framework\mutex\RetryAcquireTraitTest::testRetryAcquire
Failed asserting that 19 is identical to 20.

/yii2/tests/framework/mutex/RetryAcquireTraitTest.php:36
/yii2/vendor/phpunit/phpunit/phpunit:52

FAILURES!
Tests: 1, Assertions: 3, Failures: 1.
```

I reworked the test so that it checks the duration of the delays, but not the exact number of blocking attempts.
2019-01-08 09:55:27 -05:00

60 lines
1.5 KiB
PHP

<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace yiiunit\framework\mutex;
use Yii;
use yii\base\InvalidConfigException;
use yiiunit\framework\mutex\mocks\DumbMutex;
use yiiunit\TestCase;
/**
* Class RetryAcquireTraitTest.
*
* @group mutex
*
* @author Robert Korulczyk <robert@korulczyk.pl>
*/
class RetryAcquireTraitTest extends TestCase
{
/**
* @throws InvalidConfigException
*/
public function testRetryAcquire()
{
$mutexName = __FUNCTION__;
$mutexOne = $this->createMutex();
$mutexTwo = $this->createMutex();
$this->assertTrue($mutexOne->acquire($mutexName));
$this->assertFalse($mutexTwo->acquire($mutexName, 1));
$this->assertGreaterThanOrEqual(1, count($mutexTwo->attemptsTime));
$this->assertLessThanOrEqual(20, count($mutexTwo->attemptsTime));
foreach ($mutexTwo->attemptsTime as $i => $attemptTime) {
if ($i === 0) {
continue;
}
$intervalMilliseconds = ($mutexTwo->attemptsTime[$i] - $mutexTwo->attemptsTime[$i-1]) * 1000;
$this->assertGreaterThanOrEqual($mutexTwo->retryDelay, $intervalMilliseconds);
}
}
/**
* @return DumbMutex
* @throws InvalidConfigException
*/
private function createMutex()
{
return Yii::createObject([
'class' => DumbMutex::className(),
]);
}
}