2010-04-18 13:58:43 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Resque_Job tests.
|
|
|
|
*
|
|
|
|
* @package Resque/Tests
|
2012-10-13 06:55:41 +00:00
|
|
|
* @author Chris Boulton <chris@bigcommerce.com>
|
2010-04-18 13:58:43 +00:00
|
|
|
* @license http://www.opensource.org/licenses/mit-license.php
|
|
|
|
*/
|
|
|
|
class Resque_Tests_JobTest extends Resque_Tests_TestCase
|
|
|
|
{
|
|
|
|
protected $worker;
|
|
|
|
|
|
|
|
public function setUp()
|
|
|
|
{
|
|
|
|
parent::setUp();
|
|
|
|
|
|
|
|
// Register a worker to test with
|
|
|
|
$this->worker = new Resque_Worker('jobs');
|
2013-06-25 17:06:37 +00:00
|
|
|
$this->worker->setLogger(new Resque_Log());
|
2010-04-18 13:58:43 +00:00
|
|
|
$this->worker->registerWorker();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testJobCanBeQueued()
|
|
|
|
{
|
|
|
|
$this->assertTrue((bool)Resque::enqueue('jobs', 'Test_Job'));
|
|
|
|
}
|
|
|
|
|
2015-02-02 22:00:16 +00:00
|
|
|
/**
|
|
|
|
* @expectedException Resque_RedisException
|
|
|
|
*/
|
|
|
|
public function testRedisErrorThrowsExceptionOnJobCreation()
|
|
|
|
{
|
|
|
|
Resque::setBackend('redis://255.255.255.255:1234');
|
|
|
|
Resque::enqueue('jobs', 'This is a test');
|
|
|
|
}
|
|
|
|
|
2010-04-18 13:58:43 +00:00
|
|
|
public function testQeueuedJobCanBeReserved()
|
|
|
|
{
|
|
|
|
Resque::enqueue('jobs', 'Test_Job');
|
|
|
|
|
|
|
|
$job = Resque_Job::reserve('jobs');
|
|
|
|
if($job == false) {
|
|
|
|
$this->fail('Job could not be reserved.');
|
|
|
|
}
|
|
|
|
$this->assertEquals('jobs', $job->queue);
|
2010-04-19 00:35:50 +00:00
|
|
|
$this->assertEquals('Test_Job', $job->payload['class']);
|
2010-04-18 13:58:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @expectedException InvalidArgumentException
|
|
|
|
*/
|
2010-04-19 00:35:50 +00:00
|
|
|
public function testObjectArgumentsCannotBePassedToJob()
|
2010-04-18 13:58:43 +00:00
|
|
|
{
|
2010-04-19 00:35:50 +00:00
|
|
|
$args = new stdClass;
|
|
|
|
$args->test = 'somevalue';
|
|
|
|
Resque::enqueue('jobs', 'Test_Job', $args);
|
2010-04-18 13:58:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testQueuedJobReturnsExactSamePassedInArguments()
|
|
|
|
{
|
2010-04-19 00:35:50 +00:00
|
|
|
$args = array(
|
|
|
|
'int' => 123,
|
|
|
|
'numArray' => array(
|
|
|
|
1,
|
|
|
|
2,
|
|
|
|
),
|
|
|
|
'assocArray' => array(
|
|
|
|
'key1' => 'value1',
|
|
|
|
'key2' => 'value2'
|
|
|
|
),
|
2010-04-18 13:58:43 +00:00
|
|
|
);
|
|
|
|
Resque::enqueue('jobs', 'Test_Job', $args);
|
|
|
|
$job = Resque_Job::reserve('jobs');
|
|
|
|
|
2011-09-16 05:30:36 +00:00
|
|
|
$this->assertEquals($args, $job->getArguments());
|
2010-04-18 13:58:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testAfterJobIsReservedItIsRemoved()
|
|
|
|
{
|
|
|
|
Resque::enqueue('jobs', 'Test_Job');
|
|
|
|
Resque_Job::reserve('jobs');
|
|
|
|
$this->assertFalse(Resque_Job::reserve('jobs'));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testRecreatedJobMatchesExistingJob()
|
|
|
|
{
|
2010-04-19 00:35:50 +00:00
|
|
|
$args = array(
|
|
|
|
'int' => 123,
|
|
|
|
'numArray' => array(
|
|
|
|
1,
|
|
|
|
2,
|
|
|
|
),
|
|
|
|
'assocArray' => array(
|
|
|
|
'key1' => 'value1',
|
|
|
|
'key2' => 'value2'
|
|
|
|
),
|
2010-04-18 13:58:43 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
Resque::enqueue('jobs', 'Test_Job', $args);
|
|
|
|
$job = Resque_Job::reserve('jobs');
|
|
|
|
|
|
|
|
// Now recreate it
|
|
|
|
$job->recreate();
|
|
|
|
|
|
|
|
$newJob = Resque_Job::reserve('jobs');
|
2010-04-19 00:35:50 +00:00
|
|
|
$this->assertEquals($job->payload['class'], $newJob->payload['class']);
|
2013-05-12 05:40:35 +00:00
|
|
|
$this->assertEquals($job->getArguments(), $newJob->getArguments());
|
2010-04-18 13:58:43 +00:00
|
|
|
}
|
|
|
|
|
2011-11-22 16:55:40 +00:00
|
|
|
|
2010-04-18 13:58:43 +00:00
|
|
|
public function testFailedJobExceptionsAreCaught()
|
|
|
|
{
|
2010-04-19 00:35:50 +00:00
|
|
|
$payload = array(
|
|
|
|
'class' => 'Failing_Job',
|
|
|
|
'args' => null
|
|
|
|
);
|
2010-04-18 13:58:43 +00:00
|
|
|
$job = new Resque_Job('jobs', $payload);
|
|
|
|
$job->worker = $this->worker;
|
|
|
|
|
|
|
|
$this->worker->perform($job);
|
|
|
|
|
|
|
|
$this->assertEquals(1, Resque_Stat::get('failed'));
|
|
|
|
$this->assertEquals(1, Resque_Stat::get('failed:'.$this->worker));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @expectedException Resque_Exception
|
|
|
|
*/
|
|
|
|
public function testJobWithoutPerformMethodThrowsException()
|
|
|
|
{
|
|
|
|
Resque::enqueue('jobs', 'Test_Job_Without_Perform_Method');
|
|
|
|
$job = $this->worker->reserve();
|
|
|
|
$job->worker = $this->worker;
|
|
|
|
$job->perform();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @expectedException Resque_Exception
|
|
|
|
*/
|
|
|
|
public function testInvalidJobThrowsException()
|
|
|
|
{
|
|
|
|
Resque::enqueue('jobs', 'Invalid_Job');
|
|
|
|
$job = $this->worker->reserve();
|
|
|
|
$job->worker = $this->worker;
|
|
|
|
$job->perform();
|
|
|
|
}
|
2010-04-20 00:02:34 +00:00
|
|
|
|
|
|
|
public function testJobWithSetUpCallbackFiresSetUp()
|
|
|
|
{
|
|
|
|
$payload = array(
|
|
|
|
'class' => 'Test_Job_With_SetUp',
|
|
|
|
'args' => array(
|
|
|
|
'somevar',
|
|
|
|
'somevar2',
|
|
|
|
),
|
|
|
|
);
|
|
|
|
$job = new Resque_Job('jobs', $payload);
|
|
|
|
$job->perform();
|
|
|
|
|
|
|
|
$this->assertTrue(Test_Job_With_SetUp::$called);
|
|
|
|
}
|
|
|
|
|
2011-03-27 07:42:46 +00:00
|
|
|
public function testJobWithTearDownCallbackFiresTearDown()
|
2010-04-20 00:02:34 +00:00
|
|
|
{
|
|
|
|
$payload = array(
|
|
|
|
'class' => 'Test_Job_With_TearDown',
|
|
|
|
'args' => array(
|
|
|
|
'somevar',
|
|
|
|
'somevar2',
|
|
|
|
),
|
|
|
|
);
|
|
|
|
$job = new Resque_Job('jobs', $payload);
|
|
|
|
$job->perform();
|
|
|
|
|
|
|
|
$this->assertTrue(Test_Job_With_TearDown::$called);
|
|
|
|
}
|
2015-03-11 22:41:12 +00:00
|
|
|
|
|
|
|
public function testNamespaceNaming() {
|
|
|
|
$fixture = array(
|
|
|
|
array('test' => 'more:than:one:with:', 'assertValue' => 'more:than:one:with:'),
|
|
|
|
array('test' => 'more:than:one:without', 'assertValue' => 'more:than:one:without:'),
|
|
|
|
array('test' => 'resque', 'assertValue' => 'resque:'),
|
|
|
|
array('test' => 'resque:', 'assertValue' => 'resque:'),
|
|
|
|
);
|
|
|
|
|
|
|
|
foreach($fixture as $item) {
|
|
|
|
Resque_Redis::prefix($item['test']);
|
|
|
|
$this->assertEquals(Resque_Redis::getPrefix(), $item['assertValue']);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-11-22 16:55:40 +00:00
|
|
|
public function testJobWithNamespace()
|
|
|
|
{
|
2016-10-13 03:45:02 +00:00
|
|
|
Resque_Redis::prefix('php');
|
|
|
|
$queue = 'jobs';
|
|
|
|
$payload = array('another_value');
|
|
|
|
Resque::enqueue($queue, 'Test_Job_With_TearDown', $payload);
|
|
|
|
|
|
|
|
$this->assertEquals(Resque::queues(), array('jobs'));
|
|
|
|
$this->assertEquals(Resque::size($queue), 1);
|
|
|
|
|
|
|
|
Resque_Redis::prefix('resque');
|
|
|
|
$this->assertEquals(Resque::size($queue), 0);
|
2011-11-22 16:55:40 +00:00
|
|
|
}
|
2014-09-17 00:20:26 +00:00
|
|
|
|
|
|
|
public function testDequeueAll()
|
|
|
|
{
|
|
|
|
$queue = 'jobs';
|
|
|
|
Resque::enqueue($queue, 'Test_Job_Dequeue');
|
|
|
|
Resque::enqueue($queue, 'Test_Job_Dequeue');
|
|
|
|
$this->assertEquals(Resque::size($queue), 2);
|
|
|
|
$this->assertEquals(Resque::dequeue($queue), 2);
|
|
|
|
$this->assertEquals(Resque::size($queue), 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testDequeueMakeSureNotDeleteOthers()
|
|
|
|
{
|
|
|
|
$queue = 'jobs';
|
|
|
|
Resque::enqueue($queue, 'Test_Job_Dequeue');
|
|
|
|
Resque::enqueue($queue, 'Test_Job_Dequeue');
|
|
|
|
$other_queue = 'other_jobs';
|
|
|
|
Resque::enqueue($other_queue, 'Test_Job_Dequeue');
|
|
|
|
Resque::enqueue($other_queue, 'Test_Job_Dequeue');
|
|
|
|
$this->assertEquals(Resque::size($queue), 2);
|
|
|
|
$this->assertEquals(Resque::size($other_queue), 2);
|
|
|
|
$this->assertEquals(Resque::dequeue($queue), 2);
|
|
|
|
$this->assertEquals(Resque::size($queue), 0);
|
|
|
|
$this->assertEquals(Resque::size($other_queue), 2);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testDequeueSpecificItem()
|
|
|
|
{
|
|
|
|
$queue = 'jobs';
|
|
|
|
Resque::enqueue($queue, 'Test_Job_Dequeue1');
|
|
|
|
Resque::enqueue($queue, 'Test_Job_Dequeue2');
|
|
|
|
$this->assertEquals(Resque::size($queue), 2);
|
|
|
|
$test = array('Test_Job_Dequeue2');
|
|
|
|
$this->assertEquals(Resque::dequeue($queue, $test), 1);
|
|
|
|
$this->assertEquals(Resque::size($queue), 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testDequeueSpecificMultipleItems()
|
|
|
|
{
|
|
|
|
$queue = 'jobs';
|
|
|
|
Resque::enqueue($queue, 'Test_Job_Dequeue1');
|
|
|
|
Resque::enqueue($queue, 'Test_Job_Dequeue2');
|
|
|
|
Resque::enqueue($queue, 'Test_Job_Dequeue3');
|
|
|
|
$this->assertEquals(Resque::size($queue), 3);
|
|
|
|
$test = array('Test_Job_Dequeue2', 'Test_Job_Dequeue3');
|
|
|
|
$this->assertEquals(Resque::dequeue($queue, $test), 2);
|
|
|
|
$this->assertEquals(Resque::size($queue), 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testDequeueNonExistingItem()
|
|
|
|
{
|
|
|
|
$queue = 'jobs';
|
|
|
|
Resque::enqueue($queue, 'Test_Job_Dequeue1');
|
|
|
|
Resque::enqueue($queue, 'Test_Job_Dequeue2');
|
|
|
|
Resque::enqueue($queue, 'Test_Job_Dequeue3');
|
|
|
|
$this->assertEquals(Resque::size($queue), 3);
|
|
|
|
$test = array('Test_Job_Dequeue4');
|
|
|
|
$this->assertEquals(Resque::dequeue($queue, $test), 0);
|
|
|
|
$this->assertEquals(Resque::size($queue), 3);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testDequeueNonExistingItem2()
|
|
|
|
{
|
|
|
|
$queue = 'jobs';
|
|
|
|
Resque::enqueue($queue, 'Test_Job_Dequeue1');
|
|
|
|
Resque::enqueue($queue, 'Test_Job_Dequeue2');
|
|
|
|
Resque::enqueue($queue, 'Test_Job_Dequeue3');
|
|
|
|
$this->assertEquals(Resque::size($queue), 3);
|
|
|
|
$test = array('Test_Job_Dequeue4', 'Test_Job_Dequeue1');
|
|
|
|
$this->assertEquals(Resque::dequeue($queue, $test), 1);
|
|
|
|
$this->assertEquals(Resque::size($queue), 2);
|
|
|
|
}
|
|
|
|
|
2014-09-21 08:21:08 +00:00
|
|
|
public function testDequeueItemID()
|
|
|
|
{
|
|
|
|
$queue = 'jobs';
|
|
|
|
Resque::enqueue($queue, 'Test_Job_Dequeue');
|
|
|
|
$qid = Resque::enqueue($queue, 'Test_Job_Dequeue');
|
|
|
|
$this->assertEquals(Resque::size($queue), 2);
|
|
|
|
$test = array('Test_Job_Dequeue' => $qid);
|
|
|
|
$this->assertEquals(Resque::dequeue($queue, $test), 1);
|
|
|
|
$this->assertEquals(Resque::size($queue), 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testDequeueWrongItemID()
|
|
|
|
{
|
|
|
|
$queue = 'jobs';
|
|
|
|
Resque::enqueue($queue, 'Test_Job_Dequeue');
|
|
|
|
$qid = Resque::enqueue($queue, 'Test_Job_Dequeue');
|
|
|
|
$this->assertEquals(Resque::size($queue), 2);
|
|
|
|
#qid right but class name is wrong
|
|
|
|
$test = array('Test_Job_Dequeue1' => $qid);
|
|
|
|
$this->assertEquals(Resque::dequeue($queue, $test), 0);
|
|
|
|
$this->assertEquals(Resque::size($queue), 2);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testDequeueWrongItemID2()
|
|
|
|
{
|
|
|
|
$queue = 'jobs';
|
|
|
|
Resque::enqueue($queue, 'Test_Job_Dequeue');
|
|
|
|
$qid = Resque::enqueue($queue, 'Test_Job_Dequeue');
|
|
|
|
$this->assertEquals(Resque::size($queue), 2);
|
|
|
|
$test = array('Test_Job_Dequeue' => 'r4nD0mH4sh3dId');
|
|
|
|
$this->assertEquals(Resque::dequeue($queue, $test), 0);
|
|
|
|
$this->assertEquals(Resque::size($queue), 2);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testDequeueItemWithArg()
|
|
|
|
{
|
|
|
|
$queue = 'jobs';
|
|
|
|
$arg = array('foo' => 1, 'bar' => 2);
|
|
|
|
Resque::enqueue($queue, 'Test_Job_Dequeue9');
|
|
|
|
Resque::enqueue($queue, 'Test_Job_Dequeue9', $arg);
|
|
|
|
$this->assertEquals(Resque::size($queue), 2);
|
|
|
|
$test = array('Test_Job_Dequeue9' => $arg);
|
|
|
|
$this->assertEquals(Resque::dequeue($queue, $test), 1);
|
|
|
|
#$this->assertEquals(Resque::size($queue), 1);
|
|
|
|
}
|
2014-11-10 14:33:42 +00:00
|
|
|
|
|
|
|
public function testDequeueSeveralItemsWithArgs()
|
|
|
|
{
|
|
|
|
// GIVEN
|
|
|
|
$queue = 'jobs';
|
|
|
|
$args = array('foo' => 1, 'bar' => 10);
|
|
|
|
$removeArgs = array('foo' => 1, 'bar' => 2);
|
|
|
|
Resque::enqueue($queue, 'Test_Job_Dequeue9', $args);
|
|
|
|
Resque::enqueue($queue, 'Test_Job_Dequeue9', $removeArgs);
|
|
|
|
Resque::enqueue($queue, 'Test_Job_Dequeue9', $removeArgs);
|
|
|
|
$this->assertEquals(Resque::size($queue), 3);
|
|
|
|
|
|
|
|
// WHEN
|
|
|
|
$test = array('Test_Job_Dequeue9' => $removeArgs);
|
|
|
|
$removedItems = Resque::dequeue($queue, $test);
|
|
|
|
|
|
|
|
// THEN
|
|
|
|
$this->assertEquals($removedItems, 2);
|
|
|
|
$this->assertEquals(Resque::size($queue), 1);
|
|
|
|
$item = Resque::pop($queue);
|
|
|
|
$this->assertInternalType('array', $item['args']);
|
|
|
|
$this->assertEquals(10, $item['args'][0]['bar'], 'Wrong items were dequeued from queue!');
|
|
|
|
}
|
2014-09-21 08:21:08 +00:00
|
|
|
|
|
|
|
public function testDequeueItemWithUnorderedArg()
|
|
|
|
{
|
|
|
|
$queue = 'jobs';
|
|
|
|
$arg = array('foo' => 1, 'bar' => 2);
|
|
|
|
$arg2 = array('bar' => 2, 'foo' => 1);
|
|
|
|
Resque::enqueue($queue, 'Test_Job_Dequeue');
|
|
|
|
Resque::enqueue($queue, 'Test_Job_Dequeue', $arg);
|
|
|
|
$this->assertEquals(Resque::size($queue), 2);
|
|
|
|
$test = array('Test_Job_Dequeue' => $arg2);
|
|
|
|
$this->assertEquals(Resque::dequeue($queue, $test), 1);
|
|
|
|
$this->assertEquals(Resque::size($queue), 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testDequeueItemWithiWrongArg()
|
|
|
|
{
|
|
|
|
$queue = 'jobs';
|
|
|
|
$arg = array('foo' => 1, 'bar' => 2);
|
|
|
|
$arg2 = array('foo' => 2, 'bar' => 3);
|
|
|
|
Resque::enqueue($queue, 'Test_Job_Dequeue');
|
|
|
|
Resque::enqueue($queue, 'Test_Job_Dequeue', $arg);
|
|
|
|
$this->assertEquals(Resque::size($queue), 2);
|
|
|
|
$test = array('Test_Job_Dequeue' => $arg2);
|
|
|
|
$this->assertEquals(Resque::dequeue($queue, $test), 0);
|
|
|
|
$this->assertEquals(Resque::size($queue), 2);
|
|
|
|
}
|
|
|
|
|
2016-10-14 03:41:19 +00:00
|
|
|
public function testUseDefaultFactoryToGetJobInstance()
|
2016-10-13 03:45:02 +00:00
|
|
|
{
|
|
|
|
$payload = array(
|
|
|
|
'class' => 'Some_Job_Class',
|
|
|
|
'args' => null
|
|
|
|
);
|
|
|
|
$job = new Resque_Job('jobs', $payload);
|
2016-10-14 03:41:19 +00:00
|
|
|
$instance = $job->getInstance();
|
|
|
|
$this->assertInstanceOf('Some_Job_Class', $instance);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testUseFactoryToGetJobInstance()
|
|
|
|
{
|
|
|
|
$payload = array(
|
|
|
|
'class' => 'Some_Job_Class',
|
|
|
|
'args' => array(array())
|
|
|
|
);
|
|
|
|
$job = new Resque_Job('jobs', $payload);
|
2016-10-13 03:45:02 +00:00
|
|
|
$factory = new Some_Stub_Factory();
|
|
|
|
$job->setJobFactory($factory);
|
|
|
|
$instance = $job->getInstance();
|
|
|
|
$this->assertInstanceOf('Resque_JobInterface', $instance);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testDoNotUseFactoryToGetInstance()
|
|
|
|
{
|
|
|
|
$payload = array(
|
|
|
|
'class' => 'Some_Job_Class',
|
2016-10-14 03:41:19 +00:00
|
|
|
'args' => array(array())
|
2016-10-13 03:45:02 +00:00
|
|
|
);
|
|
|
|
$job = new Resque_Job('jobs', $payload);
|
|
|
|
$factory = $this->getMock('Resque_Job_FactoryInterface');
|
|
|
|
$testJob = $this->getMock('Resque_JobInterface');
|
|
|
|
$factory->expects(self::never())->method('create')->will(self::returnValue($testJob));
|
|
|
|
$instance = $job->getInstance();
|
|
|
|
$this->assertInstanceOf('Resque_JobInterface', $instance);
|
|
|
|
}
|
2016-10-06 01:49:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
class Some_Job_Class implements Resque_JobInterface
|
|
|
|
{
|
|
|
|
|
2016-10-13 03:45:02 +00:00
|
|
|
/**
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function perform()
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
2014-09-17 00:20:26 +00:00
|
|
|
}
|
2016-10-12 08:22:31 +00:00
|
|
|
|
|
|
|
class Some_Stub_Factory implements Resque_Job_FactoryInterface
|
|
|
|
{
|
|
|
|
|
2016-10-13 03:45:02 +00:00
|
|
|
/**
|
|
|
|
* @param $className
|
|
|
|
* @param array $args
|
|
|
|
* @param $queue
|
|
|
|
* @return Resque_JobInterface
|
|
|
|
*/
|
2016-10-14 03:41:19 +00:00
|
|
|
public function create($className, $args, $queue)
|
2016-10-13 03:45:02 +00:00
|
|
|
{
|
|
|
|
return new Some_Job_Class();
|
|
|
|
}
|
2016-10-12 08:22:31 +00:00
|
|
|
}
|