Merge pull request #7156 from owncloud/backgroundjob-public
Add the background job list to the public server containerremotes/origin/ldap_group_count
commit
750ffa8231
@ -0,0 +1,42 @@
|
||||
<?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 OCP\BackgroundJob;
|
||||
|
||||
interface IJob {
|
||||
/**
|
||||
* Run the background job with the registered argument
|
||||
*
|
||||
* @param \OCP\BackgroundJob\IJobList $jobList The job list that manages the state of this job
|
||||
* @param \OC\Log $logger
|
||||
*/
|
||||
public function execute($jobList, $logger = null);
|
||||
|
||||
/**
|
||||
* Get the id of the background job
|
||||
* This id is determined by the job list when a job is added to the list
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function getId();
|
||||
|
||||
/**
|
||||
* Get the last time this job was run as unix timestamp
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function getLastRun();
|
||||
|
||||
/**
|
||||
* Get the argument associated with the background job
|
||||
* This is the argument that will be passed to the background job
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function getArgument();
|
||||
}
|
||||
@ -0,0 +1,77 @@
|
||||
<?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 OCP\BackgroundJob;
|
||||
|
||||
interface IJobList {
|
||||
/**
|
||||
* Add a job to the list
|
||||
*
|
||||
* @param \OCP\BackgroundJob\IJob |string $job
|
||||
* @param mixed $argument The argument to be passed to $job->run() when the job is exectured
|
||||
*/
|
||||
public function add($job, $argument = null);
|
||||
|
||||
/**
|
||||
* Remove a job from the list
|
||||
*
|
||||
* @param \OCP\BackgroundJob\IJob|string $job
|
||||
* @param mixed $argument
|
||||
*/
|
||||
public function remove($job, $argument = null);
|
||||
|
||||
/**
|
||||
* check if a job is in the list
|
||||
*
|
||||
* @param $job
|
||||
* @param mixed $argument
|
||||
* @return bool
|
||||
*/
|
||||
public function has($job, $argument);
|
||||
|
||||
/**
|
||||
* get all jobs in the list
|
||||
*
|
||||
* @return \OCP\BackgroundJob\IJob[]
|
||||
*/
|
||||
public function getAll();
|
||||
|
||||
/**
|
||||
* get the next job in the list
|
||||
*
|
||||
* @return \OCP\BackgroundJob\IJob
|
||||
*/
|
||||
public function getNext();
|
||||
|
||||
/**
|
||||
* @param int $id
|
||||
* @return \OCP\BackgroundJob\IJob
|
||||
*/
|
||||
public function getById($id);
|
||||
|
||||
/**
|
||||
* set the job that was last ran to the current time
|
||||
*
|
||||
* @param \OCP\BackgroundJob\IJob $job
|
||||
*/
|
||||
public function setLastJob($job);
|
||||
|
||||
/**
|
||||
* get the id of the last ran job
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function getLastJob();
|
||||
|
||||
/**
|
||||
* set the lastRun of $job to now
|
||||
*
|
||||
* @param \OCP\BackgroundJob\IJob $job
|
||||
*/
|
||||
public function setLastRun($job);
|
||||
}
|
||||
@ -0,0 +1,203 @@
|
||||
<?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 JobList extends \PHPUnit_Framework_TestCase {
|
||||
/**
|
||||
* @var \OC\BackgroundJob\JobList
|
||||
*/
|
||||
protected $instance;
|
||||
|
||||
/**
|
||||
* @var \OCP\IConfig | \PHPUnit_Framework_MockObject_MockObject $config
|
||||
*/
|
||||
protected $config;
|
||||
|
||||
public function setUp() {
|
||||
$conn = \OC::$server->getDatabaseConnection();
|
||||
$this->config = $this->getMock('\OCP\IConfig');
|
||||
$this->instance = new \OC\BackgroundJob\JobList($conn, $this->config);
|
||||
}
|
||||
|
||||
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->instance->getAll();
|
||||
$job = new TestJob();
|
||||
$this->instance->add($job, $argument);
|
||||
|
||||
$jobs = $this->instance->getAll();
|
||||
|
||||
$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->instance->getAll();
|
||||
$this->assertEquals($existingJobs, $jobs);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider argumentProvider
|
||||
* @param $argument
|
||||
*/
|
||||
public function testRemoveDifferentArgument($argument) {
|
||||
$existingJobs = $this->instance->getAll();
|
||||
$job = new TestJob();
|
||||
$this->instance->add($job, $argument);
|
||||
|
||||
$jobs = $this->instance->getAll();
|
||||
$this->instance->remove($job, 10);
|
||||
$jobs2 = $this->instance->getAll();
|
||||
|
||||
$this->assertEquals($jobs, $jobs2);
|
||||
|
||||
$this->instance->remove($job, $argument);
|
||||
|
||||
$jobs = $this->instance->getAll();
|
||||
$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->instance->getAll();
|
||||
|
||||
$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);
|
||||
}
|
||||
|
||||
public function testGetNextWrapAround() {
|
||||
$job = new TestJob();
|
||||
$this->instance->add($job, 1);
|
||||
$this->instance->add($job, 2);
|
||||
|
||||
$jobs = $this->instance->getAll();
|
||||
|
||||
$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($jobs[0], $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->instance->getAll();
|
||||
|
||||
$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->instance->getAll();
|
||||
|
||||
$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);
|
||||
}
|
||||
}
|
||||
@ -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 Job $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);
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue