diff options
author | Robin Appelman <icewind@owncloud.com> | 2013-04-20 23:27:46 +0200 |
---|---|---|
committer | Robin Appelman <icewind@owncloud.com> | 2013-04-20 23:27:46 +0200 |
commit | 7948341a86fb08d236bb53f8aece809ae10ba5f2 (patch) | |
tree | 61bf8cc11b2212953f5f87216d33a7744869385f /tests/lib/backgroundjob | |
parent | d0a5fe1f4a39f21c596293232be19d70ad30652e (diff) | |
download | nextcloud-server-7948341a86fb08d236bb53f8aece809ae10ba5f2.tar.gz nextcloud-server-7948341a86fb08d236bb53f8aece809ae10ba5f2.zip |
Rework background job system
Diffstat (limited to 'tests/lib/backgroundjob')
-rw-r--r-- | tests/lib/backgroundjob/dummyjoblist.php | 114 | ||||
-rw-r--r-- | tests/lib/backgroundjob/queuedjob.php | 42 | ||||
-rw-r--r-- | tests/lib/backgroundjob/timedjob.php | 68 |
3 files changed, 224 insertions, 0 deletions
diff --git a/tests/lib/backgroundjob/dummyjoblist.php b/tests/lib/backgroundjob/dummyjoblist.php new file mode 100644 index 00000000000..c4ccfc22c9e --- /dev/null +++ b/tests/lib/backgroundjob/dummyjoblist.php @@ -0,0 +1,114 @@ +<?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 + */ + public function add($job) { + if (!$this->has($job)) { + $this->jobs[] = $job; + } + } + + /** + * @param \OC\BackgroundJob\Job|string $job + */ + public function remove($job) { + $index = array_search($job, $this->jobs); + if ($index !== false) { + unset($this->jobs[$index]); + } + } + + /** + * check if a job is in the list + * + * @param $job + * @return bool + */ + public function has($job) { + 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; + } + } + + /** + * 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..62d631a3ef0 --- /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() { + 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)); + $this->job->execute($this->jobList); + $this->fail("job should have been run"); + } catch (JobRun $e) { + $this->assertFalse($this->jobList->has($this->job)); + } + } +} diff --git a/tests/lib/backgroundjob/timedjob.php b/tests/lib/backgroundjob/timedjob.php new file mode 100644 index 00000000000..4147b82cbbe --- /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() { + 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"); + } + } + } +} |