Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
95 changes: 75 additions & 20 deletions test/JobTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,53 @@

class JobTest extends QlessTest
{
/**
* @expectedException \Exception
*/
public function testGetInstanceWithInvalidClassThrows() {
$queue = new Qless\Queue("testQueue", $this->client);
$this->client->config->set('heartbeat', -10);
$this->client->config->set('grace-period', 0);

$testData = ["performMethod" => 'myPerformMethod', "payload" => "otherData"];
$queue->put("ClassDoesNotExist", "jobTestDEF", $testData);

$job1 = $queue->pop("worker-1")[0];

$job1->getInstance();
}

/**
* @expectedException \Exception
*/
public function testGetInstanceWithInvalidPerformMethodThrows() {
$queue = new Qless\Queue("testQueue", $this->client);
$this->client->config->set('heartbeat', -10);
$this->client->config->set('grace-period', 0);

$testData = ["performMethod" => 'myNonexistantMethod', "payload" => "otherData"];
$queue->put("TestWorkerImpl", "jobTestDEF", $testData);

$job1 = $queue->pop("worker-1")[0];

$job1->getInstance();
}

public function testGetInstanceReturnsHandler() {
$queue = new Qless\Queue("testQueue", $this->client);
$this->client->config->set('heartbeat', -10);
$this->client->config->set('grace-period', 0);

$testData = ["performMethod" => 'myPerformMethod', "payload" => "otherData"];
$queue->put("TestWorkerImpl", "jobTestDEF", $testData);

$job1 = $queue->pop("worker-1")[0];

$instance = $job1->getInstance();

$this->assertInstanceOf(TestWorkerImpl::class, $instance);
}

/**
* @expectedException \Qless\JobLostException
*/
Expand All @@ -13,7 +60,7 @@ public function testHeartbeatForInvalidJobThrows() {
$this->client->config->set('grace-period', 0);

$testData = ["performMethod" => 'myPerformMethod', "payload" => "otherData"];
$queue->put("Sample\\TestWorkerImpl", "jobTestDEF", $testData);
$queue->put("TestWorkerImpl", "jobTestDEF", $testData);

$job1 = $queue->pop("worker-1")[0];
$queue->pop("worker-2");
Expand All @@ -22,7 +69,7 @@ public function testHeartbeatForInvalidJobThrows() {

public function testCanGetCorrectTTL() {
$queue = new Qless\Queue("testQueue", $this->client);
$queue->put("Sample\\TestWorkerImpl", "jobTestDEF", []);
$queue->put("TestWorkerImpl", "jobTestDEF", []);
$job = $queue->pop("worker-1")[0];
$ttl = $job->ttl();
$this->assertGreaterThan(55, $ttl);
Expand All @@ -32,7 +79,7 @@ public function testCompleteJob() {
$queue = new Qless\Queue("testQueue", $this->client);

$testData = ["performMethod" => 'myPerformMethod', "payload" => "otherData"];
$queue->put("Sample\\TestWorkerImpl", "jobTestDEF", $testData);
$queue->put("TestWorkerImpl", "jobTestDEF", $testData);

$job1 = $queue->pop("worker-1")[0];
$res = $job1->complete();
Expand All @@ -43,7 +90,7 @@ public function testFailJobCannotBePopped() {
$queue = new Qless\Queue("testQueue", $this->client);

$testData = ["performMethod" => 'myPerformMethod', "payload" => "otherData"];
$queue->put("Sample\\TestWorkerImpl", "jid", $testData);
$queue->put("TestWorkerImpl", "jid", $testData);

$job1 = $queue->pop("worker-1")[0];
$res = $job1->fail('account', 'failed to connect');
Expand All @@ -53,11 +100,13 @@ public function testFailJobCannotBePopped() {
$this->assertEmpty($job1);
}

#region retry

public function testRetryDoesReturnJobAndDefaultsToFiveRetries() {
$queue = new Qless\Queue("testQueue", $this->client);

$testData = ["performMethod" => 'myPerformMethod', "payload" => "otherData"];
$queue->put("Sample\\TestWorkerImpl", "jid", $testData);
$queue->put("TestWorkerImpl", "jid", $testData);

$job1 = $queue->pop("worker-1")[0];
$remaining = $job1->retry('account', 'failed to connect');
Expand All @@ -71,7 +120,7 @@ public function testRetryDoesRespectRetryParameterWithOneRetry() {
$queue = new Qless\Queue("testQueue", $this->client);

$testData = ["performMethod" => 'myPerformMethod', "payload" => "otherData"];
$queue->put("Sample\\TestWorkerImpl", "jid", $testData, 0, 1);
$queue->put("TestWorkerImpl", "jid", $testData, 0, 1);

$job1 = $queue->pop("worker-1")[0];
$remaining = $job1->retry('account', 'failed to connect');
Expand All @@ -85,7 +134,7 @@ public function testRetryDoesReturnNegativeWhenNoMoreAvailable() {
$queue = new Qless\Queue("testQueue", $this->client);

$testData = ["performMethod" => 'myPerformMethod', "payload" => "otherData"];
$queue->put("Sample\\TestWorkerImpl", "jid", $testData, 0, 0);
$queue->put("TestWorkerImpl", "jid", $testData, 0, 0);

$job1 = $queue->pop("worker-1")[0];
$remaining = $job1->retry('account', 'failed to connect');
Expand All @@ -96,7 +145,7 @@ public function testRetryTransitionsToFailedWhenExhaustedRetries() {
$queue = new Qless\Queue("testQueue", $this->client);

$testData = ["performMethod" => 'myPerformMethod', "payload" => "otherData"];
$queue->put("Sample\\TestWorkerImpl", "jid", $testData, 0, 0);
$queue->put("TestWorkerImpl", "jid", $testData, 0, 0);

$job1 = $queue->pop("worker-1")[0];
$job1->retry('account', 'failed to connect');
Expand All @@ -105,12 +154,16 @@ public function testRetryTransitionsToFailedWhenExhaustedRetries() {
$this->assertEmpty($job1);
}

#endregion

#region cancel

public function testCancelRemovesJob() {
$queue = new Qless\Queue("testQueue", $this->client);

$testData = ["performMethod" => 'myPerformMethod', "payload" => "otherData"];
$queue->put("Sample\\TestWorkerImpl", "jid-1", $testData, 0, 0);
$queue->put("Sample\\TestWorkerImpl", "jid-2", $testData, 0, 0);
$queue->put("TestWorkerImpl", "jid-1", $testData, 0, 0);
$queue->put("TestWorkerImpl", "jid-2", $testData, 0, 0);

$job1 = $queue->pop("worker-1")[0];
$res = $job1->cancel();
Expand All @@ -122,8 +175,8 @@ public function testCancelRemovesJobWithDependents() {
$queue = new Qless\Queue("testQueue", $this->client);

$testData = ["performMethod" => 'myPerformMethod', "payload" => "otherData"];
$queue->put("Sample\\TestWorkerImpl", "jid-1", $testData, 0, 0);
$queue->put("Sample\\TestWorkerImpl", "jid-2", $testData, 0, 0, true, 0, [], 0, [], ['jid-1']);
$queue->put("TestWorkerImpl", "jid-1", $testData, 0, 0);
$queue->put("TestWorkerImpl", "jid-2", $testData, 0, 0, true, 0, [], 0, [], ['jid-1']);

$job1 = $queue->pop("worker-1")[0];
$res = $job1->cancel(true);
Expand All @@ -138,19 +191,21 @@ public function testCancelThrowsExceptionWithDependents() {
$queue = new Qless\Queue("testQueue", $this->client);

$testData = ["performMethod" => 'myPerformMethod', "payload" => "otherData"];
$queue->put("Sample\\TestWorkerImpl", "jid-1", $testData, 0, 0);
$queue->put("Sample\\TestWorkerImpl", "jid-2", $testData, 0, 0, true, 0, [], 0, [], ['jid-1']);
$queue->put("TestWorkerImpl", "jid-1", $testData, 0, 0);
$queue->put("TestWorkerImpl", "jid-2", $testData, 0, 0, true, 0, [], 0, [], ['jid-1']);

$job1 = $queue->pop("worker-1")[0];
$job1->cancel();
}

#endregion

#region tags

public function testItCanAddTagsToAJobWithNoExistingTags() {
$queue = new Qless\Queue("testQueue", $this->client);
$testData = ["performMethod" => 'myPerformMethod', "payload" => "otherData"];
$queue->put("Sample\\TestWorkerImpl", "jid-1", $testData, 0, 0);
$queue->put("TestWorkerImpl", "jid-1", $testData, 0, 0);

$job1 = $queue->pop("worker-1")[0];
$job1->tag('a', 'b');
Expand All @@ -162,7 +217,7 @@ public function testItCanAddTagsToAJobWithNoExistingTags() {
public function testItCanAddTagsToAJobWithExistingTags() {
$queue = new Qless\Queue("testQueue", $this->client);
$testData = ["performMethod" => 'myPerformMethod', "payload" => "otherData"];
$queue->put("Sample\\TestWorkerImpl", "jid-1", $testData, 0, 0, true, 0, [], 0, ['1', '2']);
$queue->put("TestWorkerImpl", "jid-1", $testData, 0, 0, true, 0, [], 0, ['1', '2']);

$job1 = $queue->pop("worker-1")[0];
$job1->tag('a', 'b');
Expand All @@ -174,7 +229,7 @@ public function testItCanAddTagsToAJobWithExistingTags() {
public function testItCanRemoveExistingTags() {
$queue = new Qless\Queue("testQueue", $this->client);
$testData = ["performMethod" => 'myPerformMethod', "payload" => "otherData"];
$queue->put("Sample\\TestWorkerImpl", "jid-1", $testData, 0, 0, true, 0, [], 0, ['1', '2', '3']);
$queue->put("TestWorkerImpl", "jid-1", $testData, 0, 0, true, 0, [], 0, ['1', '2', '3']);

$job1 = $queue->pop("worker-1")[0];
$job1->untag('2', '3');
Expand All @@ -191,7 +246,7 @@ public function testRequeueJob() {
$queue = new Qless\Queue("testQueue", $this->client);

$testData = ["performMethod" => 'myPerformMethod', "payload" => "otherData"];
$queue->put("Sample\\TestWorkerImpl", "jid-1", $testData, 0, 0, true, 1, [], 5, ['tag1','tag2']);
$queue->put("TestWorkerImpl", "jid-1", $testData, 0, 0, true, 1, [], 5, ['tag1','tag2']);

$job = $queue->pop("worker-1")[0];
$job->requeue();
Expand All @@ -206,7 +261,7 @@ public function testRequeueJobWithNewTags() {
$queue = new Qless\Queue("testQueue", $this->client);

$testData = ["performMethod" => 'myPerformMethod', "payload" => "otherData"];
$queue->put("Sample\\TestWorkerImpl", "jid-1", $testData, 0, 0, true, 1, [], 5, ['tag1','tag2']);
$queue->put("TestWorkerImpl", "jid-1", $testData, 0, 0, true, 1, [], 5, ['tag1','tag2']);

$job = $queue->pop("worker-1")[0];
$job->requeue(['tags' => ['nnn']]);
Expand All @@ -224,7 +279,7 @@ public function testThrowsInvalidJobExceptionWhenRequeuingCancelledJob() {
$queue = new Qless\Queue("testQueue", $this->client);

$testData = ["performMethod" => 'myPerformMethod', "payload" => "otherData"];
$queue->put("Sample\\TestWorkerImpl", "jid-1", $testData, 0, 0, true, 1, [], 5, ['tag1','tag2']);
$queue->put("TestWorkerImpl", "jid-1", $testData, 0, 0, true, 1, [], 5, ['tag1','tag2']);

$job = $queue->pop("worker-1")[0];
$this->client->cancel('jid-1');
Expand Down
3 changes: 2 additions & 1 deletion test/QlessTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@
require_once __DIR__ . '/../lib/Qless/Client.php';
require_once __DIR__ . '/../lib/Qless/Queue.php';
require_once __DIR__ . '/../lib/Qless/Jobs.php';
require_once __DIR__ . '/../demo/TestWorkerImpl.php';
require_once __DIR__ . '/LuaTester.php';

/**
* Base class for qless-php testing
*/
class QlessTest extends PHPUnit_Framework_TestCase {
abstract class QlessTest extends PHPUnit_Framework_TestCase {

static $REDIS_HOST;
static $REDIS_PORT;
Expand Down