Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions framework/mutex/PgsqlMutex.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,19 @@ public function init()
}

/**
* Converts a string into two 16 bit integer keys using the SHA1 hash function.
* Converts a string into two 32 bit integer keys using the first 8 bytes of the SHA1 hash function.
* @param string $name
* @return array contains two 16 bit integer keys
* @return array contains two 32 bit integer keys
*/
private function getKeysFromName($name)
{
return array_values(unpack('n2', sha1($name, true)));
$keys = unpack('N2', substr(sha1($name, true), 0, 8));
Comment thread
terabytesoftw marked this conversation as resolved.
Resolved

// PostgreSQL advisory locks accept two signed 4-byte integers, while unpack('N')
// produces unsigned values. Convert values above the signed range accordingly.
return array_values(array_map(static function ($key) {
return $key > 0x7FFFFFFF ? $key - 0x100000000 : $key;
}, $keys));
Comment thread
terabytesoftw marked this conversation as resolved.
Resolved
}

/**
Expand Down
73 changes: 73 additions & 0 deletions tests/framework/mutex/PgsqlMutexKeyTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php

/**
* @link https://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license https://www.yiiframework.com/license/
*/

namespace yiiunit\framework\mutex;

use yii\db\Connection;
use yii\mutex\PgsqlMutex;
use yiiunit\TestCase;

/**
* Unit tests for PgsqlMutex lock name to key mapping that do not require a live PostgreSQL server.
*
* @group mutex
* @group db
* @group pgsql
*/
class PgsqlMutexKeyTest extends TestCase
{
/**
* @return PgsqlMutex
*/
private function createMutex()
{
return new PgsqlMutex(['db' => new Connection(['driverName' => 'pgsql'])]);
}

/**
* @param string $name
* @return array
*/
private function getKeys($name)
{
$mutex = $this->createMutex();
$method = new \ReflectionMethod(PgsqlMutex::class, 'getKeysFromName');
$method->setAccessible(true);
Comment thread
terabytesoftw marked this conversation as resolved.
Resolved

return $method->invoke($mutex, $name);
}

public function testKeysAreDeterministic()
{
$this->assertSame($this->getKeys('app:job:send-mail'), $this->getKeys('app:job:send-mail'));
}

public function testKnownVector()
{
$this->assertSame([1132950929, -43075096], $this->getKeys('app:job:send-mail'));
}

public function testKeysFitSigned32BitRange()
{
for ($i = 0; $i < 500; ++$i) {
foreach ($this->getKeys('name-' . $i) as $key) {
$this->assertGreaterThanOrEqual(-2147483648, $key);
$this->assertLessThanOrEqual(2147483647, $key);
}
}
}

public function testDistinctNamesProduceDistinctKeyPairs()
{
$pairs = [];
for ($i = 0; $i < 500; ++$i) {
$pairs[implode(',', $this->getKeys('lock-name-' . $i))] = true;
}
$this->assertCount(500, $pairs);
}
}
Loading