summaryrefslogtreecommitdiffstats
path: root/tests/lib/BackgroundJob
diff options
context:
space:
mode:
Diffstat (limited to 'tests/lib/BackgroundJob')
-rw-r--r--tests/lib/BackgroundJob/DummyJobList.php135
-rw-r--r--tests/lib/BackgroundJob/JobListTest.php255
-rw-r--r--tests/lib/BackgroundJob/JobTest.php43
-rw-r--r--tests/lib/BackgroundJob/QueuedJobTest.php56
-rw-r--r--tests/lib/BackgroundJob/TestJob.php34
-rw-r--r--tests/lib/BackgroundJob/TimedJobTest.php72
6 files changed, 595 insertions, 0 deletions
diff --git a/tests/lib/BackgroundJob/DummyJobList.php b/tests/lib/BackgroundJob/DummyJobList.php
new file mode 100644
index 00000000000..6cc690fd553
--- /dev/null
+++ b/tests/lib/BackgroundJob/DummyJobList.php
@@ -0,0 +1,135 @@
+<?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 DummyJobList
+ *
+ * in memory job list for testing purposes
+ */
+class DummyJobList extends \OC\BackgroundJob\JobList {
+ /**
+ * @var \OC\BackgroundJob\Job[]
+ */
+ private $jobs = array();
+
+ private $last = 0;
+
+ public function __construct() {
+ }
+
+ /**
+ * @param \OC\BackgroundJob\Job|string $job
+ * @param mixed $argument
+ */
+ public function add($job, $argument = null) {
+ if (is_string($job)) {
+ /** @var \OC\BackgroundJob\Job $job */
+ $job = new $job;
+ }
+ $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/JobListTest.php b/tests/lib/BackgroundJob/JobListTest.php
new file mode 100644
index 00000000000..6eed804bc3c
--- /dev/null
+++ b/tests/lib/BackgroundJob/JobListTest.php
@@ -0,0 +1,255 @@
+<?php
+/**
+ * Copyright (c) 2014 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;
+
+use OCP\BackgroundJob\IJob;
+use OCP\IDBConnection;
+use Test\TestCase;
+
+/**
+ * Class JobList
+ *
+ * @group DB
+ * @package Test\BackgroundJob
+ */
+class JobListTest extends TestCase {
+ /** @var \OC\BackgroundJob\JobList */
+ protected $instance;
+
+ /** @var \OCP\IConfig|\PHPUnit_Framework_MockObject_MockObject */
+ protected $config;
+
+ protected function setUp() {
+ parent::setUp();
+
+ $connection = \OC::$server->getDatabaseConnection();
+ $this->clearJobsList($connection);
+ $this->config = $this->getMock('\OCP\IConfig');
+ $this->instance = new \OC\BackgroundJob\JobList($connection, $this->config);
+ }
+
+ protected function clearJobsList(IDBConnection $connection) {
+ $query = $connection->getQueryBuilder();
+ $query->delete('jobs');
+ $query->execute();
+ }
+
+ protected function getAllSorted() {
+ $jobs = $this->instance->getAll();
+
+ usort($jobs, function (IJob $job1, IJob $job2) {
+ return $job1->getId() - $job2->getId();
+ });
+
+ return $jobs;
+ }
+
+ public function argumentProvider() {
+ return array(
+ array(null),
+ array(false),
+ array('foobar'),
+ array(12),
+ array(array(
+ 'asd' => 5,
+ 'foo' => 'bar'
+ ))
+ );
+ }
+
+ /**
+ * @dataProvider argumentProvider
+ * @param $argument
+ */
+ public function testAddRemove($argument) {
+ $existingJobs = $this->getAllSorted();
+ $job = new TestJob();
+ $this->instance->add($job, $argument);
+
+ $jobs = $this->getAllSorted();
+
+ $this->assertCount(count($existingJobs) + 1, $jobs);
+ $addedJob = $jobs[count($jobs) - 1];
+ $this->assertInstanceOf('\Test\BackgroundJob\TestJob', $addedJob);
+ $this->assertEquals($argument, $addedJob->getArgument());
+
+ $this->instance->remove($job, $argument);
+
+ $jobs = $this->getAllSorted();
+ $this->assertEquals($existingJobs, $jobs);
+ }
+
+ /**
+ * @dataProvider argumentProvider
+ * @param $argument
+ */
+ public function testRemoveDifferentArgument($argument) {
+ $existingJobs = $this->getAllSorted();
+ $job = new TestJob();
+ $this->instance->add($job, $argument);
+
+ $jobs = $this->getAllSorted();
+ $this->instance->remove($job, 10);
+ $jobs2 = $this->getAllSorted();
+
+ $this->assertEquals($jobs, $jobs2);
+
+ $this->instance->remove($job, $argument);
+
+ $jobs = $this->getAllSorted();
+ $this->assertEquals($existingJobs, $jobs);
+ }
+
+ /**
+ * @dataProvider argumentProvider
+ * @param $argument
+ */
+ public function testHas($argument) {
+ $job = new TestJob();
+ $this->assertFalse($this->instance->has($job, $argument));
+ $this->instance->add($job, $argument);
+
+ $this->assertTrue($this->instance->has($job, $argument));
+
+ $this->instance->remove($job, $argument);
+
+ $this->assertFalse($this->instance->has($job, $argument));
+ }
+
+ /**
+ * @dataProvider argumentProvider
+ * @param $argument
+ */
+ public function testHasDifferentArgument($argument) {
+ $job = new TestJob();
+ $this->instance->add($job, $argument);
+
+ $this->assertFalse($this->instance->has($job, 10));
+
+ $this->instance->remove($job, $argument);
+ }
+
+ public function testGetLastJob() {
+ $this->config->expects($this->once())
+ ->method('getAppValue')
+ ->with('backgroundjob', 'lastjob', 0)
+ ->will($this->returnValue(15));
+
+ $this->assertEquals(15, $this->instance->getLastJob());
+ }
+
+ public function testGetNext() {
+ $job = new TestJob();
+ $this->instance->add($job, 1);
+ $this->instance->add($job, 2);
+
+ $jobs = $this->getAllSorted();
+
+ $savedJob1 = $jobs[count($jobs) - 2];
+ $savedJob2 = $jobs[count($jobs) - 1];
+
+ $this->config->expects($this->once())
+ ->method('getAppValue')
+ ->with('backgroundjob', 'lastjob', 0)
+ ->will($this->returnValue($savedJob2->getId()));
+
+ $nextJob = $this->instance->getNext();
+
+ $this->assertEquals($savedJob1, $nextJob);
+
+ $this->instance->remove($job, 1);
+ $this->instance->remove($job, 2);
+ }
+
+ public function testGetNextWrapAround() {
+ $job = new TestJob();
+ $this->instance->add($job, 1);
+ $this->instance->add($job, 2);
+
+ $jobs = $this->getAllSorted();
+
+ $savedJob1 = $jobs[count($jobs) - 2];
+ $savedJob2 = $jobs[count($jobs) - 1];
+
+ $this->config->expects($this->once())
+ ->method('getAppValue')
+ ->with('backgroundjob', 'lastjob', 0)
+ ->will($this->returnValue($savedJob1->getId()));
+
+ $nextJob = $this->instance->getNext();
+
+ $this->assertEquals($savedJob2, $nextJob);
+
+ $this->instance->remove($job, 1);
+ $this->instance->remove($job, 2);
+ }
+
+ /**
+ * @dataProvider argumentProvider
+ * @param $argument
+ */
+ public function testGetById($argument) {
+ $job = new TestJob();
+ $this->instance->add($job, $argument);
+
+ $jobs = $this->getAllSorted();
+
+ $addedJob = $jobs[count($jobs) - 1];
+
+ $this->assertEquals($addedJob, $this->instance->getById($addedJob->getId()));
+
+ $this->instance->remove($job, $argument);
+ }
+
+ public function testSetLastRun() {
+ $job = new TestJob();
+ $this->instance->add($job);
+
+ $jobs = $this->getAllSorted();
+
+ $addedJob = $jobs[count($jobs) - 1];
+
+ $timeStart = time();
+ $this->instance->setLastRun($addedJob);
+ $timeEnd = time();
+
+ $addedJob = $this->instance->getById($addedJob->getId());
+
+ $this->assertGreaterThanOrEqual($timeStart, $addedJob->getLastRun());
+ $this->assertLessThanOrEqual($timeEnd, $addedJob->getLastRun());
+
+ $this->instance->remove($job);
+ }
+
+ public function testGetNextNonExisting() {
+ $job = new TestJob();
+ $this->instance->add($job, 1);
+ $this->instance->add('\OC\Non\Existing\Class');
+ $this->instance->add($job, 2);
+
+ $jobs = $this->getAllSorted();
+
+ $savedJob1 = $jobs[count($jobs) - 2];
+ $savedJob2 = $jobs[count($jobs) - 1];
+
+ $this->config->expects($this->any())
+ ->method('getAppValue')
+ ->with('backgroundjob', 'lastjob', 0)
+ ->will($this->returnValue($savedJob1->getId()));
+
+ $this->instance->getNext();
+
+ $nextJob = $this->instance->getNext();
+
+ $this->assertEquals($savedJob2, $nextJob);
+
+ $this->instance->remove($job, 1);
+ $this->instance->remove($job, 2);
+ }
+}
diff --git a/tests/lib/BackgroundJob/JobTest.php b/tests/lib/BackgroundJob/JobTest.php
new file mode 100644
index 00000000000..7923eef11ef
--- /dev/null
+++ b/tests/lib/BackgroundJob/JobTest.php
@@ -0,0 +1,43 @@
+<?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 JobTest extends \Test\TestCase {
+ private $run = false;
+
+ protected function setUp() {
+ parent::setUp();
+ $this->run = false;
+ }
+
+ public function testRemoveAfterException() {
+ $jobList = new DummyJobList();
+ $e = new \Exception();
+ $job = new TestJob($this, function () use ($e) {
+ throw $e;
+ });
+ $jobList->add($job);
+
+ $logger = $this->getMockBuilder('OCP\ILogger')
+ ->disableOriginalConstructor()
+ ->getMock();
+ $logger->expects($this->once())
+ ->method('logException')
+ ->with($e);
+
+ $this->assertCount(1, $jobList->getAll());
+ $job->execute($jobList, $logger);
+ $this->assertTrue($this->run);
+ $this->assertCount(1, $jobList->getAll());
+ }
+
+ public function markRun() {
+ $this->run = true;
+ }
+}
diff --git a/tests/lib/BackgroundJob/QueuedJobTest.php b/tests/lib/BackgroundJob/QueuedJobTest.php
new file mode 100644
index 00000000000..a3e9cc296a0
--- /dev/null
+++ b/tests/lib/BackgroundJob/QueuedJobTest.php
@@ -0,0 +1,56 @@
+<?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 {
+ private $testCase;
+
+ /**
+ * @param QueuedJobTest $testCase
+ */
+ public function __construct($testCase) {
+ $this->testCase = $testCase;
+ }
+
+ public function run($argument) {
+ $this->testCase->markRun();
+ }
+}
+
+class QueuedJobTest extends \Test\TestCase {
+ /**
+ * @var DummyJobList $jobList
+ */
+ private $jobList;
+ /**
+ * @var \OC\BackgroundJob\TimedJob $job
+ */
+ private $job;
+
+ private $jobRun = false;
+
+ public function markRun() {
+ $this->jobRun = true;
+ }
+
+ protected function setup() {
+ parent::setUp();
+
+ $this->jobList = new DummyJobList();
+ $this->job = new TestQueuedJob($this);
+ $this->jobList->add($this->job);
+ $this->jobRun = false;
+ }
+
+ public function testJobShouldBeRemoved() {
+ $this->assertTrue($this->jobList->has($this->job, null));
+ $this->job->execute($this->jobList);
+ $this->assertTrue($this->jobRun);
+ }
+}
diff --git a/tests/lib/BackgroundJob/TestJob.php b/tests/lib/BackgroundJob/TestJob.php
new file mode 100644
index 00000000000..a5ab4454550
--- /dev/null
+++ b/tests/lib/BackgroundJob/TestJob.php
@@ -0,0 +1,34 @@
+<?php
+/**
+ * Copyright (c) 2014 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 TestJob extends \OC\BackgroundJob\Job {
+ private $testCase;
+
+ /**
+ * @var callable $callback
+ */
+ private $callback;
+
+ /**
+ * @param JobTest $testCase
+ * @param callable $callback
+ */
+ public function __construct($testCase = null, $callback = null) {
+ $this->testCase = $testCase;
+ $this->callback = $callback;
+ }
+
+ public function run($argument) {
+ $this->testCase->markRun();
+ $callback = $this->callback;
+ $callback($argument);
+ }
+}
diff --git a/tests/lib/BackgroundJob/TimedJobTest.php b/tests/lib/BackgroundJob/TimedJobTest.php
new file mode 100644
index 00000000000..5652a5c9901
--- /dev/null
+++ b/tests/lib/BackgroundJob/TimedJobTest.php
@@ -0,0 +1,72 @@
+<?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 {
+ private $testCase;
+
+ /**
+ * @param TimedJobTest $testCase
+ */
+ public function __construct($testCase) {
+ $this->setInterval(10);
+ $this->testCase = $testCase;
+ }
+
+ public function run($argument) {
+ $this->testCase->markRun();
+ }
+}
+
+class TimedJobTest extends \Test\TestCase {
+ /**
+ * @var DummyJobList $jobList
+ */
+ private $jobList;
+ /**
+ * @var \OC\BackgroundJob\TimedJob $job
+ */
+ private $job;
+
+ private $jobRun = false;
+
+ public function markRun() {
+ $this->jobRun = true;
+ }
+
+ protected function setup() {
+ parent::setUp();
+
+ $this->jobList = new DummyJobList();
+ $this->job = new TestTimedJob($this);
+ $this->jobList->add($this->job);
+ $this->jobRun = false;
+ }
+
+ public function testShouldRunAfterInterval() {
+ $this->job->setLastRun(time() - 12);
+ $this->job->execute($this->jobList);
+ $this->assertTrue($this->jobRun);
+ }
+
+ public function testShouldNotRunWithinInterval() {
+ $this->job->setLastRun(time() - 5);
+ $this->job->execute($this->jobList);
+ $this->assertFalse($this->jobRun);
+ }
+
+ public function testShouldNotTwice() {
+ $this->job->setLastRun(time() - 15);
+ $this->job->execute($this->jobList);
+ $this->assertTrue($this->jobRun);
+ $this->jobRun = false;
+ $this->job->execute($this->jobList);
+ $this->assertFalse($this->jobRun);
+ }
+}