diff --git a/test/JobTest.php b/test/JobTest.php index 73916ee..6d6687e 100644 --- a/test/JobTest.php +++ b/test/JobTest.php @@ -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 */ @@ -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"); @@ -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); @@ -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(); @@ -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'); @@ -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'); @@ -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'); @@ -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'); @@ -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'); @@ -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(); @@ -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); @@ -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'); @@ -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'); @@ -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'); @@ -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(); @@ -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']]); @@ -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'); diff --git a/test/QlessTest.php b/test/QlessTest.php index 728ddc8..e9a3542 100644 --- a/test/QlessTest.php +++ b/test/QlessTest.php @@ -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;