Skip to content

Commit

Permalink
Merge pull request #30 from sdoatput/fix-phpredismutex-tests
Browse files Browse the repository at this point in the history
Fix PHPRedisMutex tests
  • Loading branch information
Willem Stuursma-Ruwen authored Dec 4, 2018
2 parents 5078c36 + b58b12a commit 6925de8
Showing 1 changed file with 34 additions and 6 deletions.
40 changes: 34 additions & 6 deletions tests/mutex/PHPRedisMutexTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,45 @@ class PHPRedisMutexTest extends TestCase
* @var PHPRedisMutex The SUT.
*/
private $mutex;

protected function setUp()
{
parent::setUp();

$uris = explode(",", getenv("REDIS_URIS") ?: "redis://localhost");

foreach ($uris as $redisUri) {
$uri = parse_url($redisUri);

$connection = new Redis();
// original Redis::set and Redis::eval calls will reopen the connection
$connection = new class extends Redis
{
private $is_closed = false;

public function close()
{
parent::close();
$this->is_closed = true;
}

public function set($key, $value, $timeout = 0)
{
if ($this->is_closed) {
throw new \RedisException('Connection is closed');
}

return parent::set($key, $value, $timeout);
}

public function eval($script, $args = array(), $numKeys = 0)
{
if ($this->is_closed) {
throw new \RedisException('Connection is closed');
}

return parent::eval($script, $args, $numKeys);
}
};

if (!empty($uri["port"])) {
$connection->connect($uri["host"], $uri["port"]);
Expand All @@ -65,7 +93,7 @@ private function closeMajorityConnections()
}
}

private function closeMinortyConnections()
private function closeMinorityConnections()
{
if (count($this->connections) === 1) {
$this->markTestSkipped("Cannot test this with only a single Redis server");
Expand Down Expand Up @@ -120,7 +148,7 @@ public function testSynchronizedWorks($serialization)

public function testResistantToPartialClusterFailuresForAcquiringLock()
{
$this->closeMinortyConnections();
$this->closeMinorityConnections();

$this->assertSame("test", $this->mutex->synchronized(function (): string {
return "test";
Expand All @@ -130,7 +158,7 @@ public function testResistantToPartialClusterFailuresForAcquiringLock()
public function testResistantToPartialClusterFailuresForReleasingLock()
{
$this->assertNull($this->mutex->synchronized(function () {
$this->closeMinortyConnections();
$this->closeMinorityConnections();
return null;
}));
}
Expand Down

0 comments on commit 6925de8

Please sign in to comment.