2014-02-12 12:52:13 +00:00
|
|
|
<?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;
|
|
|
|
|
2016-09-12 18:56:05 +00:00
|
|
|
use OCP\AppFramework\Utility\ITimeFactory;
|
2015-11-19 08:25:34 +00:00
|
|
|
use OCP\BackgroundJob\IJob;
|
2016-05-18 12:27:48 +00:00
|
|
|
use OCP\DB\QueryBuilder\IQueryBuilder;
|
2016-09-12 18:56:05 +00:00
|
|
|
use OCP\IConfig;
|
2015-11-03 00:52:41 +00:00
|
|
|
use Test\TestCase;
|
2015-11-19 08:25:34 +00:00
|
|
|
|
2015-11-03 00:52:41 +00:00
|
|
|
/**
|
|
|
|
* Class JobList
|
|
|
|
*
|
|
|
|
* @group DB
|
|
|
|
* @package Test\BackgroundJob
|
|
|
|
*/
|
2016-05-20 13:38:20 +00:00
|
|
|
class JobListTest extends TestCase {
|
2015-12-18 08:47:54 +00:00
|
|
|
/** @var \OC\BackgroundJob\JobList */
|
2014-02-12 12:52:13 +00:00
|
|
|
protected $instance;
|
|
|
|
|
2016-05-18 12:27:48 +00:00
|
|
|
/** @var \OCP\IDBConnection */
|
|
|
|
protected $connection;
|
|
|
|
|
2015-12-18 08:47:54 +00:00
|
|
|
/** @var \OCP\IConfig|\PHPUnit_Framework_MockObject_MockObject */
|
2014-02-12 12:52:13 +00:00
|
|
|
protected $config;
|
|
|
|
|
2016-05-18 12:27:48 +00:00
|
|
|
/** @var \OCP\AppFramework\Utility\ITimeFactory|\PHPUnit_Framework_MockObject_MockObject */
|
|
|
|
protected $timeFactory;
|
|
|
|
|
2014-11-10 22:30:38 +00:00
|
|
|
protected function setUp() {
|
|
|
|
parent::setUp();
|
|
|
|
|
2016-05-18 12:27:48 +00:00
|
|
|
$this->connection = \OC::$server->getDatabaseConnection();
|
|
|
|
$this->clearJobsList();
|
2016-09-12 18:56:05 +00:00
|
|
|
$this->config = $this->createMock(IConfig::class);
|
|
|
|
$this->timeFactory = $this->createMock(ITimeFactory::class);
|
2016-05-18 12:27:48 +00:00
|
|
|
$this->instance = new \OC\BackgroundJob\JobList(
|
|
|
|
$this->connection,
|
|
|
|
$this->config,
|
|
|
|
$this->timeFactory
|
|
|
|
);
|
2014-02-12 12:52:13 +00:00
|
|
|
}
|
|
|
|
|
2016-05-18 12:27:48 +00:00
|
|
|
protected function clearJobsList() {
|
|
|
|
$query = $this->connection->getQueryBuilder();
|
2016-04-22 08:49:55 +00:00
|
|
|
$query->delete('jobs');
|
|
|
|
$query->execute();
|
|
|
|
}
|
|
|
|
|
2015-11-19 08:25:34 +00:00
|
|
|
protected function getAllSorted() {
|
|
|
|
$jobs = $this->instance->getAll();
|
|
|
|
|
|
|
|
usort($jobs, function (IJob $job1, IJob $job2) {
|
|
|
|
return $job1->getId() - $job2->getId();
|
|
|
|
});
|
|
|
|
|
|
|
|
return $jobs;
|
|
|
|
}
|
|
|
|
|
2014-02-12 12:52:13 +00:00
|
|
|
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) {
|
2015-11-19 08:25:34 +00:00
|
|
|
$existingJobs = $this->getAllSorted();
|
2014-02-12 12:52:13 +00:00
|
|
|
$job = new TestJob();
|
|
|
|
$this->instance->add($job, $argument);
|
|
|
|
|
2015-11-19 08:25:34 +00:00
|
|
|
$jobs = $this->getAllSorted();
|
2014-02-12 12:52:13 +00:00
|
|
|
|
|
|
|
$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);
|
|
|
|
|
2015-11-19 08:25:34 +00:00
|
|
|
$jobs = $this->getAllSorted();
|
2014-02-12 12:52:13 +00:00
|
|
|
$this->assertEquals($existingJobs, $jobs);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider argumentProvider
|
|
|
|
* @param $argument
|
|
|
|
*/
|
|
|
|
public function testRemoveDifferentArgument($argument) {
|
2015-11-19 08:25:34 +00:00
|
|
|
$existingJobs = $this->getAllSorted();
|
2014-02-12 12:52:13 +00:00
|
|
|
$job = new TestJob();
|
|
|
|
$this->instance->add($job, $argument);
|
|
|
|
|
2015-11-19 08:25:34 +00:00
|
|
|
$jobs = $this->getAllSorted();
|
2014-02-12 12:52:13 +00:00
|
|
|
$this->instance->remove($job, 10);
|
2015-11-19 08:25:34 +00:00
|
|
|
$jobs2 = $this->getAllSorted();
|
2014-02-12 12:52:13 +00:00
|
|
|
|
|
|
|
$this->assertEquals($jobs, $jobs2);
|
|
|
|
|
|
|
|
$this->instance->remove($job, $argument);
|
|
|
|
|
2015-11-19 08:25:34 +00:00
|
|
|
$jobs = $this->getAllSorted();
|
2014-02-12 12:52:13 +00:00
|
|
|
$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));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetLastJob() {
|
|
|
|
$this->config->expects($this->once())
|
|
|
|
->method('getAppValue')
|
|
|
|
->with('backgroundjob', 'lastjob', 0)
|
|
|
|
->will($this->returnValue(15));
|
|
|
|
|
|
|
|
$this->assertEquals(15, $this->instance->getLastJob());
|
|
|
|
}
|
|
|
|
|
2016-05-18 12:27:48 +00:00
|
|
|
protected function createTempJob($class, $argument, $reservedTime = 0, $lastChecked = 0) {
|
|
|
|
if ($lastChecked === 0) {
|
|
|
|
$lastChecked = time();
|
|
|
|
}
|
|
|
|
|
|
|
|
$query = $this->connection->getQueryBuilder();
|
|
|
|
$query->insert('jobs')
|
|
|
|
->values([
|
|
|
|
'class' => $query->createNamedParameter($class),
|
|
|
|
'argument' => $query->createNamedParameter($argument),
|
|
|
|
'last_run' => $query->createNamedParameter(0, IQueryBuilder::PARAM_INT),
|
|
|
|
'last_checked' => $query->createNamedParameter($lastChecked, IQueryBuilder::PARAM_INT),
|
|
|
|
'reserved_at' => $query->createNamedParameter($reservedTime, IQueryBuilder::PARAM_INT),
|
|
|
|
]);
|
|
|
|
$query->execute();
|
|
|
|
}
|
|
|
|
|
2014-02-12 12:52:13 +00:00
|
|
|
public function testGetNext() {
|
|
|
|
$job = new TestJob();
|
2016-05-18 12:27:48 +00:00
|
|
|
$this->createTempJob(get_class($job), 1, 0, 12345);
|
|
|
|
$this->createTempJob(get_class($job), 2, 0, 12346);
|
2014-02-12 12:52:13 +00:00
|
|
|
|
2015-11-19 08:25:34 +00:00
|
|
|
$jobs = $this->getAllSorted();
|
2016-05-18 12:27:48 +00:00
|
|
|
$savedJob1 = $jobs[0];
|
2014-02-12 12:52:13 +00:00
|
|
|
|
2016-05-18 12:27:48 +00:00
|
|
|
$this->timeFactory->expects($this->atLeastOnce())
|
|
|
|
->method('getTime')
|
|
|
|
->willReturn(123456789);
|
2014-02-12 12:52:13 +00:00
|
|
|
$nextJob = $this->instance->getNext();
|
|
|
|
|
2016-04-22 08:49:55 +00:00
|
|
|
$this->assertEquals($savedJob1, $nextJob);
|
2014-02-12 12:52:13 +00:00
|
|
|
}
|
|
|
|
|
2016-05-18 12:27:48 +00:00
|
|
|
public function testGetNextSkipReserved() {
|
2014-02-12 12:52:13 +00:00
|
|
|
$job = new TestJob();
|
2016-05-18 12:27:48 +00:00
|
|
|
$this->createTempJob(get_class($job), 1, 123456789, 12345);
|
|
|
|
$this->createTempJob(get_class($job), 2, 0, 12346);
|
2014-02-12 12:52:13 +00:00
|
|
|
|
2016-05-18 12:27:48 +00:00
|
|
|
$this->timeFactory->expects($this->atLeastOnce())
|
|
|
|
->method('getTime')
|
|
|
|
->willReturn(123456789);
|
|
|
|
$nextJob = $this->instance->getNext();
|
2014-02-12 12:52:13 +00:00
|
|
|
|
2016-05-18 12:27:48 +00:00
|
|
|
$this->assertEquals(get_class($job), get_class($nextJob));
|
|
|
|
$this->assertEquals(2, $nextJob->getArgument());
|
|
|
|
}
|
2014-02-12 12:52:13 +00:00
|
|
|
|
2016-05-18 12:27:48 +00:00
|
|
|
public function testGetNextSkipNonExisting() {
|
|
|
|
$job = new TestJob();
|
|
|
|
$this->createTempJob('\OC\Non\Existing\Class', 1, 0, 12345);
|
|
|
|
$this->createTempJob(get_class($job), 2, 0, 12346);
|
2014-02-12 12:52:13 +00:00
|
|
|
|
2016-05-18 12:27:48 +00:00
|
|
|
$this->timeFactory->expects($this->atLeastOnce())
|
|
|
|
->method('getTime')
|
|
|
|
->willReturn(123456789);
|
2014-02-12 12:52:13 +00:00
|
|
|
$nextJob = $this->instance->getNext();
|
|
|
|
|
2016-05-18 12:27:48 +00:00
|
|
|
$this->assertEquals(get_class($job), get_class($nextJob));
|
|
|
|
$this->assertEquals(2, $nextJob->getArgument());
|
2014-02-12 12:52:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider argumentProvider
|
|
|
|
* @param $argument
|
|
|
|
*/
|
|
|
|
public function testGetById($argument) {
|
|
|
|
$job = new TestJob();
|
|
|
|
$this->instance->add($job, $argument);
|
|
|
|
|
2015-11-19 08:25:34 +00:00
|
|
|
$jobs = $this->getAllSorted();
|
2014-02-12 12:52:13 +00:00
|
|
|
|
|
|
|
$addedJob = $jobs[count($jobs) - 1];
|
|
|
|
|
|
|
|
$this->assertEquals($addedJob, $this->instance->getById($addedJob->getId()));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testSetLastRun() {
|
|
|
|
$job = new TestJob();
|
|
|
|
$this->instance->add($job);
|
|
|
|
|
2015-11-19 08:25:34 +00:00
|
|
|
$jobs = $this->getAllSorted();
|
2014-02-12 12:52:13 +00:00
|
|
|
|
|
|
|
$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());
|
2015-08-19 12:16:05 +00:00
|
|
|
}
|
2014-02-12 12:52:13 +00:00
|
|
|
}
|