diff options
author | icewind1991 <icewind1991@gmail.com> | 2013-06-02 14:29:59 -0700 |
---|---|---|
committer | icewind1991 <icewind1991@gmail.com> | 2013-06-02 14:29:59 -0700 |
commit | 2a81813e44e7530c581b44db6d58d88bd422c511 (patch) | |
tree | 7c8887d256e323278fe86fe7336b23d587243702 /tests | |
parent | 9392d2d58ebfcc9f719dd9777578ab9493ffa488 (diff) | |
parent | 24ab525669e177e37266d5b8a052e4e25990ebf0 (diff) | |
download | nextcloud-server-2a81813e44e7530c581b44db6d58d88bd422c511.tar.gz nextcloud-server-2a81813e44e7530c581b44db6d58d88bd422c511.zip |
Merge pull request #3051 from owncloud/backgroundjob
Reworked background job system.
Diffstat (limited to 'tests')
-rw-r--r-- | tests/lib/backgroundjob/dummyjoblist.php | 131 | ||||
-rw-r--r-- | tests/lib/backgroundjob/queuedjob.php | 42 | ||||
-rw-r--r-- | tests/lib/backgroundjob/timedjob.php | 68 |
3 files changed, 241 insertions, 0 deletions
diff --git a/tests/lib/backgroundjob/dummyjoblist.php b/tests/lib/backgroundjob/dummyjoblist.php new file mode 100644 index 00000000000..d91d6b344b5 --- /dev/null +++ b/tests/lib/backgroundjob/dummyjoblist.php @@ -0,0 +1,131 @@ +<?php +/** + * Copyright (c) 2013 Robin Appelman <icewind@owncloud.com> + * This file is licensed under the Affero General Public License version 3 or + * later. + * See the COPYING-README file. + */ + +namespace Test\BackgroundJob; + +class JobRun extends \Exception { +} + +/** + * Class DummyJobList + * + * in memory job list for testing purposes + */ +class DummyJobList extends \OC\BackgroundJob\JobList { + /** + * @var \OC\BackgroundJob\Job[] + */ + private $jobs = array(); + + private $last = 0; + + /** + * @param \OC\BackgroundJob\Job|string $job + * @param mixed $argument + */ + public function add($job, $argument = null) { + $job->setArgument($argument); + if (!$this->has($job, null)) { + $this->jobs[] = $job; + } + } + + /** + * @param \OC\BackgroundJob\Job|string $job + * @param mixed $argument + */ + public function remove($job, $argument = null) { + $index = array_search($job, $this->jobs); + if ($index !== false) { + unset($this->jobs[$index]); + } + } + + /** + * check if a job is in the list + * + * @param $job + * @param mixed $argument + * @return bool + */ + public function has($job, $argument) { + return array_search($job, $this->jobs) !== false; + } + + /** + * get all jobs in the list + * + * @return \OC\BackgroundJob\Job[] + */ + public function getAll() { + return $this->jobs; + } + + /** + * get the next job in the list + * + * @return \OC\BackgroundJob\Job + */ + public function getNext() { + if (count($this->jobs) > 0) { + if ($this->last < (count($this->jobs) - 1)) { + $i = $this->last + 1; + } else { + $i = 0; + } + return $this->jobs[$i]; + } else { + return null; + } + } + + /** + * set the job that was last ran + * + * @param \OC\BackgroundJob\Job $job + */ + public function setLastJob($job) { + $i = array_search($job, $this->jobs); + if ($i !== false) { + $this->last = $i; + } else { + $this->last = 0; + } + } + + /** + * @param int $id + * @return Job + */ + public function getById($id) { + foreach ($this->jobs as $job) { + if ($job->getId() === $id) { + return $job; + } + } + return null; + } + + /** + * get the id of the last ran job + * + * @return int + */ + public function getLastJob() { + return $this->last; + } + + /** + * set the lastRun of $job to now + * + * @param \OC\BackgroundJob\Job $job + */ + public function setLastRun($job) { + $job->setLastRun(time()); + } +} diff --git a/tests/lib/backgroundjob/queuedjob.php b/tests/lib/backgroundjob/queuedjob.php new file mode 100644 index 00000000000..1d373473cd7 --- /dev/null +++ b/tests/lib/backgroundjob/queuedjob.php @@ -0,0 +1,42 @@ +<?php +/** + * Copyright (c) 2013 Robin Appelman <icewind@owncloud.com> + * This file is licensed under the Affero General Public License version 3 or + * later. + * See the COPYING-README file. + */ + +namespace Test\BackgroundJob; + +class TestQueuedJob extends \OC\BackgroundJob\QueuedJob { + public function run($argument) { + throw new JobRun(); //throw an exception so we can detect if this function is called + } +} + +class QueuedJob extends \PHPUnit_Framework_TestCase { + /** + * @var DummyJobList $jobList + */ + private $jobList; + /** + * @var \OC\BackgroundJob\TimedJob $job + */ + private $job; + + public function setup() { + $this->jobList = new DummyJobList(); + $this->job = new TestQueuedJob(); + $this->jobList->add($this->job); + } + + public function testJobShouldBeRemoved() { + try { + $this->assertTrue($this->jobList->has($this->job, null)); + $this->job->execute($this->jobList); + $this->fail("job should have been run"); + } catch (JobRun $e) { + $this->assertFalse($this->jobList->has($this->job, null)); + } + } +} diff --git a/tests/lib/backgroundjob/timedjob.php b/tests/lib/backgroundjob/timedjob.php new file mode 100644 index 00000000000..0af933afef8 --- /dev/null +++ b/tests/lib/backgroundjob/timedjob.php @@ -0,0 +1,68 @@ +<?php +/** + * Copyright (c) 2013 Robin Appelman <icewind@owncloud.com> + * This file is licensed under the Affero General Public License version 3 or + * later. + * See the COPYING-README file. + */ + +namespace Test\BackgroundJob; + +class TestTimedJob extends \OC\BackgroundJob\TimedJob { + public function __construct() { + $this->setInterval(10); + } + + public function run($argument) { + throw new JobRun(); //throw an exception so we can detect if this function is called + } +} + +class TimedJob extends \PHPUnit_Framework_TestCase { + /** + * @var DummyJobList $jobList + */ + private $jobList; + /** + * @var \OC\BackgroundJob\TimedJob $job + */ + private $job; + + public function setup() { + $this->jobList = new DummyJobList(); + $this->job = new TestTimedJob(); + $this->jobList->add($this->job); + } + + public function testShouldRunAfterInterval() { + $this->job->setLastRun(time() - 12); + try { + $this->job->execute($this->jobList); + $this->fail("job should have run"); + } catch (JobRun $e) { + } + } + + public function testShouldNotRunWithinInterval() { + $this->job->setLastRun(time() - 5); + try { + $this->job->execute($this->jobList); + } catch (JobRun $e) { + $this->fail("job should not have run"); + } + } + + public function testShouldNotTwice() { + $this->job->setLastRun(time() - 15); + try { + $this->job->execute($this->jobList); + $this->fail("job should have run the first time"); + } catch (JobRun $e) { + try { + $this->job->execute($this->jobList); + } catch (JobRun $e) { + $this->fail("job should not have run the second time"); + } + } + } +} |