Skip to content

Commit

Permalink
Merge pull request #8 from gabelimon/TECHOPS-17109-pause-fixes
Browse files Browse the repository at this point in the history
TECHOPS-17109: Added support for a Predis Resque backend
  • Loading branch information
gabelimon committed Jun 22, 2015
2 parents dfd6191 + 38a25a5 commit ab93438
Show file tree
Hide file tree
Showing 10 changed files with 165 additions and 63 deletions.
7 changes: 1 addition & 6 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,7 @@ php:
- 5.4
- 5.5
- 5.6
env:
- REDIS_STANDALONE=0
before_script:
- sh -c "if [ $REDIS_STANDALONE -eq 0 ]; then wget https://github.com/nicolasff/phpredis/archive/2.2.3.zip -O php-redis.zip && unzip php-redis.zip; fi"
- sh -c "if [ $REDIS_STANDALONE -eq 0 ]; then cd phpredis-2.2.3/ && phpize && ./configure && make && make install; fi"
- sh -c "if [ $REDIS_STANDALONE -eq 0 ]; then echo \"extension=redis.so\" >> `php --ini | grep \"Loaded Configuration\" | sed -e \"s|.*:\s*||\"`; fi"
install:
- composer install
script:
- composer lint
Expand Down
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";
Expand All @@ -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()
Expand Down
26 changes: 26 additions & 0 deletions Tests/Integration/PauseWithPredisTest.php
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);
});
}
}
17 changes: 17 additions & 0 deletions Tests/Integration/PauseWithResqueRedisTest.php
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());
}
}
7 changes: 3 additions & 4 deletions Tests/Integration/misc/redis.conf
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
103 changes: 82 additions & 21 deletions Tests/Unit/JobPauserTest.php
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',
Expand All @@ -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'));
}

Expand All @@ -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'));
}

Expand All @@ -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'));
}

Expand All @@ -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'));
}

Expand All @@ -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'));
}
}
7 changes: 4 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,16 @@
"type": "library",
"description": "php-resque-pause is a PHP port of resque-pause, which adds support for pausing / unpausing Resque jobs.",
"authors": [{
"name": "Wedy Chainy",
"email": "wedy.chainy@bigcommerce.com"
"name": "The Bigcommerce tooling team",
"email": "tools@bigcommerce.com"
}],
"require": {
"chrisboulton/php-resque": "dev-master"
},
"require-dev": {
"phpunit/phpunit": "4.5.*",
"squizlabs/php_codesniffer": "2.3.0"
"squizlabs/php_codesniffer": "2.3.0",
"predis/predis": "^1.0"
},
"autoload": {
"psr-4": {
Expand Down
1 change: 1 addition & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
<testsuites>
<testsuite name="Resque Test Suite">
<directory>./Tests/</directory>
<exclude>./Tests/Integration/PauseTestBase.php</exclude>
</testsuite>
</testsuites>

Expand Down
Loading

0 comments on commit ab93438

Please sign in to comment.