-
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 #5 from bigcommerce/final-test-for-before-enqueue
TOOLING-255: Finalizing resque pause
- Loading branch information
Showing
15 changed files
with
552 additions
and
564 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,4 +1,5 @@ | ||
(c) 2014 Wedy Chainy <[email protected]> | ||
Copyright (C) Bigcommerce, 2014 | ||
All rights reserved. | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining | ||
a copy of this software and associated documentation files (the | ||
|
@@ -17,4 +18,4 @@ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | |
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE | ||
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION | ||
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION | ||
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
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 |
---|---|---|
@@ -0,0 +1,118 @@ | ||
<?php | ||
namespace Resque\Integration\Plugins\Tests; | ||
|
||
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 PauseTest extends PHPUnit_Framework_TestCase | ||
{ | ||
/** @var Pause */ | ||
protected $pauser = null; | ||
|
||
public static function setUpBeforeClass() | ||
{ | ||
$testMisc = realpath(__DIR__ . '/misc/'); | ||
$redisConf = "$testMisc/redis.conf"; | ||
|
||
// Attempt to start our own redis instance for tesitng. | ||
exec('which redis-server', $output, $returnVar); | ||
if ($returnVar != 0) { | ||
echo "Cannot find redis-server in path. Please make sure redis is installed.\n"; | ||
exit(1); | ||
} | ||
|
||
exec("cd $testMisc; redis-server $redisConf", $output, $returnVar); | ||
usleep(500000); | ||
if ($returnVar != 0) { | ||
echo "Cannot start redis-server.\n"; | ||
exit(1); | ||
} | ||
|
||
// 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]); | ||
} | ||
|
||
public function setUp() | ||
{ | ||
Resque::redis()->flushAll(); | ||
$this->pauser = new Pause(); | ||
} | ||
|
||
public function testPause() | ||
{ | ||
// Pause non-paused queue | ||
Resque::enqueue('upgrade:test', 'test'); | ||
Resque::enqueue('upgrade:test', 'test'); | ||
$this->assertEquals(2, Resque::size('upgrade:test')); | ||
$this->assertTrue($this->pauser->pause('upgrade:test')); | ||
$this->assertEquals(0, Resque::size('upgrade:test')); | ||
|
||
// Pause paused queue | ||
$this->assertFalse($this->pauser->pause('upgrade:test')); | ||
|
||
// Pause non-existent queue | ||
$this->assertFalse($this->pauser->pause('upgrade:test2')); | ||
} | ||
|
||
public function testResume() | ||
{ | ||
// Resume paused queue | ||
Resque::enqueue('upgrade:test3', 'test'); | ||
Resque::enqueue('upgrade:test3', 'test'); | ||
$this->pauser->pause('upgrade:test3'); | ||
$this->assertTrue($this->pauser->resume('upgrade:test3')); | ||
$this->assertEquals(2, Resque::size('upgrade:test3')); | ||
|
||
// Resume non-paused queue | ||
Resque::enqueue('upgrade:test4', 'test'); | ||
$this->assertFalse($this->pauser->resume('upgrade:test4')); | ||
|
||
// Resume non-existent queue | ||
$this->assertFalse($this->pauser->resume('upgrade:idontexist')); | ||
} | ||
|
||
public function testIsPaused() | ||
{ | ||
// Paused queue | ||
Resque::enqueue('upgrade:test5', 'test'); | ||
$this->pauser->pause('upgrade:test5'); | ||
$this->assertTrue($this->pauser->isPaused('upgrade:test5')); | ||
|
||
// Non-paused queue | ||
Resque::enqueue('upgrade:test6', 'test'); | ||
$this->assertFalse($this->pauser->isPaused('upgrade:test6')); | ||
|
||
// Paused and resumed queue | ||
Resque::enqueue('upgrade:test7', 'test'); | ||
$this->pauser->pause('upgrade:test7'); | ||
$this->assertTrue($this->pauser->isPaused('upgrade:test7')); | ||
$this->pauser->resume('upgrade:test7'); | ||
$this->assertFalse($this->pauser->isPaused('upgrade:test7')); | ||
|
||
// Non-existent queue | ||
$this->assertFalse($this->pauser->isPaused('upgrade:istilldontexist')); | ||
} | ||
|
||
public function testPauseCallback() | ||
{ | ||
$this->pauser->pause('upgrade:test8'); | ||
Resque::enqueue('upgrade:test8', 'test'); | ||
|
||
$this->assertEquals(0, Resque::redis()->llen('queue:upgrade:test8')); | ||
$this->assertEquals(1, Resque::redis()->llen('temp:upgrade:test8')); | ||
} | ||
} |
File renamed without changes.
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,147 @@ | ||
<?php | ||
namespace Unit\Resque\Plugins\Tests; | ||
|
||
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 extends PHPUnit_Framework_TestCase | ||
{ | ||
/** @var \PHPUnit_Framework_MockObject_MockObject */ | ||
protected $redisMock = null; | ||
|
||
public function setUp() | ||
{ | ||
$this->redisMock = $this->getMockBuilder('Resque_Redis') | ||
->disableOriginalConstructor() | ||
->setMethods(array( | ||
'sadd', | ||
'srem', | ||
'getPrefix', | ||
'rename', | ||
'sismember', | ||
)) | ||
->getMock(); | ||
} | ||
|
||
public function createDataProvider() | ||
{ | ||
return array( | ||
array(true, true), | ||
array(false, false), | ||
); | ||
} | ||
|
||
/** | ||
* @dataProvider createDataProvider | ||
* @param bool $saddSuccess | ||
*/ | ||
public function testCreate($saddSuccess, $returnSuccess) | ||
{ | ||
$this->redisMock | ||
->expects($this->once()) | ||
->method('sadd') | ||
->with($this->equalTo('pauses'), $this->equalTo('upgrade:test')) | ||
->willReturn($saddSuccess); | ||
|
||
$pauser = new JobPauser($this->redisMock, 'resqueFaker:'); | ||
$this->assertEquals($returnSuccess, $pauser->pause('upgrade:test')); | ||
} | ||
|
||
public function removeDataProvider() | ||
{ | ||
return array( | ||
array(true, true), | ||
array(false, false), | ||
); | ||
} | ||
|
||
/** | ||
* @dataProvider removeDataProvider | ||
* @param bool $sremSuccess | ||
*/ | ||
public function testRemove($sremSuccess, $returnSuccess) | ||
{ | ||
$this->redisMock | ||
->expects($this->once()) | ||
->method('srem') | ||
->with($this->equalTo('pauses'), $this->equalTo('upgrade:test2')) | ||
->willReturn($sremSuccess); | ||
|
||
$pauser = new JobPauser($this->redisMock, 'resqueFaker:'); | ||
$this->assertEquals($returnSuccess, $pauser->resume('upgrade:test2')); | ||
} | ||
|
||
public function renameDataprovider() | ||
{ | ||
return array( | ||
array(true, true), | ||
array(false, false), | ||
); | ||
} | ||
|
||
/** | ||
* @dataProvider renameDataProvider | ||
* @param bool $renameSuccess | ||
* @param bool $expectedResult | ||
*/ | ||
public function testRenameToTemp($renameSuccess, $expectedResult) | ||
{ | ||
$this->redisMock | ||
->expects($this->once()) | ||
->method('rename') | ||
->with($this->equalTo('queue:upgrade:test3'), $this->equalTo('resqueFaker:temp:upgrade:test3')) | ||
->willReturn($renameSuccess); | ||
|
||
$pauser = new JobPauser($this->redisMock, 'resqueFaker:'); | ||
$this->assertEquals($expectedResult, $pauser->renameToTemp('upgrade:test3')); | ||
} | ||
|
||
/** | ||
* @dataProvider renameDataProvider Reuses the renaming provider because the cases are the same | ||
* @param bool $renameSuccess | ||
* @param bool $expectedResult | ||
*/ | ||
public function testRenameBackFromTemp($renameSuccess, $expectedResult) | ||
{ | ||
$this->redisMock | ||
->expects($this->once()) | ||
->method('rename') | ||
->with($this->equalTo('temp:upgrade:test3'), $this->equalTo('resqueFaker:queue:upgrade:test3')) | ||
->willReturn($renameSuccess); | ||
|
||
$pauser = new JobPauser($this->redisMock, 'resqueFaker:'); | ||
$this->assertEquals($expectedResult, $pauser->renameBackFromTemp('upgrade:test3')); | ||
} | ||
|
||
public function existsDataProvider() | ||
{ | ||
return array( | ||
array(true, true), | ||
array(false, false), | ||
); | ||
} | ||
|
||
/** | ||
* @dataProvider existsDataProvider | ||
* @param bool $existsSuccess | ||
* @param bool $expectedResult | ||
*/ | ||
public function testExists($existsSuccess, $expectedResult) | ||
{ | ||
$this->redisMock | ||
->expects($this->once()) | ||
->method('sismember') | ||
->with($this->equalTo('pauses'), $this->equalTo('upgrade:test8')) | ||
->willReturn($existsSuccess); | ||
|
||
$pauser = new JobPauser($this->redisMock, 'resqueFaker:'); | ||
$this->assertEquals($expectedResult, $pauser->isPaused('upgrade:test8')); | ||
} | ||
} |
Oops, something went wrong.