Files
yii2/tests/framework/mutex/mocks/DumbMutex.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

56 lines
1.0 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\mocks;
use yii\mutex\Mutex;
use yii\mutex\RetryAcquireTrait;
/**
* Class DumbMutex.
*
* @author Robert Korulczyk <robert@korulczyk.pl>
*/
class DumbMutex extends Mutex
{
use RetryAcquireTrait;
public $attemptsTime = [];
public static $locked = false;
/**
* {@inheritdoc}
*/
protected function acquireLock($name, $timeout = 0)
{
return $this->retryAcquire($timeout, function () {
$this->attemptsTime[] = \microtime(true);
if (!static::$locked) {
static::$locked = true;
return true;
}
return false;
});
}
/**
* {@inheritdoc}
*/
protected function releaseLock($name)
{
if (static::$locked) {
static::$locked = false;
return true;
}
return false;
}
}