/** * Test if first job delay can be different than failed attempts. */ public function testFirstAttemptCanHaveDifferentDelayThanFailedAttempts() { $this->assertRecordsCount(0); // Set delay of two seconds, because we sometimes got nextInLine() when job was set in one second, and we got to // the next second during assertRecordsCount() step $failing_job_with_instant_first_attempt = new Failing(['delay' => 2, 'attempts' => 2, 'first_attempt_delay' => 0]); $this->assertEquals(0, $failing_job_with_instant_first_attempt->getFirstJobDelay()); $this->assertEquals(2, $failing_job_with_instant_first_attempt->getDelay()); // Enqueue $this->assertEquals(1, $this->dispatcher->dispatch($failing_job_with_instant_first_attempt)); $this->assertRecordsCount(1); // First attempt $next_in_line = $this->dispatcher->getQueue()->nextInLine(); $this->assertInstanceOf('ActiveCollab\\JobsQueue\\Test\\Jobs\\Failing', $next_in_line); $this->assertEquals(1, $next_in_line->getQueueId()); $this->dispatcher->getQueue()->execute($next_in_line); // Second attempt $this->assertRecordsCount(1); $this->assertAttempts(1, $next_in_line->getQueueId()); $this->assertNull($this->dispatcher->getQueue()->nextInLine()); // Not yet available sleep(2); $next_in_line = $this->dispatcher->getQueue()->nextInLine(); $this->assertInstanceOf('ActiveCollab\\JobsQueue\\Test\\Jobs\\Failing', $next_in_line); $this->assertEquals(1, $next_in_line->getQueueId()); $this->dispatcher->getQueue()->execute($next_in_line); $this->assertRecordsCount(0); }
/** * Test if job is retried after failure until attempts limit is reached. */ public function testJobFailureAttempts() { $this->assertRecordsCount(0); $failing_job = new Failing(['attempts' => 3]); $this->assertEquals(3, $failing_job->getAttempts()); $this->assertEquals(1, $this->dispatcher->dispatch($failing_job)); // First attempt $next_in_line = $this->dispatcher->getQueue()->nextInLine(); $this->assertInstanceOf('ActiveCollab\\JobsQueue\\Test\\Jobs\\Failing', $next_in_line); $this->assertEquals(1, $next_in_line->getQueueId()); $this->dispatcher->getQueue()->execute($next_in_line); $this->assertEquals('ActiveCollab\\JobsQueue\\Test\\Jobs\\Failing', $this->last_failed_job); $this->assertEquals('Built to fail!', $this->last_failure_message); $this->assertRecordsCount(1); $this->assertAttempts(1, $next_in_line->getQueueId()); // Second attempt $next_in_line = $this->dispatcher->getQueue()->nextInLine(); $this->assertInstanceOf('ActiveCollab\\JobsQueue\\Test\\Jobs\\Failing', $next_in_line); $this->assertEquals(1, $next_in_line->getQueueId()); $this->dispatcher->getQueue()->execute($next_in_line); $this->assertAttempts(2, $next_in_line->getQueueId()); // Third attempt $next_in_line = $this->dispatcher->getQueue()->nextInLine(); $this->assertInstanceOf('ActiveCollab\\JobsQueue\\Test\\Jobs\\Failing', $next_in_line); $this->assertEquals(1, $next_in_line->getQueueId()); $this->dispatcher->getQueue()->execute($next_in_line); $this->assertAttempts(null, $next_in_line->getQueueId()); $this->assertNull($this->dispatcher->getQueue()->nextInLine()); }