-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8 from gabelimon/TECHOPS-17109-pause-fixes
TECHOPS-17109: Added support for a Predis Resque backend
- Loading branch information
Showing
10 changed files
with
165 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,20 @@ | ||
<?php | ||
namespace Resque\Integration\Plugins\Tests; | ||
namespace Resque\Plugins\Tests\Integration; | ||
|
||
use Resque; | ||
use Resque\Plugins\Pause; | ||
use PHPUnit_Framework_TestCase; | ||
|
||
/** | ||
* Pause tests. | ||
* | ||
* @package PHP Resque Pause | ||
* @author Wedy Chainy <[email protected]> | ||
* @license http://www.opensource.org/licenses/mit-license.php | ||
* Class PauseTestBase Provides all cases for testing the Pause class | ||
* @package Resque\Plugins\Tests\Integration | ||
*/ | ||
class PauseTest extends PHPUnit_Framework_TestCase | ||
class PauseTestBase extends PHPUnit_Framework_TestCase | ||
{ | ||
/** @var Pause */ | ||
protected $pauser = null; | ||
|
||
public static function setUpBeforeClass() | ||
protected static function setupRedis() | ||
{ | ||
$testMisc = realpath(__DIR__ . '/misc/'); | ||
$redisConf = "$testMisc/redis.conf"; | ||
|
@@ -30,20 +27,25 @@ public static function setUpBeforeClass() | |
} | ||
|
||
exec("cd $testMisc; redis-server $redisConf", $output, $returnVar); | ||
usleep(500000); | ||
usleep(500000); // Wait to for the server to start | ||
if ($returnVar != 0) { | ||
echo "Cannot start redis-server.\n"; | ||
exit(1); | ||
} | ||
} | ||
|
||
protected static function getRedisPort() | ||
{ | ||
$testMisc = realpath(__DIR__ . '/misc/'); | ||
$redisConf = "$testMisc/redis.conf"; | ||
|
||
// Get redis port from conf | ||
$config = file_get_contents($redisConf); | ||
if (!preg_match('#^\s*port\s+([0-9]+)#m', $config, $matches)) { | ||
echo "Could not determine redis port from redis.conf"; | ||
exit(1); | ||
} | ||
|
||
Resque::setBackend('localhost:' . $matches[1]); | ||
return $matches[1]; | ||
} | ||
|
||
public function setUp() | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?php | ||
namespace Resque\Plugins\Tests\Integration; | ||
|
||
use Resque; | ||
use Predis\Client; | ||
|
||
/** | ||
* Class PauseWithPredisTest Runs the PauseTestBase cases with a Predis backend | ||
* @package Resque\Plugins\Tests\Integration | ||
*/ | ||
class PauseWithPredisTest extends PauseTestBase | ||
{ | ||
public static function setUpBeforeClass() | ||
{ | ||
static::setupRedis(); | ||
|
||
$clientParams = array( | ||
'host' => 'localhost', | ||
'port' => static::getRedisPort(), | ||
'prefix' => 'resque:', | ||
); | ||
Resque::setBackend(function () use ($clientParams) { | ||
return new Client($clientParams); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?php | ||
namespace Resque\Plugins\Tests\Integration; | ||
|
||
use Resque; | ||
|
||
/** | ||
* Class PauseWithResqueRedisTest Runs the PauseTestBase cases with a Resque_Redis backend | ||
* @package Resque\Plugins\Tests\Integration | ||
*/ | ||
class PauseWithResqueRedisTest extends PauseTestBase | ||
{ | ||
public static function setUpBeforeClass() | ||
{ | ||
static::setupRedis(); | ||
Resque::setBackend('localhost:' . static::getRedisPort()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,7 @@ | ||
daemonize yes | ||
pidfile ./redis.pid | ||
port 6379 | ||
pidfile redis.pid | ||
port 63791 | ||
bind 127.0.0.1 | ||
timeout 300 | ||
dbfilename dump.rdb | ||
dir ./ | ||
loglevel debug | ||
loglevel debug |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,35 @@ | ||
<?php | ||
namespace Unit\Resque\Plugins\Tests; | ||
namespace Resque\Plugins\Tests\Unit; | ||
|
||
use Resque\Plugins\JobPauser; | ||
use PHPUnit_Framework_TestCase; | ||
|
||
/** | ||
* Job tests. | ||
* | ||
* @package Resque/Tests | ||
* @author Wedy Chainy <[email protected]> | ||
* @license http://www.opensource.org/licenses/mit-license.php | ||
* Class JobPauserTest | ||
* @package Resque\Plugins\Tests\Unit | ||
*/ | ||
class JobPauserTest extends PHPUnit_Framework_TestCase | ||
{ | ||
/** @var \PHPUnit_Framework_MockObject_MockObject */ | ||
protected $redisMock = null; | ||
protected $resqueRedisMock = null; | ||
/** @var \PHPUnit_Framework_MockObject_MockObject */ | ||
protected $predisMock = null; | ||
|
||
public function setUp() | ||
{ | ||
$this->redisMock = $this->getMockBuilder('Resque_Redis') | ||
$this->resqueRedisMock = $this->getMockBuilder('Resque_Redis') | ||
->disableOriginalConstructor() | ||
->setMethods(array( | ||
'sadd', | ||
'srem', | ||
'getPrefix', | ||
'llen', | ||
'rename', | ||
'sismember', | ||
)) | ||
->getMock(); | ||
|
||
$this->predisMock = $this->getMockBuilder('Predis\Client') | ||
->disableOriginalConstructor() | ||
->setMethods(array( | ||
'sadd', | ||
|
@@ -42,16 +53,17 @@ public function pauseDataProvider() | |
/** | ||
* @dataProvider pauseDataProvider | ||
* @param bool $saddSuccess | ||
* @param bool $returnSuccess | ||
*/ | ||
public function testPause($saddSuccess, $returnSuccess) | ||
{ | ||
$this->redisMock | ||
$this->resqueRedisMock | ||
->expects($this->once()) | ||
->method('sadd') | ||
->with($this->equalTo('pauses'), $this->equalTo('upgrade:test')) | ||
->willReturn($saddSuccess); | ||
|
||
$pauser = new JobPauser($this->redisMock, 'resqueFaker:'); | ||
$pauser = new JobPauser($this->resqueRedisMock, 'resqueFaker:'); | ||
$this->assertEquals($returnSuccess, $pauser->pause('upgrade:test')); | ||
} | ||
|
||
|
@@ -73,19 +85,19 @@ public function resumeDataProvider() | |
*/ | ||
public function testResume($isPaused, $sremSuccess, $returnSuccess) | ||
{ | ||
$this->redisMock | ||
$this->resqueRedisMock | ||
->expects($this->once()) | ||
->method('sismember') | ||
->with($this->equalTo('pauses')) | ||
->willReturn($isPaused); | ||
|
||
$this->redisMock | ||
$this->resqueRedisMock | ||
->expects($isPaused ? $this->once() : $this->never()) | ||
->method('srem') | ||
->with($this->equalTo('pauses'), $this->equalTo('upgrade:test2')) | ||
->willReturn($sremSuccess); | ||
|
||
$pauser = new JobPauser($this->redisMock, 'resqueFaker:'); | ||
$pauser = new JobPauser($this->resqueRedisMock, 'resqueFaker:'); | ||
$this->assertEquals($returnSuccess, $pauser->resume('upgrade:test2')); | ||
} | ||
|
||
|
@@ -107,19 +119,44 @@ public function renameDataprovider() | |
*/ | ||
public function testRenameToTemp($queueLength, $renameSuccess, $expectedResult) | ||
{ | ||
$this->redisMock | ||
$this->resqueRedisMock | ||
->expects($this->once()) | ||
->method('llen') | ||
->with($this->equalTo('queue:upgrade:test3')) | ||
->willReturn($queueLength); | ||
|
||
$this->redisMock | ||
$this->resqueRedisMock | ||
->expects($queueLength ? $this->once() : $this->never()) | ||
->method('rename') | ||
->with($this->equalTo('queue:upgrade:test3'), $this->equalTo('resqueFaker:temp:upgrade:test3')) | ||
->willReturn($renameSuccess); | ||
|
||
$pauser = new JobPauser($this->redisMock, 'resqueFaker:'); | ||
$pauser = new JobPauser($this->resqueRedisMock, 'resqueFaker:'); | ||
$this->assertEquals($expectedResult, $pauser->renameToTemp('upgrade:test3')); | ||
} | ||
|
||
|
||
/** | ||
* @dataProvider renameDataProvider | ||
* @param int $queueLength | ||
* @param bool $renameSuccess | ||
* @param bool $expectedResult | ||
*/ | ||
public function testRenameToTempWithPredis($queueLength, $renameSuccess, $expectedResult) | ||
{ | ||
$this->predisMock | ||
->expects($this->once()) | ||
->method('llen') | ||
->with($this->equalTo('queue:upgrade:test3')) | ||
->willReturn($queueLength); | ||
|
||
$this->predisMock | ||
->expects($queueLength ? $this->once() : $this->never()) | ||
->method('rename') | ||
->with($this->equalTo('queue:upgrade:test3'), $this->equalTo('temp:upgrade:test3')) | ||
->willReturn($renameSuccess); | ||
|
||
$pauser = new JobPauser($this->predisMock, 'resqueFaker:'); | ||
$this->assertEquals($expectedResult, $pauser->renameToTemp('upgrade:test3')); | ||
} | ||
|
||
|
@@ -131,19 +168,43 @@ public function testRenameToTemp($queueLength, $renameSuccess, $expectedResult) | |
*/ | ||
public function testRenameBackFromTemp($queueLength, $renameSuccess, $expectedResult) | ||
{ | ||
$this->redisMock | ||
$this->resqueRedisMock | ||
->expects($this->once()) | ||
->method('llen') | ||
->with($this->equalTo('temp:upgrade:test3')) | ||
->willReturn($queueLength); | ||
|
||
$this->redisMock | ||
$this->resqueRedisMock | ||
->expects($queueLength ? $this->once() : $this->never()) | ||
->method('rename') | ||
->with($this->equalTo('temp:upgrade:test3'), $this->equalTo('resqueFaker:queue:upgrade:test3')) | ||
->willReturn($renameSuccess); | ||
|
||
$pauser = new JobPauser($this->redisMock, 'resqueFaker:'); | ||
$pauser = new JobPauser($this->resqueRedisMock, 'resqueFaker:'); | ||
$this->assertEquals($expectedResult, $pauser->renameBackFromTemp('upgrade:test3')); | ||
} | ||
|
||
/** | ||
* @dataProvider renameDataProvider Reuses the renaming provider because the cases are the same | ||
* @param int $queueLength | ||
* @param bool $renameSuccess | ||
* @param bool $expectedResult | ||
*/ | ||
public function testRenameBackFromTempWithPredis($queueLength, $renameSuccess, $expectedResult) | ||
{ | ||
$this->predisMock | ||
->expects($this->once()) | ||
->method('llen') | ||
->with($this->equalTo('temp:upgrade:test3')) | ||
->willReturn($queueLength); | ||
|
||
$this->predisMock | ||
->expects($queueLength ? $this->once() : $this->never()) | ||
->method('rename') | ||
->with($this->equalTo('temp:upgrade:test3'), $this->equalTo('queue:upgrade:test3')) | ||
->willReturn($renameSuccess); | ||
|
||
$pauser = new JobPauser($this->predisMock, 'resqueFaker:'); | ||
$this->assertEquals($expectedResult, $pauser->renameBackFromTemp('upgrade:test3')); | ||
} | ||
|
||
|
@@ -162,13 +223,13 @@ public function isPausedDataProvider() | |
*/ | ||
public function testIsPaused($existsSuccess, $expectedResult) | ||
{ | ||
$this->redisMock | ||
$this->resqueRedisMock | ||
->expects($this->once()) | ||
->method('sismember') | ||
->with($this->equalTo('pauses'), $this->equalTo('upgrade:test8')) | ||
->willReturn($existsSuccess); | ||
|
||
$pauser = new JobPauser($this->redisMock, 'resqueFaker:'); | ||
$pauser = new JobPauser($this->resqueRedisMock, 'resqueFaker:'); | ||
$this->assertEquals($expectedResult, $pauser->isPaused('upgrade:test8')); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.