Differential D9871 Diff 23765 src/infrastructure/daemon/workers/__tests__/PhabricatorWorkerTestCase.php
Changeset View
Changeset View
Standalone View
Standalone View
src/infrastructure/daemon/workers/__tests__/PhabricatorWorkerTestCase.php
<?php | <?php | ||||
final class PhabricatorWorkerTestCase extends PhabricatorTestCase { | final class PhabricatorWorkerTestCase extends PhabricatorTestCase { | ||||
protected function getPhabricatorTestCaseConfiguration() { | protected function getPhabricatorTestCaseConfiguration() { | ||||
return array( | return array( | ||||
self::PHABRICATOR_TESTCONFIG_BUILD_STORAGE_FIXTURES => true, | self::PHABRICATOR_TESTCONFIG_BUILD_STORAGE_FIXTURES => true, | ||||
); | ); | ||||
} | } | ||||
public function testLeaseTask() { | public function testLeaseTask() { | ||||
// Leasing should work. | |||||
$task = $this->scheduleTask(); | $task = $this->scheduleTask(); | ||||
$this->expectNextLease($task, 'Leasing should work.'); | |||||
$this->expectNextLease($task); | |||||
} | } | ||||
public function testMultipleLease() { | public function testMultipleLease() { | ||||
// We should not be able to lease a task multiple times. | |||||
$task = $this->scheduleTask(); | $task = $this->scheduleTask(); | ||||
$this->expectNextLease($task); | $this->expectNextLease($task); | ||||
$this->expectNextLease(null); | $this->expectNextLease( | ||||
null, | |||||
'We should not be able to lease a task multiple times.'); | |||||
} | } | ||||
public function testOldestFirst() { | public function testOldestFirst() { | ||||
// Older tasks should lease first, all else being equal. | |||||
$task1 = $this->scheduleTask(); | $task1 = $this->scheduleTask(); | ||||
$task2 = $this->scheduleTask(); | $task2 = $this->scheduleTask(); | ||||
$this->expectNextLease($task1); | $this->expectNextLease( | ||||
$task1, | |||||
'Older tasks should lease first, all else being equal.'); | |||||
$this->expectNextLease($task2); | $this->expectNextLease($task2); | ||||
} | } | ||||
public function testNewBeforeLeased() { | public function testNewBeforeLeased() { | ||||
// Tasks not previously leased should lease before previously leased tasks. | |||||
$task1 = $this->scheduleTask(); | $task1 = $this->scheduleTask(); | ||||
$task2 = $this->scheduleTask(); | $task2 = $this->scheduleTask(); | ||||
$task1->setLeaseOwner('test'); | $task1->setLeaseOwner('test'); | ||||
$task1->setLeaseExpires(time() - 100000); | $task1->setLeaseExpires(time() - 100000); | ||||
$task1->forceSaveWithoutLease(); | $task1->forceSaveWithoutLease(); | ||||
$this->expectNextLease($task2); | $this->expectNextLease( | ||||
$task2, | |||||
'Tasks not previously leased should lease before previously '. | |||||
'leased tasks.'); | |||||
$this->expectNextLease($task1); | $this->expectNextLease($task1); | ||||
} | } | ||||
public function testExecuteTask() { | public function testExecuteTask() { | ||||
$task = $this->scheduleAndExecuteTask(); | $task = $this->scheduleAndExecuteTask(); | ||||
$this->assertEqual(true, $task->isArchived()); | $this->assertEqual(true, $task->isArchived()); | ||||
$this->assertEqual( | $this->assertEqual( | ||||
▲ Show 20 Lines • Show All 76 Lines • ▼ Show 20 Lines | public function testWaitBeforeRetry() { | ||||
$this->expectNextLease($task)->executeTask(); | $this->expectNextLease($task)->executeTask(); | ||||
$this->expectNextLease(null); | $this->expectNextLease(null); | ||||
} | } | ||||
public function testRequiredLeaseTime() { | public function testRequiredLeaseTime() { | ||||
$task = $this->scheduleAndExecuteTask( | $task = $this->scheduleAndExecuteTask( | ||||
array( | array( | ||||
'getRequiredLeaseTime' => 1000000, | 'getRequiredLeaseTime' => 1000000, | ||||
)); | )); | ||||
$this->assertTrue(($task->getLeaseExpires() - time()) > 1000); | $this->assertTrue(($task->getLeaseExpires() - time()) > 1000); | ||||
} | } | ||||
public function testLeasedIsOldestFirst() { | public function testLeasedIsOldestFirst() { | ||||
// Tasks which expired earlier should lease first, all else being equal. | |||||
$task1 = $this->scheduleTask(); | $task1 = $this->scheduleTask(); | ||||
$task2 = $this->scheduleTask(); | $task2 = $this->scheduleTask(); | ||||
$task1->setLeaseOwner('test'); | $task1->setLeaseOwner('test'); | ||||
$task1->setLeaseExpires(time() - 100000); | $task1->setLeaseExpires(time() - 100000); | ||||
$task1->forceSaveWithoutLease(); | $task1->forceSaveWithoutLease(); | ||||
$task2->setLeaseOwner('test'); | $task2->setLeaseOwner('test'); | ||||
$task2->setLeaseExpires(time() - 200000); | $task2->setLeaseExpires(time() - 200000); | ||||
$task2->forceSaveWithoutLease(); | $task2->forceSaveWithoutLease(); | ||||
$this->expectNextLease($task2); | $this->expectNextLease( | ||||
$task2, | |||||
'Tasks which expired earlier should lease first, all else being equal.'); | |||||
$this->expectNextLease($task1); | $this->expectNextLease($task1); | ||||
} | } | ||||
private function expectNextLease($task) { | public function testLeasedIsHighestPriority() { | ||||
$task1 = $this->scheduleTask(array(), 2); | |||||
$task2 = $this->scheduleTask(array(), 1); | |||||
$task3 = $this->scheduleTask(array(), 1); | |||||
epriestley: This test works coincidentally because FIFO order and priority order are the same. | |||||
$this->expectNextLease( | |||||
$task1, | |||||
'Tasks with a higher priority should be scheduled first.'); | |||||
$this->expectNextLease( | |||||
$task2, | |||||
'Tasks with the same priority should be FIFO.'); | |||||
$this->expectNextLease($task3); | |||||
} | |||||
private function expectNextLease($task, $message = null) { | |||||
$leased = id(new PhabricatorWorkerLeaseQuery()) | $leased = id(new PhabricatorWorkerLeaseQuery()) | ||||
->setLimit(1) | ->setLimit(1) | ||||
->execute(); | ->execute(); | ||||
if ($task === null) { | if ($task === null) { | ||||
$this->assertEqual(0, count($leased)); | $this->assertEqual(0, count($leased), $message); | ||||
return null; | return null; | ||||
} else { | } else { | ||||
$this->assertEqual(1, count($leased)); | $this->assertEqual(1, count($leased), $message); | ||||
$this->assertEqual( | $this->assertEqual( | ||||
(int)head($leased)->getID(), | (int)head($leased)->getID(), | ||||
(int)$task->getID()); | (int)$task->getID(), | ||||
$message); | |||||
return head($leased); | return head($leased); | ||||
} | } | ||||
} | } | ||||
private function scheduleAndExecuteTask(array $data = array()) { | private function scheduleAndExecuteTask( | ||||
$task = $this->scheduleTask($data); | array $data = array(), | ||||
$priority = null) { | |||||
$task = $this->scheduleTask($data, $priority); | |||||
$task = $this->expectNextLease($task); | $task = $this->expectNextLease($task); | ||||
$task = $task->executeTask(); | $task = $task->executeTask(); | ||||
return $task; | return $task; | ||||
} | } | ||||
private function scheduleTask(array $data = array()) { | private function scheduleTask(array $data = array(), $priority = null) { | ||||
return PhabricatorWorker::scheduleTask('PhabricatorTestWorker', $data); | return PhabricatorWorker::scheduleTask( | ||||
'PhabricatorTestWorker', | |||||
$data, | |||||
$priority); | |||||
} | } | ||||
} | } |
This test works coincidentally because FIFO order and priority order are the same.